Skip to content

Commit

Permalink
Remove caching and prepare for tooltip support
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Nov 12, 2024
1 parent e952a29 commit 5b96f07
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
15 changes: 14 additions & 1 deletion cosmic-applet-status-area/src/components/status_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use crate::subscriptions::status_notifier_item::{IconNameOrPixmap, Layout, Statu
pub enum Msg {
Icon(Option<IconNameOrPixmap>),
Layout(Result<Layout, String>),
Tooltip(String),
Click(i32, bool),
}

#[derive(Debug)]
pub struct State {
item: StatusNotifierItem,
layout: Option<Layout>,
tooltip: String,
icon: Option<IconNameOrPixmap>,
expanded: Option<i32>,
}
Expand All @@ -28,6 +30,7 @@ impl State {
layout: None,
expanded: None,
icon: None,
tooltip: Default::default(),
},
iced::Task::none(),
)
Expand All @@ -39,6 +42,10 @@ impl State {
self.icon = icon;
iced::Command::none()
}
Msg::Tooltip(tooltip) => {
self.tooltip = tooltip;
iced::Command::none()
}
Msg::Layout(layout) => {
match layout {
Ok(layout) => {
Expand Down Expand Up @@ -72,11 +79,16 @@ impl State {
}

pub fn icon_handle(&self) -> icon::Handle {
self.icon.as_ref()
self.icon
.as_ref()
.map(|i| i.clone().into())
.unwrap_or_else(|| icon::from_raster_bytes(&[]))
}

pub fn tooltip(&self) -> &str {
&self.tooltip
}

pub fn popup_view(&self) -> cosmic::Element<Msg> {
if let Some(layout) = self.layout.as_ref() {
layout_view(layout, self.expanded)
Expand All @@ -88,6 +100,7 @@ impl State {
pub fn subscription(&self) -> iced::Subscription<Msg> {
let subs = vec![
self.item.icon_subscription().map(Msg::Icon),
self.item.tooltip_subscription().map(Msg::Tooltip),
self.item.layout_subscription().map(Msg::Layout),
];

Expand Down
53 changes: 41 additions & 12 deletions cosmic-applet-status-area/src/subscriptions/status_notifier_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct StatusNotifierItem {
name: String,
// icon_name: String,
// icon_pixmap: Option<icon::Handle>,
_item_proxy: StatusNotifierItemProxy<'static>,
item_proxy: StatusNotifierItemProxy<'static>,
menu_proxy: DBusMenuProxy<'static>,
}

Expand Down Expand Up @@ -55,7 +55,6 @@ impl StatusNotifierItem {
};

let item_proxy = StatusNotifierItemProxy::builder(connection)
.cache_properties(zbus::proxy::CacheProperties::Yes)
.destination(dest.to_string())?
.path(path.to_string())?
.build()
Expand All @@ -70,7 +69,7 @@ impl StatusNotifierItem {

Ok(Self {
name,
_item_proxy: item_proxy,
item_proxy,
menu_proxy,
})
}
Expand All @@ -80,9 +79,9 @@ impl StatusNotifierItem {
}

pub fn icon_subscription(&self) -> iced::Subscription<Option<IconNameOrPixmap>> {
let item_proxy = self._item_proxy.clone();
let item_proxy = self.item_proxy.clone();
iced::subscription::run_with_id(
format!("status-notifier-newicon-{}", &self.name),
format!("status-notifier-icon-{}", &self.name),
async move {
let initial = futures::stream::once(get_icon(item_proxy.clone()));
let updates = item_proxy
Expand All @@ -96,6 +95,20 @@ impl StatusNotifierItem {
)
}

pub fn tooltip_subscription(&self) -> iced::Subscription<String> {
let item_proxy = self.item_proxy.clone();
iced::subscription::run_with_id(
format!("status-notifier-tooltip-{}", &self.name),
async move {
let initial = futures::stream::once(get_tooltip(item_proxy.clone()));
let update_stream = item_proxy.receive_new_tooltip().await.unwrap();
let updates = update_stream.then(move |_| get_tooltip(item_proxy.clone()));
initial.chain(updates)
}
.flatten_stream()
)
}

// 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();
Expand Down Expand Up @@ -123,20 +136,24 @@ async fn get_layout(menu_proxy: DBusMenuProxy<'static>) -> Result<Layout, String
}
}

async fn get_icon(item_proxy: StatusNotifierItemProxy<'static>) -> Option<IconNameOrPixmap> {
if let Ok(pixmaps) = item_proxy.icon_pixmap().await {
// TODO Handle icon with multiple sizes?
return Some(IconNameOrPixmap::Pixmap(
pixmaps.into_iter().max_by_key(|i| (i.width, i.height))?,
));
}
async fn get_tooltip(item_proxy: StatusNotifierItemProxy<'static>) -> String {
item_proxy.tooltip().await.unwrap_or_default()
}

async fn get_icon(item_proxy: StatusNotifierItemProxy<'static>) -> Option<IconNameOrPixmap> {
if let Ok(icon_name) = item_proxy.icon_name().await {
if icon_name != "" {
return Some(IconNameOrPixmap::Name(icon_name));
}
}

if let Ok(pixmaps) = item_proxy.icon_pixmap().await {
// TODO Handle icon with multiple sizes
return Some(IconNameOrPixmap::Pixmap(
pixmaps.into_iter().max_by_key(|i| (i.width, i.height))?,
));
}

None
}

Expand All @@ -149,11 +166,23 @@ trait StatusNotifierItem {
#[zbus(property)]
fn icon_pixmap(&self) -> zbus::Result<Vec<Icon>>;

#[zbus(property)]
fn title(&self) -> zbus::Result<String>;

#[zbus(property)]
fn tooltip(&self) -> zbus::Result<String>;

#[zbus(property)]
fn menu(&self) -> zbus::Result<zvariant::OwnedObjectPath>;

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

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

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

#[derive(Clone, Debug)]
Expand Down

0 comments on commit 5b96f07

Please sign in to comment.