sync item properties on bind

This commit is contained in:
yggverse 2024-12-08 06:40:37 +02:00
parent 74a3e2c879
commit a361c5aa97

View File

@ -12,7 +12,7 @@ use gtk::{
ListStore, ListStore,
}, },
glib::Uri, glib::Uri,
prelude::{BoxExt, ListItemExt, WidgetExt}, prelude::{BoxExt, ListItemExt, ObjectExt, WidgetExt},
Align, Box, DropDown, Image, Label, ListItem, Orientation, SignalListItemFactory, Align, Box, DropDown, Image, Label, ListItem, Orientation, SignalListItemFactory,
}; };
@ -96,26 +96,46 @@ impl List {
let item = list_item.item().and_downcast::<Item>().unwrap(); let item = list_item.item().and_downcast::<Item>().unwrap();
let child = list_item.child().and_downcast::<Box>().unwrap(); let child = list_item.child().and_downcast::<Box>().unwrap();
// Update `title` // Bind `title`
let title = child.first_child().and_downcast::<Label>().unwrap(); match child.first_child().and_downcast::<Label>() {
Some(label) => {
label.set_label(&item.title());
label.set_css_classes(if item.is_active() { &["success"] } else { &[] });
item.bind_property("title", &label, "label").build(); // sync label
item.bind_property("is-active", &label, "css-classes")
.transform_to(|_, is_active| {
if is_active {
Some(vec!["success".to_string()])
} else {
Some(vec![])
}
})
.build(); // sync class by status
}
None => todo!(),
};
title.set_label(&item.title()); // Bind `subtitle`
title.set_css_classes(if item.is_active() { &["success"] } else { &[] });
// Update `subtitle`
let subtitle = child.last_child().and_downcast::<Box>().unwrap(); let subtitle = child.last_child().and_downcast::<Box>().unwrap();
subtitle match subtitle.last_child().and_downcast::<Label>() {
.last_child() Some(label) => {
.and_downcast::<Label>() label.set_label(&item.subtitle());
.unwrap() item.bind_property("subtitle", &label, "label").build(); // sync
.set_label(&item.subtitle()); }
None => todo!(),
};
// Update `tooltip` // Bind `tooltip`
let tooltip = subtitle.first_child().and_downcast::<Image>().unwrap(); match subtitle.first_child().and_downcast::<Image>() {
Some(tooltip) => {
tooltip.set_visible(!item.tooltip().is_empty()); tooltip.set_visible(!item.tooltip().is_empty());
tooltip.set_tooltip_markup(Some(&item.tooltip())); tooltip.set_tooltip_markup(Some(&item.tooltip()));
item.bind_property("tooltip", &tooltip, "tooltip-markup")
.build(); // sync
}
None => todo!(),
};
}); });
// Init main widget // Init main widget