feat(time): Option to display seconds on the clock

Closes: #494
This commit is contained in:
Josh Megnauth 2024-08-19 03:12:39 -04:00 committed by Michael Murphy
parent a46587fb6e
commit 4992bec146
2 changed files with 32 additions and 2 deletions

View file

@ -38,6 +38,7 @@ pub struct Page {
cosmic_applet_config: cosmic_config::Config,
first_day_of_week: usize,
military_time: bool,
show_seconds: bool,
ntp_enabled: bool,
show_date_in_top_panel: bool,
timezone_context: bool,
@ -63,6 +64,16 @@ impl Default for Page {
false
});
let show_seconds = cosmic_applet_config
.get("show_seconds")
.unwrap_or_else(|err| {
if !matches!(err, cosmic_config::Error::NoConfigDirectory) {
error!(?err, "Failed to read config 'show_seconds'");
}
false
});
let first_day_of_week = cosmic_applet_config
.get("first_day_of_week")
.unwrap_or_else(|err| {
@ -89,6 +100,7 @@ impl Default for Page {
formatted_date: String::new(),
local_time: None,
military_time,
show_seconds,
ntp_enabled: false,
show_date_in_top_panel,
timezone: None,
@ -181,6 +193,15 @@ impl Page {
}
}
Message::ShowSeconds(enable) => {
self.show_seconds = enable;
self.update_local_time();
if let Err(err) = self.cosmic_applet_config.set("show_seconds", enable) {
error!(?err, "Failed to set config 'show_seconds'");
}
}
Message::FirstDayOfWeek(weekday) => {
self.first_day_of_week = weekday;
@ -322,7 +343,7 @@ impl Page {
self.local_time = Some(update_local_time());
self.formatted_date = match self.local_time {
Some(ref time) => format_date(time, self.military_time),
Some(ref time) => format_date(time, self.military_time, self.show_seconds),
None => fl!("unknown"),
}
}
@ -332,6 +353,7 @@ impl Page {
pub enum Message {
Error(String),
MilitaryTime(bool),
ShowSeconds(bool),
None,
FirstDayOfWeek(usize),
Refresh(Info),
@ -369,6 +391,7 @@ fn format() -> Section<crate::pages::Message> {
let mut descriptions = Slab::new();
let military = descriptions.insert(fl!("time-format", "twenty-four"));
let show_seconds = descriptions.insert(fl!("time-format", "show-seconds"));
let first = descriptions.insert(fl!("time-format", "first"));
let show_date = descriptions.insert(fl!("time-format", "show-date"));
@ -382,6 +405,11 @@ fn format() -> Section<crate::pages::Message> {
settings::item::builder(&section.descriptions[military])
.toggler(page.military_time, Message::MilitaryTime),
)
// Show seconds in time format
.add(
settings::item::builder(&section.descriptions[show_seconds])
.toggler(page.show_seconds, Message::ShowSeconds),
)
// First day of week
.add(
settings::item::builder(&section.descriptions[first]).control(dropdown(
@ -460,7 +488,7 @@ fn locale() -> Result<Locale, Box<dyn std::error::Error>> {
Ok(locale)
}
fn format_date(date: &DateTime<Iso>, military: bool) -> String {
fn format_date(date: &DateTime<Iso>, military: bool, show_seconds: bool) -> String {
let Ok(locale) = locale() else {
return String::new();
};
@ -472,6 +500,7 @@ fn format_date(date: &DateTime<Iso>, military: bool) -> String {
bag.month = Some(icu::datetime::options::components::Month::Long);
bag.hour = Some(icu::datetime::options::components::Numeric::Numeric);
bag.minute = Some(icu::datetime::options::components::Numeric::Numeric);
bag.second = show_seconds.then_some(icu::datetime::options::components::Numeric::Numeric);
bag.preferences = Some(icu::datetime::options::preferences::Bag::from_hour_cycle(
if military {
icu::datetime::options::preferences::HourCycle::H23

View file

@ -532,6 +532,7 @@ time-zone = Time Zone
time-format = Date & Time Format
.twenty-four = 24-hour time
.show-seconds = Show seconds
.first = First day of week
.show-date = Show Date on Top Panel
.friday = Friday