draft widget actions

This commit is contained in:
yggverse 2024-11-09 09:41:59 +02:00
parent 5b37d303b9
commit f803d89f52
3 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,67 @@
mod close;
mod debug;
use close::Close;
use debug::Debug;
use gtk::{
gio::SimpleActionGroup,
glib::{gformat, uuid_string_random, GString},
prelude::{IsA, WidgetExt},
Window,
};
pub struct Action {
// Actions
close: Close,
debug: Debug,
// Group
gobject: SimpleActionGroup,
name: GString,
}
impl Action {
// Constructors
/// Create **activated** [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) set
/// with new [SimpleActionGroup](https://docs.gtk.org/gio/class.SimpleActionGroup.html)
/// for given [Window](https://docs.gtk.org/gtk4/class.Window.html)
/// * useful for object-oriented work with GTK `detailed_name`, e.g. on GTK [Menu](https://docs.gtk.org/gio/class.Menu.html) build
/// * this implementation also encapsulates `GObject` to prevent unexpected assignments
/// * 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 {
// Init group
let name = uuid_string_random();
let gobject = SimpleActionGroup::new();
// Add group to window
window.insert_action_group(&name, Some(&gobject));
// Init actions
let close = Close::new_for(&gobject, window.clone());
let debug = Debug::new_for(&gobject, window.clone());
Self {
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)
}
}

View File

@ -0,0 +1,40 @@
use gtk::{
gio::{SimpleAction, SimpleActionGroup},
glib::{uuid_string_random, GString},
prelude::{ActionExt, ActionMapExt, GtkWindowExt, IsA},
Window,
};
pub struct Close {
gobject: SimpleAction,
}
impl Close {
// Constructors
/// Create new [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html)
/// for given [SimpleActionGroup](https://docs.gtk.org/gio/class.SimpleActionGroup.html)
/// and [Window](https://docs.gtk.org/gtk4/class.Window.html)
/// * this constructor **activate** default feature
pub fn new_for(group: &SimpleActionGroup, window: impl IsA<Window>) -> Self {
// Init action GObject
let gobject = SimpleAction::new(&uuid_string_random(), None);
// Add action to given group
group.add_action(&gobject);
// Connect default feature on activate
gobject.connect_activate(move |_, _| {
window.close();
});
// Done
Self { gobject }
}
// Getters
pub fn name(&self) -> GString {
self.gobject.name()
}
}

View File

@ -0,0 +1,40 @@
use gtk::{
gio::{SimpleAction, SimpleActionGroup},
glib::{uuid_string_random, GString},
prelude::{ActionExt, ActionMapExt, GtkWindowExt, IsA},
Window,
};
pub struct Debug {
gobject: SimpleAction,
}
impl Debug {
// Constructors
/// Create new [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html)
/// for given [SimpleActionGroup](https://docs.gtk.org/gio/class.SimpleActionGroup.html)
/// and [Window](https://docs.gtk.org/gtk4/class.Window.html)
/// * this constructor **activate** default feature
pub fn new_for(group: &SimpleActionGroup, window: impl IsA<Window>) -> Self {
// Init action GObject
let gobject = SimpleAction::new(&uuid_string_random(), None);
// Add action to given group
group.add_action(&gobject);
// Connect default feature on activate
gobject.connect_activate(move |_, _| {
window.emit_enable_debugging(true);
});
// Done
Self { gobject }
}
// Getters
pub fn name(&self) -> GString {
self.gobject.name()
}
}