diff --git a/Cargo.toml b/Cargo.toml index 71db9848..0ce5b1cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ async-io = "1.6.0" # zbus zbus = "2.0.0-beta.7" zvariant = "2.10.0" +zvariant_derive = "2.10.0" futures-util = "0.3.19" [dependencies.gtk4] diff --git a/examples/dock/dock_item/mod.rs b/examples/dock/dock_item/mod.rs index 605f7956..cbaf9653 100644 --- a/examples/dock/dock_item/mod.rs +++ b/examples/dock/dock_item/mod.rs @@ -1,16 +1,12 @@ -use crate::BoxedSearchResults; +use crate::utils::BoxedWindowList; use gdk4::ContentProvider; use gdk4::Display; use gio::DesktopAppInfo; use gio::Icon; use gio::ListStore; use gtk4 as gtk; -use gtk4::Align; -use gtk4::Box; use gtk4::DragSource; use gtk4::IconTheme; -use gtk4::Label; -use gtk4::Orientation; mod imp; use gtk::glib; @@ -123,7 +119,7 @@ impl DockItem { println!("initializing dock item failed..."); } if let Ok(active_value) = app_info.property("active") { - if let Ok(active) = active_value.get::() { + if let Ok(active) = active_value.get::() { self_.dots.set_text(""); for _ in active.0 { self_ diff --git a/examples/dock/dock_object/imp.rs b/examples/dock/dock_object/imp.rs index 66fc50d1..e717b6dd 100644 --- a/examples/dock/dock_object/imp.rs +++ b/examples/dock/dock_object/imp.rs @@ -1,4 +1,4 @@ -use crate::utils::BoxedSearchResults; +use crate::utils::BoxedWindowList; use gio::DesktopAppInfo; use glib::{ParamFlags, ParamSpec, Value}; use gtk4::glib; @@ -12,7 +12,7 @@ use std::cell::RefCell; #[derive(Default)] pub struct DockObject { appinfo: RefCell>, - active: RefCell, + active: RefCell, saved: Cell, } @@ -47,7 +47,7 @@ impl ObjectImpl for DockObject { "active", // Short description "active", - BoxedSearchResults::static_type(), + BoxedWindowList::static_type(), // The property can be read and written to ParamFlags::READWRITE, ), @@ -72,7 +72,7 @@ impl ObjectImpl for DockObject { self.appinfo.replace(appinfo); } "active" => { - let active = value.get().expect("Value needs to be BoxedSearchResults"); + let active = value.get().expect("Value needs to be BoxedWindowList"); self.active.replace(active); } "saved" => { diff --git a/examples/dock/dock_object/mod.rs b/examples/dock/dock_object/mod.rs index 5cf6ddcf..749bf46f 100644 --- a/examples/dock/dock_object/mod.rs +++ b/examples/dock/dock_object/mod.rs @@ -1,6 +1,6 @@ mod imp; -use crate::utils::BoxedSearchResults; +use crate::utils::BoxedWindowList; use gdk4::glib::Object; use gio::DesktopAppInfo; use gtk4::glib; @@ -16,7 +16,7 @@ impl DockObject { .expect("Failed to create `DockObject`.") } - pub fn from_search_results(results: BoxedSearchResults) -> Self { + pub fn from_search_results(results: BoxedWindowList) -> Self { let appinfo = if let Some(first) = results.0.iter().next() { xdg::BaseDirectories::new() .expect("could not access XDG Base directory") diff --git a/examples/dock/main.rs b/examples/dock/main.rs index 5884ee8e..78405429 100644 --- a/examples/dock/main.rs +++ b/examples/dock/main.rs @@ -4,7 +4,7 @@ mod dock_object; mod utils; mod window; -use crate::utils::BoxedSearchResults; +use crate::utils::BoxedWindowList; use async_io::Timer; use futures::StreamExt; use gdk4::Display; @@ -17,13 +17,15 @@ use gtk4 as gtk; use gtk4::CssProvider; use gtk4::StyleContext; use once_cell::sync::OnceCell; -use pop_launcher::SearchResult; use pop_launcher_service::IpcClient; use postage::mpsc::Sender; use postage::prelude::*; +use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::time::Duration; use x11rb::rust_connection::RustConnection; +use zbus::Connection; +use zvariant_derive::Type; use self::dock_object::DockObject; use self::window::Window; @@ -34,11 +36,20 @@ static X11_CONN: OnceCell = OnceCell::new(); pub enum Event { Response(pop_launcher::Response), + WindowList(Vec), RefreshFromCache, Search(String), Activate(u32), } +#[derive(Debug, Deserialize, Serialize, Type, Clone, PartialEq, Eq)] +pub struct Item { + entity: (u32, u32), + name: String, + description: String, + desktop_entry: String, +} + fn spawn_launcher(tx: Sender) -> IpcClient { let (launcher, responses) = pop_launcher_service::IpcClient::new().expect("failed to connect to launcher service"); @@ -51,12 +62,25 @@ fn spawn_launcher(tx: Sender) -> IpcClient { } }); - // TODO listen for signal indicating change from dock service... let mut sender = tx.clone(); glib::MainContext::default().spawn_local(async move { loop { - let _ = sender.send(Event::Search(String::new())).await; - Timer::after(Duration::from_secs(1)).await; + let connection = Connection::session().await.unwrap(); + let m = connection + .call_method( + Some("com.System76.PopShell"), + "/com/System76/PopShell", + Some("com.System76.PopShell"), + "WindowList", + &(), + ) + .await; + if let Ok(m) = m { + if let Ok(reply) = m.body::>() { + let _ = sender.send(Event::WindowList(reply)).await; + } + Timer::after(Duration::from_secs(1)).await; + } } }); @@ -86,7 +110,7 @@ fn load_css() { } fn main() { - assert!(utils::BoxedSearchResults::static_type().is_valid()); + assert!(utils::BoxedWindowList::static_type().is_valid()); let app = gtk::Application::builder() .application_id("com.cosmic.Launcher") .build(); @@ -116,7 +140,7 @@ fn main() { let window = Window::new(app); window.show(); - let cached_results: Vec = vec![]; + let cached_results: Vec = vec![]; glib::MainContext::default().spawn_local(async move { futures::pin_mut!(cached_results); while let Some(event) = rx.recv().await { @@ -130,18 +154,17 @@ fn main() { Event::RefreshFromCache => { //TODO refresh the model from cached_results (required after DnD for example) } - Event::Response(event) => { - // TODO investigate why polled results are out of date after launching a new window - if let pop_launcher::Response::Update(mut results) = event { - // sort to make comparison with cache easier + Event::WindowList(mut results) => { +// sort to make comparison with cache easier let mut cached_results = cached_results.as_mut(); results.sort_by(|a, b| a.name.cmp(&b.name)); - dbg!(&results); - dbg!(&cached_results); - // check if cache equals the new polled results + + // dbg!(&results); + // dbg!(&cached_results); + // // check if cache equals the new polled results // skip if equal - if cached_results.len() == results.len() && results.iter().zip(cached_results.iter()).fold(0, |acc, z: (&SearchResult, &SearchResult)| { + if cached_results.len() == results.len() && results.iter().zip(cached_results.iter()).fold(0, |acc, z: (&Item, &Item)| { let (a, b) = z; if a.name == b.name {acc + 1} else {acc} }) == cached_results.len() { @@ -150,15 +173,15 @@ fn main() { println!("updating active apps"); // build active app stacks for each app - let stack_active = results.iter().fold(BTreeMap::new(), |mut acc: BTreeMap, elem| { + let stack_active = results.iter().fold(BTreeMap::new(), |mut acc: BTreeMap, elem| { if let Some(v) = acc.get_mut(&elem.description) { v.0.push(elem.clone()); } else { - acc.insert(elem.description.clone(), BoxedSearchResults(vec![elem.clone()])); + acc.insert(elem.description.clone(), BoxedWindowList(vec![elem.clone()])); } acc }); - let mut stack_active: Vec = stack_active.into_values().collect(); + let mut stack_active: Vec = stack_active.into_values().collect(); // update active app stacks for saved apps into the saved app model // then put the rest in the active app model (which doesn't include saved apps) @@ -188,8 +211,10 @@ fn main() { .collect(); active_app_model.splice(0, model_len, &new_results[..]); cached_results.splice(.., results); - } - else if let pop_launcher::Response::DesktopEntry { + } + Event::Response(event) => { + // TODO investigate why polled results are out of date after launching a new window + if let pop_launcher::Response::DesktopEntry { path, gpu_preference: _gpu_preference, // TODO use GPU preference when launching app } = event diff --git a/examples/dock/utils.rs b/examples/dock/utils.rs index cc87d10f..d42968d2 100644 --- a/examples/dock/utils.rs +++ b/examples/dock/utils.rs @@ -1,3 +1,5 @@ +use crate::Item; + #[derive(Clone, Debug, Default, glib::GBoxed)] -#[gboxed(type_name = "BoxedLauncherActive")] -pub struct BoxedSearchResults(pub Vec); +#[gboxed(type_name = "BoxedWindowList")] +pub struct BoxedWindowList(pub Vec);