From 4afa029d5c381e333903e653b134c083c67e486c Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 12 Feb 2025 12:40:13 -0800 Subject: [PATCH] Avoid redundancy now that `ToplevelInfo` contains handle --- cosmic-app-list/src/app.rs | 79 ++++++----- cosmic-app-list/src/wayland_handler.rs | 6 +- cosmic-app-list/src/wayland_subscription.rs | 4 +- cosmic-applet-minimize/src/lib.rs | 129 +++++++++--------- cosmic-applet-minimize/src/wayland_handler.rs | 9 +- .../src/wayland_subscription.rs | 4 +- 6 files changed, 116 insertions(+), 115 deletions(-) diff --git a/cosmic-app-list/src/app.rs b/cosmic-app-list/src/app.rs index b20c3871..2d6219bf 100755 --- a/cosmic-app-list/src/app.rs +++ b/cosmic-app-list/src/app.rs @@ -128,11 +128,7 @@ struct DockItem { // ID used internally in the applet. Each dock item // have an unique id id: u32, - toplevels: Vec<( - ExtForeignToplevelHandleV1, - ToplevelInfo, - Option, - )>, + toplevels: Vec<(ToplevelInfo, Option)>, // Information found in the .desktop file desktop_info: DesktopEntry<'static>, // We must use this because the id in `DesktopEntry` is an estimation. @@ -253,7 +249,9 @@ impl DockItem { .on_press_maybe(if toplevels.is_empty() { launch_on_preferred_gpu(desktop_info, gpus) } else if toplevels.len() == 1 { - toplevels.first().map(|t| Message::Toggle(t.0.clone())) + toplevels + .first() + .map(|t| Message::Toggle(t.0.foreign_toplevel.clone())) } else { Some(Message::TopLevelListPopup((*id).into(), window_id)) }) @@ -726,9 +724,10 @@ impl cosmic::Application for CosmicAppList { .chain(self.pinned_list.iter()) .find(|t| t.id == id) { - for (ref handle, _, _) in &toplevel_group.toplevels { + for (info, _) in &toplevel_group.toplevels { if let Some(tx) = self.wayland_sender.as_ref() { - let _ = tx.send(WaylandRequest::Screencopy(handle.clone())); + let _ = + tx.send(WaylandRequest::Screencopy(info.foreign_toplevel.clone())); } } @@ -849,10 +848,10 @@ impl cosmic::Application for CosmicAppList { .chain(self.pinned_list.iter()) .find(|t| t.desktop_info.id() == id) { - for (handle, _, _) in &toplevel_group.toplevels { + for (info, _) in &toplevel_group.toplevels { if let Some(tx) = self.wayland_sender.as_ref() { let _ = tx.send(WaylandRequest::Toplevel(ToplevelRequest::Quit( - handle.clone(), + info.foreign_toplevel.clone(), ))); } } @@ -1021,10 +1020,10 @@ impl cosmic::Application for CosmicAppList { .iter_mut() .chain(self.pinned_list.iter_mut()) { - if let Some((_, _, ref mut handle_img)) = x + if let Some((_, ref mut handle_img)) = x .toplevels .iter_mut() - .find(|(toplevel_handle, _, _)| toplevel_handle.clone() == handle) + .find(|(info, _)| info.foreign_toplevel == handle) { *handle_img = Some(img); break 'img_update; @@ -1055,7 +1054,7 @@ impl cosmic::Application for CosmicAppList { .map(cosmic::app::message::app); } WaylandUpdate::Toplevel(event) => match event { - ToplevelUpdate::Add(handle, mut info) => { + ToplevelUpdate::Add(mut info) => { let new_desktop_info = load_desktop_entries_from_app_ids(&[&info.app_id], &self.locales) .remove(0); @@ -1068,7 +1067,7 @@ impl cosmic::Application for CosmicAppList { desktop_info.id() == new_desktop_info.id() }) { - t.toplevels.push((handle, info, None)); + t.toplevels.push((info, None)); } else { if info.app_id.is_empty() { info.app_id = format!("Unknown Application {}", self.item_ctr); @@ -1078,7 +1077,7 @@ impl cosmic::Application for CosmicAppList { self.active_list.push(DockItem { id: self.item_ctr, original_app_id: info.app_id.clone(), - toplevels: vec![(handle, info, None)], + toplevels: vec![(info, None)], desktop_info: new_desktop_info, }); } @@ -1089,11 +1088,12 @@ impl cosmic::Application for CosmicAppList { .iter_mut() .chain(self.pinned_list.iter_mut()) { - t.toplevels.retain(|(t_handle, _, _)| t_handle != &handle); + t.toplevels + .retain(|(info, _)| info.foreign_toplevel != handle); } self.active_list.retain(|t| !t.toplevels.is_empty()); } - ToplevelUpdate::Update(handle, info) => { + ToplevelUpdate::Update(info) => { // TODO probably want to make sure it is removed if info.app_id.is_empty() { return Task::none(); @@ -1103,8 +1103,8 @@ impl cosmic::Application for CosmicAppList { .iter_mut() .chain(self.pinned_list.iter_mut()) { - for (t_handle, t_info, _) in &mut toplevel_list.toplevels { - if &handle == t_handle { + for (t_info, _) in &mut toplevel_list.toplevels { + if info.foreign_toplevel == t_info.foreign_toplevel { *t_info = info; break 'toplevel_loop; } @@ -1409,7 +1409,7 @@ impl cosmic::Application for CosmicAppList { dock_item .toplevels .iter() - .any(|y| focused_item.contains(&y.0)), + .any(|y| focused_item.contains(&y.0.foreign_toplevel)), theme.cosmic().radius_xs(), self.core.main_window_id().unwrap(), ) @@ -1452,7 +1452,9 @@ impl cosmic::Application for CosmicAppList { false, self.config.enable_drag_source, self.gpus.as_deref(), - item.toplevels.iter().any(|y| focused_item.contains(&y.0)), + item.toplevels + .iter() + .any(|y| focused_item.contains(&y.0.foreign_toplevel)), dot_radius, self.core.main_window_id().unwrap(), ), @@ -1489,7 +1491,7 @@ impl cosmic::Application for CosmicAppList { dock_item .toplevels .iter() - .any(|y| focused_item.contains(&y.0)), + .any(|y| focused_item.contains(&y.0.foreign_toplevel)), dot_radius, self.core.main_window_id().unwrap(), ) @@ -1729,16 +1731,17 @@ impl cosmic::Application for CosmicAppList { if !toplevels.is_empty() { let mut list_col = column![]; - for (handle, info, _) in toplevels { + for (info, _) in toplevels { let title = if info.title.len() > 34 { format!("{:.32}...", &info.title) } else { info.title.clone() }; - list_col = list_col.push( - menu_button(text::body(title)) - .on_press(Message::Activate(handle.clone())), - ); + list_col = + list_col + .push(menu_button(text::body(title)).on_press( + Message::Activate(info.foreign_toplevel.clone()), + )); } content = content.push(list_col); content = content.push(divider::horizontal::light()); @@ -1819,7 +1822,7 @@ impl cosmic::Application for CosmicAppList { PanelAnchor::Left | PanelAnchor::Right => { let mut content = column![].padding(8).align_x(Alignment::Center).spacing(8); - for (handle, info, img) in toplevels { + for (info, img) in toplevels { let title = if info.title.len() > 18 { format!("{:.16}...", &info.title) } else { @@ -1827,9 +1830,10 @@ impl cosmic::Application for CosmicAppList { }; content = content.push(toplevel_button( img.clone(), - Message::Toggle(handle.clone()), + Message::Toggle(info.foreign_toplevel.clone()), title, - self.currently_active_toplevel().contains(handle), + self.currently_active_toplevel() + .contains(&info.foreign_toplevel), )); } self.core @@ -1840,7 +1844,7 @@ impl cosmic::Application for CosmicAppList { } PanelAnchor::Bottom | PanelAnchor::Top => { let mut content = row![].padding(8).align_y(Alignment::Center).spacing(8); - for (handle, info, img) in toplevels { + for (info, img) in toplevels { let title = if info.title.len() > 18 { format!("{:.16}...", &info.title) } else { @@ -1848,9 +1852,10 @@ impl cosmic::Application for CosmicAppList { }; content = content.push(toplevel_button( img.clone(), - Message::Toggle(handle.clone()), + Message::Toggle(info.foreign_toplevel.clone()), title, - self.currently_active_toplevel().contains(handle), + self.currently_active_toplevel() + .contains(&info.foreign_toplevel), )); } self.core @@ -1891,7 +1896,7 @@ impl cosmic::Application for CosmicAppList { dock_item .toplevels .iter() - .any(|y| focused_item.contains(&y.0)), + .any(|y| focused_item.contains(&y.0.foreign_toplevel)), dot_radius, id, ) @@ -1978,7 +1983,7 @@ impl cosmic::Application for CosmicAppList { dock_item .toplevels .iter() - .any(|y| focused_item.contains(&y.0)), + .any(|y| focused_item.contains(&y.0.foreign_toplevel)), dot_radius, id, ) @@ -2142,7 +2147,7 @@ impl CosmicAppList { let mut focused_toplevels: Vec = Vec::new(); let active_workspaces = self.active_workspaces.clone(); for toplevel_list in self.active_list.iter().chain(self.pinned_list.iter()) { - for (t_handle, t_info, _) in &toplevel_list.toplevels { + for (t_info, _) in &toplevel_list.toplevels { if t_info.state.contains(&State::Activated) && active_workspaces .iter() @@ -2153,7 +2158,7 @@ impl CosmicAppList { }) }) { - focused_toplevels.push(t_handle.clone()); + focused_toplevels.push(t_info.foreign_toplevel.clone()); } } } diff --git a/cosmic-app-list/src/wayland_handler.rs b/cosmic-app-list/src/wayland_handler.rs index 5af16faf..54b55f18 100644 --- a/cosmic-app-list/src/wayland_handler.rs +++ b/cosmic-app-list/src/wayland_handler.rs @@ -241,10 +241,7 @@ impl ToplevelInfoHandler for AppData { if let Some(info) = self.toplevel_info_state.info(toplevel) { let _ = self .tx - .unbounded_send(WaylandUpdate::Toplevel(ToplevelUpdate::Add( - toplevel.clone(), - info.clone(), - ))); + .unbounded_send(WaylandUpdate::Toplevel(ToplevelUpdate::Add(info.clone()))); } } @@ -258,7 +255,6 @@ impl ToplevelInfoHandler for AppData { let _ = self .tx .unbounded_send(WaylandUpdate::Toplevel(ToplevelUpdate::Update( - toplevel.clone(), info.clone(), ))); } diff --git a/cosmic-app-list/src/wayland_subscription.rs b/cosmic-app-list/src/wayland_subscription.rs index 7c9cafa2..967c91e2 100644 --- a/cosmic-app-list/src/wayland_subscription.rs +++ b/cosmic-app-list/src/wayland_subscription.rs @@ -123,8 +123,8 @@ pub enum WaylandUpdate { #[derive(Clone, Debug)] pub enum ToplevelUpdate { - Add(ExtForeignToplevelHandleV1, ToplevelInfo), - Update(ExtForeignToplevelHandleV1, ToplevelInfo), + Add(ToplevelInfo), + Update(ToplevelInfo), Remove(ExtForeignToplevelHandleV1), } diff --git a/cosmic-applet-minimize/src/lib.rs b/cosmic-applet-minimize/src/lib.rs index 4b7a2e42..75a695de 100644 --- a/cosmic-applet-minimize/src/lib.rs +++ b/cosmic-applet-minimize/src/lib.rs @@ -45,12 +45,7 @@ pub fn run() -> cosmic::iced::Result { #[derive(Default)] struct Minimize { core: cosmic::app::Core, - apps: Vec<( - ExtForeignToplevelHandleV1, - ToplevelInfo, - DesktopEntryData, - Option, - )>, + apps: Vec<(ToplevelInfo, DesktopEntryData, Option)>, tx: Option>, overflow_popup: Option, } @@ -127,7 +122,7 @@ impl cosmic::Application for Minimize { panic!("Wayland Subscription ended...") } WaylandUpdate::Toplevel(t) => match t { - ToplevelUpdate::Add(handle, info) | ToplevelUpdate::Update(handle, info) => { + ToplevelUpdate::Add(info) | ToplevelUpdate::Update(info) => { let data = |id| { cosmic::desktop::load_applications_for_app_ids( None, @@ -137,24 +132,32 @@ impl cosmic::Application for Minimize { ) .remove(0) }; - if let Some(pos) = self.apps.iter_mut().position(|a| a.0 == handle) { - if self.apps[pos].1.app_id != info.app_id { - self.apps[pos].2 = data(&info.app_id) + if let Some(pos) = self + .apps + .iter_mut() + .position(|a| a.0.foreign_toplevel == info.foreign_toplevel) + { + if self.apps[pos].0.app_id != info.app_id { + self.apps[pos].1 = data(&info.app_id) } - self.apps[pos].1 = info; + self.apps[pos].0 = info; } else { let data = data(&info.app_id); - self.apps.push((handle, info, data, None)); + self.apps.push((info, data, None)); } } ToplevelUpdate::Remove(handle) => { - self.apps.retain(|a| a.0 != handle); + self.apps.retain(|a| a.0.foreign_toplevel != handle); self.apps.shrink_to_fit(); } }, WaylandUpdate::Image(handle, img) => { - if let Some(pos) = self.apps.iter().position(|a| a.0 == handle) { - self.apps[pos].3 = Some(img); + if let Some(pos) = self + .apps + .iter() + .position(|a| a.0.foreign_toplevel == handle) + { + self.apps[pos].2 = Some(img); } } }, @@ -230,31 +233,29 @@ impl cosmic::Application for Minimize { let padding = self.core.applet.suggested_padding(false); let theme = self.core.system_theme().cosmic(); let space_xxs = theme.space_xxs(); - let icon_buttons = self.apps[..max_icon_count] - .iter() - .map(|(handle, _, data, img)| { - tooltip( - Element::from(crate::window_image::WindowImage::new( - img.clone(), - &data.icon, - width as f32, - Message::Activate(handle.clone()), - padding, - )), - text(data.name.clone()).shaping(text::Shaping::Advanced), - // tooltip::Position::FollowCursor, - // FIXME tooltip fails to appear when created as indicated in design - // maybe it should be a subsurface - match self.core.applet.anchor { - PanelAnchor::Left => tooltip::Position::Right, - PanelAnchor::Right => tooltip::Position::Left, - PanelAnchor::Top => tooltip::Position::Bottom, - PanelAnchor::Bottom => tooltip::Position::Top, - }, - ) - .snap_within_viewport(false) - .into() - }); + let icon_buttons = self.apps[..max_icon_count].iter().map(|(info, data, img)| { + tooltip( + Element::from(crate::window_image::WindowImage::new( + img.clone(), + &data.icon, + width as f32, + Message::Activate(info.foreign_toplevel.clone()), + padding, + )), + text(data.name.clone()).shaping(text::Shaping::Advanced), + // tooltip::Position::FollowCursor, + // FIXME tooltip fails to appear when created as indicated in design + // maybe it should be a subsurface + match self.core.applet.anchor { + PanelAnchor::Left => tooltip::Position::Right, + PanelAnchor::Right => tooltip::Position::Left, + PanelAnchor::Top => tooltip::Position::Bottom, + PanelAnchor::Bottom => tooltip::Position::Top, + }, + ) + .snap_within_viewport(false) + .into() + }); let overflow_btn = if max_icon_count < self.apps.len() { let icon = match self.core.applet.anchor { PanelAnchor::Bottom => "go-up-symbolic", @@ -335,31 +336,29 @@ impl cosmic::Application for Minimize { let padding = self.core.applet.suggested_padding(false); let theme = self.core.system_theme().cosmic(); let space_xxs = theme.space_xxs(); - let icon_buttons = self.apps[max_icon_count..] - .iter() - .map(|(handle, _, data, img)| { - tooltip( - Element::from(crate::window_image::WindowImage::new( - img.clone(), - &data.icon, - width as f32, - Message::Activate(handle.clone()), - padding, - )), - text(data.name.clone()).shaping(text::Shaping::Advanced), - // tooltip::Position::FollowCursor, - // FIXME tooltip fails to appear when created as indicated in design - // maybe it should be a subsurface - match self.core.applet.anchor { - PanelAnchor::Left => tooltip::Position::Right, - PanelAnchor::Right => tooltip::Position::Left, - PanelAnchor::Top => tooltip::Position::Bottom, - PanelAnchor::Bottom => tooltip::Position::Top, - }, - ) - .snap_within_viewport(false) - .into() - }); + let icon_buttons = self.apps[max_icon_count..].iter().map(|(info, data, img)| { + tooltip( + Element::from(crate::window_image::WindowImage::new( + img.clone(), + &data.icon, + width as f32, + Message::Activate(info.foreign_toplevel.clone()), + padding, + )), + text(data.name.clone()).shaping(text::Shaping::Advanced), + // tooltip::Position::FollowCursor, + // FIXME tooltip fails to appear when created as indicated in design + // maybe it should be a subsurface + match self.core.applet.anchor { + PanelAnchor::Left => tooltip::Position::Right, + PanelAnchor::Right => tooltip::Position::Left, + PanelAnchor::Top => tooltip::Position::Bottom, + PanelAnchor::Bottom => tooltip::Position::Top, + }, + ) + .snap_within_viewport(false) + .into() + }); // TODO optional dividers on ends if detects app list neighbor // not sure the best way to tell if there is an adjacent app-list diff --git a/cosmic-applet-minimize/src/wayland_handler.rs b/cosmic-applet-minimize/src/wayland_handler.rs index 041e7980..7084b9e4 100644 --- a/cosmic-applet-minimize/src/wayland_handler.rs +++ b/cosmic-applet-minimize/src/wayland_handler.rs @@ -376,9 +376,10 @@ impl ToplevelInfoHandler for AppData { { // spawn thread for sending the image self.send_image(toplevel.clone()); - let _ = futures::executor::block_on(self.tx.send(WaylandUpdate::Toplevel( - ToplevelUpdate::Add(toplevel.clone(), info.clone()), - ))); + let _ = futures::executor::block_on( + self.tx + .send(WaylandUpdate::Toplevel(ToplevelUpdate::Add(info.clone()))), + ); } else { let _ = futures::executor::block_on(self.tx.send(WaylandUpdate::Toplevel( ToplevelUpdate::Remove(toplevel.clone()), @@ -400,7 +401,7 @@ impl ToplevelInfoHandler for AppData { { self.send_image(toplevel.clone()); let _ = futures::executor::block_on(self.tx.send(WaylandUpdate::Toplevel( - ToplevelUpdate::Update(toplevel.clone(), info.clone()), + ToplevelUpdate::Update(info.clone()), ))); } else { let _ = futures::executor::block_on(self.tx.send(WaylandUpdate::Toplevel( diff --git a/cosmic-applet-minimize/src/wayland_subscription.rs b/cosmic-applet-minimize/src/wayland_subscription.rs index 1de64f67..becabebf 100644 --- a/cosmic-applet-minimize/src/wayland_subscription.rs +++ b/cosmic-applet-minimize/src/wayland_subscription.rs @@ -75,8 +75,8 @@ impl AsRef<[u8]> for WaylandImage { #[derive(Clone, Debug)] pub enum ToplevelUpdate { - Add(ExtForeignToplevelHandleV1, ToplevelInfo), - Update(ExtForeignToplevelHandleV1, ToplevelInfo), + Add(ToplevelInfo), + Update(ToplevelInfo), Remove(ExtForeignToplevelHandleV1), }