mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
implement stateful api for append action
This commit is contained in:
parent
a6ef61486d
commit
3a0deabc14
5 changed files with 102 additions and 17 deletions
|
|
@ -1,9 +1,14 @@
|
|||
use gtk::{
|
||||
gio::SimpleAction,
|
||||
glib::{uuid_string_random, GString},
|
||||
prelude::ActionExt,
|
||||
prelude::{ActionExt, ToVariant},
|
||||
};
|
||||
|
||||
/// C-compatible variant type
|
||||
const DEFAULT_POSITION: i32 = -1;
|
||||
const DEFAULT_IS_PINNED: bool = false;
|
||||
const DEFAULT_IS_SELECTED: bool = true;
|
||||
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Append` action of `Window` group
|
||||
pub struct Append {
|
||||
gobject: SimpleAction,
|
||||
|
|
@ -12,26 +17,87 @@ pub struct Append {
|
|||
impl Append {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
/// Create new `Self` with default action state preset
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
gobject: SimpleAction::new(&uuid_string_random(), None),
|
||||
gobject: SimpleAction::new_stateful(
|
||||
&uuid_string_random(),
|
||||
None,
|
||||
&(DEFAULT_POSITION, DEFAULT_IS_PINNED, DEFAULT_IS_SELECTED).to_variant(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||
pub fn activate(&self) {
|
||||
/// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal with default state
|
||||
/// * this action reset previous state for action after activation
|
||||
pub fn activate_default_once(&self) {
|
||||
// Save current state in memory
|
||||
let (position, is_pinned, is_selected) = state(&self.gobject);
|
||||
|
||||
// Set default state
|
||||
self.change_state(
|
||||
Some(DEFAULT_POSITION),
|
||||
DEFAULT_IS_PINNED,
|
||||
DEFAULT_IS_SELECTED,
|
||||
);
|
||||
|
||||
// Activate action
|
||||
self.gobject.activate(None);
|
||||
|
||||
// Return previous state
|
||||
self.change_state(position, is_pinned, is_selected);
|
||||
}
|
||||
|
||||
/// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||
/// * this action reset previous state for action after activation
|
||||
pub fn activate_stateful_once(
|
||||
&self,
|
||||
position: Option<i32>,
|
||||
is_pinned: bool,
|
||||
is_selected: bool,
|
||||
) {
|
||||
// Save current state in memory
|
||||
let (_position, _is_pinned, _is_selected) = state(&self.gobject);
|
||||
|
||||
// Apply requested state
|
||||
self.change_state(position, is_pinned, is_selected);
|
||||
|
||||
// Activate action
|
||||
self.gobject.activate(None);
|
||||
|
||||
// Return previous state
|
||||
self.change_state(_position, _is_pinned, _is_selected);
|
||||
}
|
||||
|
||||
/// Emit state change for action
|
||||
pub fn change_state(&self, position: Option<i32>, is_pinned: bool, is_selected: bool) {
|
||||
self.gobject.change_state(
|
||||
&(
|
||||
// Convert Option to C-based variant value
|
||||
if let Some(position) = position {
|
||||
position
|
||||
} else {
|
||||
DEFAULT_POSITION
|
||||
},
|
||||
is_pinned,
|
||||
is_selected,
|
||||
)
|
||||
.to_variant(),
|
||||
);
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
/// Define callback function for
|
||||
/// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||
pub fn connect_activate(&self, callback: impl Fn() + 'static) {
|
||||
self.gobject.connect_activate(move |_, _| callback());
|
||||
/// * return `position`, `is_pinned`, `is_selected` state as tuple
|
||||
pub fn connect_activate(&self, callback: impl Fn(Option<i32>, bool, bool) + 'static) {
|
||||
self.gobject.connect_activate(move |this, _| {
|
||||
let (position, is_pinned, is_selected) = state(this);
|
||||
callback(position, is_pinned, is_selected)
|
||||
});
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
|
@ -46,3 +112,22 @@ impl Append {
|
|||
self.gobject.name()
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared helper to get C-based action state in Optional format
|
||||
pub fn state(this: &SimpleAction) -> (Option<i32>, bool, bool) {
|
||||
let (position, is_pinned, is_selected) = this
|
||||
.state()
|
||||
.expect("Expected (`position`,`is_pinned`,`is_selected`) state")
|
||||
.get::<(i32, bool, bool)>()
|
||||
.expect("Parameter type does not match (`i32`,`bool`,`bool`) tuple");
|
||||
(
|
||||
// Convert from C-based variant value to Option
|
||||
if position == DEFAULT_POSITION {
|
||||
None
|
||||
} else {
|
||||
Some(position)
|
||||
},
|
||||
is_pinned,
|
||||
is_selected,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue