Skip to content

Commit 0ed1e8c

Browse files
time: Simplify strftime config
1 parent ebef287 commit 0ed1e8c

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

cosmic-applet-time/src/window.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,12 @@ impl Window {
159159
}
160160

161161
fn vertical_layout(&self) -> Element<'_, Message> {
162-
let elements: Vec<Element<'_, Message>> = if let Some(formatted) = self
163-
.config
164-
.format_strftime
165-
.as_deref()
166-
.map(|format| self.now.format(format).to_string())
167-
{
162+
let elements: Vec<Element<'_, Message>> = if !self.config.format_strftime.is_empty() {
168163
// strftime formatter may override locale specific elements so it stands alone rather
169164
// than using ICU to determine a format.
170-
formatted
165+
self.now
166+
.format(&self.config.format_strftime)
167+
.to_string()
171168
.split_whitespace()
172169
.map(|piece| self.core.applet.text(piece.to_owned()).into())
173170
.collect()
@@ -232,13 +229,8 @@ impl Window {
232229
}
233230

234231
fn horizontal_layout(&self) -> Element<'_, Message> {
235-
let formatted_date = if let Some(formatted) = self
236-
.config
237-
.format_strftime
238-
.as_deref()
239-
.map(|format| self.now.format(format).to_string())
240-
{
241-
formatted
232+
let formatted_date = if !self.config.format_strftime.is_empty() {
233+
self.now.format(&self.config.format_strftime).to_string()
242234
} else {
243235
let datetime = self.create_datetime(&self.now);
244236
let mut prefs = DateTimeFormatterPreferences::from(self.locale.clone());

cosmic-applets-config/src/time.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub struct TimeAppletConfig {
1111
pub first_day_of_week: u8,
1212
pub show_date_in_top_panel: bool,
1313
pub show_weekday: bool,
14-
#[serde(default, deserialize_with = "strftime_opt_de")]
15-
pub format_strftime: Option<String>,
14+
#[serde(default, skip_serializing_if = "str::is_empty")]
15+
pub format_strftime: String,
1616
}
1717

1818
impl Default for TimeAppletConfig {
@@ -23,21 +23,7 @@ impl Default for TimeAppletConfig {
2323
first_day_of_week: 6,
2424
show_date_in_top_panel: true,
2525
show_weekday: false,
26-
format_strftime: None,
26+
format_strftime: Default::default(),
2727
}
2828
}
2929
}
30-
31-
/// Deserialize optional String but only if it is non-empty.
32-
fn strftime_opt_de<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
33-
where
34-
D: serde::Deserializer<'de>,
35-
{
36-
serde::Deserialize::deserialize(deserializer).map(|strftime: Option<String>| {
37-
if strftime.as_deref().is_none_or(str::is_empty) {
38-
None
39-
} else {
40-
strftime
41-
}
42-
})
43-
}

0 commit comments

Comments
 (0)