reduce codebase by use common row pattern

This commit is contained in:
yggverse 2025-03-26 05:53:53 +02:00
parent 1dd7aafb0c
commit 0a5e837140

View file

@ -5,7 +5,10 @@ use adw::{
ActionRowExt, ExpanderRowExt, PreferencesDialogExt, PreferencesGroupExt, PreferencesPageExt, ActionRowExt, ExpanderRowExt, PreferencesDialogExt, PreferencesGroupExt, PreferencesPageExt,
}, },
}; };
use gtk::{Align, glib::gformat}; use gtk::{
Align,
glib::{GString, gformat},
};
pub trait Dialog { pub trait Dialog {
fn info(profile: &Profile, info: &Info) -> Self; fn info(profile: &Profile, info: &Info) -> Self;
@ -13,6 +16,17 @@ pub trait Dialog {
impl Dialog for PreferencesDialog { impl Dialog for PreferencesDialog {
fn info(profile: &Profile, info: &Info) -> Self { fn info(profile: &Profile, info: &Info) -> Self {
/// Common `ActionRow` widget pattern
fn row(title: impl Into<GString>, subtitle: impl Into<GString>) -> ActionRow {
ActionRow::builder()
.css_classes(["property"])
.use_markup(true)
.subtitle(subtitle)
.subtitle_selectable(true)
.title_selectable(true)
.title(title)
.build()
}
let d = PreferencesDialog::builder() let d = PreferencesDialog::builder()
.search_enabled(true) .search_enabled(true)
.title("Page info") .title("Page info")
@ -26,15 +40,7 @@ impl Dialog for PreferencesDialog {
p.add(&{ p.add(&{
let g = PreferencesGroup::builder().title("Meta").build(); let g = PreferencesGroup::builder().title("Meta").build();
if let Some(ref mime) = info.mime { if let Some(ref mime) = info.mime {
g.add( g.add(&row("Content type", mime))
&ActionRow::builder()
.css_classes(["property"])
.subtitle_selectable(true)
.subtitle(mime)
.title_selectable(true)
.title("Content type")
.build(),
)
} }
g g
}); });
@ -42,16 +48,6 @@ impl Dialog for PreferencesDialog {
if info.size.is_some() || info.header.is_some() { if info.size.is_some() || info.header.is_some() {
p.add(&{ p.add(&{
use crate::tool::Format; use crate::tool::Format;
/// Common `ActionRow` widget pattern
fn r(title: &str, subtitle: String) -> ActionRow {
ActionRow::builder()
.css_classes(["property"])
.subtitle_selectable(true)
.subtitle(subtitle)
.title_selectable(true)
.title(title)
.build()
}
let g = PreferencesGroup::builder().title("Size").build(); let g = PreferencesGroup::builder().title("Size").build();
let mut i = 0; // count group members let mut i = 0; // count group members
let mut t = 0; // count total size let mut t = 0; // count total size
@ -91,10 +87,10 @@ impl Dialog for PreferencesDialog {
if let Some(ref c) = info.size { if let Some(ref c) = info.size {
i += 1; i += 1;
t += c; t += c;
g.add(&r("Content", c.bytes())) g.add(&row("Content", c.bytes()))
} }
if i > 1 && t > 0 { if i > 1 && t > 0 {
g.add(&r("Total", t.bytes())) g.add(&row("Total", t.bytes()))
} }
g g
}); });
@ -121,16 +117,6 @@ impl Dialog for PreferencesDialog {
_ => panic!(), _ => panic!(),
} }
} }
/// Common `ActionRow` widget pattern
fn r(title: &str, subtitle: &str) -> ActionRow {
ActionRow::builder()
.css_classes(["property"])
.subtitle_selectable(true)
.subtitle(subtitle)
.title_selectable(true)
.title(title)
.build()
}
/// Lookup [MaxMind](https://www.maxmind.com) database /// Lookup [MaxMind](https://www.maxmind.com) database
fn l(profile: &Profile, socket_address: &SocketAddress) -> Option<String> { fn l(profile: &Profile, socket_address: &SocketAddress) -> Option<String> {
use maxminddb::{ use maxminddb::{
@ -172,19 +158,19 @@ impl Dialog for PreferencesDialog {
} }
p.add(&{ p.add(&{
let g = PreferencesGroup::builder().title("Remote").build(); let g = PreferencesGroup::builder().title("Remote").build();
g.add(&r("Address", &socket.remote_address.to_string())); g.add(&row("Address", socket.remote_address.to_string()));
g.add(&r("Family", f2s(&socket.remote_address.family()))); g.add(&row("Family", f2s(&socket.remote_address.family())));
if let Some(location) = l(profile, &socket.remote_address) { if let Some(location) = l(profile, &socket.remote_address) {
g.add(&r("Location", &location)); g.add(&row("Location", location))
} }
g g
}); });
p.add(&{ p.add(&{
let g = PreferencesGroup::builder().title("Local").build(); let g = PreferencesGroup::builder().title("Local").build();
g.add(&r("Address", &socket.local_address.to_string())); g.add(&row("Address", socket.local_address.to_string()));
g.add(&r("Family", f2s(&socket.local_address.family()))); g.add(&row("Family", f2s(&socket.local_address.family())));
if let Some(location) = l(profile, &socket.local_address) { if let Some(location) = l(profile, &socket.local_address) {
g.add(&r("Location", &location)); g.add(&row("Location", location));
} }
g g
}); });
@ -217,6 +203,7 @@ impl Dialog for PreferencesDialog {
for (i, r) in b.iter().enumerate() { for (i, r) in b.iter().enumerate() {
g.add(&{ g.add(&{
let a = ActionRow::builder() let a = ActionRow::builder()
.css_classes(["property"])
.subtitle_selectable(true) .subtitle_selectable(true)
.title_selectable(true) .title_selectable(true)
.title(r.request().unwrap()) .title(r.request().unwrap())
@ -268,28 +255,15 @@ impl Dialog for PreferencesDialog {
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(&row(n, t.format_iso8601().unwrap()));
&ActionRow::builder()
.subtitle_selectable(true)
.subtitle(t.format_iso8601().unwrap())
.title_selectable(true)
.title(n)
.build(),
);
for (i, e) in info.event[1..].iter().enumerate() { for (i, e) in info.event[1..].iter().enumerate() {
g.add(&{ g.add(&{
use gtk::{Align, Label}; use gtk::{Align, Label};
let c = e.time().difference(info.event[i].time()).as_milliseconds(); let c = e.time().difference(info.event[i].time()).as_milliseconds();
let a = ActionRow::builder() let a = row(
.use_markup(true) e.name(),
.subtitle(gformat!( gformat!("{} ms", e.time().difference(t).as_milliseconds()),
"{} ms", );
e.time().difference(t).as_milliseconds()
))
.subtitle_selectable(true)
.title_selectable(true)
.title(e.name())
.build();
a.add_suffix( a.add_suffix(
&Label::builder() &Label::builder()
.css_classes([ .css_classes([