implement Page::info::reset method

This commit is contained in:
yggverse 2025-03-27 01:04:56 +02:00
parent a832936054
commit 560adff490
2 changed files with 35 additions and 44 deletions

View file

@ -39,7 +39,7 @@ impl Gemini {
p.set_progress(match event {
// 0.1 reserved for handle begin
SocketClientEvent::Resolving => {
i.clear_events().add_event("Resolving".to_string());
i.reset().add_event("Resolving".to_string());
0.2
}
SocketClientEvent::Resolved => {
@ -181,13 +181,7 @@ fn handle(
/// * includes commit action!
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_header(None)
.set_mime(None)
.set_size(None)
.commit();
i.add_event(event_name.to_string()).commit();
page.navigation.request.update_secondary_icon(&i)
}
// Update socket info at the point, where the connection is active yet
@ -197,10 +191,10 @@ fn handle(
let mut i = page.navigation.request.info.borrow_mut();
i
.set_request(Some(uri.to_string()))
.set_socket(
.set_socket(Some((
connection.socket_connection.local_address().unwrap(),
connection.socket_connection.remote_address().unwrap()
);
)));
// * unwrap fails only on `connection.socket_connection.is_closed()`
// drop the panic as unexpected.
}
@ -219,7 +213,6 @@ fn handle(
.add_event(EVENT_COMPLETED.to_string())
.set_header(Some(input.as_str().to_string()))
.set_size(Some(input.as_bytes().len()))
.set_mime(None)
.commit();
page.navigation.request.update_secondary_icon(&i);
match input {
@ -493,7 +486,6 @@ fn handle(
.add_event(EVENT_COMPLETED.to_string())
.set_header(Some(success.as_header_str().to_string()))
.set_mime(Some(mime.to_string()))
.set_size(None)
.commit();
page.navigation.request.update_secondary_icon(&i)
},
@ -562,10 +554,7 @@ fn handle(
i
.add_event(EVENT_COMPLETED.to_string())
.set_header(Some(redirect.as_str().to_string()))
.set_mime(None)
.set_size(None)
.commit();
page.navigation.request.info.replace(match redirect {
Redirect::Permanent { .. } => i.into_permanent_redirect(),
Redirect::Temporary { .. } => i.into_temporary_redirect(),
@ -590,7 +579,6 @@ fn handle(
.add_event(EVENT_COMPLETED.to_string())
.set_header(Some(certificate.as_str().to_string()))
.set_size(Some(certificate.as_bytes().len()))
.set_mime(None)
.commit();
page.navigation.request.update_secondary_icon(&i);
// update page content widget
@ -631,12 +619,7 @@ fn handle(
}
redirects.replace(0); // reset
let mut i = page.navigation.request.info.borrow_mut();
i.add_event(EVENT_COMPLETED.to_string())
.set_request(Some(uri.to_string()))
.set_header(None)
.set_size(None)
.set_mime(None)
.commit();
i.add_event(EVENT_COMPLETED.to_string()).set_request(Some(uri.to_string())).commit();
page.navigation.request.update_secondary_icon(&i)
}
}

View file

@ -53,6 +53,22 @@ impl Info {
}
}
/// Take `Self`, convert it into the new `Redirect` member,
/// * return new `Self` back
fn into_redirect(self, method: redirect::Method) -> Self {
let mut this = Self::new();
this.redirect = Some(Box::new(Redirect { info: self, method }));
this
}
pub fn into_permanent_redirect(self) -> Self {
self.into_redirect(redirect::Method::Permanent)
}
pub fn into_temporary_redirect(self) -> Self {
self.into_redirect(redirect::Method::Temporary)
}
// Actions
pub fn dialog(&self, parent: &impl IsA<gtk::Widget>, profile: &Profile) {
@ -66,28 +82,24 @@ impl Info {
}
/// Mark `Self` as deprecated
/// * tip: usually called on page handler begin
/// * tip: usually called on page load begin,
/// to prevent driver implementation mistakes
pub fn deprecate(&mut self) {
self.is_deprecated = true;
}
// Setters
// * useful to update `Self` as chain of values
// * update `Self` in chain
/// Take `Self`, convert it into the redirect member,
/// then, return new `Self` back
pub fn into_redirect(self, method: redirect::Method) -> Self {
let mut this = Self::new();
this.redirect = Some(Box::new(Redirect { info: self, method }));
this
}
pub fn into_permanent_redirect(self) -> Self {
self.into_redirect(redirect::Method::Permanent)
}
pub fn into_temporary_redirect(self) -> Self {
self.into_redirect(redirect::Method::Temporary)
/// Reset `Self` to the clean state
/// * this method keeps `Redirect` value!
pub fn reset(&mut self) -> &mut Self {
self.clear_events()
.set_header(None)
.set_mime(None)
.set_request(None)
.set_size(None)
.set_socket(None)
}
pub fn add_event(&mut self, name: String) -> &mut Self {
@ -110,12 +122,8 @@ impl Info {
self
}
pub fn set_socket(
&mut self,
local_address: SocketAddress,
remote_address: SocketAddress,
) -> &mut Self {
self.socket = Some(Socket {
pub fn set_socket(&mut self, address: Option<(SocketAddress, SocketAddress)>) -> &mut Self {
self.socket = address.map(|(local_address, remote_address)| Socket {
local_address,
remote_address,
});