use crate::launch::OPENED_PREFIX;
use crate::menu::{ItemKind, Menu, MenuItem};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
use zbus::object_server::SignalEmitter;
use zbus::zvariant::{OwnedValue, StructureBuilder, Type, Value};
#[derive(Debug, Clone, serde::Serialize, Type)]
pub struct LayoutNode(i32, HashMap<String, OwnedValue>, Vec<OwnedValue>);
fn properties(item: &MenuItem) -> HashMap<String, OwnedValue> {
let mut props: HashMap<String, OwnedValue> = HashMap::new();
if item.kind == ItemKind::Separator {
props.insert(
"type".to_string(),
OwnedValue::try_from(Value::from("separator")).expect("a string is a value"),
);
return props;
}
props.insert(
"label".to_string(),
OwnedValue::try_from(Value::from(item.label.as_str())).expect("a string is a value"),
);
props.insert(
"enabled".to_string(),
OwnedValue::try_from(Value::from(item.enabled)).expect("a bool is a value"),
);
props.insert(
"visible".to_string(),
OwnedValue::try_from(Value::from(item.visible)).expect("a bool is a value"),
);
if let Some(on) = item.toggle {
props.insert(
"toggle-type".to_string(),
OwnedValue::try_from(Value::from("checkmark")).expect("a string is a value"),
);
props.insert(
"toggle-state".to_string(),
OwnedValue::try_from(Value::from(if on { 1i32 } else { 0i32 })).expect("an int is a value"),
);
}
if !item.children.is_empty() {
props.insert(
"children-display".to_string(),
OwnedValue::try_from(Value::from("submenu")).expect("a string is a value"),
);
}
props
}
fn node(
id: i32,
props: HashMap<String, OwnedValue>,
children: &[MenuItem],
depth: i32,
) -> LayoutNode {
let kids: Vec<OwnedValue> = if depth == 0 {
Vec::new()
} else {
children
.iter()
.map(|child| {
let child_node = node(
child.id,
properties(child),
&child.children,
depth.saturating_sub(1),
);
into_value(child_node)
})
.collect()
};
LayoutNode(id, props, kids)
}
fn into_value(node: LayoutNode) -> OwnedValue {
let structure = StructureBuilder::new()
.add_field(node.0)
.add_field(node.1)
.add_field(node.2)
.build()
.expect("a node is always exactly three fields");
OwnedValue::try_from(Value::from(structure)).expect("a structure is a value")
}
pub struct MenuInterface {
id: String,
menu: Arc<Mutex<Menu>>,
revision: Arc<Mutex<u32>>,
events: tokio::sync::mpsc::UnboundedSender<String>,
}
impl MenuInterface {
pub fn new(
id: String,
menu: Arc<Mutex<Menu>>,
revision: Arc<Mutex<u32>>,
events: tokio::sync::mpsc::UnboundedSender<String>,
) -> Self {
MenuInterface {
id,
menu,
revision,
events,
}
}
}
#[zbus::interface(name = "com.canonical.dbusmenu")]
impl MenuInterface {
async fn get_layout(
&self,
parent_id: i32,
recursion_depth: i32,
_property_names: Vec<String>,
) -> (u32, LayoutNode) {
let menu = self.menu.lock().await;
let revision = *self.revision.lock().await;
if parent_id == 0 {
let mut root: HashMap<String, OwnedValue> = HashMap::new();
root.insert(
"children-display".to_string(),
OwnedValue::try_from(Value::from("submenu")).expect("a string is a value"),
);
return (revision, node(0, root, &menu.items, recursion_depth));
}
match menu.find(parent_id) {
Some(item) => (
revision,
node(item.id, properties(item), &item.children, recursion_depth),
),
None => (revision, node(parent_id, HashMap::new(), &[], 0)),
}
}
async fn get_group_properties(
&self,
ids: Vec<i32>,
_property_names: Vec<String>,
) -> Vec<(i32, HashMap<String, OwnedValue>)> {
let menu = self.menu.lock().await;
menu.flatten()
.into_iter()
.filter(|item| ids.is_empty() || ids.contains(&item.id))
.map(|item| (item.id, properties(item)))
.collect()
}
async fn get_property(&self, id: i32, name: String) -> OwnedValue {
let menu = self.menu.lock().await;
menu.find(id)
.and_then(|item| properties(item).remove(&name))
.unwrap_or_else(|| {
OwnedValue::try_from(Value::from("")).expect("a string is a value")
})
}
async fn event(&self, id: i32, event_id: String, _data: Value<'_>, _timestamp: u32) {
if event_id != "clicked" {
return;
}
let action = self.menu.lock().await.action_for(id).map(str::to_string);
if let Some(action) = action {
let _ = self.events.send(action);
}
}
async fn event_group(
&self,
events: Vec<(i32, String, Value<'_>, u32)>,
) -> Vec<i32> {
for (id, event_id, data, timestamp) in events {
self.event(id, event_id, data, timestamp).await;
}
Vec::new()
}
async fn about_to_show(&self, _id: i32) -> bool {
let _ = self.events.send(format!("{OPENED_PREFIX}{}", self.id));
false
}
async fn about_to_show_group(&self, _ids: Vec<i32>) -> (Vec<i32>, Vec<i32>) {
let _ = self.events.send(format!("{OPENED_PREFIX}{}", self.id));
(Vec::new(), Vec::new())
}
#[zbus(property)]
async fn version(&self) -> u32 {
3
}
#[zbus(property)]
async fn status(&self) -> String {
"normal".to_string()
}
#[zbus(property)]
async fn text_direction(&self) -> String {
"ltr".to_string()
}
#[zbus(property)]
async fn icon_theme_path(&self) -> Vec<String> {
Vec::new()
}
#[zbus(signal)]
pub async fn layout_updated(
emitter: &SignalEmitter<'_>,
revision: u32,
parent: i32,
) -> zbus::Result<()>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::menu::MenuItem;
fn menu() -> Menu {
Menu::new(vec![
MenuItem::checkmark("Wi-Fi", true, "radio:toggle"),
MenuItem::separator(),
MenuItem::standard("home", "connect:home"),
])
}
#[tokio::test]
async fn both_forms_of_about_to_show_announce_which_menu_opened() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let iface = MenuInterface::new(
"hyprforge-network".to_string(),
Arc::new(Mutex::new(menu())),
Arc::new(Mutex::new(1)),
tx,
);
assert!(!iface.about_to_show(0).await, "the served layout is current");
assert_eq!(
rx.recv().await.unwrap(),
"menu:opened:hyprforge-network",
"the event carries the id, so a daemon knows which menu it was"
);
let _ = iface.about_to_show_group(vec![0]).await;
assert_eq!(rx.recv().await.unwrap(), "menu:opened:hyprforge-network");
}
#[test]
fn a_separator_is_marked_as_one_and_carries_nothing_else() {
let menu = menu();
let sep = menu.flatten().into_iter().find(|i| i.kind == ItemKind::Separator).unwrap();
let props = properties(sep);
assert_eq!(
String::try_from(props["type"].clone()).unwrap(),
"separator"
);
assert!(!props.contains_key("label"), "a separator has no label to draw");
}
#[test]
fn a_checkmark_sends_both_the_type_and_the_state() {
let menu = menu();
let wifi = menu.flatten().into_iter().find(|i| i.label == "Wi-Fi").unwrap();
let props = properties(wifi);
assert_eq!(
String::try_from(props["toggle-type"].clone()).unwrap(),
"checkmark"
);
assert_eq!(i32::try_from(props["toggle-state"].clone()).unwrap(), 1);
}
#[test]
fn an_ordinary_row_is_not_given_a_toggle_a_host_would_draw() {
let menu = menu();
let home = menu.flatten().into_iter().find(|i| i.label == "home").unwrap();
let props = properties(home);
assert!(!props.contains_key("toggle-type"));
assert!(!props.contains_key("toggle-state"));
}
#[test]
fn a_layout_node_has_three_fields_and_its_children_are_nested_nodes() {
let menu = menu();
let root = node(0, HashMap::new(), &menu.items, -1);
assert_eq!(root.0, 0, "the root is id 0");
assert_eq!(root.2.len(), 3, "three top-level rows");
let first = root.2.first().expect("a child");
let Value::Structure(child) = Value::from(first.clone()) else {
panic!("each child is a variant wrapping a structure");
};
assert_eq!(
child.fields().len(),
3,
"a child is a node of the same (id, properties, children) shape"
);
}
#[test]
fn depth_zero_returns_the_node_without_its_children() {
let menu = Menu::new(vec![MenuItem {
children: vec![MenuItem::standard("child", "c")],
..MenuItem::standard("parent", "p")
}]);
let shallow = node(0, HashMap::new(), &menu.items, 0);
assert!(shallow.2.is_empty(), "depth 0 stops at this node");
}
#[test]
fn the_layout_node_signature_is_the_one_the_protocol_specifies() {
assert_eq!(LayoutNode::SIGNATURE.to_string(), "(ia{sv}av)");
}
}