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

@@ -131,7 +131,7 @@ elements:
font_size: 24 # Use 18 for 12-hour time
fg_color: "#8bd5ca"
source: Clock
timezone: "Asia/Tokyo" # change TZ1 here
timezone: 0 # change TZ1 here
format: "%H:%M" # 23:59
#format: "%I:%M %p" # 11:59 PM
- type: Label
@@ -139,8 +139,8 @@ elements:
corner_radius: 4
font_size: 14
fg_color: "#8bd5ca"
source: Static
text: "Tokyo" # change TZ1 label here
source: Timezone
timezone: 0 # change TZ1 label here
# alt clock 2
- type: Label
@@ -149,7 +149,7 @@ elements:
font_size: 24 # Use 18 for 12-hour time
fg_color: "#b7bdf8"
source: Clock
timezone: "America/Chicago" # change TZ2 here
timezone: 1 # change TZ2 here
format: "%H:%M" # 23:59
#format: "%I:%M %p" # 11:59 PM
- type: Label
@@ -157,8 +157,8 @@ elements:
corner_radius: 4
font_size: 14
fg_color: "#b7bdf8"
source: Static
text: "Chicago" # change TZ2 label here
source: Timezone
timezone: 1 # change TZ2 label here
# batteries
- type: BatteryList

View File

@@ -1,3 +1,4 @@
use std::i32;
use std::path::PathBuf;
use std::sync::Arc;
@@ -9,6 +10,7 @@ use crate::overlays::toast::DisplayMethod;
use crate::overlays::toast::ToastTopic;
use crate::state::LeftRight;
use anyhow::bail;
use chrono::Offset;
use config::Config;
use config::File;
use glam::vec3a;
@@ -137,6 +139,19 @@ fn def_empty_vec_string() -> Vec<String> {
Vec::new()
}
fn def_timezones() -> Vec<String> {
let offset = chrono::Local::now().offset().fix();
const EMEA: i32 = -1;
const APAC: i32 = 60 * 60 * 5 - 1;
match offset.local_minus_utc() {
i32::MIN..EMEA => vec!["Europe/Paris".into(), "Asia/Tokyo".into()],
EMEA..APAC => vec!["America/New_York".into(), "Asia/Tokyo".into()],
APAC..=i32::MAX => vec!["Europe/Paris".into(), "America/New_York".into()],
}
}
fn def_screens() -> AStrSet {
AStrSet::new()
}
@@ -290,6 +305,9 @@ pub struct GeneralConfig {
#[serde(default = "def_empty_vec_string")]
pub alt_click_up: Vec<String>,
#[serde(default = "def_timezones")]
pub timezones: Vec<String>,
}
impl GeneralConfig {

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 {

View File

@@ -116,7 +116,7 @@ elements:
font_size: 24 # Use 18 for 12-hour time
fg_color: "#8bd5ca"
source: Clock
timezone: "Asia/Tokyo" # change TZ1 here
timezone: 0
format: "%H:%M" # 23:59
#format: "%I:%M %p" # 11:59 PM
- type: Label
@@ -124,8 +124,8 @@ elements:
corner_radius: 4
font_size: 14
fg_color: "#8bd5ca"
source: Static
text: "Tokyo" # change TZ1 label here
source: Timezone
timezone: 0
# alt clock 2
- type: Label
@@ -134,7 +134,7 @@ elements:
font_size: 24 # Use 18 for 12-hour time
fg_color: "#b7bdf8"
source: Clock
timezone: "America/Chicago" # change TZ2 here
timezone: 1
format: "%H:%M" # 23:59
#format: "%I:%M %p" # 11:59 PM
- type: Label
@@ -142,8 +142,8 @@ elements:
corner_radius: 4
font_size: 14
fg_color: "#b7bdf8"
source: Static
text: "Chicago" # change TZ2 label here
source: Timezone
timezone: 1
# batteries
- type: BatteryList