From 710093b16162abe4df51811ae6d08f4b2126d73c Mon Sep 17 00:00:00 2001 From: yggverse Date: Mon, 4 Nov 2024 17:38:40 +0200 Subject: [PATCH] fix active page reset --- src/app.rs | 2 +- src/app/action.rs | 3 +++ src/app/browser.rs | 6 ++++- src/app/browser/window.rs | 3 ++- src/app/browser/window/tab.rs | 42 ++++++++++++++++++++++------------- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index 9833231a..70ff86f4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -52,7 +52,7 @@ impl App { let action_page_home = Action::new("win", false, None); let action_page_history_back = Action::new("win", false, None); let action_page_history_forward = Action::new("win", false, None); - let action_page_reload = Action::new_stateful("win", true, None, &0.to_variant()); // @TODO + let action_page_reload = Action::new_stateful("win", true, None, &(-1).to_variant()); let action_page_pin = Action::new("win", true, None); // Init GTK diff --git a/src/app/action.rs b/src/app/action.rs index 1c1a7108..2eca5e1b 100644 --- a/src/app/action.rs +++ b/src/app/action.rs @@ -27,6 +27,9 @@ impl Action { Self { group, simple } } + /// Stateful constructors useful for actions that require custom values + /// but could not receive it directly with action argument + /// e.g. [MenuModel](https://docs.gtk.org/gio/class.MenuModel.html) pub fn new_stateful( group: &str, is_enabled: bool, diff --git a/src/app/browser.rs b/src/app/browser.rs index 86bb2a1f..89d61770 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -181,7 +181,11 @@ impl Browser { .get::() .expect("Parameter does not match `i32`"); - window.tab_page_navigation_reload(page_position); + if page_position > -1 { + window.tab_page_navigation_reload(Some(page_position)); + } else { + window.tab_page_navigation_reload(None); + } } }); diff --git a/src/app/browser/window.rs b/src/app/browser/window.rs index 51dbffa1..584d655d 100644 --- a/src/app/browser/window.rs +++ b/src/app/browser/window.rs @@ -92,7 +92,8 @@ impl Window { self.tab.page_navigation_history_forward(); } - pub fn tab_page_navigation_reload(&self, page_position: i32) { + /// Reload page at `i32` position or selected page on `None` given + pub fn tab_page_navigation_reload(&self, page_position: Option) { self.tab.page_navigation_reload(page_position); } diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index a2e97742..8bad75a6 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -72,23 +72,21 @@ impl Tab { // Menu opened: // setup actions to operate with page selected only Some(this) => { - let position = tab_view.page_position(this).to_variant(); + // Set state + let state = tab_view.page_position(this).to_variant(); - action_page_reload.change_state(&position); + // Update related actions + action_page_reload.change_state(&state); } // Menu closed: // return actions to default values - None => match tab_view.selected_page() { - Some(selected) => { - // Get position of page selected - let position = tab_view.page_position(&selected).to_variant(); + None => { + // Set state + let state = &(-1).to_variant(); - // Update related actions - action_page_reload.change_state(&position); - } - // No selected page found, disable related actions - None => action_page_reload.set_enabled(false), - }, + // Update related actions + action_page_reload.change_state(&state); + } } } }); @@ -242,10 +240,22 @@ impl Tab { } } - pub fn page_navigation_reload(&self, page_position: i32) { - if let Some(id) = self.widget.gobject().nth_page(page_position).keyword() { - if let Some(item) = self.index.borrow().get(&id) { - item.page_navigation_reload(); + /// Reload page at `i32` position or selected page on `None` given + pub fn page_navigation_reload(&self, page_position: Option) { + match page_position { + Some(value) => { + if let Some(id) = self.widget.gobject().nth_page(value).keyword() { + if let Some(item) = self.index.borrow().get(&id) { + item.page_navigation_reload(); + } + } + } + None => { + if let Some(id) = self.widget.current_page_keyword() { + if let Some(item) = self.index.borrow().get(&id) { + item.page_navigation_reload(); + } + } } } }