move timezones out of watch.yaml + sane defaults

This commit is contained in:
galister
2024-11-23 20:01:49 +09:00
parent 54737b02d3
commit 51174b64d3
5 changed files with 83 additions and 24 deletions

View File

@@ -15,6 +15,13 @@ use serde::Deserialize;
use super::{color_parse_or_default, ExecArgs, GuiColor, ModularControl, ModularData};
#[derive(Deserialize)]
#[serde(untagged)]
pub enum TimezoneDef {
Idx(usize),
Str(Arc<str>),
}
#[derive(Deserialize)]
#[serde(tag = "source")]
pub enum LabelContent {
@@ -27,7 +34,10 @@ pub enum LabelContent {
},
Clock {
format: Arc<str>,
timezone: Option<Arc<str>>,
timezone: Option<TimezoneDef>,
},
Timezone {
timezone: usize,
},
Timer {
format: Arc<str>,
@@ -70,7 +80,7 @@ pub enum LabelData {
DragMultiplier,
}
pub fn modular_label_init(label: &mut ModularControl, content: &LabelContent) {
pub fn modular_label_init(label: &mut ModularControl, content: &LabelContent, app: &AppState) {
let state = match content {
LabelContent::Battery {
device,
@@ -85,19 +95,49 @@ pub fn modular_label_init(label: &mut ModularControl, content: &LabelContent) {
charging_color: color_parse_or_default(charging_color),
}),
LabelContent::Clock { format, timezone } => {
let tz: Option<Tz> = timezone.as_ref().map(|tz| {
tz.parse().unwrap_or_else(|_| {
log::error!("Failed to parse timezone '{}'", &tz);
label.set_fg_color(*FALLBACK_COLOR);
Tz::UTC
})
});
let tz_str = match timezone {
Some(TimezoneDef::Idx(idx)) => {
if let Some(tz) = app.session.config.timezones.get(*idx) {
Some(tz.as_str())
} else {
log::error!("Timezone index out of range '{}'", idx);
label.set_fg_color(*FALLBACK_COLOR);
None
}
}
Some(TimezoneDef::Str(tz_str)) => Some(tz_str.as_ref()),
None => None,
};
Some(LabelData::Clock {
format: format.clone(),
timezone: tz,
timezone: tz_str.and_then(|tz| {
tz.parse()
.map_err(|_| {
log::error!("Failed to parse timezone '{}'", &tz);
label.set_fg_color(*FALLBACK_COLOR);
})
.ok()
}),
})
}
LabelContent::Timezone { timezone } => {
if let Some(tz) = app.session.config.timezones.get(*timezone) {
let pretty_tz = tz.split('/').last().map(|x| x.replace("_", " "));
if let Some(pretty_tz) = pretty_tz {
label.set_text(&pretty_tz);
return;
} else {
log::error!("Timezone name not valid '{}'", &tz);
}
} else {
log::error!("Timezone index out of range '{}'", &timezone);
}
label.set_fg_color(*FALLBACK_COLOR);
label.set_text("Error");
None
}
LabelContent::Timer { format } => Some(LabelData::Timer {
format: format.clone(),
start: Instant::now(),

View File

@@ -189,7 +189,7 @@ pub fn modular_canvas(
corner_radius.unwrap_or_default(),
empty_str.clone(),
);
modular_label_init(label, data);
modular_label_init(label, data, state);
}
ModularElement::CenteredLabel {
rect: [x, y, w, h],
@@ -208,7 +208,7 @@ pub fn modular_canvas(
corner_radius.unwrap_or_default(),
empty_str.clone(),
);
modular_label_init(label, data);
modular_label_init(label, data, state);
}
ModularElement::Sprite {
rect: [x, y, w, h],
@@ -292,6 +292,7 @@ pub fn modular_canvas(
low_color: fg_color_low.clone(),
charging_color: fg_color_charging.clone(),
},
state,
);
button_x += match layout {