Modularize the widegets module

- Divide into sub-modules
- Seperate the logic of different widgets
- Better readability and maintainability
This commit is contained in:
Candifloss 2025-11-20 18:56:09 +05:30
parent 4f75b3164e
commit de1bd068d6
3 changed files with 22 additions and 15 deletions

6
src/widgets/common.rs Normal file
View File

@ -0,0 +1,6 @@
use std::process::{Child, Command};
// Run a shell command without blocking
pub fn run_cmd(cmd: &str) -> Option<Child> {
Command::new(cmd).spawn().ok()
}

13
src/widgets/mod.rs Normal file
View File

@ -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);
}

View File

@ -1,13 +1,9 @@
use crate::TopBar; use crate::TopBar;
use chrono::{Local, Timelike}; use chrono::{Local, Timelike};
use slint::{ComponentHandle, SharedString, Timer, TimerMode}; use slint::{ComponentHandle, SharedString, Timer, TimerMode};
use std::process::{Child, Command};
use std::time::Duration; use std::time::Duration;
// Run a shell command without blocking use super::common::run_cmd;
fn run_cmd(cmd: &str) -> Option<Child> {
Command::new(cmd).spawn().ok()
}
// Format current time as "hh:mm AM" // Format current time as "hh:mm AM"
fn format_time() -> SharedString { fn format_time() -> SharedString {
@ -27,24 +23,18 @@ fn time_until_next_minute() -> Duration {
fn start_clock_updater(ui: &TopBar) { fn start_clock_updater(ui: &TopBar) {
let weak = ui.as_weak(); let weak = ui.as_weak();
// One-shot: waits until the next round minute
let initial_timer = Box::leak(Box::new(Timer::default())); 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())); let repeating_timer = Box::leak(Box::new(Timer::default()));
// Set the time immediately when the bar starts
if let Some(ui) = weak.upgrade() { if let Some(ui) = weak.upgrade() {
ui.set_time_text(format_time()); ui.set_time_text(format_time());
} }
// Wait until the next minute boundary
initial_timer.start(TimerMode::SingleShot, time_until_next_minute(), move || { initial_timer.start(TimerMode::SingleShot, time_until_next_minute(), move || {
if let Some(ui) = weak.upgrade() { if let Some(ui) = weak.upgrade() {
ui.set_time_text(format_time()); ui.set_time_text(format_time());
} }
// Then update every 60 seconds
let weak_clone = weak.clone(); let weak_clone = weak.clone();
repeating_timer.start(TimerMode::Repeated, Duration::from_secs(60), move || { repeating_timer.start(TimerMode::Repeated, Duration::from_secs(60), move || {
if let Some(ui) = weak_clone.upgrade() { if let Some(ui) = weak_clone.upgrade() {
@ -54,17 +44,15 @@ fn start_clock_updater(ui: &TopBar) {
}); });
} }
// Connect widget callbacks and start the clock // Public entry for this widget
pub fn install_callbacks(ui: &TopBar) { pub fn install(ui: &TopBar) {
let weak = ui.as_weak(); let weak = ui.as_weak();
// Run xclock when the widget emits show_clock()
ui.on_show_clock(move || { ui.on_show_clock(move || {
if weak.upgrade().is_some() { if weak.upgrade().is_some() {
run_cmd("xclock"); run_cmd("xclock");
} }
}); });
// Start the clock updater
start_clock_updater(ui); start_clock_updater(ui);
} }