use gtk::{
gio::SimpleAction,
glib::{uuid_string_random, GString},
prelude::{ActionExt, ToVariant},
};
const DEFAULT_STATE: i32 = -1;
pub struct Pin {
gobject: SimpleAction,
}
impl Pin {
pub fn new() -> Self {
Self {
gobject: SimpleAction::new_stateful(
&uuid_string_random(),
None,
&DEFAULT_STATE.to_variant(),
),
}
}
pub fn change_state(&self, state: Option<i32>) {
self.gobject.change_state(
&match state {
Some(value) => value,
None => DEFAULT_STATE,
}
.to_variant(),
)
}
pub fn connect_activate(&self, callback: impl Fn(Option<i32>) + 'static) {
self.gobject.connect_activate(move |this, _| {
let state = this
.state()
.expect("State value required")
.get::<i32>()
.expect("Parameter type does not match `i32`");
callback(if state == DEFAULT_STATE {
None
} else {
Some(state)
})
});
}
pub fn gobject(&self) -> &SimpleAction {
&self.gobject
}
pub fn id(&self) -> GString {
self.gobject.name()
}
}