add header size, reorganize Size struct, add unset methods

This commit is contained in:
yggverse 2025-03-23 02:35:44 +02:00
parent bf4c2c1e84
commit 1a1fa86ac5
4 changed files with 60 additions and 29 deletions

View file

@ -182,8 +182,8 @@ fn handle(
fn update_page_info(page: &Page, event_name: &str) {
let mut i = page.navigation.request.info.borrow_mut();
i.add_event(event_name.to_string())
.set_mime(None)
.set_size(None)
.unset_mime()
.unset_size()
.commit()
}
// Update socket info at the point, where the connection is active yet
@ -322,7 +322,7 @@ fn handle(
i
.add_event("Parsing".to_string())
.set_mime(Some(success.mime().to_string()))
.set_size(Some(data.len()));
.set_size(None, Some(data.len()));
let w = if matches!(*feature, Feature::Source) {
page.content.to_text_source(data)
} else {
@ -426,7 +426,7 @@ fn handle(
i
.add_event(EVENT_COMPLETED.to_string())
.set_mime(Some(success.mime().to_string()))
.set_size(Some(buffer.byte_length()))
.set_size(None, Some(buffer.byte_length()))
.commit();
}
}
@ -439,7 +439,7 @@ fn handle(
i
.add_event(EVENT_COMPLETED.to_string())
.set_mime(Some(success.mime().to_string()))
.set_size(None)
.unset_size()
.commit();
}
}
@ -466,7 +466,7 @@ fn handle(
i
.add_event(EVENT_COMPLETED.to_string())
.set_mime(Some(success.mime().to_string()))
.set_size(None)
.unset_size()
.commit();
}
}
@ -491,7 +491,7 @@ fn handle(
i
.add_event(EVENT_COMPLETED.to_string())
.set_mime(Some(mime.to_string()))
.set_size(None)
.unset_size()
.commit();
}
},
@ -547,8 +547,8 @@ fn handle(
let mut i = page.navigation.request.info.take();
i
.add_event(EVENT_COMPLETED.to_string())
.set_mime(None)
.set_size(None)
.unset_mime()
.unset_size()
.commit();
page.navigation.request.info.replace(i.into_redirect());
@ -640,8 +640,8 @@ fn handle(
let mut i = page.navigation.request.info.borrow_mut();
i.add_event(EVENT_COMPLETED.to_string())
.set_request(Some(uri.to_string()))
.set_mime(None)
.set_size(None)
.unset_mime()
.unset_size()
.commit()
}
}

View file

@ -1,11 +1,13 @@
mod dialog;
mod event;
mod size;
mod socket;
use super::Profile;
use dialog::Dialog;
use event::Event;
use gtk::{gio::SocketAddress, prelude::IsA};
use size::Size;
use socket::Socket;
/// Common, shared `Page` information holder
@ -24,8 +26,8 @@ pub struct Info {
redirect: Option<Box<Self>>,
/// Key to relate data collected with the specific request
request: Option<String>,
/// Hold page content size
size: Option<usize>,
/// Hold size info
size: Size,
/// Optional socket details
/// * useful also for geo-location feature
socket: Option<Socket>,
@ -42,7 +44,7 @@ impl Info {
mime: None,
redirect: None,
request: None,
size: None,
size: Size::default(),
socket: None,
}
}
@ -87,6 +89,11 @@ impl Info {
self
}
pub fn unset_mime(&mut self) -> &mut Self {
self.mime = None;
self
}
pub fn set_socket(
&mut self,
local_address: SocketAddress,
@ -104,8 +111,13 @@ impl Info {
self
}
pub fn set_size(&mut self, size: Option<usize>) -> &mut Self {
self.size = size;
pub fn set_size(&mut self, header: Option<usize>, content: Option<usize>) -> &mut Self {
self.size = Size { content, header };
self
}
pub fn unset_size(&mut self) -> &mut Self {
self.size = Size::default();
self
}

View file

@ -37,20 +37,34 @@ impl Dialog for PreferencesDialog {
g
});
} // @TODO content language, header size, etc.
if this.size.is_some() {
if this.size.header.is_some() || this.size.content.is_some() {
p.add(&{
use crate::tool::Format;
/// 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()
}
let g = PreferencesGroup::builder().title("Size").build();
if let Some(ref size) = this.size {
g.add(&{
use crate::tool::Format;
ActionRow::builder()
.css_classes(["property"])
.subtitle_selectable(true)
.subtitle(size.bytes())
.title_selectable(true)
.title("Content")
.build()
})
let mut i = 0; // count group members
let mut t = 0; // count total size
if let Some(ref c) = this.size.header {
i += 1;
t += c;
g.add(&r("Header", &c.bytes()))
}
if let Some(ref c) = this.size.content {
i += 1;
t += c;
g.add(&r("Content", &c.bytes()))
}
if i > 1 && t > 0 {
g.add(&r("Total", &t.bytes()))
}
g
});
@ -77,7 +91,7 @@ impl Dialog for PreferencesDialog {
_ => panic!(),
}
}
/// Build common `ActionRow` widget
/// Common `ActionRow` widget pattern
fn r(title: &str, subtitle: &str) -> ActionRow {
ActionRow::builder()
.css_classes(["property"])

View file

@ -0,0 +1,5 @@
#[derive(Default)]
pub struct Size {
pub content: Option<usize>,
pub header: Option<usize>,
}