fix page load relation to self

This commit is contained in:
yggverse 2024-11-05 09:01:53 +02:00
parent 5c4b711044
commit 21fc566559

View File

@ -34,8 +34,8 @@ use std::{sync::Arc, time::Duration};
pub struct Page { pub struct Page {
id: GString, id: GString,
// Actions // Actions
action_page_load: SimpleAction,
action_page_open: SimpleAction, action_page_open: SimpleAction,
action_page_reload: SimpleAction,
action_update: SimpleAction, action_update: SimpleAction,
// Components // Components
navigation: Arc<Navigation>, navigation: Arc<Navigation>,
@ -59,6 +59,7 @@ impl Page {
action_update: SimpleAction, action_update: SimpleAction,
) -> Arc<Self> { ) -> Arc<Self> {
// Init local actions // Init local actions
let action_page_load = SimpleAction::new(&uuid_string_random(), None);
let action_page_open = let action_page_open =
SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type())); SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type()));
@ -86,42 +87,13 @@ impl Page {
let meta = Meta::new_arc(Status::New, gformat!("New page")); let meta = Meta::new_arc(Status::New, gformat!("New page"));
// Init events // Init `Self`
action_page_open.connect_activate({ let this = Arc::new(Self {
let navigation = navigation.clone();
let action_page_reload = action_page_reload.clone();
move |_, parameter| {
// Get request value from action parameter
let request = parameter
.expect("Parameter required for this action")
.get::<String>()
.expect("Parameter does not match `String`");
// Update navigation entry
navigation.set_request_text(&request);
// Add history record
match navigation.history_current() {
Some(current) => {
if current != request {
navigation.history_add(request.into());
}
}
None => navigation.history_add(request.into()),
}
// Reload page
action_page_reload.activate(None);
}
});
// Return activated `Self`
Arc::new(Self {
id, id,
// Actions // Actions
action_page_open, action_page_load: action_page_load.clone(),
action_page_reload, action_page_open: action_page_open.clone(),
action_update, action_update: action_update.clone(),
// Components // Components
content, content,
navigation, navigation,
@ -130,7 +102,33 @@ impl Page {
meta, meta,
// GTK // GTK
widget, widget,
}) });
// Init events
action_page_load.connect_activate({
let this = this.clone();
move |_, _| this.load(false)
});
action_page_open.connect_activate({
let this = this.clone();
move |_, parameter| {
// Get request value from action parameter
let request = parameter
.expect("Parameter required for this action")
.get::<String>()
.expect("Parameter does not match `String`");
// Update navigation entry
this.navigation.set_request_text(&request);
// Reload page
this.load(true);
}
});
// Return activated `Self`
this
} }
// Actions // Actions
@ -147,8 +145,8 @@ impl Page {
// Update navigation entry // Update navigation entry
self.navigation.set_request_text(&request); self.navigation.set_request_text(&request);
// Reload page (without history record) // Load page (without history record)
self.reload(); self.load(false);
} }
} }
@ -157,12 +155,16 @@ impl Page {
// Update navigation entry // Update navigation entry
self.navigation.set_request_text(&request); self.navigation.set_request_text(&request);
// Reload page (without history record) // Load page (without history record)
self.reload(); self.load(false);
} }
} }
pub fn reload(&self) { pub fn reload(&self) {
self.load(true);
}
pub fn load(&self, history: bool) {
/// Global limit to prevent infinitive redirects (ALL protocols) /// Global limit to prevent infinitive redirects (ALL protocols)
/// * every protocol implementation has own value checker, according to specification /// * every protocol implementation has own value checker, according to specification
const DEFAULT_MAX_REDIRECT_COUNT: i8 = 10; const DEFAULT_MAX_REDIRECT_COUNT: i8 = 10;
@ -206,6 +208,18 @@ impl Page {
self.navigation.request_text() self.navigation.request_text()
}; };
// Add history record
if history {
match self.navigation.history_current() {
Some(current) => {
if current != request {
self.navigation.history_add(request.clone());
}
}
None => self.navigation.history_add(request.clone()),
}
}
// Update // Update
self.meta.set_status(Status::Reload).set_title(&"Loading.."); self.meta.set_status(Status::Reload).set_title(&"Loading..");
self.action_update.activate(Some(&id)); self.action_update.activate(Some(&id));
@ -254,8 +268,8 @@ impl Page {
// Update navigation entry // Update navigation entry
self.navigation.set_request_text(&request); self.navigation.set_request_text(&request);
// Reload page (without history record) // Load page (without history record)
self.reload(); self.load(false);
} }
Err(_) => { Err(_) => {
// @TODO any action here? // @TODO any action here?
@ -271,8 +285,8 @@ impl Page {
// Update navigation entry // Update navigation entry
self.navigation.set_request_text(&request); self.navigation.set_request_text(&request);
// Reload page (without history record) // Load page (without history record)
self.reload(); self.load(false);
} }
} }
}; // Uri::parse }; // Uri::parse
@ -404,8 +418,8 @@ impl Page {
// use gemini::client::response:: // use gemini::client::response::
// Init shared objects (async) // Init shared objects (async)
let action_page_load = self.action_page_load.clone();
let action_page_open = self.action_page_open.clone(); let action_page_open = self.action_page_open.clone();
let action_page_reload = self.action_page_reload.clone();
let action_update = self.action_update.clone(); let action_update = self.action_update.clone();
let content = self.content.clone(); let content = self.content.clone();
let id = self.id.to_variant(); let id = self.id.to_variant();
@ -750,7 +764,7 @@ impl Page {
.set_title(&"Redirect"); .set_title(&"Redirect");
// Reload page to apply redirection // Reload page to apply redirection
action_page_reload.activate(None); action_page_load.activate(None);
} }
}, },
Err(reason) => { Err(reason) => {