diff --git a/src/widgets/common.rs b/src/widgets/common.rs new file mode 100644 index 0000000..fa90170 --- /dev/null +++ b/src/widgets/common.rs @@ -0,0 +1,6 @@ +use std::process::{Child, Command}; + +// Run a shell command without blocking +pub fn run_cmd(cmd: &str) -> Option { + Command::new(cmd).spawn().ok() +} diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs new file mode 100644 index 0000000..3859080 --- /dev/null +++ b/src/widgets/mod.rs @@ -0,0 +1,13 @@ +mod common; +mod timewidget; + +use crate::TopBar; + +pub fn install_callbacks(ui: &TopBar) { + timewidget::install(ui); + + // In the future: + // datewidget::install(ui); + // networkwidget::install(ui); + // batterywidget::install(ui); +} diff --git a/src/widgets.rs b/src/widgets/timewidget.rs similarity index 72% rename from src/widgets.rs rename to src/widgets/timewidget.rs index 2cf1322..141e5a2 100644 --- a/src/widgets.rs +++ b/src/widgets/timewidget.rs @@ -1,13 +1,9 @@ use crate::TopBar; use chrono::{Local, Timelike}; use slint::{ComponentHandle, SharedString, Timer, TimerMode}; -use std::process::{Child, Command}; use std::time::Duration; -// Run a shell command without blocking -fn run_cmd(cmd: &str) -> Option { - Command::new(cmd).spawn().ok() -} +use super::common::run_cmd; // Format current time as "hh:mm AM" fn format_time() -> SharedString { @@ -27,24 +23,18 @@ fn time_until_next_minute() -> Duration { fn start_clock_updater(ui: &TopBar) { let weak = ui.as_weak(); - // One-shot: waits until the next round minute let initial_timer = Box::leak(Box::new(Timer::default())); - - // Repeating: runs every 60 seconds after that let repeating_timer = Box::leak(Box::new(Timer::default())); - // Set the time immediately when the bar starts if let Some(ui) = weak.upgrade() { ui.set_time_text(format_time()); } - // Wait until the next minute boundary initial_timer.start(TimerMode::SingleShot, time_until_next_minute(), move || { if let Some(ui) = weak.upgrade() { ui.set_time_text(format_time()); } - // Then update every 60 seconds let weak_clone = weak.clone(); repeating_timer.start(TimerMode::Repeated, Duration::from_secs(60), move || { if let Some(ui) = weak_clone.upgrade() { @@ -54,17 +44,15 @@ fn start_clock_updater(ui: &TopBar) { }); } -// Connect widget callbacks and start the clock -pub fn install_callbacks(ui: &TopBar) { +// Public entry for this widget +pub fn install(ui: &TopBar) { let weak = ui.as_weak(); - // Run xclock when the widget emits show_clock() ui.on_show_clock(move || { if weak.upgrade().is_some() { run_cmd("xclock"); } }); - // Start the clock updater start_clock_updater(ui); }