reorginise

This commit is contained in:
2026-09-10 02:08:45 +01:00
parent 8696425590
commit bdec970b62
20 changed files with 593 additions and 548 deletions
+1
View File
@@ -9,6 +9,7 @@ path = "src/lib.rs"
[dependencies]
common = { path = "../../common" }
service_datetime = { package = "service-datetime", path = "../../services/datetime" }
iced = { workspace = true }
thiserror = { workspace = true }
toml = { workspace = true }
+2 -114
View File
@@ -2,118 +2,6 @@
//! 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};
mod module;
use common::{BarModule, ClockKind, ClockPayload, ModuleEffect, ModuleMsg, Service};
/// 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,
}
impl Clock {
pub fn new() -> Self {
Self {
value: "--:--:--".to_string(),
show_seconds: true,
}
}
/// 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()
}
}
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![Service::Clock(ClockKind::Seconds)]
}
fn popup_size(&self) -> Option<(u32, u32)> {
Some((POPUP_W, POPUP_H))
}
fn config(&mut self, config: toml::Table) -> Result<(), Box<dyn std::error::Error>> {
if let toml::Value::Boolean(e) = config["format"] {
if e {
self.show_seconds = true;
};
}
Ok(())
}
}
pub use module::{Clock, ClockError, ClockMsg};
+116
View File
@@ -0,0 +1,116 @@
use iced::widget::{container, mouse_area, text};
use iced::{Element, Task};
use common::{BarModule, ModuleEffect, ModuleMsg, Service};
use service_datetime::{ClockKind, ClockPayload};
/// 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,
}
impl Clock {
pub fn new() -> Self {
Self {
value: "--:--:--".to_string(),
show_seconds: true,
}
}
/// 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()
}
}
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))
}
fn config(&mut self, config: toml::Table) -> Result<(), Box<dyn std::error::Error>> {
if let toml::Value::Boolean(e) = config["format"] {
if e {
self.show_seconds = true;
};
}
Ok(())
}
}