Comments
This commit is contained in:
parent
5b79941341
commit
4f75b3164e
@ -4,14 +4,17 @@ use slint::{ComponentHandle, SharedString, Timer, TimerMode};
|
|||||||
use std::process::{Child, Command};
|
use std::process::{Child, Command};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
// Run a shell command without blocking
|
||||||
fn run_cmd(cmd: &str) -> Option<Child> {
|
fn run_cmd(cmd: &str) -> Option<Child> {
|
||||||
Command::new(cmd).spawn().ok()
|
Command::new(cmd).spawn().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format current time as "hh:mm AM"
|
||||||
fn format_time() -> SharedString {
|
fn format_time() -> SharedString {
|
||||||
SharedString::from(Local::now().format("%I:%M %p").to_string())
|
SharedString::from(Local::now().format("%I:%M %p").to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Time until the next minute starts
|
||||||
fn time_until_next_minute() -> Duration {
|
fn time_until_next_minute() -> Duration {
|
||||||
let now = Local::now();
|
let now = Local::now();
|
||||||
let secs = now.second();
|
let secs = now.second();
|
||||||
@ -20,28 +23,28 @@ fn time_until_next_minute() -> Duration {
|
|||||||
Duration::from_secs(60 - u64::from(secs)) - Duration::from_nanos(u64::from(nanos))
|
Duration::from_secs(60 - u64::from(secs)) - Duration::from_nanos(u64::from(nanos))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create timers that update the time_text property every minute
|
||||||
fn start_clock_updater(ui: &TopBar) {
|
fn start_clock_updater(ui: &TopBar) {
|
||||||
let weak = ui.as_weak();
|
let weak = ui.as_weak();
|
||||||
|
|
||||||
// One-shot timer for the first update
|
// 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 timer (every 60 seconds)
|
// 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()));
|
||||||
|
|
||||||
// 1. Initial immediate update
|
// 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Schedule first update exactly at next minute boundary
|
// 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 || {
|
||||||
// Update at the boundary
|
|
||||||
if let Some(ui) = weak.upgrade() {
|
if let Some(ui) = weak.upgrade() {
|
||||||
ui.set_time_text(format_time());
|
ui.set_time_text(format_time());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Start repeating 60-sec timer
|
// 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() {
|
||||||
@ -51,16 +54,17 @@ fn start_clock_updater(ui: &TopBar) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect widget callbacks and start the clock
|
||||||
pub fn install_callbacks(ui: &TopBar) {
|
pub fn install_callbacks(ui: &TopBar) {
|
||||||
let weak = ui.as_weak();
|
let weak = ui.as_weak();
|
||||||
|
|
||||||
// Click handler
|
// 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 time-updating timer
|
// Start the clock updater
|
||||||
start_clock_updater(ui);
|
start_clock_updater(ui);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user