From 9e481f78ce157b9666ebde50ae794cfc34554f38 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 9 Nov 2024 14:59:58 +0200 Subject: [PATCH] begin global actions config implementation --- src/action.rs | 17 +++++++++++++++++ src/app.rs | 23 +++++++++++------------ src/app/browser.rs | 16 +--------------- src/app/browser/widget.rs | 4 ++++ src/app/browser/widget/action.rs | 22 +--------------------- src/app/browser/widget/action/close.rs | 15 ++++++--------- src/app/browser/widget/action/debug.rs | 15 ++++++--------- src/app/browser/window/header/bar/menu.rs | 13 +++++++++++-- src/main.rs | 1 + 9 files changed, 58 insertions(+), 68 deletions(-) create mode 100644 src/action.rs diff --git a/src/action.rs b/src/action.rs new file mode 100644 index 00000000..ef01cff2 --- /dev/null +++ b/src/action.rs @@ -0,0 +1,17 @@ +//! Global actions config + +use gtk::glib::VariantTy; + +// app/browser/widget.rs +pub const APP_BROWSER_WIDGET: &str = "app_browser_widget"; + +// group | action | variant +pub const APP_BROWSER_WIDGET_ABOUT: (&str, &str, Option<&VariantTy>) = + (APP_BROWSER_WIDGET, "about", None); + +pub const APP_BROWSER_WIDGET_CLOSE: (&str, &str, Option<&VariantTy>, &[&str]) = + (APP_BROWSER_WIDGET, "close", None, &["i"]); + +// group | action | variant | accels +pub const APP_BROWSER_WIDGET_DEBUG: (&str, &str, Option<&VariantTy>, &[&str]) = + (APP_BROWSER_WIDGET, "debug", None, &["q"]); diff --git a/src/app.rs b/src/app.rs index c0b9da42..15dedde9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -174,12 +174,17 @@ impl App { }); // Init accels - let accels_config = &[ + for (detailed_action_name, accels) in &[ // Browser actions - ( - gformat!("win.{}", browser.action().debug().name()), - &["i"], - ), + { + let (group, action, _, accels) = crate::action::APP_BROWSER_WIDGET_DEBUG; + (gformat!("{group}.{action}"), accels) + }, + { + let (group, action, _, accels) = crate::action::APP_BROWSER_WIDGET_CLOSE; + (gformat!("{group}.{action}"), accels) + }, + // @TODO ( gformat!("win.{}", browser.action().quit().name()), &["Escape"], @@ -193,10 +198,6 @@ impl App { gformat!("win.{}", action_page_reload.name()), &["r"], ), - ( - gformat!("win.{}", action_page_close.name()), - &["q"], - ), ( gformat!("win.{}", action_page_history_back.name()), &["Left"], @@ -208,9 +209,7 @@ impl App { (gformat!("win.{}", action_page_home.name()), &["h"]), (gformat!("win.{}", action_page_new.name()), &["t"]), (gformat!("win.{}", action_page_pin.name()), &["p"]), - ]; // @TODO config - - for (detailed_action_name, &accels) in accels_config { + ] { gobject.set_accels_for_action(detailed_action_name, &accels); } diff --git a/src/app/browser.rs b/src/app/browser.rs index 393dc773..35ab6892 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -15,7 +15,7 @@ use adw::ApplicationWindow; use gtk::{ gio::{Cancellable, File, SimpleAction}, glib::Variant, - prelude::{ActionExt, GtkWindowExt}, + prelude::ActionExt, FileLauncher, }; use sqlite::Transaction; @@ -86,13 +86,6 @@ impl Browser { } }); - action.debug().connect_activate({ - let widget = widget.clone(); - move |_, _| { - widget.gobject().emit_enable_debugging(true); - } - }); - action.profile().connect_activate({ move |_, _| { FileLauncher::new(Some(&File::for_path(profile.config_path()))).launch( @@ -107,13 +100,6 @@ impl Browser { } }); - action.quit().connect_activate({ - let widget = widget.clone(); - move |_, _| { - widget.gobject().close(); - } - }); - action.update().connect_activate({ let window = window.clone(); move |_, this| window.update(string_from_variant(this).as_str()) diff --git a/src/app/browser/widget.rs b/src/app/browser/widget.rs index d8cfea9e..32c34572 100644 --- a/src/app/browser/widget.rs +++ b/src/app/browser/widget.rs @@ -1,5 +1,7 @@ +mod action; mod database; +use action::Action; use database::Database; use adw::ApplicationWindow; @@ -29,6 +31,8 @@ impl Widget { .maximized(MAXIMIZED) .build(); + Action::new_for(&gobject); + // Register actions for action in actions { gobject.add_action(action); diff --git a/src/app/browser/widget/action.rs b/src/app/browser/widget/action.rs index 4c15a704..1cfead02 100644 --- a/src/app/browser/widget/action.rs +++ b/src/app/browser/widget/action.rs @@ -6,7 +6,6 @@ use debug::Debug; use gtk::{ gio::SimpleActionGroup, - glib::{gformat, uuid_string_random, GString}, prelude::{IsA, WidgetExt}, Window, }; @@ -17,7 +16,6 @@ pub struct Action { debug: Debug, // Group gobject: SimpleActionGroup, - name: GString, } impl Action { @@ -31,11 +29,10 @@ impl Action { /// * children actions implemented as wrapper also, that extend default [Variant](https://docs.gtk.org/glib/struct.Variant.html) features, etc pub fn new_for(window: &(impl IsA + WidgetExt)) -> Self { // Init group - let name = uuid_string_random(); let gobject = SimpleActionGroup::new(); // Add group to window - window.insert_action_group(&name, Some(&gobject)); + window.insert_action_group(crate::action::APP_BROWSER_WIDGET, Some(&gobject)); // Init actions let close = Close::new_for(&gobject, window.clone()); @@ -45,23 +42,6 @@ impl Action { close, debug, gobject, - name, } } - - // Getters - - pub fn debug(&self) -> GString { - self.detailed_name(self.debug.name()) - } - - pub fn close(&self) -> GString { - self.detailed_name(self.close.name()) - } - - // Helpers - - fn detailed_name(&self, action_name: GString) -> GString { - gformat!("{}.{}", self.name, action_name) - } } diff --git a/src/app/browser/widget/action/close.rs b/src/app/browser/widget/action/close.rs index 131c736d..6ea9a297 100644 --- a/src/app/browser/widget/action/close.rs +++ b/src/app/browser/widget/action/close.rs @@ -1,7 +1,6 @@ use gtk::{ gio::{SimpleAction, SimpleActionGroup}, - glib::{uuid_string_random, GString}, - prelude::{ActionExt, ActionMapExt, GtkWindowExt, IsA}, + prelude::{ActionMapExt, GtkWindowExt, IsA}, Window, }; @@ -17,8 +16,12 @@ impl Close { /// and [Window](https://docs.gtk.org/gtk4/class.Window.html) /// * this constructor **activate** default feature pub fn new_for(group: &SimpleActionGroup, window: impl IsA) -> Self { + // Get action config + let (_group_name, action_name, parameter_type, _accels) = + crate::action::APP_BROWSER_WIDGET_CLOSE; + // Init action GObject - let gobject = SimpleAction::new(&uuid_string_random(), None); + let gobject = SimpleAction::new(&action_name, parameter_type); // Add action to given group group.add_action(&gobject); @@ -31,10 +34,4 @@ impl Close { // Done Self { gobject } } - - // Getters - - pub fn name(&self) -> GString { - self.gobject.name() - } } diff --git a/src/app/browser/widget/action/debug.rs b/src/app/browser/widget/action/debug.rs index 147592fd..afe8bad3 100644 --- a/src/app/browser/widget/action/debug.rs +++ b/src/app/browser/widget/action/debug.rs @@ -1,7 +1,6 @@ use gtk::{ gio::{SimpleAction, SimpleActionGroup}, - glib::{uuid_string_random, GString}, - prelude::{ActionExt, ActionMapExt, GtkWindowExt, IsA}, + prelude::{ActionMapExt, GtkWindowExt, IsA}, Window, }; @@ -17,8 +16,12 @@ impl Debug { /// and [Window](https://docs.gtk.org/gtk4/class.Window.html) /// * this constructor **activate** default feature pub fn new_for(group: &SimpleActionGroup, window: impl IsA) -> Self { + // Get action config + let (_group_name, action_name, parameter_type, _accels) = + crate::action::APP_BROWSER_WIDGET_DEBUG; + // Init action GObject - let gobject = SimpleAction::new(&uuid_string_random(), None); + let gobject = SimpleAction::new(&action_name, parameter_type); // Add action to given group group.add_action(&gobject); @@ -31,10 +34,4 @@ impl Debug { // Done Self { gobject } } - - // Getters - - pub fn name(&self) -> GString { - self.gobject.name() - } } diff --git a/src/app/browser/window/header/bar/menu.rs b/src/app/browser/window/header/bar/menu.rs index b3c7f9fd..59cee476 100644 --- a/src/app/browser/window/header/bar/menu.rs +++ b/src/app/browser/window/header/bar/menu.rs @@ -60,13 +60,22 @@ impl Menu { // Main > Tool let main_tool = gio::Menu::new(); - main_tool.append(Some("Debug"), Some(&detailed_action_name(browser_action.debug()))); + + { // Debug + let (group, action, _, _) = crate::action::APP_BROWSER_WIDGET_DEBUG; + main_tool.append(Some("Debug"), Some(&gformat!("{group}.{action}"))); + } + main_tool.append(Some("Profile"), Some(&detailed_action_name(browser_action.profile()))); main_tool.append(Some("About"), Some(&detailed_action_name(browser_action.about()))); main.append_submenu(Some("Tool"), &main_tool); - main.append(Some("Quit"), Some(&detailed_action_name(browser_action.quit()))); + { + // Quit + let (group, action, _, _) = crate::action::APP_BROWSER_WIDGET_CLOSE; + main.append(Some("Quit"), Some(&gformat!("{group}.{action}"))); + } // Result Rc::new(Self { widget:Widget::new_rc(&main) }) diff --git a/src/main.rs b/src/main.rs index a03c3a7c..d8267714 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod action; mod app; mod profile;