Initial implementation of StatusArea

Just shows icons, and doesn't handle added/removed icons.
This commit is contained in:
Ian Douglas Scott 2021-08-26 14:22:48 -07:00
parent 843fba67b1
commit 720c40f5f2
2 changed files with 90 additions and 2 deletions

View file

@ -243,7 +243,7 @@ impl MprisControls {
let artist = metadata
.artist()
.and_then(|x| x.get(0).cloned())
.unwrap_or_else(|| String::new());
.unwrap_or_default();
let _album = metadata.album(); // TODO

View file

@ -1,6 +1,12 @@
// TODO
// - Implement StatusNotifierWatcher if one is not running
// - Register with StatusNotiferWatcher
// - Handle signals for registered/unreigisted items
use cascade::cascade;
use gtk4::{
glib,
gio,
glib::{self, clone},
prelude::*,
subclass::prelude::*,
};
@ -31,6 +37,21 @@ impl ObjectImpl for StatusAreaInner {
};
self.box_.set(box_);
glib::MainContext::default().spawn_local(clone!(@strong obj => async move {
let watcher = match StatusNotifierWatcher::new().await {
Ok(watcher) => watcher,
Err(err) => {
eprintln!("Failed to connect to 'org.kde.StatusNotifierWatcher': {}", err);
return;
}
};
for i in watcher.registered_status_notifier_items().await {
let image = gtk4::Image::from_icon_name(i.icon_name().as_deref());
obj.inner().box_.append(&image);
}
}));
}
fn dispose(&self, _obj: &StatusArea) {
@ -54,3 +75,70 @@ impl StatusArea {
StatusAreaInner::from_instance(self)
}
}
struct StatusNotifierItem(gio::DBusProxy);
impl StatusNotifierItem {
async fn new(dest: &str, path: &str) -> Result<Self, glib::Error> {
let proxy = gio::DBusProxy::for_bus_future(
gio::BusType::Session,
gio::DBusProxyFlags::NONE,
None,
dest,
path,
"org.kde.StatusNotifierItem",
)
.await?;
Ok(Self(proxy))
}
fn property<T: glib::FromVariant>(&self, prop: &str) -> Option<T> {
self.0.cached_property(prop)?.get()
}
fn icon_name(&self) -> Option<String> {
// TODO: IconThemePath? AttentionIconName?
self.property("IconName")
}
fn menu(&self) -> Option<String> {
// TODO: Return menu rather than just string
self.property("Menu")
}
}
struct StatusNotifierWatcher(gio::DBusProxy);
impl StatusNotifierWatcher {
async fn new() -> Result<Self, glib::Error> {
let proxy = gio::DBusProxy::for_bus_future(
gio::BusType::Session,
gio::DBusProxyFlags::NONE,
None,
"org.kde.StatusNotifierWatcher",
"/StatusNotifierWatcher",
"org.kde.StatusNotifierWatcher",
)
.await?;
Ok(Self(proxy))
}
fn property<T: glib::FromVariant>(&self, prop: &str) -> Option<T> {
self.0.cached_property(prop)?.get()
}
async fn registered_status_notifier_items(&self) -> Vec<StatusNotifierItem> {
let mut items = Vec::new();
for i in self
.property::<Vec<String>>("RegisteredStatusNotifierItems")
.unwrap_or_default()
{
let idx = i.find('/').unwrap();
match StatusNotifierItem::new(&i[..idx], &i[idx..]).await {
Ok(item) => items.push(item),
Err(err) => eprintln!("Failed to connect to '{}': {}", i, err),
}
}
items
}
}