Minor refactoring

- Return `SharedString` from `format_time()`
- Avoid repetition
This commit is contained in:
Candifloss 2025-11-20 12:45:38 +05:30
parent c878c7f678
commit 5b79941341

View File

@ -8,8 +8,8 @@ fn run_cmd(cmd: &str) -> Option<Child> {
Command::new(cmd).spawn().ok() Command::new(cmd).spawn().ok()
} }
fn format_time() -> String { fn format_time() -> SharedString {
Local::now().format("%I:%M %p").to_string() SharedString::from(Local::now().format("%I:%M %p").to_string())
} }
fn time_until_next_minute() -> Duration { fn time_until_next_minute() -> Duration {
@ -31,21 +31,21 @@ fn start_clock_updater(ui: &TopBar) {
// 1. Initial immediate update // 1. Initial immediate update
if let Some(ui) = weak.upgrade() { if let Some(ui) = weak.upgrade() {
ui.set_time_text(SharedString::from(format_time())); ui.set_time_text(format_time());
} }
// 2. Schedule first update exactly at next minute boundary // 2. Schedule first update exactly at 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 // Update at the boundary
if let Some(ui) = weak.upgrade() { if let Some(ui) = weak.upgrade() {
ui.set_time_text(SharedString::from(format_time())); ui.set_time_text(format_time());
} }
// 3. Start repeating 60-sec timer // 3. Start repeating 60-sec timer
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() {
ui.set_time_text(SharedString::from(format_time())); ui.set_time_text(format_time());
} }
}); });
}); });
@ -54,7 +54,7 @@ fn start_clock_updater(ui: &TopBar) {
pub fn install_callbacks(ui: &TopBar) { pub fn install_callbacks(ui: &TopBar) {
let weak = ui.as_weak(); let weak = ui.as_weak();
// Click handler // Click handler
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");