Yoda/src/app/action.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

2024-10-02 02:14:00 +03:00
// 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 std::sync::Arc;
use gtk::{
gio::SimpleAction,
2024-10-15 08:20:43 +03:00
glib::{gformat, uuid_string_random, GString, VariantTy},
2024-10-02 02:14:00 +03:00
prelude::ActionExt,
};
pub struct Action {
group: GString,
simple: Arc<SimpleAction>,
}
impl Action {
// Construct
2024-10-15 08:20:43 +03:00
pub fn new(group: &str, is_enabled: bool, parameter_type: Option<&VariantTy>) -> Self {
2024-10-02 02:14:00 +03:00
// Create random action name as no static values should be in use
2024-10-15 08:20:43 +03:00
let simple = Arc::new(SimpleAction::new(&uuid_string_random(), parameter_type));
2024-10-02 02:14:00 +03:00
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) -> Arc<SimpleAction> {
self.simple.clone()
}
}