Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/clients/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ impl Client {
pub async fn activate(&self, req: ActivateRequest) -> system_tray::error::Result<()> {
self.client.activate(req).await
}

/// Requests the menu to refresh its contents before being shown
/// (DBusMenu `AboutToShow`). Returns whether the menu should be re-fetched.
pub async fn about_to_show_menuitem(
&self,
address: String,
menu_path: String,
id: i32,
) -> system_tray::error::Result<bool> {
self.client
.about_to_show_menuitem(address, menu_path, id)
.await
}
}

register_fallible_client!(Client, tray);
28 changes: 22 additions & 6 deletions src/modules/tray/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use gtk::{
ShortcutTrigger, prelude::*,
};
use gtk::{Button, Label, PopoverMenu};
use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc;
use system_tray::client::ActivateRequest;
use system_tray::item::{IconPixmap, Status, StatusNotifierItem, Tooltip};
use system_tray::menu::ToggleState;
Expand All @@ -27,7 +29,7 @@ pub(crate) struct TrayMenu {
image_widget: Option<Picture>,
label_widget: Option<Label>,
tx: mpsc::Sender<UiEvent>,
path: Option<String>,
path: Rc<RefCell<Option<String>>>,
address: String,

pub title: Option<String>,
Expand All @@ -49,6 +51,10 @@ impl TrayMenu {

let has_menu = item.menu.is_some();

// Shared with the click handlers so they can request a menu refresh
// (AboutToShow) using the D-Bus object path once it is known.
let path = Rc::new(RefCell::new(None));

// Capture metadata for placeholder substitution in custom commands
let item_name = if !item.id.is_empty() {
item.id.clone()
Expand All @@ -64,6 +70,7 @@ impl TrayMenu {
tx: &mpsc::Sender<UiEvent>,
address: &str,
has_menu: bool,
path: &Option<String>,
name: &str,
title: &Option<String>,
icon_name: &Option<String>| {
Expand All @@ -72,6 +79,12 @@ impl TrayMenu {
trace!("TrayClickAction::Reserved(Menu)");

if has_menu {
if let Some(path) = path {
tx.send_spawn(UiEvent::AboutToShow {
address: address.to_owned(),
path: path.clone(),
});
}
popover.popup();
tx.send_spawn(UiEvent::Menu(true));
} else {
Expand Down Expand Up @@ -131,14 +144,17 @@ impl TrayMenu {
let name = item_name.clone();
let title = item_title.clone();
let icon = item_icon_name.clone();
let path = path.clone();

move || {
let path = path.borrow();
execute_action(
action.clone(),
&pe,
&tx,
&address_owned,
has_menu,
&path,
&name,
&title,
&icon,
Expand Down Expand Up @@ -215,7 +231,7 @@ impl TrayMenu {
icon_name: item.icon_name,
icon_theme_path: item.icon_theme_path.map(PathBuf::from),
icon_pixmap: item.icon_pixmap,
path: None,
path,
address: address.to_owned(),
}
}
Expand Down Expand Up @@ -295,7 +311,7 @@ impl TrayMenu {

pub fn set_menu(&mut self, menu: &str) {
trace!("set menu {}", menu);
self.path = Some(menu.to_owned());
*self.path.borrow_mut() = Some(menu.to_owned());
}

pub fn set_menu_widget(&self, tray_menu: &system_tray::menu::TrayMenu) {
Expand Down Expand Up @@ -324,7 +340,7 @@ impl TrayMenu {
let action = SimpleAction::new(&action_name, None);
let address = self.address.clone();

if let Some(path) = self.path.clone() {
if let Some(path) = self.path.borrow().clone() {
action.connect_activate(move |_, _| activate(&tx, &address, &path, id));
}

Expand All @@ -347,7 +363,7 @@ impl TrayMenu {

let address = self.address.clone();

if let Some(path) = self.path.clone() {
if let Some(path) = self.path.borrow().clone() {
action.connect_change_state(move |_, _| activate(&tx, &address, &path, id));

action.connect_change_state(move |ac, _| {
Expand Down Expand Up @@ -386,7 +402,7 @@ impl TrayMenu {

let address = self.address.clone();

if let Some(path) = self.path.clone() {
if let Some(path) = self.path.borrow().clone() {
action.connect_change_state(move |_, _| activate(&tx, &address, &path, id));
}

Expand Down
10 changes: 10 additions & 0 deletions src/modules/tray/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl Default for TrayModule {

pub enum UiEvent {
Menu(bool),
AboutToShow { address: String, path: String },
Activate(ActivateRequest),
}

Expand Down Expand Up @@ -251,6 +252,15 @@ impl Module<gtk::Box> for TrayModule {
UiEvent::Menu(open) => {
tx.send_expect(ModuleUpdateEvent::LockVisible(open)).await;
}
UiEvent::AboutToShow { address, path } => {
debug!("requesting menu refresh for '{address}'");
if let Err(err) = client
.about_to_show_menuitem(address.clone(), path.clone(), 0)
.await
{
error!("{err:?}");
}
}
UiEvent::Activate(action) => {
debug!("activating: {action:?}");
if let Err(err) = client.activate(action).await {
Expand Down
Loading