Create seperate widget for status menu
This commit is contained in:
parent
1c2e1fbe5b
commit
87eb5e173b
3 changed files with 231 additions and 172 deletions
|
|
@ -3,6 +3,7 @@ use gtk4::{gdk, glib, prelude::*};
|
||||||
mod deref_cell;
|
mod deref_cell;
|
||||||
mod mpris;
|
mod mpris;
|
||||||
mod status_area;
|
mod status_area;
|
||||||
|
mod status_menu;
|
||||||
mod time_button;
|
mod time_button;
|
||||||
mod window;
|
mod window;
|
||||||
mod x;
|
mod x;
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
// - Register with StatusNotiferWatcher
|
// - Register with StatusNotiferWatcher
|
||||||
// - Handle signals for registered/unreigisted items
|
// - Handle signals for registered/unreigisted items
|
||||||
|
|
||||||
use byte_string::ByteStr;
|
|
||||||
use cascade::cascade;
|
use cascade::cascade;
|
||||||
use gtk4::{
|
use gtk4::{
|
||||||
gio,
|
gio,
|
||||||
|
|
@ -12,15 +11,16 @@ use gtk4::{
|
||||||
subclass::prelude::*,
|
subclass::prelude::*,
|
||||||
};
|
};
|
||||||
use once_cell::unsync::OnceCell;
|
use once_cell::unsync::OnceCell;
|
||||||
use std::{borrow::Cow, cell::RefCell, collections::HashMap, fmt};
|
use std::{cell::RefCell, collections::HashMap};
|
||||||
|
|
||||||
use crate::deref_cell::DerefCell;
|
use crate::deref_cell::DerefCell;
|
||||||
|
use crate::status_menu::StatusMenu;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct StatusAreaInner {
|
pub struct StatusAreaInner {
|
||||||
box_: DerefCell<gtk4::Box>,
|
box_: DerefCell<gtk4::Box>,
|
||||||
watcher: OnceCell<StatusNotifierWatcher>,
|
watcher: OnceCell<StatusNotifierWatcher>,
|
||||||
icons: RefCell<HashMap<String, gtk4::Image>>,
|
icons: RefCell<HashMap<String, StatusMenu>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[glib::object_subclass]
|
#[glib::object_subclass]
|
||||||
|
|
@ -94,20 +94,15 @@ impl StatusArea {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn item_registered(&self, name: &str) {
|
async fn item_registered(&self, name: &str) {
|
||||||
match StatusNotifierItem::new(&name).await {
|
match StatusMenu::new(&name).await {
|
||||||
Ok(item) => {
|
Ok(item) => {
|
||||||
let image = gtk4::Image::from_icon_name(item.icon_name().as_deref());
|
self.inner().box_.append(&item);
|
||||||
self.inner().box_.append(&image);
|
|
||||||
|
|
||||||
if let Some(menu) = item.menu() {
|
|
||||||
println!("{:#?}", menu.get_layout(0, -1, &[]).await);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.item_unregistered(name);
|
self.item_unregistered(name);
|
||||||
self.inner()
|
self.inner()
|
||||||
.icons
|
.icons
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.insert(name.to_owned(), image);
|
.insert(name.to_owned(), item);
|
||||||
}
|
}
|
||||||
Err(err) => eprintln!("Failed to connect to '{}': {}", name, err),
|
Err(err) => eprintln!("Failed to connect to '{}': {}", name, err),
|
||||||
}
|
}
|
||||||
|
|
@ -120,167 +115,6 @@ impl StatusArea {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//#[derive(Debug)]
|
|
||||||
struct Layout(i32, HashMap<String, glib::Variant>, Vec<Layout>);
|
|
||||||
|
|
||||||
impl fmt::Debug for Layout {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let mut s = f.debug_struct("Layout");
|
|
||||||
s.field("id", &self.0);
|
|
||||||
for (k, v) in &self.1 {
|
|
||||||
if let Some(v) = v.get::<String>() {
|
|
||||||
s.field(k, &v);
|
|
||||||
} else if let Some(v) = v.get::<i32>() {
|
|
||||||
s.field(k, &v);
|
|
||||||
} else if let Some(v) = v.get::<bool>() {
|
|
||||||
s.field(k, &v);
|
|
||||||
} else if let Some(v) = v.get::<Vec<u8>>() {
|
|
||||||
s.field(k, &ByteStr::new(&v));
|
|
||||||
} else {
|
|
||||||
s.field(k, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.field("children", &self.2);
|
|
||||||
s.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
impl Layout {
|
|
||||||
fn prop<T: glib::FromVariant>(&self, name: &str) -> Option<T> {
|
|
||||||
self.1.get(name)?.get()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn accessible_desc(&self) -> Option<String> {
|
|
||||||
self.prop("accessible-desc")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn children_display(&self) -> Option<String> {
|
|
||||||
self.prop("children-display")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn label(&self) -> Option<String> {
|
|
||||||
self.prop("label")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn enabled(&self) -> Option<bool> {
|
|
||||||
self.prop("enabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visible(&self) -> Option<bool> {
|
|
||||||
self.prop("visible")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn type_(&self) -> Option<String> {
|
|
||||||
self.prop("type")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn toggle_type(&self) -> Option<String> {
|
|
||||||
self.prop("toggle-type")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn toggle_state(&self) -> Option<bool> {
|
|
||||||
self.prop("toggle-state")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn icon_data(&self) -> Option<Vec<u8>> {
|
|
||||||
self.prop("icon-data")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl glib::StaticVariantType for Layout {
|
|
||||||
fn static_variant_type() -> Cow<'static, glib::VariantTy> {
|
|
||||||
glib::VariantTy::new("(ia{sv}av)").unwrap().into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl glib::FromVariant for Layout {
|
|
||||||
fn from_variant(variant: &glib::Variant) -> Option<Self> {
|
|
||||||
let (id, props, children) = variant.get::<(_, _, Vec<glib::Variant>)>()?;
|
|
||||||
let children = children.iter().filter_map(Self::from_variant).collect();
|
|
||||||
Some(Self(id, props, children))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct DBusMenu(gio::DBusProxy);
|
|
||||||
|
|
||||||
impl DBusMenu {
|
|
||||||
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,
|
|
||||||
"com.canonical.dbusmenu",
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(Self(proxy))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_layout(
|
|
||||||
&self,
|
|
||||||
parent: i32,
|
|
||||||
depth: i32,
|
|
||||||
properties: &[&str],
|
|
||||||
) -> Result<(u32, Layout), glib::Error> {
|
|
||||||
// XXX unwrap
|
|
||||||
Ok(self
|
|
||||||
.0
|
|
||||||
.call_future(
|
|
||||||
"GetLayout",
|
|
||||||
Some(&(parent, depth, properties).to_variant()),
|
|
||||||
gio::DBusCallFlags::NONE,
|
|
||||||
1000,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
.get()
|
|
||||||
.unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct StatusNotifierItem(gio::DBusProxy, Option<DBusMenu>);
|
|
||||||
|
|
||||||
impl StatusNotifierItem {
|
|
||||||
async fn new(name: &str) -> Result<Self, glib::Error> {
|
|
||||||
let idx = name.find('/').unwrap();
|
|
||||||
let dest = &name[..idx];
|
|
||||||
let path = &name[idx..];
|
|
||||||
let proxy = gio::DBusProxy::for_bus_future(
|
|
||||||
gio::BusType::Session,
|
|
||||||
gio::DBusProxyFlags::NONE,
|
|
||||||
None,
|
|
||||||
dest,
|
|
||||||
path,
|
|
||||||
"org.kde.StatusNotifierItem",
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let menu_path = proxy
|
|
||||||
.cached_property("Menu")
|
|
||||||
.and_then(|x| x.get::<String>());
|
|
||||||
let menu = if let Some(menu_path) = menu_path {
|
|
||||||
Some(DBusMenu::new(dest, &menu_path).await?)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
Ok(Self(proxy, menu))
|
|
||||||
}
|
|
||||||
|
|
||||||
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<DBusMenu> {
|
|
||||||
self.1.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct StatusNotifierWatcher(gio::DBusProxy);
|
struct StatusNotifierWatcher(gio::DBusProxy);
|
||||||
|
|
||||||
impl StatusNotifierWatcher {
|
impl StatusNotifierWatcher {
|
||||||
|
|
|
||||||
224
src/status_menu.rs
Normal file
224
src/status_menu.rs
Normal file
|
|
@ -0,0 +1,224 @@
|
||||||
|
use byte_string::ByteStr;
|
||||||
|
use cascade::cascade;
|
||||||
|
use gtk4::{gio, glib, prelude::*, subclass::prelude::*};
|
||||||
|
use std::{borrow::Cow, collections::HashMap, fmt};
|
||||||
|
|
||||||
|
use crate::deref_cell::DerefCell;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct StatusMenuInner {
|
||||||
|
menu_button: DerefCell<gtk4::MenuButton>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for StatusMenuInner {
|
||||||
|
const NAME: &'static str = "S76StatusMenu";
|
||||||
|
type ParentType = gtk4::Widget;
|
||||||
|
type Type = StatusMenu;
|
||||||
|
|
||||||
|
fn class_init(klass: &mut Self::Class) {
|
||||||
|
klass.set_layout_manager_type::<gtk4::BinLayout>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for StatusMenuInner {
|
||||||
|
fn constructed(&self, obj: &StatusMenu) {
|
||||||
|
let menu_button = cascade! {
|
||||||
|
gtk4::MenuButton::new();
|
||||||
|
..set_parent(obj);
|
||||||
|
};
|
||||||
|
|
||||||
|
self.menu_button.set(menu_button);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dispose(&self, _obj: &StatusMenu) {
|
||||||
|
self.menu_button.unparent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WidgetImpl for StatusMenuInner {}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct StatusMenu(ObjectSubclass<StatusMenuInner>)
|
||||||
|
@extends gtk4::Widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StatusMenu {
|
||||||
|
pub async fn new(name: &str) -> Result<Self, glib::Error> {
|
||||||
|
let item = StatusNotifierItem::new(name).await?;
|
||||||
|
let obj = glib::Object::new::<Self>(&[]).unwrap();
|
||||||
|
if let Some(icon_name) = item.icon_name().as_deref() {
|
||||||
|
obj.inner().menu_button.set_icon_name(&icon_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(menu) = item.menu() {
|
||||||
|
println!("{:#?}", menu.get_layout(0, -1, &[]).await);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inner(&self) -> &StatusMenuInner {
|
||||||
|
StatusMenuInner::from_instance(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Layout(i32, HashMap<String, glib::Variant>, Vec<Layout>);
|
||||||
|
|
||||||
|
impl fmt::Debug for Layout {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let mut s = f.debug_struct("Layout");
|
||||||
|
s.field("id", &self.0);
|
||||||
|
for (k, v) in &self.1 {
|
||||||
|
if let Some(v) = v.get::<String>() {
|
||||||
|
s.field(k, &v);
|
||||||
|
} else if let Some(v) = v.get::<i32>() {
|
||||||
|
s.field(k, &v);
|
||||||
|
} else if let Some(v) = v.get::<bool>() {
|
||||||
|
s.field(k, &v);
|
||||||
|
} else if let Some(v) = v.get::<Vec<u8>>() {
|
||||||
|
s.field(k, &ByteStr::new(&v));
|
||||||
|
} else {
|
||||||
|
s.field(k, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.field("children", &self.2);
|
||||||
|
s.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl Layout {
|
||||||
|
fn prop<T: glib::FromVariant>(&self, name: &str) -> Option<T> {
|
||||||
|
self.1.get(name)?.get()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn accessible_desc(&self) -> Option<String> {
|
||||||
|
self.prop("accessible-desc")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn children_display(&self) -> Option<String> {
|
||||||
|
self.prop("children-display")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn label(&self) -> Option<String> {
|
||||||
|
self.prop("label")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enabled(&self) -> Option<bool> {
|
||||||
|
self.prop("enabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visible(&self) -> Option<bool> {
|
||||||
|
self.prop("visible")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type_(&self) -> Option<String> {
|
||||||
|
self.prop("type")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn toggle_type(&self) -> Option<String> {
|
||||||
|
self.prop("toggle-type")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn toggle_state(&self) -> Option<bool> {
|
||||||
|
self.prop("toggle-state")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn icon_data(&self) -> Option<Vec<u8>> {
|
||||||
|
self.prop("icon-data")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl glib::StaticVariantType for Layout {
|
||||||
|
fn static_variant_type() -> Cow<'static, glib::VariantTy> {
|
||||||
|
glib::VariantTy::new("(ia{sv}av)").unwrap().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl glib::FromVariant for Layout {
|
||||||
|
fn from_variant(variant: &glib::Variant) -> Option<Self> {
|
||||||
|
let (id, props, children) = variant.get::<(_, _, Vec<glib::Variant>)>()?;
|
||||||
|
let children = children.iter().filter_map(Self::from_variant).collect();
|
||||||
|
Some(Self(id, props, children))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct DBusMenu(gio::DBusProxy);
|
||||||
|
|
||||||
|
impl DBusMenu {
|
||||||
|
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,
|
||||||
|
"com.canonical.dbusmenu",
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Ok(Self(proxy))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_layout(
|
||||||
|
&self,
|
||||||
|
parent: i32,
|
||||||
|
depth: i32,
|
||||||
|
properties: &[&str],
|
||||||
|
) -> Result<(u32, Layout), glib::Error> {
|
||||||
|
// XXX unwrap
|
||||||
|
Ok(self
|
||||||
|
.0
|
||||||
|
.call_future(
|
||||||
|
"GetLayout",
|
||||||
|
Some(&(parent, depth, properties).to_variant()),
|
||||||
|
gio::DBusCallFlags::NONE,
|
||||||
|
1000,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
.get()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct StatusNotifierItem(gio::DBusProxy, Option<DBusMenu>);
|
||||||
|
|
||||||
|
impl StatusNotifierItem {
|
||||||
|
async fn new(name: &str) -> Result<Self, glib::Error> {
|
||||||
|
let idx = name.find('/').unwrap();
|
||||||
|
let dest = &name[..idx];
|
||||||
|
let path = &name[idx..];
|
||||||
|
let proxy = gio::DBusProxy::for_bus_future(
|
||||||
|
gio::BusType::Session,
|
||||||
|
gio::DBusProxyFlags::NONE,
|
||||||
|
None,
|
||||||
|
dest,
|
||||||
|
path,
|
||||||
|
"org.kde.StatusNotifierItem",
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let menu_path = proxy
|
||||||
|
.cached_property("Menu")
|
||||||
|
.and_then(|x| x.get::<String>());
|
||||||
|
let menu = if let Some(menu_path) = menu_path {
|
||||||
|
Some(DBusMenu::new(dest, &menu_path).await?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
Ok(Self(proxy, menu))
|
||||||
|
}
|
||||||
|
|
||||||
|
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<DBusMenu> {
|
||||||
|
self.1.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue