From bc6a9a05751c325225ee68a23748edafa3dc2d9c Mon Sep 17 00:00:00 2001 From: yggverse Date: Mon, 4 Nov 2024 19:35:39 +0200 Subject: [PATCH] implement tmp helpers --- src/app/browser.rs | 49 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/app/browser.rs b/src/app/browser.rs index 89d61770..46fe3fa0 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -11,6 +11,7 @@ use window::Window; use adw::ApplicationWindow; use gtk::{ gio::{Cancellable, File, SimpleAction}, + glib::Variant, prelude::{ActionExt, GtkWindowExt}, FileLauncher, }; @@ -120,14 +121,7 @@ impl Browser { action_update.connect_activate({ let window = window.clone(); - move |_, id| { - window.update( - id.expect("Page ID required for update action") - .get::() - .expect("Parameter does not match `String`") - .as_str(), - ); - } + move |_, this| window.update(string_from_variant(this).as_str()) }); action_page_new.connect_activate({ @@ -174,19 +168,7 @@ impl Browser { action_page_reload.connect_activate({ let window = window.clone(); - move |this, _| { - let page_position = this - .state() - .expect("Page position required for reload action") - .get::() - .expect("Parameter does not match `i32`"); - - if page_position > -1 { - window.tab_page_navigation_reload(Some(page_position)); - } else { - window.tab_page_navigation_reload(None); - } - } + move |this, _| window.tab_page_navigation_reload(page_position_from_action_state(this)) }); action_page_pin.connect_activate({ @@ -290,3 +272,28 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> { // Success Ok(()) } + +/// Extract formatted page position from C-based +/// [action state](https://docs.gtk.org/gio/property.SimpleAction.state.html) +fn page_position_from_action_state(action: &SimpleAction) -> Option { + let page_position = action + .state() + .expect("Page position required for reload action") + .get::() + .expect("Parameter does not match `i32`"); + + if page_position > -1 { + Some(page_position) + } else { + None + } +} // @TODO move outside + +/// Extract `String` from C-based +/// [Variant](https://docs.gtk.org/glib/struct.Variant.html) +fn string_from_variant(variant: Option<&Variant>) -> String { + variant + .expect("Variant required for this action") + .get::() + .expect("Parameter does not match `String`") +} // @TODO move outside