update timing counter widget

This commit is contained in:
yggverse 2025-03-25 19:08:42 +02:00
parent 48aa19d1e4
commit 719c25e126

View file

@ -3,10 +3,7 @@ use adw::{
ActionRow, PreferencesDialog, PreferencesGroup, PreferencesPage, ActionRow, PreferencesDialog, PreferencesGroup, PreferencesPage,
prelude::{ActionRowExt, PreferencesDialogExt, PreferencesGroupExt, PreferencesPageExt}, prelude::{ActionRowExt, PreferencesDialogExt, PreferencesGroupExt, PreferencesPageExt},
}; };
use gtk::{ use gtk::glib::gformat;
glib::gformat,
prelude::{ButtonExt, WidgetExt},
};
pub trait Dialog { pub trait Dialog {
fn info(profile: &Profile, info: &Info) -> Self; fn info(profile: &Profile, info: &Info) -> Self;
@ -237,69 +234,49 @@ impl Dialog for PreferencesDialog {
.icon_name("system-run-symbolic") .icon_name("system-run-symbolic")
.build(); .build();
p.add(&{ p.add(&{
use gtk::{Align, Button}; use gtk::{Align, Label};
/// Common event badge pattern /// Right (prefix) widget
fn b() -> Button { fn r(c: i64) -> Label {
Button::builder() Label::builder()
.css_classes(["flat"]) .css_classes(["flat", if c == 0 { "success" } else { "warning" }])
.halign(Align::End)
.label(if c > 0 {
format!("+{c} ms")
} else {
c.to_string()
})
.sensitive(false) .sensitive(false)
.valign(Align::Center) .valign(Align::Center)
.halign(Align::Center)
.width_request(64)
.build() .build()
} }
/// Left (prefix) widget
fn l(b: Button, v: Option<(i64, i64)>) -> Button {
if let Some((d, t)) = v {
b.add_css_class(if d == 0 {
"success"
} else if d > t {
"danger"
} else {
"warning"
});
b.set_label(&if d > 0 {
format!("+{d}")
} else {
d.to_string()
});
} else {
b.add_css_class("success");
b.set_icon_name("media-record-symbolic");
}
b
}
let g = PreferencesGroup::new(); let g = PreferencesGroup::new();
let e = &info.event[0]; let e = &info.event[0];
let t = e.time(); let t = e.time();
let n = e.name(); let n = e.name();
g.add(&{ g.add(
let a = ActionRow::builder() &ActionRow::builder()
.subtitle_selectable(true) .subtitle_selectable(true)
.subtitle(t.format_iso8601().unwrap()) .subtitle(t.format_iso8601().unwrap())
.title_selectable(true) .title_selectable(true)
.title(n) .title(n)
.build(); .build(),
a.add_prefix(&l(b(), None)); );
a
});
for (i, e) in info.event[1..].iter().enumerate() { for (i, e) in info.event[1..].iter().enumerate() {
g.add(&{ g.add(&{
let total = e.time().difference(t).as_milliseconds();
let a = ActionRow::builder() let a = ActionRow::builder()
.use_markup(true) .use_markup(true)
.subtitle(gformat!("{total} ms")) .subtitle(gformat!(
"{} ms",
e.time().difference(t).as_milliseconds()
))
.subtitle_selectable(true) .subtitle_selectable(true)
.title_selectable(true) .title_selectable(true)
.title(e.name()) .title(e.name())
.build(); .build();
a.add_prefix(&l( a.add_suffix(&r(e
b(), .time()
Some(( .difference(info.event[i].time())
e.time().difference(info.event[i].time()).as_milliseconds(), .as_milliseconds()));
total,
)),
));
a a
}) })
} }