diff --git a/src/app.rs b/src/app.rs index 257c95a0..7aadd721 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,14 +1,13 @@ -mod action; mod browser; mod database; -use action::Action; use browser::Browser; use database::Database; use adw::Application; use gtk::{ - glib::ExitCode, + gio::SimpleAction, + glib::{gformat, uuid_string_random, ExitCode}, prelude::{ ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt, StaticVariantType, ToVariant, @@ -44,19 +43,26 @@ impl App { let default_state = (-1).to_variant(); // Init actions - let action_about = Action::new("win", true, None); - let action_debug = Action::new("win", true, None); - let action_profile = Action::new("win", true, None); - let action_quit = Action::new("win", true, None); - let action_update = Action::new("win", true, Some(&String::static_variant_type())); - let action_page_new = Action::new("win", true, None); - let action_page_close = Action::new_stateful("win", true, None, &default_state); - let action_page_close_all = Action::new("win", true, None); - let action_page_home = Action::new_stateful("win", false, None, &default_state); - let action_page_history_back = Action::new_stateful("win", false, None, &default_state); - let action_page_history_forward = Action::new_stateful("win", false, None, &default_state); - let action_page_reload = Action::new_stateful("win", true, None, &default_state); - let action_page_pin = Action::new_stateful("win", true, None, &default_state); + let action_about = SimpleAction::new(&uuid_string_random(), None); + let action_debug = SimpleAction::new(&uuid_string_random(), None); + let action_profile = SimpleAction::new(&uuid_string_random(), None); + let action_quit = SimpleAction::new(&uuid_string_random(), None); + let action_update = + SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type())); + let action_page_new = SimpleAction::new(&uuid_string_random(), None); + 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 = + SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); + let action_page_reload = + SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); + let action_page_pin = + SimpleAction::new_stateful(&uuid_string_random(), None, &default_state); // Init GTK let gobject = Application::builder() @@ -65,19 +71,28 @@ impl App { // Init accels let accels_config = &[ - (action_page_reload.detailed_name(), &["r"]), - (action_debug.detailed_name(), &["i"]), - (action_page_close.detailed_name(), &["q"]), - (action_page_history_back.detailed_name(), &["Left"]), ( - action_page_history_forward.detailed_name(), + gformat!("win.{}", action_page_reload.name()), + &["r"], + ), + (gformat!("win.{}", action_debug.name()), &["i"]), + ( + gformat!("win.{}", action_page_close.name()), + &["q"], + ), + ( + gformat!("win.{}", action_page_history_back.name()), + &["Left"], + ), + ( + gformat!("win.{}", action_page_history_forward.name()), &["Right"], ), - (action_page_home.detailed_name(), &["h"]), - (action_page_new.detailed_name(), &["t"]), - (action_page_pin.detailed_name(), &["p"]), - (action_quit.detailed_name(), &["Escape"]), - (action_update.detailed_name(), &["u"]), + (gformat!("win.{}", action_page_home.name()), &["h"]), + (gformat!("win.{}", action_page_new.name()), &["t"]), + (gformat!("win.{}", action_page_pin.name()), &["p"]), + (gformat!("win.{}", action_quit.name()), &["Escape"]), + (gformat!("win.{}", action_update.name()), &["u"]), ]; // @TODO config for (detailed_action_name, &accels) in accels_config { @@ -87,24 +102,24 @@ impl App { // Init components let browser = Arc::new(Browser::new( profile_path, - action_about.simple(), - action_debug.simple(), - action_profile.simple(), - action_quit.simple(), - action_update.simple(), - action_page_new.simple(), - action_page_close.simple(), - action_page_close_all.simple(), - action_page_home.simple(), - action_page_history_back.simple(), - action_page_history_forward.simple(), - action_page_reload.simple(), - action_page_pin.simple(), + action_about.clone(), + action_debug.clone(), + action_profile.clone(), + action_quit.clone(), + action_update.clone(), + action_page_new.clone(), + action_page_close.clone(), + action_page_close_all.clone(), + action_page_home.clone(), + action_page_history_back.clone(), + action_page_history_forward.clone(), + action_page_reload.clone(), + action_page_pin.clone(), )); // Init events gobject.connect_activate({ - let action_update = action_update.simple(); + let action_update = action_update.clone(); move |_| { // Make initial update action_update.activate(Some(&"".to_variant())); // @TODO diff --git a/src/app/action.rs b/src/app/action.rs deleted file mode 100644 index 2eca5e1b..00000000 --- a/src/app/action.rs +++ /dev/null @@ -1,60 +0,0 @@ -// This helper created as the attempt to drop static names usage -// and replace them with objects (to follow encapsulation for children mods) -// @TODO find alternative implementation, if exist for GTK 4 - -use gtk::{ - gio::SimpleAction, - glib::{gformat, uuid_string_random, GString, Variant, VariantTy}, - prelude::ActionExt, -}; - -pub struct Action { - group: GString, - simple: SimpleAction, -} - -impl Action { - // Construct - pub fn new(group: &str, is_enabled: bool, parameter_type: Option<&VariantTy>) -> Self { - // Create random action name as no static values should be in use - let simple = SimpleAction::new(&uuid_string_random(), parameter_type); - simple.set_enabled(is_enabled); - - // Assign action to the group - let group = GString::from(group); - - // Return new 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, - parameter_type: Option<&VariantTy>, - state: &Variant, - ) -> Self { - // Create random action name as no static values should be in use - let simple = SimpleAction::new_stateful(&uuid_string_random(), parameter_type, state); - simple.set_enabled(is_enabled); - - // Assign action to the group - let group = GString::from(group); - - // Return new Action - Self { group, simple } - } - - // Getters - pub fn detailed_name(&self) -> GString { - gformat!("{}.{}", self.group, self.simple.name()) // @TODO find the way to ident parent group - // from SimpleAction object - } - - // App mods work with simple and system-wide data types, let them take it - pub fn simple(&self) -> SimpleAction { - self.simple.clone() - } -}