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()`
This commit is contained in:
Candifloss 2025-11-19 21:51:13 +05:30
parent 34ccdb6ba0
commit 1d222795a3
3 changed files with 14 additions and 9 deletions

View File

@ -1,15 +1,20 @@
use crate::TopBar; use crate::TopBar;
use slint::ComponentHandle; use slint::ComponentHandle;
use std::process::Command; use std::process::{Child, Command};
// Run a command
fn run_cmd(cmd: &str) -> Option<Child> {
Command::new(cmd).spawn().ok()
}
/// Connect widget callbacks for the top bar. /// Connect widget callbacks for the top bar.
pub fn install_callbacks(ui: &TopBar) { pub fn install_callbacks(ui: &TopBar) {
let weak = ui.as_weak(); let weak = ui.as_weak();
ui.on_open_program(move || { ui.on_show_clock(move || {
if weak.upgrade().is_some() { if weak.upgrade().is_none() {
// Run program on click return;
Command::new("xclock").spawn().ok();
} }
run_cmd("xclock");
}); });
} }

View File

@ -1,7 +1,7 @@
export component TimeWidget { export component TimeWidget {
in-out property <string> time_text; in-out property <string> 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 { Rectangle {
background: touch_area.pressed ? #555 : touch_area.has-hover ? #444 : #333; // Bg color based on click & hover 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 // Area to sense click and hover
touch_area:= TouchArea { touch_area:= TouchArea {
clicked => { clicked => {
open_program(); show_clock();
} }
} }
} }

View File

@ -5,7 +5,7 @@ export component TopBar inherits Window {
in property<int> bar_width; in property<int> bar_width;
in property<int> bar_height; in property<int> bar_height;
callback open_program(); callback show_clock();
title: "chocobar"; title: "chocobar";
width: bar_width *1px; width: bar_width *1px;
@ -53,7 +53,7 @@ export component TopBar inherits Window {
TimeWidget { TimeWidget {
time_text: "11:43 AM"; // Fix this: Replace with time value 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
} }
} }
} }