From 51eed6ec1210423634e90fceabc0437c96162132 Mon Sep 17 00:00:00 2001 From: maciekk64 Date: Fri, 22 Mar 2024 17:22:43 +0100 Subject: [PATCH] feat(time): military_time and show_date_in_top_panel config --- cosmic-applet-time/src/config.rs | 18 +++++++++ cosmic-applet-time/src/lib.rs | 1 + cosmic-applet-time/src/window.rs | 63 ++++++++++++++++++++++++-------- 3 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 cosmic-applet-time/src/config.rs diff --git a/cosmic-applet-time/src/config.rs b/cosmic-applet-time/src/config.rs new file mode 100644 index 00000000..c861f688 --- /dev/null +++ b/cosmic-applet-time/src/config.rs @@ -0,0 +1,18 @@ +use cosmic::cosmic_config::cosmic_config_derive::CosmicConfigEntry; +use cosmic::cosmic_config::{self, CosmicConfigEntry}; + +#[derive(Debug, Clone, CosmicConfigEntry, PartialEq, Eq)] +#[version = 1] +pub struct TimeAppletConfig { + pub military_time: bool, + pub show_date_in_top_panel: bool, +} + +impl Default for TimeAppletConfig { + fn default() -> Self { + Self { + military_time: false, + show_date_in_top_panel: true, + } + } +} diff --git a/cosmic-applet-time/src/lib.rs b/cosmic-applet-time/src/lib.rs index f95a68ac..4dc512fb 100644 --- a/cosmic-applet-time/src/lib.rs +++ b/cosmic-applet-time/src/lib.rs @@ -1,3 +1,4 @@ +mod config; mod localize; mod time; mod window; diff --git a/cosmic-applet-time/src/window.rs b/cosmic-applet-time/src/window.rs index 87c7bc42..d3fef494 100644 --- a/cosmic-applet-time/src/window.rs +++ b/cosmic-applet-time/src/window.rs @@ -17,6 +17,7 @@ use cosmic::{ use chrono::{DateTime, Datelike, DurationRound, Local, Months, NaiveDate, Weekday}; +use crate::config::TimeAppletConfig; use crate::fl; use crate::time::get_calender_first; use cosmic::applet::token::subscription::{ @@ -39,6 +40,7 @@ pub struct Window { rectangle_tracker: Option>, rectangle: Rectangle, token_tx: Option>, + config: TimeAppletConfig, } #[derive(Debug, Clone)] @@ -52,6 +54,7 @@ pub enum Message { NextMonth, OpenDateTimeSettings, Token(TokenUpdate), + ConfigChanged(TimeAppletConfig), } impl cosmic::Application for Window { @@ -75,6 +78,7 @@ impl cosmic::Application for Window { rectangle_tracker: None, rectangle: Rectangle::default(), token_tx: None, + config: TimeAppletConfig::default(), }, Command::none(), ) @@ -97,6 +101,12 @@ impl cosmic::Application for Window { rectangle_tracker_subscription(0).map(|e| Message::Rectangle(e.1)), time_subscription(self.update_at).map(|_| Message::Tick), activation_token_subscription(0).map(Message::Token), + self.core.watch_config(Self::APP_ID).map(|u| { + for err in u.errors { + tracing::error!(?err, "Error watching config"); + } + Message::ConfigChanged(u.config) + }), ]) } @@ -205,6 +215,10 @@ impl cosmic::Application for Window { } Command::none() } + Message::ConfigChanged(c) => { + self.config = c; + Command::none() + } } } @@ -214,9 +228,18 @@ impl cosmic::Application for Window { PanelAnchor::Top | PanelAnchor::Bottom ); let button = cosmic::widget::button(if horizontal { + let format = match ( + self.config.military_time, + self.config.show_date_in_top_panel, + ) { + (true, true) => "%b %-d %H:%M", + (true, false) => "%H:%M", + (false, true) => "%b %-d %-I:%M %p", + (false, false) => "%-I:%M %p", + }; Element::from( row!( - cosmic::widget::text(self.now.format("%b %-d %-I:%M %p").to_string()).size(14), + cosmic::widget::text(self.now.format(format).to_string()).size(14), container(vertical_space(Length::Fixed( (self.core.applet.suggested_size().1 + 2 * self.core.applet.suggested_padding()) @@ -226,23 +249,31 @@ impl cosmic::Application for Window { .align_items(Alignment::Center), ) } else { - let mut date_time_col = column![ - icon::from_name("emoji-recent-symbolic") - .size(self.core.applet.suggested_size().0) - .symbolic(true), - text(self.now.format("%I").to_string()).size(14), - text(self.now.format("%M").to_string()).size(14), - text(self.now.format("%p").to_string()).size(14), - vertical_space(Length::Fixed(4.0)), - // TODO better calendar icon? - icon::from_name("calendar-go-today-symbolic") - .size(self.core.applet.suggested_size().0) - .symbolic(true), - ] + let mut date_time_col = if self.config.military_time { + column![ + text(self.now.format("%H").to_string()).size(14), + text(self.now.format("%M").to_string()).size(14), + ] + } else { + column![ + text(self.now.format("%I").to_string()).size(14), + text(self.now.format("%M").to_string()).size(14), + text(self.now.format("%p").to_string()).size(14), + ] + } .align_items(Alignment::Center) .spacing(4); - for d in self.now.format("%x").to_string().split('/') { - date_time_col = date_time_col.push(text(d.to_string()).size(14)); + if self.config.show_date_in_top_panel { + date_time_col = date_time_col.push(vertical_space(Length::Fixed(4.0))); + date_time_col = date_time_col.push( + // TODO better calendar icon? + icon::from_name("calendar-go-today-symbolic") + .size(self.core.applet.suggested_size().0) + .symbolic(true), + ); + for d in self.now.format("%x").to_string().split('/') { + date_time_col = date_time_col.push(text(d.to_string()).size(14)); + } } Element::from( column!(