Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

systray: add :visible-empty property for systray widget #1097

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ All notable changes to eww will be listed here, starting at changes since versio

### Fixes
- Fix and refactor nix flake (By: w-lfchen)
- Fix deleting items from tray (By: vnva)

### Features
- Add `:truncate` property to labels, disabled by default (except in cases where truncation would be enabled in version `0.5.0` and before) (By: Rayzeq).
- Add support for `:hover` css selectors for tray items (By: zeapoz)
- Add `:visible-empty` property for `systray` widget (By: vnva)

## [0.6.0] (21.04.2024)

Expand Down
17 changes: 15 additions & 2 deletions crates/eww/src/widgets/systray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,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>>,
pub visible_empty: Rc<RefCell<bool>>,
}

impl Props {
pub fn new() -> Self {
let (icon_size_tx, _) = tokio::sync::watch::channel(24);
Self { icon_size_tx, prepend_new: Rc::new(RefCell::new(false)) }
Self { icon_size_tx, prepend_new: Rc::new(RefCell::new(false)), visible_empty: Rc::new(RefCell::new(true)) }
}

pub fn icon_size(&self, value: i32) {
Expand All @@ -60,6 +61,7 @@ struct Tray {

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

pub fn spawn_systray(container: &gtk::Box, props: &Props) {
Expand All @@ -68,6 +70,7 @@ pub fn spawn_systray(container: &gtk::Box, props: &Props) {
items: Default::default(),
icon_size: props.icon_size_tx.subscribe(),
prepend_new: props.prepend_new.clone(),
visible_empty: props.visible_empty.clone(),
};

let task = glib::MainContext::default().spawn_local(async move {
Expand All @@ -79,7 +82,10 @@ pub fn spawn_systray(container: &gtk::Box, props: &Props) {
}
};

systray.container.show();
if !*systray.visible_empty.borrow() {
systray.container.hide();
}

let e = notifier_host::run_host(&mut systray, &s.snw).await;
log::error!("notifier host error: {}", e);
});
Expand All @@ -101,14 +107,21 @@ impl notifier_host::Host for Tray {
if let Some(old_item) = self.items.insert(id.to_string(), item) {
self.container.remove(&old_item.widget);
}
if !*self.visible_empty.borrow() && !self.container.is_visible() && self.items.len() > 0 {
self.container.show();
}
}

fn remove_item(&mut self, id: &str) {
if let Some(item) = self.items.get(id) {
self.container.remove(&item.widget);
self.items.remove(id);
} else {
log::warn!("Tried to remove nonexistent item {:?} from systray", id);
}
if !*self.visible_empty.borrow() && self.container.is_visible() && self.items.len() == 0 {
self.container.hide();
}
}
}

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 @@ -1200,6 +1200,7 @@ fn build_systray(bargs: &mut BuilderArgs) -> Result<gtk::Box> {
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
let props_clone3 = props.clone(); // copies for def_widget

def_widget!(bargs, _g, gtk_widget, {
// @prop spacing - spacing between elements
Expand All @@ -1220,6 +1221,10 @@ fn build_systray(bargs: &mut BuilderArgs) -> Result<gtk::Box> {
prop(prepend_new: as_bool = true) {
*props_clone2.prepend_new.borrow_mut() = prepend_new;
},
// @prop visible-empty - visibility of the widget when the systray is empty
prop(visible_empty: as_bool = true) {
*props_clone3.visible_empty.borrow_mut() = visible_empty;
},
});

systray::spawn_systray(&gtk_widget, &props_clone);
Expand Down