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:
parent
34ccdb6ba0
commit
1d222795a3
@ -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<Child> {
|
||||
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");
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
export component TimeWidget {
|
||||
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 {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@ export component TopBar inherits Window {
|
||||
in property<int> bar_width;
|
||||
in property<int> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user