begin global actions config implementation

This commit is contained in:
yggverse 2024-11-09 14:59:58 +02:00
parent f803d89f52
commit 9e481f78ce
9 changed files with 58 additions and 68 deletions

17
src/action.rs Normal file
View File

@ -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, &["<Primary>i"]);
// group | action | variant | accels
pub const APP_BROWSER_WIDGET_DEBUG: (&str, &str, Option<&VariantTy>, &[&str]) =
(APP_BROWSER_WIDGET, "debug", None, &["<Primary>q"]);

View File

@ -174,12 +174,17 @@ impl App {
}); });
// Init accels // Init accels
let accels_config = &[ for (detailed_action_name, accels) in &[
// Browser actions // Browser actions
( {
gformat!("win.{}", browser.action().debug().name()), let (group, action, _, accels) = crate::action::APP_BROWSER_WIDGET_DEBUG;
&["<Primary>i"], (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()), gformat!("win.{}", browser.action().quit().name()),
&["<Primary>Escape"], &["<Primary>Escape"],
@ -193,10 +198,6 @@ impl App {
gformat!("win.{}", action_page_reload.name()), gformat!("win.{}", action_page_reload.name()),
&["<Primary>r"], &["<Primary>r"],
), ),
(
gformat!("win.{}", action_page_close.name()),
&["<Primary>q"],
),
( (
gformat!("win.{}", action_page_history_back.name()), gformat!("win.{}", action_page_history_back.name()),
&["<Primary>Left"], &["<Primary>Left"],
@ -208,9 +209,7 @@ impl App {
(gformat!("win.{}", action_page_home.name()), &["<Primary>h"]), (gformat!("win.{}", action_page_home.name()), &["<Primary>h"]),
(gformat!("win.{}", action_page_new.name()), &["<Primary>t"]), (gformat!("win.{}", action_page_new.name()), &["<Primary>t"]),
(gformat!("win.{}", action_page_pin.name()), &["<Primary>p"]), (gformat!("win.{}", action_page_pin.name()), &["<Primary>p"]),
]; // @TODO config ] {
for (detailed_action_name, &accels) in accels_config {
gobject.set_accels_for_action(detailed_action_name, &accels); gobject.set_accels_for_action(detailed_action_name, &accels);
} }

View File

@ -15,7 +15,7 @@ use adw::ApplicationWindow;
use gtk::{ use gtk::{
gio::{Cancellable, File, SimpleAction}, gio::{Cancellable, File, SimpleAction},
glib::Variant, glib::Variant,
prelude::{ActionExt, GtkWindowExt}, prelude::ActionExt,
FileLauncher, FileLauncher,
}; };
use sqlite::Transaction; 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({ action.profile().connect_activate({
move |_, _| { move |_, _| {
FileLauncher::new(Some(&File::for_path(profile.config_path()))).launch( 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({ action.update().connect_activate({
let window = window.clone(); let window = window.clone();
move |_, this| window.update(string_from_variant(this).as_str()) move |_, this| window.update(string_from_variant(this).as_str())

View File

@ -1,5 +1,7 @@
mod action;
mod database; mod database;
use action::Action;
use database::Database; use database::Database;
use adw::ApplicationWindow; use adw::ApplicationWindow;
@ -29,6 +31,8 @@ impl Widget {
.maximized(MAXIMIZED) .maximized(MAXIMIZED)
.build(); .build();
Action::new_for(&gobject);
// Register actions // Register actions
for action in actions { for action in actions {
gobject.add_action(action); gobject.add_action(action);

View File

@ -6,7 +6,6 @@ use debug::Debug;
use gtk::{ use gtk::{
gio::SimpleActionGroup, gio::SimpleActionGroup,
glib::{gformat, uuid_string_random, GString},
prelude::{IsA, WidgetExt}, prelude::{IsA, WidgetExt},
Window, Window,
}; };
@ -17,7 +16,6 @@ pub struct Action {
debug: Debug, debug: Debug,
// Group // Group
gobject: SimpleActionGroup, gobject: SimpleActionGroup,
name: GString,
} }
impl Action { 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 /// * 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<Window> + WidgetExt)) -> Self { pub fn new_for(window: &(impl IsA<Window> + WidgetExt)) -> Self {
// Init group // Init group
let name = uuid_string_random();
let gobject = SimpleActionGroup::new(); let gobject = SimpleActionGroup::new();
// Add group to window // Add group to window
window.insert_action_group(&name, Some(&gobject)); window.insert_action_group(crate::action::APP_BROWSER_WIDGET, Some(&gobject));
// Init actions // Init actions
let close = Close::new_for(&gobject, window.clone()); let close = Close::new_for(&gobject, window.clone());
@ -45,23 +42,6 @@ impl Action {
close, close,
debug, debug,
gobject, 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)
}
} }

View File

@ -1,7 +1,6 @@
use gtk::{ use gtk::{
gio::{SimpleAction, SimpleActionGroup}, gio::{SimpleAction, SimpleActionGroup},
glib::{uuid_string_random, GString}, prelude::{ActionMapExt, GtkWindowExt, IsA},
prelude::{ActionExt, ActionMapExt, GtkWindowExt, IsA},
Window, Window,
}; };
@ -17,8 +16,12 @@ impl Close {
/// and [Window](https://docs.gtk.org/gtk4/class.Window.html) /// and [Window](https://docs.gtk.org/gtk4/class.Window.html)
/// * this constructor **activate** default feature /// * this constructor **activate** default feature
pub fn new_for(group: &SimpleActionGroup, window: impl IsA<Window>) -> Self { pub fn new_for(group: &SimpleActionGroup, window: impl IsA<Window>) -> Self {
// Get action config
let (_group_name, action_name, parameter_type, _accels) =
crate::action::APP_BROWSER_WIDGET_CLOSE;
// Init action GObject // Init action GObject
let gobject = SimpleAction::new(&uuid_string_random(), None); let gobject = SimpleAction::new(&action_name, parameter_type);
// Add action to given group // Add action to given group
group.add_action(&gobject); group.add_action(&gobject);
@ -31,10 +34,4 @@ impl Close {
// Done // Done
Self { gobject } Self { gobject }
} }
// Getters
pub fn name(&self) -> GString {
self.gobject.name()
}
} }

View File

@ -1,7 +1,6 @@
use gtk::{ use gtk::{
gio::{SimpleAction, SimpleActionGroup}, gio::{SimpleAction, SimpleActionGroup},
glib::{uuid_string_random, GString}, prelude::{ActionMapExt, GtkWindowExt, IsA},
prelude::{ActionExt, ActionMapExt, GtkWindowExt, IsA},
Window, Window,
}; };
@ -17,8 +16,12 @@ impl Debug {
/// and [Window](https://docs.gtk.org/gtk4/class.Window.html) /// and [Window](https://docs.gtk.org/gtk4/class.Window.html)
/// * this constructor **activate** default feature /// * this constructor **activate** default feature
pub fn new_for(group: &SimpleActionGroup, window: impl IsA<Window>) -> Self { pub fn new_for(group: &SimpleActionGroup, window: impl IsA<Window>) -> Self {
// Get action config
let (_group_name, action_name, parameter_type, _accels) =
crate::action::APP_BROWSER_WIDGET_DEBUG;
// Init action GObject // Init action GObject
let gobject = SimpleAction::new(&uuid_string_random(), None); let gobject = SimpleAction::new(&action_name, parameter_type);
// Add action to given group // Add action to given group
group.add_action(&gobject); group.add_action(&gobject);
@ -31,10 +34,4 @@ impl Debug {
// Done // Done
Self { gobject } Self { gobject }
} }
// Getters
pub fn name(&self) -> GString {
self.gobject.name()
}
} }

View File

@ -60,13 +60,22 @@ impl Menu {
// Main > Tool // Main > Tool
let main_tool = gio::Menu::new(); 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("Profile"), Some(&detailed_action_name(browser_action.profile())));
main_tool.append(Some("About"), Some(&detailed_action_name(browser_action.about()))); main_tool.append(Some("About"), Some(&detailed_action_name(browser_action.about())));
main.append_submenu(Some("Tool"), &main_tool); 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 // Result
Rc::new(Self { widget:Widget::new_rc(&main) }) Rc::new(Self { widget:Widget::new_rc(&main) })

View File

@ -1,3 +1,4 @@
mod action;
mod app; mod app;
mod profile; mod profile;