implement is_attention tab state decorator

This commit is contained in:
yggverse 2024-11-11 14:47:51 +02:00
parent f6d015c8c1
commit 8f9f5d3eaa
5 changed files with 87 additions and 22 deletions

View file

@ -9,6 +9,7 @@ const DEFAULT_POSITION: i32 = -1;
const DEFAULT_REQUEST: String = String::new();
const DEFAULT_IS_PINNED: bool = false;
const DEFAULT_IS_SELECTED: bool = true;
const DEFAULT_IS_ATTENTION: bool = false;
const DEFAULT_IS_LOAD: bool = false;
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Append` action of `Window` group
@ -30,6 +31,7 @@ impl Append {
DEFAULT_REQUEST,
DEFAULT_IS_PINNED,
DEFAULT_IS_SELECTED,
DEFAULT_IS_ATTENTION,
DEFAULT_IS_LOAD,
)
.to_variant(),
@ -43,7 +45,8 @@ impl Append {
/// * this action reset previous state for action after activation
pub fn activate_default_once(&self) {
// Save current state in memory
let (position, request, is_pinned, is_selected, is_load) = state(&self.gobject);
let (position, request, is_pinned, is_selected, is_attention, is_load) =
state(&self.gobject);
// Set default state
self.change_state(
@ -51,6 +54,7 @@ impl Append {
Some(DEFAULT_REQUEST),
DEFAULT_IS_PINNED,
DEFAULT_IS_SELECTED,
DEFAULT_IS_ATTENTION,
DEFAULT_IS_LOAD,
);
@ -58,7 +62,14 @@ impl Append {
self.gobject.activate(None);
// Return previous state
self.change_state(position, request, is_pinned, is_selected, is_load);
self.change_state(
position,
request,
is_pinned,
is_selected,
is_attention,
is_load,
);
}
/// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
@ -69,19 +80,35 @@ impl Append {
request: Option<String>,
is_pinned: bool,
is_selected: bool,
is_attention: bool,
is_load: bool,
) {
// Save current state in memory
let (_position, _request, _is_pinned, _is_selected, _is_load) = state(&self.gobject);
let (_position, _request, _is_pinned, _is_selected, _is_attention, _is_load) =
state(&self.gobject);
// Apply requested state
self.change_state(position, request, is_pinned, is_selected, is_load);
self.change_state(
position,
request,
is_pinned,
is_selected,
is_attention,
is_load,
);
// Activate action
self.gobject.activate(None);
// Return previous state
self.change_state(_position, _request, _is_pinned, _is_selected, _is_load);
self.change_state(
_position,
_request,
_is_pinned,
_is_selected,
_is_attention,
_is_load,
);
}
/// Emit state change for action
@ -91,6 +118,7 @@ impl Append {
request: Option<String>,
is_pinned: bool,
is_selected: bool,
is_attention: bool,
is_load: bool,
) {
self.gobject.change_state(
@ -108,6 +136,7 @@ impl Append {
},
is_pinned,
is_selected,
is_attention,
is_load,
)
.to_variant(),
@ -118,14 +147,21 @@ impl Append {
/// Define callback function for
/// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
/// * return `position`,`request`,`is_pinned`,`is_selected`,`is_load` state as tuple
/// * return `position`,`request`,`is_pinned`,`is_selected`,`is_attention`,`is_load` state as tuple
pub fn connect_activate(
&self,
callback: impl Fn(Option<i32>, Option<String>, bool, bool, bool) + 'static,
callback: impl Fn(Option<i32>, Option<String>, bool, bool, bool, bool) + 'static,
) {
self.gobject.connect_activate(move |this, _| {
let (position, request, is_pinned, is_selected, is_load) = state(this);
callback(position, request, is_pinned, is_selected, is_load)
let (position, request, is_pinned, is_selected, is_attention, is_load) = state(this);
callback(
position,
request,
is_pinned,
is_selected,
is_attention,
is_load,
)
});
}
@ -143,12 +179,12 @@ impl Append {
}
/// Shared helper to get C-based action state in Optional format
pub fn state(this: &SimpleAction) -> (Option<i32>, Option<String>, bool, bool, bool) {
let (position, request, is_pinned, is_selected, is_load) = this
pub fn state(this: &SimpleAction) -> (Option<i32>, Option<String>, bool, bool, bool, bool) {
let (position, request, is_pinned, is_selected, is_attention, is_load) = this
.state()
.expect("Expected (`position`,`request`,`is_pinned`,`is_selected`,`is_load`) state")
.get::<(i32, String, bool, bool, bool)>()
.expect("Parameter type does not match (`i32`,`String`,`bool`,`bool`,`bool`) tuple");
.expect("Expected (`position`,`request`,`is_pinned`,`is_selected`,`is_attention`,`is_load`) state")
.get::<(i32, String, bool, bool, bool,bool)>()
.expect("Parameter type does not match (`i32`,`String`,`bool`,`bool`,`bool`,`bool`) tuple");
(
// Convert from C-based variant value to Option
if position == DEFAULT_POSITION {
@ -163,6 +199,7 @@ pub fn state(this: &SimpleAction) -> (Option<i32>, Option<String>, bool, bool, b
},
is_pinned,
is_selected,
is_attention,
is_load,
)
}