add open app listto dock

This commit is contained in:
Ashley Wulber 2021-12-15 14:03:23 -05:00
parent 0984a13fa5
commit 32cc7e7344
5 changed files with 54 additions and 39 deletions

View file

@ -120,7 +120,7 @@ fn main() {
Event::Response(event) => {
if let pop_launcher::Response::Update(results) = event {
let model = window.model();
let model = window.saved_app_model();
let model_len = model.n_items();
dbg!(&results);
let new_results: Vec<glib::Object> = results

View file

@ -1,21 +1,21 @@
row:hover {
transition: 100ms;
background: #888888;
border-radius: 10px;
border-radius: 12px;
}
row {
background: #333333;
border-radius: 10px;
border-radius: 12px;
}
listview {
border-radius: 10px;
border-radius: 12px;
background: #333333;
}
window {
background: rgba(50,50,50,0.0);
}
box#dock-container {
padding-right: 10px;
border-radius: 10px;
padding-right: 12px;
border-radius: 12px;
background: #333333;
}

View file

@ -15,14 +15,17 @@ use once_cell::sync::OnceCell;
#[template(file = "window.ui")]
pub struct Window {
#[template_child]
pub list_view: TemplateChild<ListView>,
pub saved_app_list_view: TemplateChild<ListView>,
#[template_child]
pub unsaved_open_app_list_view: TemplateChild<ListView>,
#[template_child]
pub revealer: TemplateChild<Revealer>,
#[template_child]
pub cursor_enter_handle: TemplateChild<Box>,
#[template_child]
pub cursor_leave_handle: TemplateChild<Box>,
pub model: OnceCell<gio::ListStore>,
pub saved_app_model: OnceCell<gio::ListStore>,
pub unsaved_open_app_model: OnceCell<gio::ListStore>,
pub enter_event_controller: OnceCell<EventControllerMotion>,
pub leave_event_controller: OnceCell<EventControllerMotion>,
}

View file

@ -33,23 +33,25 @@ impl Window {
self_
}
pub fn model(&self) -> &gio::ListStore {
pub fn saved_app_model(&self) -> &gio::ListStore {
// Get state
let imp = imp::Window::from_instance(self);
imp.model.get().expect("Could not get model")
imp.saved_app_model
.get()
.expect("Could not get saved_app_model")
}
fn setup_model(&self) {
// Get state and set model
let imp = imp::Window::from_instance(self);
let model = gio::ListStore::new(DesktopAppInfo::static_type());
let saved_app_model = gio::ListStore::new(DesktopAppInfo::static_type());
let selection_model = gtk::SingleSelection::builder()
.autoselect(false)
.can_unselect(true)
.selected(gtk4::INVALID_LIST_POSITION)
.model(&model)
.model(&saved_app_model)
.build();
xdg::BaseDirectories::new()
.expect("could not access XDG Base directory")
@ -69,7 +71,7 @@ impl Window {
&& defaults.contains(&app_info.name().as_str())
{
dbg!(app_info.name());
model.append(&app_info)
saved_app_model.append(&app_info)
} else {
// println!("Ignoring {}", path);
}
@ -83,42 +85,46 @@ impl Window {
}
});
imp.model.set(model).expect("Could not set model");
imp.saved_app_model
.set(saved_app_model)
.expect("Could not set model");
// Wrap model with selection and pass it to the list view
imp.list_view.set_model(Some(&selection_model));
imp.saved_app_list_view.set_model(Some(&selection_model));
}
fn setup_callbacks(&self) {
// Get state
let imp = imp::Window::from_instance(self);
let window = self.clone().upcast::<gtk::Window>();
let list_view = &imp.list_view;
let saved_app_list_view = &imp.saved_app_list_view;
let app_selection_model = list_view
let saved_app_selection_model = saved_app_list_view
.model()
.expect("List view missing selection model")
.downcast::<gtk::SingleSelection>()
.expect("could not downcast listview model to single selection model");
app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| {
let position = model.selected();
println!("selected app {}", position);
// Launch the application when an item of the list is activated
if let Some(item) = model.item(position) {
let app_info = item.downcast::<gio::DesktopAppInfo>().unwrap();
let context = window.display().app_launch_context();
if let Err(err) = app_info.launch(&[], Some(&context)) {
gtk::MessageDialog::builder()
.text(&format!("Failed to start {}", app_info.name()))
.secondary_text(&err.to_string())
.message_type(gtk::MessageType::Error)
.modal(true)
.transient_for(&window)
.build()
.show();
saved_app_selection_model.connect_selected_notify(
glib::clone!(@weak window => move |model| {
let position = model.selected();
println!("selected app {}", position);
// Launch the application when an item of the list is activated
if let Some(item) = model.item(position) {
let app_info = item.downcast::<gio::DesktopAppInfo>().unwrap();
let context = window.display().app_launch_context();
if let Err(err) = app_info.launch(&[], Some(&context)) {
gtk::MessageDialog::builder()
.text(&format!("Failed to start {}", app_info.name()))
.secondary_text(&err.to_string())
.message_type(gtk::MessageType::Error)
.modal(true)
.transient_for(&window)
.build()
.show();
}
}
}
}));
}),
);
let enter_event_controller = &imp.enter_event_controller.get().unwrap();
let leave_event_controller = &imp.leave_event_controller.get().unwrap();
@ -242,6 +248,6 @@ impl Window {
});
// Set the factory of the list view
let imp = imp::Window::from_instance(self);
imp.list_view.set_factory(Some(&factory));
imp.saved_app_list_view.set_factory(Some(&factory));
}
}

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="LauncherWindow" parent="GtkApplicationWindow">
<property name="height-request">70</property>
<property name="height-request">80</property>
<property name="title">Gtk Pop Dock</property>
<property name="decorated">false</property>
<property name="resizable">false</property>
@ -27,7 +27,7 @@
<property name="spacing">4</property>
<property name="name">dock-container</property>
<child>
<object class="GtkListView" id="list_view">
<object class="GtkListView" id="saved_app_list_view">
<property name="orientation">horizontal</property>
<property name="hexpand">true</property>
</object>
@ -39,13 +39,19 @@
<property name="orientation">vertical</property>
</object>
</child>
<child>
<object class="GtkListView" id="unsaved_open_app_list_view">
<property name="orientation">horizontal</property>
<property name="hexpand">true</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="cursor_enter_handle">
<property name="height-request">10</property>
<property name="height-request">4</property>
</object>
</child>
</object>