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(name.gobject());
// Connect events
list.on_select(move |key| name.gobject().set_visible(key.is_none()));
// Return activated `Self`
Self { gobject }
}

View File

@ -42,22 +42,20 @@ impl List {
self.model.append(&item);
}
/* @TODO not in use
/// Get selected `key` or panic on selection not found
/// * return `None` if current selection key match `PROPERTY_KEY_NONE_VALUE`
pub fn selected(&self) -> Option<i64> {
match self.gobject.selected_item() {
Some(gobject) => {
// Convert back from C-based GObject type
let key = gobject.property::<i64>(PROPERTY_KEY_NAME);
selected(&self.gobject)
}*/
if key == PROPERTY_KEY_NONE_VALUE {
None
} else {
Some(key)
}
}
None => panic!(),
}
// 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
@ -66,3 +64,23 @@ impl List {
&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
let key = this.property::<i64>(PROPERTY_KEY_NAME);
if key == PROPERTY_KEY_NONE_VALUE {
None
} else {
Some(key)
}
}
None => panic!(),
}
}