From 1d222795a30c459a585845a3572573034b65a2eb Mon Sep 17 00:00:00 2001 From: Candifloss Date: Wed, 19 Nov 2025 21:51:13 +0530 Subject: [PATCH] Refactor for simplicity & modularity - Write `run_cmd()` to run commands - Separate command execution from Slint callbacks - Rename `TimeWidget`'s callback `open_program()` as `show_clock()` --- src/widgets.rs | 15 ++++++++++----- ui/time-widget.slint | 4 ++-- ui/topbar.slint | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/widgets.rs b/src/widgets.rs index fc6f454..6a69ede 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -1,15 +1,20 @@ use crate::TopBar; use slint::ComponentHandle; -use std::process::Command; +use std::process::{Child, Command}; + +// Run a command +fn run_cmd(cmd: &str) -> Option { + Command::new(cmd).spawn().ok() +} /// Connect widget callbacks for the top bar. pub fn install_callbacks(ui: &TopBar) { let weak = ui.as_weak(); - ui.on_open_program(move || { - if weak.upgrade().is_some() { - // Run program on click - Command::new("xclock").spawn().ok(); + ui.on_show_clock(move || { + if weak.upgrade().is_none() { + return; } + run_cmd("xclock"); }); } diff --git a/ui/time-widget.slint b/ui/time-widget.slint index a1934f9..5aa7024 100644 --- a/ui/time-widget.slint +++ b/ui/time-widget.slint @@ -1,7 +1,7 @@ export component TimeWidget { in-out property time_text; - callback open_program(); // Callback to execute things from Rust, because Slint can't + callback show_clock(); // Callback to execute things from Rust, because Slint can't Rectangle { background: touch_area.pressed ? #555 : touch_area.has-hover ? #444 : #333; // Bg color based on click & hover @@ -21,7 +21,7 @@ export component TimeWidget { // Area to sense click and hover touch_area:= TouchArea { clicked => { - open_program(); + show_clock(); } } } diff --git a/ui/topbar.slint b/ui/topbar.slint index e2f782b..8291351 100644 --- a/ui/topbar.slint +++ b/ui/topbar.slint @@ -5,7 +5,7 @@ export component TopBar inherits Window { in property bar_width; in property bar_height; - callback open_program(); + callback show_clock(); title: "chocobar"; width: bar_width *1px; @@ -53,7 +53,7 @@ export component TopBar inherits Window { TimeWidget { time_text: "11:43 AM"; // Fix this: Replace with time value - open_program => root.open_program(); // Forward the widget's callback to the root's to access from Rust + show_clock => root.show_clock(); // Forward the widget's callback to the root's to access from Rust } } }