fucking something i forgot

This commit is contained in:
2026-09-12 12:24:36 +01:00
parent bdec970b62
commit 539c80cda9
20 changed files with 260 additions and 128 deletions
+129
View File
@@ -0,0 +1,129 @@
//! Clock module: subscribes to second ticks, renders the time in the bar.
//! Left-click toggles the seconds suffix; right-click opens a popup with
//! the current time in large text.
use iced::widget::{container, mouse_area, text};
use iced::{Element, Task};
use common::{BarModule, ModuleEffect, ModuleMsg, Service};
use serde::Deserialize;
use services::datetime::{ClockKind, ClockPayload};
use toml::Table;
/// Big-text popup size (logical px).
const POPUP_W: u32 = 360;
const POPUP_H: u32 = 160;
/// Errors digesting the `module.clock` config table.
#[derive(Debug, thiserror::Error)]
pub enum ClockError {
/// Config value was present but not a bool.
#[error("module.clock.{0} must be a bool")]
NotBool(&'static str),
}
/// A module-local message for the clock.
#[derive(Clone)]
pub enum ClockMsg {
/// Toggle the seconds suffix.
ToggleSeconds,
/// Open the big-time popup.
OpenPopup,
}
pub struct Clock {
value: String,
show_seconds: bool,
}
#[derive(Deserialize, Default)]
struct ClockConfig {
#[serde(default)]
format: bool,
}
impl Clock {
pub fn new(config: Option<Table>) -> Self {
let module_config = config_clock(config);
match module_config {
Some(x) => Self {
value: "--:--:--".to_string(),
show_seconds: { x.format },
},
None => Self::default(),
}
}
/// Display string: HH:MM:SS, or HH:MM when seconds are hidden.
fn shown(&self) -> String {
if self.show_seconds {
self.value.clone()
} else {
self.value
.split(':') // [hh, mm, ss]
.take(2)
.collect::<Vec<_>>()
.join(":")
}
}
}
impl Default for Clock {
fn default() -> Self {
Self::new(None)
}
}
fn config_clock(table: Option<Table>) -> Option<ClockConfig> {
let clock = table?.get("clock")?.clone();
clock.try_into().ok()
}
impl BarModule for Clock {
fn id(&self) -> &'static str {
"clock"
}
fn view(&self, window_id: Option<iced::window::Id>) -> Element<'_, ModuleMsg> {
match window_id {
// Popup surface: current time in large text.
Some(_) => container(text(&self.value).size(56)).padding(20).into(),
// Bar surface: clickable time. Container carries the module id
// so the popup can anchor to these bounds.
None => container(
mouse_area(text(self.shown()).size(16))
.on_press(ModuleMsg::new(self.id(), ClockMsg::ToggleSeconds))
.on_right_press(ModuleMsg::new(self.id(), ClockMsg::OpenPopup)),
)
.id(iced::widget::Id::from(self.id()))
.into(),
}
}
fn update(&mut self, msg: ModuleMsg) -> Task<ModuleEffect> {
// Subscription fan-out delivers a raw `ClockPayload`, not `ClockMsg`.
if let Some(payload) = msg.downcast::<ClockPayload>() {
self.value = payload.value.clone();
return Task::none();
}
match msg.downcast::<ClockMsg>() {
Some(ClockMsg::ToggleSeconds) => {
self.show_seconds = !self.show_seconds;
Task::none()
}
Some(ClockMsg::OpenPopup) => Task::done(ModuleEffect::RequestPopup(
self.id().to_string(),
self.id().to_string(),
)),
None => Task::none(),
}
}
fn services(&self) -> Vec<Service> {
vec![ClockKind::Seconds.key()]
}
fn popup_size(&self) -> Option<(u32, u32)> {
Some((POPUP_W, POPUP_H))
}
}