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) => { Event::Response(event) => {
if let pop_launcher::Response::Update(results) = 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(); let model_len = model.n_items();
dbg!(&results); dbg!(&results);
let new_results: Vec<glib::Object> = results let new_results: Vec<glib::Object> = results

View file

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

View file

@ -15,14 +15,17 @@ use once_cell::sync::OnceCell;
#[template(file = "window.ui")] #[template(file = "window.ui")]
pub struct Window { pub struct Window {
#[template_child] #[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] #[template_child]
pub revealer: TemplateChild<Revealer>, pub revealer: TemplateChild<Revealer>,
#[template_child] #[template_child]
pub cursor_enter_handle: TemplateChild<Box>, pub cursor_enter_handle: TemplateChild<Box>,
#[template_child] #[template_child]
pub cursor_leave_handle: TemplateChild<Box>, 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 enter_event_controller: OnceCell<EventControllerMotion>,
pub leave_event_controller: OnceCell<EventControllerMotion>, pub leave_event_controller: OnceCell<EventControllerMotion>,
} }

View file

@ -33,23 +33,25 @@ impl Window {
self_ self_
} }
pub fn model(&self) -> &gio::ListStore { pub fn saved_app_model(&self) -> &gio::ListStore {
// Get state // Get state
let imp = imp::Window::from_instance(self); 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) { fn setup_model(&self) {
// Get state and set model // Get state and set model
let imp = imp::Window::from_instance(self); 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() let selection_model = gtk::SingleSelection::builder()
.autoselect(false) .autoselect(false)
.can_unselect(true) .can_unselect(true)
.selected(gtk4::INVALID_LIST_POSITION) .selected(gtk4::INVALID_LIST_POSITION)
.model(&model) .model(&saved_app_model)
.build(); .build();
xdg::BaseDirectories::new() xdg::BaseDirectories::new()
.expect("could not access XDG Base directory") .expect("could not access XDG Base directory")
@ -69,7 +71,7 @@ impl Window {
&& defaults.contains(&app_info.name().as_str()) && defaults.contains(&app_info.name().as_str())
{ {
dbg!(app_info.name()); dbg!(app_info.name());
model.append(&app_info) saved_app_model.append(&app_info)
} else { } else {
// println!("Ignoring {}", path); // 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 // 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) { fn setup_callbacks(&self) {
// Get state // Get state
let imp = imp::Window::from_instance(self); let imp = imp::Window::from_instance(self);
let window = self.clone().upcast::<gtk::Window>(); 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() .model()
.expect("List view missing selection model") .expect("List view missing selection model")
.downcast::<gtk::SingleSelection>() .downcast::<gtk::SingleSelection>()
.expect("could not downcast listview model to single selection model"); .expect("could not downcast listview model to single selection model");
app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| { saved_app_selection_model.connect_selected_notify(
let position = model.selected(); glib::clone!(@weak window => move |model| {
println!("selected app {}", position); let position = model.selected();
// Launch the application when an item of the list is activated println!("selected app {}", position);
if let Some(item) = model.item(position) { // Launch the application when an item of the list is activated
let app_info = item.downcast::<gio::DesktopAppInfo>().unwrap(); if let Some(item) = model.item(position) {
let context = window.display().app_launch_context(); let app_info = item.downcast::<gio::DesktopAppInfo>().unwrap();
if let Err(err) = app_info.launch(&[], Some(&context)) { let context = window.display().app_launch_context();
gtk::MessageDialog::builder() if let Err(err) = app_info.launch(&[], Some(&context)) {
.text(&format!("Failed to start {}", app_info.name())) gtk::MessageDialog::builder()
.secondary_text(&err.to_string()) .text(&format!("Failed to start {}", app_info.name()))
.message_type(gtk::MessageType::Error) .secondary_text(&err.to_string())
.modal(true) .message_type(gtk::MessageType::Error)
.transient_for(&window) .modal(true)
.build() .transient_for(&window)
.show(); .build()
.show();
}
} }
} }),
})); );
let enter_event_controller = &imp.enter_event_controller.get().unwrap(); let enter_event_controller = &imp.enter_event_controller.get().unwrap();
let leave_event_controller = &imp.leave_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 // Set the factory of the list view
let imp = imp::Window::from_instance(self); 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"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<template class="LauncherWindow" parent="GtkApplicationWindow"> <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="title">Gtk Pop Dock</property>
<property name="decorated">false</property> <property name="decorated">false</property>
<property name="resizable">false</property> <property name="resizable">false</property>
@ -27,7 +27,7 @@
<property name="spacing">4</property> <property name="spacing">4</property>
<property name="name">dock-container</property> <property name="name">dock-container</property>
<child> <child>
<object class="GtkListView" id="list_view"> <object class="GtkListView" id="saved_app_list_view">
<property name="orientation">horizontal</property> <property name="orientation">horizontal</property>
<property name="hexpand">true</property> <property name="hexpand">true</property>
</object> </object>
@ -39,13 +39,19 @@
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
</object> </object>
</child> </child>
<child>
<object class="GtkListView" id="unsaved_open_app_list_view">
<property name="orientation">horizontal</property>
<property name="hexpand">true</property>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>
</child> </child>
<child> <child>
<object class="GtkBox" id="cursor_enter_handle"> <object class="GtkBox" id="cursor_enter_handle">
<property name="height-request">10</property> <property name="height-request">4</property>
</object> </object>
</child> </child>
</object> </object>