Detect registered/unregisterered in StatusArea
This commit is contained in:
parent
720c40f5f2
commit
9fca893fe1
1 changed files with 69 additions and 18 deletions
|
|
@ -10,12 +10,16 @@ use gtk4::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
subclass::prelude::*,
|
subclass::prelude::*,
|
||||||
};
|
};
|
||||||
|
use once_cell::unsync::OnceCell;
|
||||||
|
use std::{cell::RefCell, collections::HashMap};
|
||||||
|
|
||||||
use crate::deref_cell::DerefCell;
|
use crate::deref_cell::DerefCell;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct StatusAreaInner {
|
pub struct StatusAreaInner {
|
||||||
box_: DerefCell<gtk4::Box>,
|
box_: DerefCell<gtk4::Box>,
|
||||||
|
watcher: OnceCell<StatusNotifierWatcher>,
|
||||||
|
icons: RefCell<HashMap<String, gtk4::Image>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[glib::object_subclass]
|
#[glib::object_subclass]
|
||||||
|
|
@ -47,10 +51,23 @@ impl ObjectImpl for StatusAreaInner {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for i in watcher.registered_status_notifier_items().await {
|
for name in watcher.registered_items().await {
|
||||||
let image = gtk4::Image::from_icon_name(i.icon_name().as_deref());
|
glib::MainContext::default().spawn_local(clone!(@strong obj => async move {
|
||||||
obj.inner().box_.append(&image);
|
obj.item_registered(&name).await;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
watcher.connect_item_registered_unregistered(clone!(@strong obj => move |name, registered| {
|
||||||
|
if registered {
|
||||||
|
glib::MainContext::default().spawn_local(clone!(@strong obj => async move {
|
||||||
|
obj.item_registered(&name).await;
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
obj.item_unregistered(&name);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
let _ = obj.inner().watcher.set(watcher);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,18 +91,41 @@ impl StatusArea {
|
||||||
fn inner(&self) -> &StatusAreaInner {
|
fn inner(&self) -> &StatusAreaInner {
|
||||||
StatusAreaInner::from_instance(self)
|
StatusAreaInner::from_instance(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn item_registered(&self, name: &str) {
|
||||||
|
match StatusNotifierItem::new(&name).await {
|
||||||
|
Ok(item) => {
|
||||||
|
let image = gtk4::Image::from_icon_name(item.icon_name().as_deref());
|
||||||
|
self.inner().box_.append(&image);
|
||||||
|
|
||||||
|
self.item_unregistered(name);
|
||||||
|
self.inner()
|
||||||
|
.icons
|
||||||
|
.borrow_mut()
|
||||||
|
.insert(name.to_owned(), image);
|
||||||
|
}
|
||||||
|
Err(err) => eprintln!("Failed to connect to '{}': {}", name, err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_unregistered(&self, name: &str) {
|
||||||
|
if let Some(icon) = self.inner().icons.borrow_mut().remove(name) {
|
||||||
|
self.inner().box_.remove(&icon);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StatusNotifierItem(gio::DBusProxy);
|
struct StatusNotifierItem(gio::DBusProxy);
|
||||||
|
|
||||||
impl StatusNotifierItem {
|
impl StatusNotifierItem {
|
||||||
async fn new(dest: &str, path: &str) -> Result<Self, glib::Error> {
|
async fn new(name: &str) -> Result<Self, glib::Error> {
|
||||||
|
let idx = name.find('/').unwrap();
|
||||||
let proxy = gio::DBusProxy::for_bus_future(
|
let proxy = gio::DBusProxy::for_bus_future(
|
||||||
gio::BusType::Session,
|
gio::BusType::Session,
|
||||||
gio::DBusProxyFlags::NONE,
|
gio::DBusProxyFlags::NONE,
|
||||||
None,
|
None,
|
||||||
dest,
|
&name[..idx],
|
||||||
path,
|
&name[idx..],
|
||||||
"org.kde.StatusNotifierItem",
|
"org.kde.StatusNotifierItem",
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -127,18 +167,29 @@ impl StatusNotifierWatcher {
|
||||||
self.0.cached_property(prop)?.get()
|
self.0.cached_property(prop)?.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn registered_status_notifier_items(&self) -> Vec<StatusNotifierItem> {
|
async fn registered_items(&self) -> Vec<String> {
|
||||||
let mut items = Vec::new();
|
self.property::<Vec<String>>("RegisteredStatusNotifierItems")
|
||||||
for i in self
|
|
||||||
.property::<Vec<String>>("RegisteredStatusNotifierItems")
|
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
{
|
}
|
||||||
let idx = i.find('/').unwrap();
|
|
||||||
match StatusNotifierItem::new(&i[..idx], &i[idx..]).await {
|
fn connect_item_registered_unregistered<F: Fn(String, bool) + 'static>(
|
||||||
Ok(item) => items.push(item),
|
&self,
|
||||||
Err(err) => eprintln!("Failed to connect to '{}': {}", i, err),
|
f: F,
|
||||||
}
|
) -> glib::SignalHandlerId {
|
||||||
}
|
self.0
|
||||||
items
|
.connect_local("g-signal", false, move |args| {
|
||||||
|
let signal_args = args[3].get::<glib::Variant>().unwrap();
|
||||||
|
match args[2].get::<String>().unwrap().as_str() {
|
||||||
|
"StatusNotifierItemRegistered" => {
|
||||||
|
f(signal_args.get::<(String,)>().unwrap().0, true);
|
||||||
|
}
|
||||||
|
"StatusNotifierItemUnregistered" => {
|
||||||
|
f(signal_args.get::<(String,)>().unwrap().0, false);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue