feat(time): military_time and show_date_in_top_panel config

This commit is contained in:
maciekk64 2024-03-22 17:22:43 +01:00 • committed by Michael Murphy
parent 22d7501aee
commit 51eed6ec12
3 changed files with 66 additions and 16 deletions

View file

@ -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,
}
}
}

View file

@ -1,3 +1,4 @@
mod config;
mod localize; mod localize;
mod time; mod time;
mod window; mod window;

View file

@ -17,6 +17,7 @@ use cosmic::{
use chrono::{DateTime, Datelike, DurationRound, Local, Months, NaiveDate, Weekday}; use chrono::{DateTime, Datelike, DurationRound, Local, Months, NaiveDate, Weekday};
use crate::config::TimeAppletConfig;
use crate::fl; use crate::fl;
use crate::time::get_calender_first; use crate::time::get_calender_first;
use cosmic::applet::token::subscription::{ use cosmic::applet::token::subscription::{
@ -39,6 +40,7 @@ pub struct Window {
rectangle_tracker: Option<RectangleTracker<u32>>, rectangle_tracker: Option<RectangleTracker<u32>>,
rectangle: Rectangle, rectangle: Rectangle,
token_tx: Option<calloop::channel::Sender<TokenRequest>>, token_tx: Option<calloop::channel::Sender<TokenRequest>>,
config: TimeAppletConfig,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -52,6 +54,7 @@ pub enum Message {
NextMonth, NextMonth,
OpenDateTimeSettings, OpenDateTimeSettings,
Token(TokenUpdate), Token(TokenUpdate),
ConfigChanged(TimeAppletConfig),
} }
impl cosmic::Application for Window { impl cosmic::Application for Window {
@ -75,6 +78,7 @@ impl cosmic::Application for Window {
rectangle_tracker: None, rectangle_tracker: None,
rectangle: Rectangle::default(), rectangle: Rectangle::default(),
token_tx: None, token_tx: None,
config: TimeAppletConfig::default(),
}, },
Command::none(), Command::none(),
) )
@ -97,6 +101,12 @@ impl cosmic::Application for Window {
rectangle_tracker_subscription(0).map(|e| Message::Rectangle(e.1)), rectangle_tracker_subscription(0).map(|e| Message::Rectangle(e.1)),
time_subscription(self.update_at).map(|_| Message::Tick), time_subscription(self.update_at).map(|_| Message::Tick),
activation_token_subscription(0).map(Message::Token), 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() Command::none()
} }
Message::ConfigChanged(c) => {
self.config = c;
Command::none()
}
} }
} }
@ -214,9 +228,18 @@ impl cosmic::Application for Window {
PanelAnchor::Top | PanelAnchor::Bottom PanelAnchor::Top | PanelAnchor::Bottom
); );
let button = cosmic::widget::button(if horizontal { 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( Element::from(
row!( 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( container(vertical_space(Length::Fixed(
(self.core.applet.suggested_size().1 (self.core.applet.suggested_size().1
+ 2 * self.core.applet.suggested_padding()) + 2 * self.core.applet.suggested_padding())
@ -226,23 +249,31 @@ impl cosmic::Application for Window {
.align_items(Alignment::Center), .align_items(Alignment::Center),
) )
} else { } else {
let mut date_time_col = column![ let mut date_time_col = if self.config.military_time {
icon::from_name("emoji-recent-symbolic") column![
.size(self.core.applet.suggested_size().0) text(self.now.format("%H").to_string()).size(14),
.symbolic(true), text(self.now.format("%M").to_string()).size(14),
text(self.now.format("%I").to_string()).size(14), ]
text(self.now.format("%M").to_string()).size(14), } else {
text(self.now.format("%p").to_string()).size(14), column![
vertical_space(Length::Fixed(4.0)), text(self.now.format("%I").to_string()).size(14),
// TODO better calendar icon? text(self.now.format("%M").to_string()).size(14),
icon::from_name("calendar-go-today-symbolic") text(self.now.format("%p").to_string()).size(14),
.size(self.core.applet.suggested_size().0) ]
.symbolic(true), }
]
.align_items(Alignment::Center) .align_items(Alignment::Center)
.spacing(4); .spacing(4);
for d in self.now.format("%x").to_string().split('/') { if self.config.show_date_in_top_panel {
date_time_col = date_time_col.push(text(d.to_string()).size(14)); 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( Element::from(
column!( column!(