implement on_select wrapper

This commit is contained in:
yggverse 2024-11-17 18:55:03 +02:00
parent 36bfc30664
commit 41aebd4fcc
2 changed files with 33 additions and 12 deletions

View File

@ -28,6 +28,9 @@ impl Form {
gobject.append(list.gobject()); gobject.append(list.gobject());
gobject.append(name.gobject()); gobject.append(name.gobject());
// Connect events
list.on_select(move |key| name.gobject().set_visible(key.is_none()));
// Return activated `Self` // Return activated `Self`
Self { gobject } Self { gobject }
} }

View File

@ -42,13 +42,38 @@ impl List {
self.model.append(&item); self.model.append(&item);
} }
/* @TODO not in use
/// Get selected `key` or panic on selection not found /// Get selected `key` or panic on selection not found
/// * return `None` if current selection key match `PROPERTY_KEY_NONE_VALUE` /// * return `None` if current selection key match `PROPERTY_KEY_NONE_VALUE`
pub fn selected(&self) -> Option<i64> { pub fn selected(&self) -> Option<i64> {
match self.gobject.selected_item() { selected(&self.gobject)
Some(gobject) => { }*/
// Events
/// Run callback function on `connect_selected_notify`
/// * return formatted key as result
pub fn on_select(&self, callback: impl Fn(Option<i64>) + 'static) {
self.gobject
.connect_selected_notify(move |this| callback(selected(this)));
}
// Getters
pub fn gobject(&self) -> &DropDown {
&self.gobject
}
}
// Tools
/// Get selected `key` or panic on selection not found
/// * return `None` if current selection key match `PROPERTY_KEY_NONE_VALUE`
fn selected(list: &DropDown) -> Option<i64> {
match list.selected_item() {
Some(this) => {
// Convert back from C-based GObject type // Convert back from C-based GObject type
let key = gobject.property::<i64>(PROPERTY_KEY_NAME); let key = this.property::<i64>(PROPERTY_KEY_NAME);
if key == PROPERTY_KEY_NONE_VALUE { if key == PROPERTY_KEY_NONE_VALUE {
None None
@ -59,10 +84,3 @@ impl List {
None => panic!(), None => panic!(),
} }
} }
// Getters
pub fn gobject(&self) -> &DropDown {
&self.gobject
}
}