use gtk::{
gio::SimpleAction,
glib::{uuid_string_random, GString},
prelude::{ActionExt, StaticVariantType, ToVariant},
};
pub struct Update {
pub gobject: SimpleAction,
}
impl Update {
pub fn new() -> Self {
Self {
gobject: SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type())),
}
}
pub fn activate(&self, tab_item_id: Option<&str>) {
self.gobject.activate(Some(
&match tab_item_id {
Some(value) => String::from(value),
None => String::new(),
}
.to_variant(),
));
}
pub fn connect_activate(&self, callback: impl Fn(Option<GString>) + 'static) {
self.gobject.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()),
})
});
}
}