fix dropdown item factory

This commit is contained in:
yggverse 2024-11-20 16:32:31 +02:00
parent 22f0e59479
commit 9b16022820

View File

@ -6,7 +6,7 @@ use gtk::{
prelude::{Cast, CastNone}, prelude::{Cast, CastNone},
ListStore, ListStore,
}, },
prelude::{BoxExt, ListItemExt}, prelude::{BoxExt, ListItemExt, WidgetExt},
Align, Box, DropDown, Label, ListItem, SignalListItemFactory, Align, Box, DropDown, Label, ListItem, SignalListItemFactory,
}; };
@ -23,39 +23,58 @@ impl List {
// Init model with custom `GObject` properties // Init model with custom `GObject` properties
let model = ListStore::new::<Item>(); let model = ListStore::new::<Item>();
// Setup item factory to append items after `DropDown` init // Setup item factory
// * wanted only to append items after `DropDown` init
let factory = SignalListItemFactory::new(); let factory = SignalListItemFactory::new();
// @TODO factory.connect_setup(move |_, gobject| {}); factory.connect_setup(move |_, gobject| {
factory.connect_bind(move |_, gobject| { // Init row widget for dropdown item
// Cast components let widget = Box::builder()
let list_item = gobject.downcast_ref::<ListItem>().unwrap();
let item = list_item.item().and_downcast::<Item>().unwrap();
// Build row widget
let child = Box::builder()
.orientation(gtk::Orientation::Vertical) .orientation(gtk::Orientation::Vertical)
.build(); .build();
child.append( // Title
&Label::builder() widget.append(&Label::builder().halign(Align::Start).build());
.halign(Align::Start)
.label(item.title())
.build(),
);
child.append( // Subtitle
widget.append(
&Label::builder() &Label::builder()
.halign(Align::Start) .halign(Align::Start)
.label(item.subtitle())
.css_classes(["caption", "dim-label"]) .css_classes(["caption", "dim-label"])
.build(), .build(),
); );
// Update menu item // Update menu item
list_item.set_child(Some(&child)); gobject
.downcast_ref::<ListItem>()
.unwrap()
.set_child(Some(&widget));
}); });
// Init list `GObject` factory.connect_bind(move |_, gobject| {
// Downcast requirements
let list_item = gobject.downcast_ref::<ListItem>().unwrap();
let item = list_item.item().and_downcast::<Item>().unwrap();
let container = list_item.child().and_downcast::<Box>().unwrap();
// Update Title (expected as the first child)
container
.first_child()
.unwrap()
.downcast::<Label>()
.unwrap()
.set_label(&item.title());
// Update Subtitle (expected as the last child)
container
.last_child()
.unwrap()
.downcast::<Label>()
.unwrap()
.set_label(&item.subtitle());
});
// Init main `GObject`
let gobject = DropDown::builder().model(&model).factory(&factory).build(); let gobject = DropDown::builder().model(&model).factory(&factory).build();
// Return activated `Self` // Return activated `Self`