mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
store dropdown item keys in gobject properties to prevent selection mismatch
This commit is contained in:
parent
14c31734fd
commit
36bfc30664
@ -17,9 +17,9 @@ impl Form {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new(list_options: Vec<(Option<i64>, String, bool)>) -> Self {
|
||||
pub fn new(items: Vec<(Option<i64>, String, bool)>) -> Self {
|
||||
// Init components
|
||||
let list = List::new(&list_options);
|
||||
let list = List::new();
|
||||
let name = Name::new();
|
||||
|
||||
// Init main container
|
||||
@ -28,17 +28,6 @@ impl Form {
|
||||
gobject.append(list.gobject());
|
||||
gobject.append(name.gobject());
|
||||
|
||||
// Init events
|
||||
list.gobject().connect_selected_notify(move |this| {
|
||||
// Get selection ID from vector @TODO use GObject storage instead
|
||||
// https://gtk-rs.org/gtk4-rs/stable/latest/book/list_widgets.html
|
||||
match list_options.get(this.selected() as usize) {
|
||||
// Hide name entry on existing identity selected
|
||||
Some((id, _, _)) => name.gobject().set_visible(id.is_none()),
|
||||
None => todo!(),
|
||||
}
|
||||
});
|
||||
|
||||
// Return activated `Self`
|
||||
Self { gobject }
|
||||
}
|
||||
|
@ -1,35 +1,63 @@
|
||||
use gtk::{DropDown, StringList};
|
||||
use gtk::{gio::ListStore, prelude::ObjectExt, DropDown, Label};
|
||||
|
||||
const PROPERTY_KEY_NAME: &str = "key"; // Store item key as GTK property
|
||||
const PROPERTY_KEY_NONE_VALUE: i64 = -1; // C-type conversion for `None` values
|
||||
|
||||
pub struct List {
|
||||
gobject: DropDown,
|
||||
model: ListStore,
|
||||
}
|
||||
|
||||
impl List {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new(list_options: &Vec<(Option<i64>, String, bool)>) -> Self {
|
||||
// Init empty list model
|
||||
let model = StringList::new(&[]);
|
||||
|
||||
// Init `GObject`
|
||||
pub fn new() -> Self {
|
||||
let model = ListStore::new::<Label>();
|
||||
let gobject = DropDown::builder().model(&model).build();
|
||||
|
||||
// Build selection list
|
||||
let mut index = 0;
|
||||
Self { model, gobject }
|
||||
}
|
||||
|
||||
for (_key, value, is_selected) in list_options {
|
||||
model.append(&value);
|
||||
// Actions
|
||||
|
||||
if *is_selected {
|
||||
gobject.set_selected(index);
|
||||
/// Append new item with `profile_identity_gemini_id` as `key` and name as `value`
|
||||
pub fn append(&self, key: Option<i64>, value: &str) {
|
||||
// Create new label for item
|
||||
let item = Label::new(Some(value));
|
||||
|
||||
// Store key as property
|
||||
item.set_property(
|
||||
PROPERTY_KEY_NAME,
|
||||
match key {
|
||||
Some(key) => key,
|
||||
None => PROPERTY_KEY_NONE_VALUE,
|
||||
},
|
||||
);
|
||||
|
||||
// Set value as label
|
||||
item.set_label(value);
|
||||
|
||||
// Append formatted record
|
||||
self.model.append(&item);
|
||||
}
|
||||
|
||||
/// 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);
|
||||
|
||||
if key == PROPERTY_KEY_NONE_VALUE {
|
||||
None
|
||||
} else {
|
||||
Some(key)
|
||||
}
|
||||
}
|
||||
|
||||
index += 1;
|
||||
None => panic!(),
|
||||
}
|
||||
|
||||
// Done
|
||||
Self { gobject }
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
Loading…
x
Reference in New Issue
Block a user