Skip to content

Commit

Permalink
systray: add prepend-new property to prepend new icons to the container
Browse files Browse the repository at this point in the history
  • Loading branch information
moetayuko committed Feb 26, 2024
1 parent 059c608 commit 9c5d5fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 16 additions & 7 deletions crates/eww/src/widgets/systray.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::widgets::window::Window;
use gtk::{cairo::Surface, gdk::ffi::gdk_cairo_surface_create_from_pixbuf, prelude::*};
use notifier_host::{self, export::ordered_stream::OrderedStreamExt};
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 {
Expand All @@ -21,9 +21,7 @@ async fn dbus_session() -> zbus::Result<&'static DBusSession> {
let name = notifier_host::attach_new_wellknown_name(&con).await?;
let snw = notifier_host::register_to_watcher(&con, &name).await?;

Ok(DBusSession {
snw,
})
Ok(DBusSession { snw })
})
.await
}
Expand All @@ -35,12 +33,13 @@ fn run_async_task<F: Future>(f: F) -> F::Output {

pub struct Props {
icon_size_tx: tokio::sync::watch::Sender<i32>,
pub prepend_new: Rc<RefCell<bool>>,
}

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) {
Expand All @@ -60,10 +59,16 @@ struct Tray {
items: std::collections::HashMap<String, Item>,

icon_size: tokio::sync::watch::Receiver<i32>,
prepend_new: Rc<RefCell<bool>>,
}

pub fn spawn_systray(container: &gtk::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 {
Expand All @@ -89,7 +94,11 @@ pub fn spawn_systray(container: &gtk::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);
}
Expand Down
5 changes: 5 additions & 0 deletions crates/eww/src/widgets/widget_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ fn build_systray(bargs: &mut BuilderArgs) -> Result<gtk::Box> {
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
Expand All @@ -1158,6 +1159,10 @@ fn build_systray(bargs: &mut BuilderArgs) -> Result<gtk::Box> {
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(&gtk_widget, &props_clone);
Expand Down

0 comments on commit 9c5d5fb

Please sign in to comment.