Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions cosmic-settings/src/pages/time/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct Page {
show_seconds: bool,
ntp_enabled: bool,
show_date_in_top_panel: bool,
format_strftime: String,
timezone_context: bool,
local_time: Option<DateTime<Gregorian>>,
timezone: Option<usize>,
Expand Down Expand Up @@ -102,6 +103,15 @@ impl Default for Page {
true
});

let format_strftime = cosmic_applet_config
.get("format_strftime")
.inspect_err(|err| {
if err.is_err() {
error!(?err, "Failed to read config 'format_strftime'");
}
})
.unwrap_or_default();

Self {
entity: page::Entity::null(),
cosmic_applet_config,
Expand All @@ -112,6 +122,7 @@ impl Default for Page {
show_seconds,
ntp_enabled: false,
show_date_in_top_panel,
format_strftime,
timezone: None,
timezone_context: false,
timezone_list: Vec::new(),
Expand Down Expand Up @@ -242,6 +253,17 @@ impl Page {
}
}

Message::Strftime(format) => {
self.format_strftime = format;

if let Err(err) = self
.cosmic_applet_config
.set("format_strftime", &self.format_strftime)
{
error!(?err, "Failed to set config 'format_strftime'");
}
}

Message::TimezoneSearch(text) => {
self.timezone_search = text;
}
Expand Down Expand Up @@ -382,9 +404,14 @@ impl Page {
fn update_local_time(&mut self) {
self.local_time = Some(update_local_time());

self.formatted_date = match self.local_time {
Some(ref time) => format_date(time, self.military_time, self.show_seconds),
None => fl!("unknown"),
self.formatted_date = if !self.format_strftime.is_empty() {
chrono::Local::now()
.format(&self.format_strftime)
.to_string()
} else if let Some(time) = self.local_time.as_ref() {
format_date(time, self.military_time, self.show_seconds)
} else {
fl!("unknown")
}
}
}
Expand All @@ -398,6 +425,7 @@ pub enum Message {
FirstDayOfWeek(usize),
Refresh(Info),
ShowDate(bool),
Strftime(String),
Timezone(usize),
TimezoneContext,
TimezoneSearch(String),
Expand Down Expand Up @@ -435,6 +463,7 @@ fn format() -> Section<crate::pages::Message> {
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"));
let format_strftime = descriptions.insert(fl!("time-format", "format-strftime"));

Section::default()
.title(fl!("time-format"))
Expand Down Expand Up @@ -486,6 +515,12 @@ fn format() -> Section<crate::pages::Message> {
settings::item::builder(&section.descriptions[show_date])
.toggler(page.show_date_in_top_panel, Message::ShowDate),
)
// Format with strftime
.add(
settings::item::builder(&section.descriptions[format_strftime]).control(
widget::text_input("", &page.format_strftime).on_input(Message::Strftime),
),
)
.apply(cosmic::Element::from)
.map(crate::pages::Message::DateAndTime)
})
Expand Down
1 change: 1 addition & 0 deletions i18n/en/cosmic_settings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ time-format = Date & time format
.show-seconds = Show seconds
.first = First day of week
.show-date = Show date in the time applet
.format-strftime = Format date and time with strftime
.friday = Friday
.saturday = Saturday
.sunday = Sunday
Expand Down
Loading