From 25eab25511536a072406a03f83923d5f8f38d5f6 Mon Sep 17 00:00:00 2001 From: candifloss Date: Mon, 12 Jan 2026 12:59:02 +0530 Subject: [PATCH] Replace hard-coded menu options with config - Accept command, icon, option name, from config --- src/powermenu.rs | 54 ++++++++++++++++++++++++++++++++++++--------- ui/power-menu.slint | 49 +++++++++++++--------------------------- 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/powermenu.rs b/src/powermenu.rs index d0516a8..330289b 100644 --- a/src/powermenu.rs +++ b/src/powermenu.rs @@ -1,5 +1,6 @@ use crate::config::ResolvedConfig; -use slint::{Color, LogicalPosition, LogicalSize, SharedString}; +use slint::{Color, LogicalPosition, LogicalSize, ModelRc, SharedString, VecModel}; +use std::{collections::HashMap, process::Command}; slint::include_modules!(); @@ -25,6 +26,31 @@ fn to_slint_button_style(s: crate::config::ButtonStyleResolved) -> ButtonStyle { } } +fn spawn_command(cmd: &str) { + let _ = Command::new("sh").arg("-c").arg(cmd).spawn(); +} + +fn build_options_model( + opts: &[crate::config::MenuOption], +) -> (ModelRc, HashMap) { + let mut command_map = HashMap::new(); + + let slint_opts: Vec = opts + .iter() + .map(|o| { + command_map.insert(o.id.clone(), o.command.clone()); + + MenuOption { + op_id: o.id.clone().into(), + icon: o.icon.clone().into(), + text: o.text.clone().into(), + } + }) + .collect(); + + (ModelRc::new(VecModel::from(slint_opts)), command_map) +} + pub fn run_power_menu(cfg: ResolvedConfig) -> Result<(), Box> { let ui = PowerMenu::new()?; @@ -47,18 +73,24 @@ pub fn run_power_menu(cfg: ResolvedConfig) -> Result<(), Box menu_bg; - in property ico_lockscreen; - in property ico_logout; - in property ico_reboot; - in property ico_poweroff; - - in property text_lockscreen; - in property text_logout; - in property text_reboot; - in property text_poweroff; + in property <[MenuOption]> options; + callback option_clicked(string); no-frame: true; always-on-top: true; @@ -145,32 +144,14 @@ export component PowerMenu inherits Window { padding: menu_padding_x *1px; spacing: menu_spacing *1px; - // Lock screen - PowerMenuButton { - option_icon: ico_lockscreen; - option_text: text_lockscreen; - button_style: root.button_style; - } - - // Log out - PowerMenuButton { - option_icon: ico_logout; - option_text: text_logout; - button_style: root.button_style; - } - - // Reboot - PowerMenuButton { - option_icon: ico_reboot; - option_text: text_reboot; - button_style: root.button_style; - } - - // Power off - PowerMenuButton { - option_icon: ico_poweroff; - option_text: text_poweroff; + for opt in options : PowerMenuButton { + option_icon: opt.icon; + option_text: opt.text; button_style: root.button_style; + + menu_btn_callback => { + root.option_clicked(opt.op_id); + } } } }