Update timer
- Change time-updater from 1-minute timer to 1-second timer - More accuracy
This commit is contained in:
parent
de1bd068d6
commit
497288c585
@ -10,49 +10,42 @@ 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
|
// Start a 1-second timer that updates only when the minute changes
|
||||||
fn time_until_next_minute() -> Duration {
|
|
||||||
let now = Local::now();
|
|
||||||
let secs = now.second();
|
|
||||||
let nanos = now.nanosecond();
|
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
let initial_timer = Box::leak(Box::new(Timer::default()));
|
// Update immediately when the bar first appears
|
||||||
let repeating_timer = Box::leak(Box::new(Timer::default()));
|
|
||||||
|
|
||||||
if let Some(ui) = weak.upgrade() {
|
if let Some(ui) = weak.upgrade() {
|
||||||
ui.set_time_text(format_time());
|
ui.set_time_text(format_time());
|
||||||
}
|
}
|
||||||
|
|
||||||
initial_timer.start(TimerMode::SingleShot, time_until_next_minute(), move || {
|
// Timer that ticks every second
|
||||||
if let Some(ui) = weak.upgrade() {
|
let timer = Box::leak(Box::new(Timer::default()));
|
||||||
ui.set_time_text(format_time());
|
|
||||||
}
|
|
||||||
|
|
||||||
let weak_clone = weak.clone();
|
timer.start(TimerMode::Repeated, Duration::from_secs(1), move || {
|
||||||
repeating_timer.start(TimerMode::Repeated, Duration::from_secs(60), move || {
|
// Access UI safely through the weak pointer
|
||||||
if let Some(ui) = weak_clone.upgrade() {
|
if let Some(ui) = weak.upgrade() {
|
||||||
|
let now = Local::now();
|
||||||
|
|
||||||
|
// Only update when the minute has rolled over
|
||||||
|
if now.second() == 0 {
|
||||||
ui.set_time_text(format_time());
|
ui.set_time_text(format_time());
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public entry for this widget
|
// Public entry point: connect widget events + start clock
|
||||||
pub fn install(ui: &TopBar) {
|
pub fn install(ui: &TopBar) {
|
||||||
let weak = ui.as_weak();
|
let weak = ui.as_weak();
|
||||||
|
|
||||||
|
// When the widget emits show_clock(), run the program
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user