From 4afa2c204c2e2c9990b6dda508077d0742f18bf7 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 10 Nov 2024 10:46:46 +0200 Subject: [PATCH] create separated wrapper for home action --- src/app.rs | 12 ++- src/app/browser.rs | 10 --- src/app/browser/window.rs | 12 ++- src/app/browser/window/action.rs | 11 +++ src/app/browser/window/action/home.rs | 85 +++++++++++++++++++ src/app/browser/window/header.rs | 2 - src/app/browser/window/header/bar.rs | 2 - src/app/browser/window/header/bar/menu.rs | 8 +- src/app/browser/window/tab.rs | 12 +-- src/app/browser/window/tab/item.rs | 4 - src/app/browser/window/tab/item/page.rs | 2 - .../window/tab/item/page/navigation.rs | 3 +- .../window/tab/item/page/navigation/home.rs | 12 +-- .../tab/item/page/navigation/home/widget.rs | 13 +-- src/app/browser/window/tab/menu.rs | 10 ++- 15 files changed, 136 insertions(+), 62 deletions(-) create mode 100644 src/app/browser/window/action/home.rs diff --git a/src/app.rs b/src/app.rs index bc183c67..b99e8196 100644 --- a/src/app.rs +++ b/src/app.rs @@ -35,8 +35,6 @@ impl App { let action_page_close = SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); let action_page_close_all = SimpleAction::new(&uuid_string_random(), None); - let action_page_home = - SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); let action_page_history_back = SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); let action_page_history_forward = @@ -52,7 +50,6 @@ impl App { profile.clone(), action_page_close.clone(), action_page_close_all.clone(), - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), )); @@ -214,6 +211,14 @@ impl App { ), &["r"], ), + ( + format!( + "{}.{}", + browser.window().action().id(), + browser.window().action().home().id() + ), + &["h"], + ), // @TODO ( format!("win.{}", action_page_history_back.name()), @@ -223,7 +228,6 @@ impl App { format!("win.{}", action_page_history_forward.name()), &["Right"], ), - (format!("win.{}", action_page_home.name()), &["h"]), // @TODO page close missed ] { gobject.set_accels_for_action(detailed_action_name, &accels); diff --git a/src/app/browser.rs b/src/app/browser.rs index 0048c04c..66112042 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -32,7 +32,6 @@ impl Browser { profile: Rc, action_page_close: SimpleAction, action_page_close_all: SimpleAction, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, ) -> Browser { @@ -42,7 +41,6 @@ impl Browser { action.clone(), action_page_close.clone(), action_page_close_all.clone(), - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), )); @@ -53,7 +51,6 @@ impl Browser { &[ action_page_close.clone(), action_page_close_all.clone(), - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), ], @@ -127,13 +124,6 @@ impl Browser { } }); - action_page_home.connect_activate({ - let window = window.clone(); - move |this, _| { - window.tab_page_home(page_position_from_action_state(this)); - } - }); - action_page_history_back.connect_activate({ let window = window.clone(); move |this, _| { diff --git a/src/app/browser/window.rs b/src/app/browser/window.rs index 4b5ac099..89847f48 100644 --- a/src/app/browser/window.rs +++ b/src/app/browser/window.rs @@ -29,7 +29,6 @@ impl Window { browser_action: Rc, action_page_close: SimpleAction, action_page_close_all: SimpleAction, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, ) -> Self { @@ -42,7 +41,6 @@ impl Window { action.clone(), action_page_close.clone(), action_page_close_all.clone(), - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), ); @@ -53,7 +51,6 @@ impl Window { action.clone(), action_page_close, action_page_close_all, - action_page_home, action_page_history_back, action_page_history_forward, // Widgets @@ -81,6 +78,11 @@ impl Window { move |position| tab.page_reload(position) }); + action.home().connect_activate({ + let tab = tab.clone(); + move |position| tab.page_home(position) + }); + // Init struct Self { //header, @@ -91,10 +93,6 @@ impl Window { } // Actions - pub fn tab_page_home(&self, page_position: Option) { - self.tab.page_home(page_position); - } - pub fn tab_page_history_back(&self, page_position: Option) { self.tab.page_history_back(page_position); } diff --git a/src/app/browser/window/action.rs b/src/app/browser/window/action.rs index 8b83b0f4..6a3ceae3 100644 --- a/src/app/browser/window/action.rs +++ b/src/app/browser/window/action.rs @@ -1,8 +1,10 @@ mod append; +mod home; mod pin; mod reload; use append::Append; +use home::Home; use pin::Pin; use reload::Reload; @@ -17,6 +19,7 @@ use std::rc::Rc; pub struct Action { // Actions append: Rc, + home: Rc, pin: Rc, reload: Rc, // Group @@ -31,6 +34,7 @@ impl Action { pub fn new() -> Self { // Init actions let append = Rc::new(Append::new()); + let home = Rc::new(Home::new()); let pin = Rc::new(Pin::new()); let reload = Rc::new(Reload::new()); @@ -42,12 +46,14 @@ impl Action { // Add action to given group gobject.add_action(append.gobject()); + gobject.add_action(home.gobject()); gobject.add_action(pin.gobject()); gobject.add_action(reload.gobject()); // Done Self { append, + home, pin, reload, id, @@ -62,6 +68,11 @@ impl Action { &self.append } + /// Get reference `Home` action + pub fn home(&self) -> &Rc { + &self.home + } + /// Get reference `Pin` action pub fn pin(&self) -> &Rc { &self.pin diff --git a/src/app/browser/window/action/home.rs b/src/app/browser/window/action/home.rs new file mode 100644 index 00000000..536616a8 --- /dev/null +++ b/src/app/browser/window/action/home.rs @@ -0,0 +1,85 @@ +use gtk::{ + gio::SimpleAction, + glib::{uuid_string_random, GString}, + prelude::{ActionExt, ToVariant}, +}; + +// Defaults + +/// C-compatible variant type +const DEFAULT_STATE: i32 = -1; + +/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Home` action of `Window` group +pub struct Home { + gobject: SimpleAction, +} + +impl Home { + // Constructors + + /// Create new `Self` + pub fn new() -> Self { + Self { + gobject: SimpleAction::new_stateful( + &uuid_string_random(), + None, + &DEFAULT_STATE.to_variant(), + ), + } + } + + // Actions + + /// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal + pub fn activate(&self) { + self.gobject.activate(None); + } + + /// Change action [state](https://docs.gtk.org/gio/method.SimpleAction.set_state.html) + /// * set `DEFAULT_STATE` on `None` + pub fn change_state(&self, state: Option) { + self.gobject.change_state( + &match state { + Some(value) => value, + None => DEFAULT_STATE, + } + .to_variant(), + ) + } + + // Events + + /// Define callback function for + /// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal + pub fn connect_activate(&self, callback: impl Fn(Option) + 'static) { + let state = self.state(); + self.gobject.connect_activate(move |_, _| callback(state)); + } + + // Getters + + pub fn state(&self) -> Option { + let state = self + .gobject + .state() + .expect("State value required") + .get::() + .expect("Parameter type does not match `i32`"); + + if state != DEFAULT_STATE { + Some(state) + } else { + None + } + } + + /// Get reference to [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) GObject + pub fn gobject(&self) -> &SimpleAction { + &self.gobject + } + + /// Get auto-generated [action name](https://docs.gtk.org/gio/property.SimpleAction.name.html) + pub fn id(&self) -> GString { + self.gobject.name() + } +} diff --git a/src/app/browser/window/header.rs b/src/app/browser/window/header.rs index 24177c24..78729da3 100644 --- a/src/app/browser/window/header.rs +++ b/src/app/browser/window/header.rs @@ -22,7 +22,6 @@ impl Header { window_action: Rc, action_page_close: SimpleAction, action_page_close_all: SimpleAction, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, // Widgets @@ -34,7 +33,6 @@ impl Header { window_action, action_page_close, action_page_close_all, - action_page_home, action_page_history_back, action_page_history_forward, tab_view, diff --git a/src/app/browser/window/header/bar.rs b/src/app/browser/window/header/bar.rs index 00503d55..2fb7bf00 100644 --- a/src/app/browser/window/header/bar.rs +++ b/src/app/browser/window/header/bar.rs @@ -25,7 +25,6 @@ impl Bar { window_action: Rc, action_page_close: SimpleAction, action_page_close_all: SimpleAction, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, view: &TabView, @@ -38,7 +37,6 @@ impl Bar { window_action, action_page_close, action_page_close_all, - action_page_home, action_page_history_back, action_page_history_forward, ); diff --git a/src/app/browser/window/header/bar/menu.rs b/src/app/browser/window/header/bar/menu.rs index 3bcb84c7..c0dcdbbc 100644 --- a/src/app/browser/window/header/bar/menu.rs +++ b/src/app/browser/window/header/bar/menu.rs @@ -21,7 +21,6 @@ impl Menu { window_action: Rc, action_page_close: SimpleAction, action_page_close_all: SimpleAction, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, ) -> Rc { @@ -50,7 +49,12 @@ impl Menu { // Main > Page > Navigation let main_page_navigation = gio::Menu::new(); - main_page_navigation.append(Some("Home"), Some(&detailed_action_name(&action_page_home))); + + main_page.append(Some("Home"), Some(&format!( + "{}.{}", + window_action.id(), + window_action.home().id() + ))); // Main > Page > Navigation > History let main_page_navigation_history = gio::Menu::new(); diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index a1e24b47..cff4978b 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -27,7 +27,6 @@ pub struct Tab { browser_action: Rc, window_action: Rc, // Page actions - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, // Dynamically allocated reference index @@ -43,7 +42,6 @@ impl Tab { window_action: Rc, action_page_close: SimpleAction, action_page_close_all: SimpleAction, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, ) -> Rc { @@ -60,7 +58,6 @@ impl Tab { action_page_close.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), - action_page_home.clone(), ); // Init widget @@ -76,7 +73,6 @@ impl Tab { let window_action = window_action.clone(); let action = action.clone(); - let action_page_home = action_page_home.clone(); let action_page_history_back = action_page_history_back.clone(); let action_page_history_forward = action_page_history_forward.clone(); @@ -89,7 +85,6 @@ impl Tab { window_action.clone(), action.clone(), // Page actions - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), // Options @@ -116,7 +111,6 @@ impl Tab { let action_page_close = action_page_close.clone(); let action_page_history_back = action_page_history_back.clone(); let action_page_history_forward = action_page_history_forward.clone(); - let action_page_home = action_page_home.clone(); let window_action = window_action.clone(); move |tab_view, tab_page| { // Update actions @@ -126,8 +120,10 @@ impl Tab { // Context menu closed (reset state to defaults) None => None, }; // @TODO + window_action.pin().change_state(state_v2); window_action.reload().change_state(state_v2); + window_action.home().change_state(state_v2); // @TODO old version requires update // Setup state for selected page @@ -141,7 +137,6 @@ impl Tab { action_page_close.change_state(&state); action_page_history_back.change_state(&state); action_page_history_forward.change_state(&state); - action_page_home.change_state(&state); } }); @@ -182,7 +177,6 @@ impl Tab { browser_action, window_action, // Global actions - action_page_home, action_page_history_back, action_page_history_forward, // Init empty HashMap index as no tabs appended yet @@ -202,7 +196,6 @@ impl Tab { // Local actions self.action.clone(), // Global actions - self.action_page_home.clone(), self.action_page_history_back.clone(), self.action_page_history_forward.clone(), // Options @@ -345,7 +338,6 @@ impl Tab { self.browser_action.clone(), self.window_action.clone(), self.action.clone(), - self.action_page_home.clone(), self.action_page_history_back.clone(), self.action_page_history_forward.clone(), ) { diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index 3fbe9760..bea44332 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -35,7 +35,6 @@ impl Item { window_action: Rc, tab_action: Rc, // @TODO - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, // Options @@ -53,7 +52,6 @@ impl Item { browser_action, window_action, tab_action, - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), ); @@ -135,7 +133,6 @@ impl Item { browser_action: Rc, window_action: Rc, tab_action: Rc, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, ) -> Result>, String> { @@ -151,7 +148,6 @@ impl Item { browser_action.clone(), window_action.clone(), tab_action.clone(), - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), // Options diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index 378104f1..090d83e6 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -60,7 +60,6 @@ impl Page { browser_action: Rc, window_action: Rc, tab_action: Rc, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, ) -> Rc { @@ -75,7 +74,6 @@ impl Page { let navigation = Navigation::new_rc( browser_action.clone(), window_action.clone(), - action_page_home.clone(), action_page_history_back.clone(), action_page_history_forward.clone(), action_page_open.clone(), diff --git a/src/app/browser/window/tab/item/page/navigation.rs b/src/app/browser/window/tab/item/page/navigation.rs index 07b75fd7..b6c097a9 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -33,13 +33,12 @@ impl Navigation { pub fn new_rc( browser_action: Rc, window_action: Rc, - action_page_home: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, action_page_open: SimpleAction, ) -> Rc { // Init components - let home = Home::new_rc(action_page_home); + let home = Home::new_rc(window_action.clone()); let history = History::new_rc(action_page_history_back, action_page_history_forward); let reload = Reload::new_rc(window_action); let request = Request::new_rc(browser_action, action_page_open.clone()); diff --git a/src/app/browser/window/tab/item/page/navigation/home.rs b/src/app/browser/window/tab/item/page/navigation/home.rs index 2a12a091..54efeda0 100644 --- a/src/app/browser/window/tab/item/page/navigation/home.rs +++ b/src/app/browser/window/tab/item/page/navigation/home.rs @@ -2,26 +2,26 @@ mod widget; use widget::Widget; +use crate::app::browser::window::action::Action as WindowAction; use gtk::{ - gio::SimpleAction, glib::{gformat, GString, Uri}, Button, }; use std::{cell::RefCell, rc::Rc}; pub struct Home { - action_page_home: SimpleAction, + window_action: Rc, uri: RefCell>, widget: Rc, } impl Home { // Construct - pub fn new_rc(action_page_home: SimpleAction) -> Rc { + pub fn new_rc(window_action: Rc) -> Rc { Rc::new(Self { - action_page_home: action_page_home.clone(), + window_action: window_action.clone(), uri: RefCell::new(None), - widget: Widget::new_rc(action_page_home), + widget: Widget::new_rc(window_action), }) } @@ -37,7 +37,7 @@ impl Home { self.uri.replace(uri); // Update action status - self.action_page_home.set_enabled(status); + self.window_action.home().gobject().set_enabled(status); // Update child components self.widget.update(status); diff --git a/src/app/browser/window/tab/item/page/navigation/home/widget.rs b/src/app/browser/window/tab/item/page/navigation/home/widget.rs index eaf5aa51..1eadfb64 100644 --- a/src/app/browser/window/tab/item/page/navigation/home/widget.rs +++ b/src/app/browser/window/tab/item/page/navigation/home/widget.rs @@ -1,6 +1,6 @@ +use crate::app::browser::window::action::Action as WindowAction; use gtk::{ - gio::SimpleAction, - prelude::{ActionExt, ButtonExt, WidgetExt}, + prelude::{ButtonExt, WidgetExt}, Button, }; use std::rc::Rc; @@ -11,7 +11,7 @@ pub struct Widget { impl Widget { // Construct - pub fn new_rc(action_page_home: SimpleAction) -> Rc { + pub fn new_rc(window_action: Rc) -> Rc { // Init gobject let gobject = Button::builder() .icon_name("go-home-symbolic") @@ -20,12 +20,7 @@ impl Widget { .build(); // Init events - gobject.connect_clicked({ - let action_page_home = action_page_home.clone(); - move |_| { - action_page_home.activate(None); - } - }); + gobject.connect_clicked(move |_| window_action.home().activate()); // Return activated struct Rc::new(Self { gobject }) diff --git a/src/app/browser/window/tab/menu.rs b/src/app/browser/window/tab/menu.rs index a9bcf227..7a8e8cc0 100644 --- a/src/app/browser/window/tab/menu.rs +++ b/src/app/browser/window/tab/menu.rs @@ -20,7 +20,6 @@ impl Menu { action_page_close: SimpleAction, action_page_history_back: SimpleAction, action_page_history_forward: SimpleAction, - action_page_home: SimpleAction, ) -> Self { let main = gtk::gio::Menu::new(); @@ -44,7 +43,14 @@ impl Menu { let navigation = gtk::gio::Menu::new(); - navigation.append(Some("Home"), Some(&detailed_action_name(action_page_home))); + navigation.append( + Some("Home"), + Some(&format!( + "{}.{}", + window_action.id(), + window_action.home().id() + )), + ); main.append_section(None, &navigation);