From 149727ce1f7dd4f461ab1d61d560546f3d1f32a1 Mon Sep 17 00:00:00 2001 From: MoetaYuko Date: Mon, 26 Feb 2024 13:38:35 +0800 Subject: [PATCH] systray: add prepend-new property to prepend new icons to the container --- crates/eww/src/widgets/systray.rs | 19 +++++++++++++++---- crates/eww/src/widgets/widget_definitions.rs | 5 +++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/eww/src/widgets/systray.rs b/crates/eww/src/widgets/systray.rs index 2a9b46f1..feab47e4 100644 --- a/crates/eww/src/widgets/systray.rs +++ b/crates/eww/src/widgets/systray.rs @@ -2,7 +2,7 @@ use crate::widgets::window::Window; use futures::StreamExt; use gtk::{cairo::Surface, gdk::ffi::gdk_cairo_surface_create_from_pixbuf, prelude::*}; use notifier_host; -use std::{future::Future, rc::Rc}; +use std::{cell::RefCell, future::Future, rc::Rc}; // DBus state shared between systray instances, to avoid creating too many connections etc. struct DBusSession { @@ -32,12 +32,13 @@ fn run_async_task(f: F) -> F::Output { pub struct Props { icon_size_tx: tokio::sync::watch::Sender, + pub prepend_new: Rc>, } impl Props { pub fn new() -> Self { let (icon_size_tx, _) = tokio::sync::watch::channel(24); - Self { icon_size_tx } + Self { icon_size_tx, prepend_new: Rc::new(RefCell::new(false)) } } pub fn icon_size(&self, value: i32) { @@ -57,10 +58,16 @@ struct Tray { items: std::collections::HashMap, icon_size: tokio::sync::watch::Receiver, + prepend_new: Rc>, } pub fn spawn_systray(container: >k::Box, props: &Props) { - let mut systray = Tray { container: container.clone(), items: Default::default(), icon_size: props.icon_size_tx.subscribe() }; + let mut systray = Tray { + container: container.clone(), + items: Default::default(), + icon_size: props.icon_size_tx.subscribe(), + prepend_new: props.prepend_new.clone(), + }; let task = glib::MainContext::default().spawn_local(async move { let s = match dbus_session().await { @@ -85,7 +92,11 @@ pub fn spawn_systray(container: >k::Box, props: &Props) { impl notifier_host::Host for Tray { fn add_item(&mut self, id: &str, item: notifier_host::Item) { let item = Item::new(id.to_owned(), item, self.icon_size.clone()); - self.container.add(&item.widget); + if *self.prepend_new.borrow() { + self.container.pack_end(&item.widget, true, true, 0); + } else { + self.container.pack_start(&item.widget, true, true, 0); + } if let Some(old_item) = self.items.insert(id.to_string(), item) { self.container.remove(&old_item.widget); } diff --git a/crates/eww/src/widgets/widget_definitions.rs b/crates/eww/src/widgets/widget_definitions.rs index 18dff789..a1d9014b 100644 --- a/crates/eww/src/widgets/widget_definitions.rs +++ b/crates/eww/src/widgets/widget_definitions.rs @@ -1142,6 +1142,7 @@ fn build_systray(bargs: &mut BuilderArgs) -> Result { let gtk_widget = gtk::Box::new(gtk::Orientation::Horizontal, 0); let props = Rc::new(systray::Props::new()); let props_clone = props.clone(); // copies for def_widget + let props_clone2 = props.clone(); // copies for def_widget def_widget!(bargs, _g, gtk_widget, { // @prop spacing - spacing between elements @@ -1158,6 +1159,10 @@ fn build_systray(bargs: &mut BuilderArgs) -> Result { props.icon_size(icon_size); } }, + // @prop prepend-new - prepend new icons. + prop(prepend_new: as_bool = true) { + *props_clone2.prepend_new.borrow_mut() = prepend_new; + }, }); systray::spawn_systray(>k_widget, &props_clone);