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

View File

@@ -1,3 +1,4 @@
use std::i32;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
@@ -9,6 +10,7 @@ use crate::overlays::toast::DisplayMethod;
use crate::overlays::toast::ToastTopic; use crate::overlays::toast::ToastTopic;
use crate::state::LeftRight; use crate::state::LeftRight;
use anyhow::bail; use anyhow::bail;
use chrono::Offset;
use config::Config; use config::Config;
use config::File; use config::File;
use glam::vec3a; use glam::vec3a;
@@ -137,6 +139,19 @@ fn def_empty_vec_string() -> Vec<String> {
Vec::new() 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 { fn def_screens() -> AStrSet {
AStrSet::new() AStrSet::new()
} }
@@ -290,6 +305,9 @@ pub struct GeneralConfig {
#[serde(default = "def_empty_vec_string")] #[serde(default = "def_empty_vec_string")]
pub alt_click_up: Vec<String>, pub alt_click_up: Vec<String>,
#[serde(default = "def_timezones")]
pub timezones: Vec<String>,
} }
impl GeneralConfig { impl GeneralConfig {

View File

@@ -15,6 +15,13 @@ use serde::Deserialize;
use super::{color_parse_or_default, ExecArgs, GuiColor, ModularControl, ModularData}; use super::{color_parse_or_default, ExecArgs, GuiColor, ModularControl, ModularData};
#[derive(Deserialize)]
#[serde(untagged)]
pub enum TimezoneDef {
Idx(usize),
Str(Arc<str>),
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(tag = "source")] #[serde(tag = "source")]
pub enum LabelContent { pub enum LabelContent {
@@ -27,7 +34,10 @@ pub enum LabelContent {
}, },
Clock { Clock {
format: Arc<str>, format: Arc<str>,
timezone: Option<Arc<str>>, timezone: Option<TimezoneDef>,
},
Timezone {
timezone: usize,
}, },
Timer { Timer {
format: Arc<str>, format: Arc<str>,
@@ -70,7 +80,7 @@ pub enum LabelData {
DragMultiplier, 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 { let state = match content {
LabelContent::Battery { LabelContent::Battery {
device, device,
@@ -85,18 +95,48 @@ pub fn modular_label_init(label: &mut ModularControl, content: &LabelContent) {
charging_color: color_parse_or_default(charging_color), charging_color: color_parse_or_default(charging_color),
}), }),
LabelContent::Clock { format, timezone } => { LabelContent::Clock { format, timezone } => {
let tz: Option<Tz> = timezone.as_ref().map(|tz| { let tz_str = match timezone {
tz.parse().unwrap_or_else(|_| { Some(TimezoneDef::Idx(idx)) => {
log::error!("Failed to parse timezone '{}'", &tz); 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); label.set_fg_color(*FALLBACK_COLOR);
Tz::UTC None
}) }
}); }
Some(TimezoneDef::Str(tz_str)) => Some(tz_str.as_ref()),
None => None,
};
Some(LabelData::Clock { Some(LabelData::Clock {
format: format.clone(), 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 { LabelContent::Timer { format } => Some(LabelData::Timer {
format: format.clone(), format: format.clone(),

View File

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

View File

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