create separated wrapper for home action

This commit is contained in:
yggverse 2024-11-10 10:46:46 +02:00
parent b4dee17768
commit 4afa2c204c
15 changed files with 136 additions and 62 deletions

View File

@ -35,8 +35,6 @@ impl App {
let action_page_close = let action_page_close =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
let action_page_close_all = SimpleAction::new(&uuid_string_random(), None); 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 = let action_page_history_back =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
let action_page_history_forward = let action_page_history_forward =
@ -52,7 +50,6 @@ impl App {
profile.clone(), profile.clone(),
action_page_close.clone(), action_page_close.clone(),
action_page_close_all.clone(), action_page_close_all.clone(),
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
)); ));
@ -214,6 +211,14 @@ impl App {
), ),
&["<Primary>r"], &["<Primary>r"],
), ),
(
format!(
"{}.{}",
browser.window().action().id(),
browser.window().action().home().id()
),
&["<Primary>h"],
),
// @TODO // @TODO
( (
format!("win.{}", action_page_history_back.name()), format!("win.{}", action_page_history_back.name()),
@ -223,7 +228,6 @@ impl App {
format!("win.{}", action_page_history_forward.name()), format!("win.{}", action_page_history_forward.name()),
&["<Primary>Right"], &["<Primary>Right"],
), ),
(format!("win.{}", action_page_home.name()), &["<Primary>h"]),
// @TODO page close missed // @TODO page close missed
] { ] {
gobject.set_accels_for_action(detailed_action_name, &accels); gobject.set_accels_for_action(detailed_action_name, &accels);

View File

@ -32,7 +32,6 @@ impl Browser {
profile: Rc<Profile>, profile: Rc<Profile>,
action_page_close: SimpleAction, action_page_close: SimpleAction,
action_page_close_all: SimpleAction, action_page_close_all: SimpleAction,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
) -> Browser { ) -> Browser {
@ -42,7 +41,6 @@ impl Browser {
action.clone(), action.clone(),
action_page_close.clone(), action_page_close.clone(),
action_page_close_all.clone(), action_page_close_all.clone(),
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
)); ));
@ -53,7 +51,6 @@ impl Browser {
&[ &[
action_page_close.clone(), action_page_close.clone(),
action_page_close_all.clone(), action_page_close_all.clone(),
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.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({ action_page_history_back.connect_activate({
let window = window.clone(); let window = window.clone();
move |this, _| { move |this, _| {

View File

@ -29,7 +29,6 @@ impl Window {
browser_action: Rc<BrowserAction>, browser_action: Rc<BrowserAction>,
action_page_close: SimpleAction, action_page_close: SimpleAction,
action_page_close_all: SimpleAction, action_page_close_all: SimpleAction,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
) -> Self { ) -> Self {
@ -42,7 +41,6 @@ impl Window {
action.clone(), action.clone(),
action_page_close.clone(), action_page_close.clone(),
action_page_close_all.clone(), action_page_close_all.clone(),
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
); );
@ -53,7 +51,6 @@ impl Window {
action.clone(), action.clone(),
action_page_close, action_page_close,
action_page_close_all, action_page_close_all,
action_page_home,
action_page_history_back, action_page_history_back,
action_page_history_forward, action_page_history_forward,
// Widgets // Widgets
@ -81,6 +78,11 @@ impl Window {
move |position| tab.page_reload(position) move |position| tab.page_reload(position)
}); });
action.home().connect_activate({
let tab = tab.clone();
move |position| tab.page_home(position)
});
// Init struct // Init struct
Self { Self {
//header, //header,
@ -91,10 +93,6 @@ impl Window {
} }
// Actions // Actions
pub fn tab_page_home(&self, page_position: Option<i32>) {
self.tab.page_home(page_position);
}
pub fn tab_page_history_back(&self, page_position: Option<i32>) { pub fn tab_page_history_back(&self, page_position: Option<i32>) {
self.tab.page_history_back(page_position); self.tab.page_history_back(page_position);
} }

View File

@ -1,8 +1,10 @@
mod append; mod append;
mod home;
mod pin; mod pin;
mod reload; mod reload;
use append::Append; use append::Append;
use home::Home;
use pin::Pin; use pin::Pin;
use reload::Reload; use reload::Reload;
@ -17,6 +19,7 @@ use std::rc::Rc;
pub struct Action { pub struct Action {
// Actions // Actions
append: Rc<Append>, append: Rc<Append>,
home: Rc<Home>,
pin: Rc<Pin>, pin: Rc<Pin>,
reload: Rc<Reload>, reload: Rc<Reload>,
// Group // Group
@ -31,6 +34,7 @@ impl Action {
pub fn new() -> Self { pub fn new() -> Self {
// Init actions // Init actions
let append = Rc::new(Append::new()); let append = Rc::new(Append::new());
let home = Rc::new(Home::new());
let pin = Rc::new(Pin::new()); let pin = Rc::new(Pin::new());
let reload = Rc::new(Reload::new()); let reload = Rc::new(Reload::new());
@ -42,12 +46,14 @@ impl Action {
// Add action to given group // Add action to given group
gobject.add_action(append.gobject()); gobject.add_action(append.gobject());
gobject.add_action(home.gobject());
gobject.add_action(pin.gobject()); gobject.add_action(pin.gobject());
gobject.add_action(reload.gobject()); gobject.add_action(reload.gobject());
// Done // Done
Self { Self {
append, append,
home,
pin, pin,
reload, reload,
id, id,
@ -62,6 +68,11 @@ impl Action {
&self.append &self.append
} }
/// Get reference `Home` action
pub fn home(&self) -> &Rc<Home> {
&self.home
}
/// Get reference `Pin` action /// Get reference `Pin` action
pub fn pin(&self) -> &Rc<Pin> { pub fn pin(&self) -> &Rc<Pin> {
&self.pin &self.pin

View File

@ -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<i32>) {
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<i32>) + 'static) {
let state = self.state();
self.gobject.connect_activate(move |_, _| callback(state));
}
// Getters
pub fn state(&self) -> Option<i32> {
let state = self
.gobject
.state()
.expect("State value required")
.get::<i32>()
.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()
}
}

View File

@ -22,7 +22,6 @@ impl Header {
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
action_page_close: SimpleAction, action_page_close: SimpleAction,
action_page_close_all: SimpleAction, action_page_close_all: SimpleAction,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
// Widgets // Widgets
@ -34,7 +33,6 @@ impl Header {
window_action, window_action,
action_page_close, action_page_close,
action_page_close_all, action_page_close_all,
action_page_home,
action_page_history_back, action_page_history_back,
action_page_history_forward, action_page_history_forward,
tab_view, tab_view,

View File

@ -25,7 +25,6 @@ impl Bar {
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
action_page_close: SimpleAction, action_page_close: SimpleAction,
action_page_close_all: SimpleAction, action_page_close_all: SimpleAction,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
view: &TabView, view: &TabView,
@ -38,7 +37,6 @@ impl Bar {
window_action, window_action,
action_page_close, action_page_close,
action_page_close_all, action_page_close_all,
action_page_home,
action_page_history_back, action_page_history_back,
action_page_history_forward, action_page_history_forward,
); );

View File

@ -21,7 +21,6 @@ impl Menu {
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
action_page_close: SimpleAction, action_page_close: SimpleAction,
action_page_close_all: SimpleAction, action_page_close_all: SimpleAction,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
) -> Rc<Self> { ) -> Rc<Self> {
@ -50,7 +49,12 @@ impl Menu {
// Main > Page > Navigation // Main > Page > Navigation
let main_page_navigation = gio::Menu::new(); 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 // Main > Page > Navigation > History
let main_page_navigation_history = gio::Menu::new(); let main_page_navigation_history = gio::Menu::new();

View File

@ -27,7 +27,6 @@ pub struct Tab {
browser_action: Rc<BrowserAction>, browser_action: Rc<BrowserAction>,
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
// Page actions // Page actions
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
// Dynamically allocated reference index // Dynamically allocated reference index
@ -43,7 +42,6 @@ impl Tab {
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
action_page_close: SimpleAction, action_page_close: SimpleAction,
action_page_close_all: SimpleAction, action_page_close_all: SimpleAction,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
) -> Rc<Self> { ) -> Rc<Self> {
@ -60,7 +58,6 @@ impl Tab {
action_page_close.clone(), action_page_close.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
action_page_home.clone(),
); );
// Init widget // Init widget
@ -76,7 +73,6 @@ impl Tab {
let window_action = window_action.clone(); let window_action = window_action.clone();
let action = 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_back = action_page_history_back.clone();
let action_page_history_forward = action_page_history_forward.clone(); let action_page_history_forward = action_page_history_forward.clone();
@ -89,7 +85,6 @@ impl Tab {
window_action.clone(), window_action.clone(),
action.clone(), action.clone(),
// Page actions // Page actions
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
// Options // Options
@ -116,7 +111,6 @@ impl Tab {
let action_page_close = action_page_close.clone(); let action_page_close = action_page_close.clone();
let action_page_history_back = action_page_history_back.clone(); let action_page_history_back = action_page_history_back.clone();
let action_page_history_forward = action_page_history_forward.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(); let window_action = window_action.clone();
move |tab_view, tab_page| { move |tab_view, tab_page| {
// Update actions // Update actions
@ -126,8 +120,10 @@ impl Tab {
// Context menu closed (reset state to defaults) // Context menu closed (reset state to defaults)
None => None, None => None,
}; // @TODO }; // @TODO
window_action.pin().change_state(state_v2); window_action.pin().change_state(state_v2);
window_action.reload().change_state(state_v2); window_action.reload().change_state(state_v2);
window_action.home().change_state(state_v2);
// @TODO old version requires update // @TODO old version requires update
// Setup state for selected page // Setup state for selected page
@ -141,7 +137,6 @@ impl Tab {
action_page_close.change_state(&state); action_page_close.change_state(&state);
action_page_history_back.change_state(&state); action_page_history_back.change_state(&state);
action_page_history_forward.change_state(&state); action_page_history_forward.change_state(&state);
action_page_home.change_state(&state);
} }
}); });
@ -182,7 +177,6 @@ impl Tab {
browser_action, browser_action,
window_action, window_action,
// Global actions // Global actions
action_page_home,
action_page_history_back, action_page_history_back,
action_page_history_forward, action_page_history_forward,
// Init empty HashMap index as no tabs appended yet // Init empty HashMap index as no tabs appended yet
@ -202,7 +196,6 @@ impl Tab {
// Local actions // Local actions
self.action.clone(), self.action.clone(),
// Global actions // Global actions
self.action_page_home.clone(),
self.action_page_history_back.clone(), self.action_page_history_back.clone(),
self.action_page_history_forward.clone(), self.action_page_history_forward.clone(),
// Options // Options
@ -345,7 +338,6 @@ impl Tab {
self.browser_action.clone(), self.browser_action.clone(),
self.window_action.clone(), self.window_action.clone(),
self.action.clone(), self.action.clone(),
self.action_page_home.clone(),
self.action_page_history_back.clone(), self.action_page_history_back.clone(),
self.action_page_history_forward.clone(), self.action_page_history_forward.clone(),
) { ) {

View File

@ -35,7 +35,6 @@ impl Item {
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
tab_action: Rc<TabAction>, tab_action: Rc<TabAction>,
// @TODO // @TODO
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
// Options // Options
@ -53,7 +52,6 @@ impl Item {
browser_action, browser_action,
window_action, window_action,
tab_action, tab_action,
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
); );
@ -135,7 +133,6 @@ impl Item {
browser_action: Rc<BrowserAction>, browser_action: Rc<BrowserAction>,
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
tab_action: Rc<TabAction>, tab_action: Rc<TabAction>,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
) -> Result<Vec<Rc<Item>>, String> { ) -> Result<Vec<Rc<Item>>, String> {
@ -151,7 +148,6 @@ impl Item {
browser_action.clone(), browser_action.clone(),
window_action.clone(), window_action.clone(),
tab_action.clone(), tab_action.clone(),
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
// Options // Options

View File

@ -60,7 +60,6 @@ impl Page {
browser_action: Rc<BrowserAction>, browser_action: Rc<BrowserAction>,
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
tab_action: Rc<TabAction>, tab_action: Rc<TabAction>,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
) -> Rc<Self> { ) -> Rc<Self> {
@ -75,7 +74,6 @@ impl Page {
let navigation = Navigation::new_rc( let navigation = Navigation::new_rc(
browser_action.clone(), browser_action.clone(),
window_action.clone(), window_action.clone(),
action_page_home.clone(),
action_page_history_back.clone(), action_page_history_back.clone(),
action_page_history_forward.clone(), action_page_history_forward.clone(),
action_page_open.clone(), action_page_open.clone(),

View File

@ -33,13 +33,12 @@ impl Navigation {
pub fn new_rc( pub fn new_rc(
browser_action: Rc<BrowserAction>, browser_action: Rc<BrowserAction>,
window_action: Rc<WindowAction>, window_action: Rc<WindowAction>,
action_page_home: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
action_page_open: SimpleAction, action_page_open: SimpleAction,
) -> Rc<Self> { ) -> Rc<Self> {
// Init components // 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 history = History::new_rc(action_page_history_back, action_page_history_forward);
let reload = Reload::new_rc(window_action); let reload = Reload::new_rc(window_action);
let request = Request::new_rc(browser_action, action_page_open.clone()); let request = Request::new_rc(browser_action, action_page_open.clone());

View File

@ -2,26 +2,26 @@ mod widget;
use widget::Widget; use widget::Widget;
use crate::app::browser::window::action::Action as WindowAction;
use gtk::{ use gtk::{
gio::SimpleAction,
glib::{gformat, GString, Uri}, glib::{gformat, GString, Uri},
Button, Button,
}; };
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
pub struct Home { pub struct Home {
action_page_home: SimpleAction, window_action: Rc<WindowAction>,
uri: RefCell<Option<Uri>>, uri: RefCell<Option<Uri>>,
widget: Rc<Widget>, widget: Rc<Widget>,
} }
impl Home { impl Home {
// Construct // Construct
pub fn new_rc(action_page_home: SimpleAction) -> Rc<Self> { pub fn new_rc(window_action: Rc<WindowAction>) -> Rc<Self> {
Rc::new(Self { Rc::new(Self {
action_page_home: action_page_home.clone(), window_action: window_action.clone(),
uri: RefCell::new(None), 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); self.uri.replace(uri);
// Update action status // Update action status
self.action_page_home.set_enabled(status); self.window_action.home().gobject().set_enabled(status);
// Update child components // Update child components
self.widget.update(status); self.widget.update(status);

View File

@ -1,6 +1,6 @@
use crate::app::browser::window::action::Action as WindowAction;
use gtk::{ use gtk::{
gio::SimpleAction, prelude::{ButtonExt, WidgetExt},
prelude::{ActionExt, ButtonExt, WidgetExt},
Button, Button,
}; };
use std::rc::Rc; use std::rc::Rc;
@ -11,7 +11,7 @@ pub struct Widget {
impl Widget { impl Widget {
// Construct // Construct
pub fn new_rc(action_page_home: SimpleAction) -> Rc<Self> { pub fn new_rc(window_action: Rc<WindowAction>) -> Rc<Self> {
// Init gobject // Init gobject
let gobject = Button::builder() let gobject = Button::builder()
.icon_name("go-home-symbolic") .icon_name("go-home-symbolic")
@ -20,12 +20,7 @@ impl Widget {
.build(); .build();
// Init events // Init events
gobject.connect_clicked({ gobject.connect_clicked(move |_| window_action.home().activate());
let action_page_home = action_page_home.clone();
move |_| {
action_page_home.activate(None);
}
});
// Return activated struct // Return activated struct
Rc::new(Self { gobject }) Rc::new(Self { gobject })

View File

@ -20,7 +20,6 @@ impl Menu {
action_page_close: SimpleAction, action_page_close: SimpleAction,
action_page_history_back: SimpleAction, action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction, action_page_history_forward: SimpleAction,
action_page_home: SimpleAction,
) -> Self { ) -> Self {
let main = gtk::gio::Menu::new(); let main = gtk::gio::Menu::new();
@ -44,7 +43,14 @@ impl Menu {
let navigation = gtk::gio::Menu::new(); 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); main.append_section(None, &navigation);