mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 17:15:28 +00:00
begin request entry refactory
This commit is contained in:
parent
5255708be3
commit
e7bd5bbdc6
24 changed files with 351 additions and 849 deletions
|
|
@ -1,85 +1,12 @@
|
|||
use gtk::{
|
||||
gio::SimpleAction,
|
||||
glib::{uuid_string_random, GString},
|
||||
prelude::{ActionExt, ToVariant},
|
||||
};
|
||||
use gtk::{gio::SimpleAction, glib::uuid_string_random};
|
||||
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Escape` action of `Browser` group
|
||||
pub struct Escape {
|
||||
pub simple_action: SimpleAction,
|
||||
pub trait Escape {
|
||||
fn escape() -> Self;
|
||||
}
|
||||
|
||||
impl Default for Escape {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Escape {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
simple_action: SimpleAction::new_stateful(
|
||||
&uuid_string_random(),
|
||||
None,
|
||||
&String::new().to_variant(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// 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, tab_item_id: Option<GString>) {
|
||||
// Save current state in memory
|
||||
let _tab_item_id = state(&self.simple_action);
|
||||
|
||||
// Apply requested state
|
||||
self.change_state(tab_item_id);
|
||||
|
||||
// Activate action
|
||||
self.simple_action.activate(None);
|
||||
|
||||
// Return previous state
|
||||
self.change_state(_tab_item_id);
|
||||
}
|
||||
|
||||
/// Change action [state](https://docs.gtk.org/gio/method.SimpleAction.set_state.html)
|
||||
/// * set default state on `None`
|
||||
pub fn change_state(&self, state: Option<GString>) {
|
||||
self.simple_action.change_state(
|
||||
&match state {
|
||||
Some(value) => value.to_string(),
|
||||
None => String::new(),
|
||||
}
|
||||
.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(Option<GString>) + 'static) {
|
||||
self.simple_action
|
||||
.connect_activate(move |this, _| callback(state(this)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared helper to get C-based action state in Optional format
|
||||
fn state(this: &SimpleAction) -> Option<GString> {
|
||||
let state = this
|
||||
.state()
|
||||
.expect("State value required")
|
||||
.get::<String>()
|
||||
.expect("Parameter type does not match `String`");
|
||||
|
||||
if state.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(state.into())
|
||||
impl Escape for SimpleAction {
|
||||
fn escape() -> Self {
|
||||
SimpleAction::new(&uuid_string_random(), None)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,62 +0,0 @@
|
|||
use gtk::{
|
||||
gio::SimpleAction,
|
||||
glib::{uuid_string_random, GString},
|
||||
prelude::{ActionExt, StaticVariantType, ToVariant},
|
||||
};
|
||||
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Update` action of `Browser` group
|
||||
pub struct Update {
|
||||
pub simple_action: SimpleAction,
|
||||
}
|
||||
|
||||
impl Default for Update {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Update {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
simple_action: SimpleAction::new(
|
||||
&uuid_string_random(),
|
||||
Some(&String::static_variant_type()),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||
/// with formatted for this action [Variant](https://docs.gtk.org/glib/struct.Variant.html) value
|
||||
pub fn activate(&self, tab_item_id: Option<&str>) {
|
||||
self.simple_action.activate(Some(
|
||||
&match tab_item_id {
|
||||
Some(value) => String::from(value),
|
||||
None => String::new(),
|
||||
}
|
||||
.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(Option<GString>) + 'static) {
|
||||
self.simple_action.connect_activate(move |_, variant| {
|
||||
let tab_item_id = variant
|
||||
.expect("Variant required to call this action")
|
||||
.get::<String>()
|
||||
.expect("Parameter type does not match `String`");
|
||||
|
||||
callback(match tab_item_id.is_empty() {
|
||||
true => None,
|
||||
false => Some(tab_item_id.into()),
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue