Readd uptime timer label (#336)
* Readd uptime timer label * Fixed timer formatting
This commit is contained in:
@@ -18,6 +18,16 @@ See the Custom Timezones section for more info on timezones. Skip `_timezone` to
|
||||
<label _source="clock" _display="time" _timezone="0" [...] />
|
||||
```
|
||||
|
||||
#### Timer label
|
||||
|
||||
Instead of a clock, this label shows the amount of time since program start, (aka time in VR).
|
||||
|
||||
Use `_format` to arrange `%h` hours, `%m` minutes, and `%s` seconds.
|
||||
|
||||
```xml
|
||||
<label _source="timer" _format="%h:%m:%s" [...] />
|
||||
```
|
||||
|
||||
#### Battery label
|
||||
|
||||
This is a label type that's used internally to display battery states.
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use chrono::DateTime;
|
||||
use chrono::Local;
|
||||
use chrono_tz::Tz;
|
||||
use wgui::{
|
||||
@@ -121,6 +122,19 @@ pub(super) fn setup_custom_label<S: 'static>(
|
||||
Ok(EventResult::Pass)
|
||||
})
|
||||
}
|
||||
"timer" => {
|
||||
let format = attribs.get_value("_format").unwrap_or("%h:%m");
|
||||
|
||||
let state = TimerLabelState {
|
||||
start: Local::now(),
|
||||
format: format.into(),
|
||||
};
|
||||
|
||||
Box::new(move |common, data, _, _| {
|
||||
timer_on_tick(&state, common, data);
|
||||
Ok(EventResult::Pass)
|
||||
})
|
||||
}
|
||||
"ipd" => Box::new(|common, data, app, _| {
|
||||
ipd_on_tick(common, data, app);
|
||||
Ok(EventResult::Pass)
|
||||
@@ -198,6 +212,30 @@ fn clock_on_tick(
|
||||
label.set_text(common, Translation::from_raw_text(&date_time));
|
||||
}
|
||||
|
||||
struct TimerLabelState {
|
||||
start: DateTime<Local>,
|
||||
format: Rc<str>,
|
||||
}
|
||||
|
||||
fn timer_on_tick(
|
||||
state: &TimerLabelState,
|
||||
common: &mut event::CallbackDataCommon,
|
||||
data: &mut event::CallbackData,
|
||||
) {
|
||||
let duration = Local::now()
|
||||
.signed_duration_since(&state.start)
|
||||
.num_seconds();
|
||||
|
||||
let time = &state
|
||||
.format
|
||||
.replace("%s", &format!("{:02}", (duration % 60)))
|
||||
.replace("%m", &format!("{:02}", ((duration / 60) % 60)))
|
||||
.replace("%h", &format!("{:02}", ((duration / 60) / 60)));
|
||||
|
||||
let label = data.obj.get_as_mut::<WidgetLabel>().unwrap();
|
||||
label.set_text(common, Translation::from_raw_text(&time));
|
||||
}
|
||||
|
||||
fn ipd_on_tick(
|
||||
common: &mut event::CallbackDataCommon,
|
||||
data: &mut event::CallbackData,
|
||||
|
||||
Reference in New Issue
Block a user