Add DateWidget

- Working date widget
- Update at midnight or on date change
- `src/widgets/datewidget.rs` module
This commit is contained in:
Candifloss 2025-11-20 19:41:19 +05:30
parent 497288c585
commit 48a116c547
4 changed files with 98 additions and 3 deletions

52
src/widgets/datewidget.rs Normal file
View File

@ -0,0 +1,52 @@
use crate::TopBar;
use chrono::{Datelike, Local};
use slint::{ComponentHandle, SharedString, Timer, TimerMode};
use std::time::Duration;
use super::common::run_cmd;
// Format date as "Mon 20 Jan"
fn format_date() -> SharedString {
SharedString::from(Local::now().format("%a %d %b").to_string())
}
// Update the date_text property only when it actually changes
fn start_date_updater(ui: &TopBar) {
let weak = ui.as_weak();
// Store the previous date to detect changes
let mut last_day = Local::now().day();
// Initialize on start
if let Some(ui) = weak.upgrade() {
ui.set_date_text(format_date());
}
// 1-minute timer — cheap, safe, reliable
let timer = Box::leak(Box::new(Timer::default()));
timer.start(TimerMode::Repeated, Duration::from_secs(60), move || {
if let Some(ui) = weak.upgrade() {
let now = Local::now();
let today = now.day();
if today != last_day {
// date changed (midnight or clock adjustments)
last_day = today;
ui.set_date_text(format_date());
}
}
});
}
// Connect click callback + start updater
pub fn install(ui: &TopBar) {
let weak = ui.as_weak();
ui.on_show_calendar(move || {
if weak.upgrade().is_some() {
run_cmd("xclock"); // Fix later
}
});
start_date_updater(ui);
}

View File

@ -1,13 +1,14 @@
mod common;
mod datewidget;
mod timewidget;
use crate::TopBar;
pub fn install_callbacks(ui: &TopBar) {
timewidget::install(ui);
datewidget::install(ui);
// In the future:
// datewidget::install(ui);
// networkwidget::install(ui);
// batterywidget::install(ui);
}

28
ui/date-widget.slint Normal file
View File

@ -0,0 +1,28 @@
export component DateWidget {
in-out property <string> date_text;
callback show_calendar(); // Callback to execute things from Rust, because Slint can't
Rectangle {
background: touch_area.pressed ? #704a4a : touch_area.has-hover ? #4d8a7a : #6f6291; // Bg color based on click & hover
border-radius: 3px;
HorizontalLayout {
padding-right: 3px;
padding-left: 3px;
Text {
text: date_text;
color: white;
vertical-alignment: center;
}
}
// Area to sense click and hover
touch_area:= TouchArea {
clicked => {
show_calendar();
}
}
}
}

View File

@ -1,4 +1,5 @@
import { TimeWidget } from "time-widget.slint";
import { DateWidget } from "date-widget.slint";
export component TopBar inherits Window {
@ -12,6 +13,10 @@ export component TopBar inherits Window {
in-out property <string> time_text;
callback show_clock();
// Date widget
in-out property <string> date_text;
callback show_calendar();
title: "chocobar";
width: bar_width *1px;
height: bar_height *1px;
@ -56,9 +61,18 @@ export component TopBar inherits Window {
HorizontalLayout {
alignment: end; // Right-align
DateWidget {
// Get from root
date_text: root.date_text;
// Forward the widget's callback to the root's to access from Rust
show_calendar => root.show_calendar();
}
TimeWidget {
time_text: root.time_text; // Get from root
show_clock => root.show_clock(); // Forward the widget's callback to the root's to access from Rust
// Get from root
time_text: root.time_text;
// Forward the widget's callback to the root's to access from Rust
show_clock => root.show_clock();
}
}
}