Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions cosmic-applet-status-area/src/components/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum Msg {
TogglePopup(usize),
Hovered(usize),
Surface(surface::Action),
RightPressed(usize),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to name this.
If we go with this name, it might also make sense to rename TogglePopup to LeftPressed to better reflect the logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be named based on what action the message will perform.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I renamed it to OpenContextMenu. Let me know if that's a suitable name.

}

#[derive(Default)]
Expand Down Expand Up @@ -188,6 +189,10 @@ impl cosmic::Application for App {
}
Task::none()
}
Msg::RightPressed(id) => {
self.menus[&id].ctx_menu_activate();
Task::none()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be sure I'm not sure about what to return here.
I need to better familiarize myself with tasks (and async Rust in general)

}
Msg::Hovered(id) => {
let mut cmds = Vec::new();
if let Some(old_id) = self.open_menu.take() {
Expand Down Expand Up @@ -261,6 +266,7 @@ impl cosmic::Application for App {
.on_press_down(Msg::TogglePopup(*id)),
)
.on_enter(Msg::Hovered(*id))
.on_right_press(Msg::RightPressed(*id))
.into()
});
self.core
Expand Down
48 changes: 42 additions & 6 deletions cosmic-applet-status-area/src/components/status_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ impl State {

iced::Task::none()
}
Msg::Click(id, is_submenu) => {
let menu_proxy = self.item.menu_proxy().clone();
Msg::Click(id, is_submenu) => 'block: {
let Some(menu_proxy) = self.item.menu_proxy().cloned() else {
tracing::error!("Msg::click on item without menu_proxy");
break 'block iced::Task::none();
};
tokio::spawn(async move {
let _ = menu_proxy.event(id, "clicked", &0.into(), 0).await;
});
Expand Down Expand Up @@ -118,15 +121,48 @@ impl State {
}

pub fn opened(&self) {
let menu_proxy = self.item.menu_proxy().clone();
let Some(menu_proxy) = self.item.menu_proxy().cloned() else {
let item_proxy = self.item.item_proxy().clone();
tokio::spawn(async move {
if let Err(e) = item_proxy.activate(0, 0).await {
tracing::error!("Error on Activate: {e}");
}
});
return;
};
let item_proxy = self.item.item_proxy().clone();
tokio::spawn(async move {
let is_menu = match item_proxy.item_is_menu().await {
Ok(is_menu) => is_menu,
Err(e) => {
tracing::error!("Error on ItemIsMenu: {e}");
false
}
};
if is_menu {
let _ = menu_proxy.event(0, "opened", &0i32.into(), 0).await;
let _ = menu_proxy.about_to_show(0).await;
} else {
if let Err(e) = item_proxy.activate(0, 0).await {
tracing::error!("Error on Activate: {e}");
}
}
});
}

pub fn ctx_menu_activate(&self) {
let item_proxy = self.item.item_proxy().clone();
tokio::spawn(async move {
let _ = menu_proxy.event(0, "opened", &0i32.into(), 0).await;
let _ = menu_proxy.about_to_show(0).await;
if let Err(e) = item_proxy.context_menu(1920, 0).await {
tracing::error!("Error on ContextMenu: {e}");
}
});
}

pub fn closed(&self) {
let menu_proxy = self.item.menu_proxy().clone();
let Some(menu_proxy) = self.item.menu_proxy().cloned() else {
return;
};
tokio::spawn(async move {
let _ = menu_proxy.event(0, "closed", &0i32.into(), 0).await;
});
Expand Down
37 changes: 27 additions & 10 deletions cosmic-applet-status-area/src/subscriptions/status_notifier_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use zbus::zvariant::{self, OwnedValue};
pub struct StatusNotifierItem {
name: String,
item_proxy: StatusNotifierItemProxy<'static>,
menu_proxy: DBusMenuProxy<'static>,
menu_proxy: Option<DBusMenuProxy<'static>>,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We make menu_proxy optional to reflect the fact that some clients don't provide a DBus menu.

}

#[derive(Clone, Debug, zvariant::Value)]
Expand Down Expand Up @@ -44,12 +44,19 @@ impl StatusNotifierItem {
.build()
.await?;

let menu_path = item_proxy.menu().await?;
let menu_proxy = DBusMenuProxy::builder(connection)
.destination(dest.to_string())?
.path(menu_path)?
.build()
.await?;
let menu_proxy = match item_proxy.menu().await {
Ok(menu_path) => Some(
DBusMenuProxy::builder(connection)
.destination(dest.to_string())?
.path(menu_path)?
.build()
.await?,
),
Err(e) => {
eprintln!("Error: {e}");
None
}
};

Ok(Self {
name,
Expand All @@ -64,7 +71,9 @@ impl StatusNotifierItem {

// TODO: Only fetch changed part of layout, if that's any faster
pub fn layout_subscription(&self) -> iced::Subscription<Result<Layout, String>> {
let menu_proxy = self.menu_proxy.clone();
let Some(menu_proxy) = self.menu_proxy.clone() else {
return iced::Subscription::none();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what iced subscriptions are, or if it's okay to return none here. I'm gonna have to look into that as well.

Copy link
Member

@mmstick mmstick May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are tasks that automatically spawn for the duration that they remain in the subscription state of the application. Typically kept active for the whole duration of the application.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. It looks like Layout is used for menus only, so Subscription::none() can be safely returned here.
I also renamed layout_subscription to menu_layout_subscription to help clarify this.

};
Subscription::run_with_id(
format!("status-notifier-item-layout-{}", &self.name),
async move {
Expand Down Expand Up @@ -109,8 +118,12 @@ impl StatusNotifierItem {
)
}

pub fn menu_proxy(&self) -> &DBusMenuProxy<'static> {
&self.menu_proxy
pub fn menu_proxy(&self) -> Option<&DBusMenuProxy<'static>> {
self.menu_proxy.as_ref()
}

pub fn item_proxy(&self) -> &StatusNotifierItemProxy<'static> {
&self.item_proxy
}
}

Expand All @@ -135,6 +148,10 @@ trait StatusNotifierItem {

#[zbus(signal)]
fn new_icon(&self) -> zbus::Result<()>;

fn activate(&self, x: i32, y: i32) -> zbus::Result<()>;
fn context_menu(&self, x: i32, y: i32) -> zbus::Result<()>;
fn item_is_menu(&self) -> zbus::Result<bool>;
}

#[derive(Clone, Debug)]
Expand Down