Skip to content

Instantly share code, notes, and snippets.

@saivert
Last active September 22, 2023 23:25
Show Gist options
  • Save saivert/43f7f4875d2e9692f902dd1d632f3f72 to your computer and use it in GitHub Desktop.
Save saivert/43f7f4875d2e9692f902dd1d632f3f72 to your computer and use it in GitHub Desktop.
Reimplementation of gtk::DropDown's default list factory
let listfactory = gtk::SignalListItemFactory::new();
listfactory.connect_setup(|_, item| setup_handler(item, false));
let dropdown = self.outputdevice_dropdown.get();
listfactory.connect_bind(clone!(@weak dropdown => move |_factory, item| {
let item: gtk::ListItem = item.downcast_ref().cloned().unwrap();
fn selected_item_handler(dropdown: &gtk::DropDown, listitem: &gtk::ListItem) {
if let Some (child) = listitem.child() {
let box_: gtk::Box = child.downcast().unwrap();
let icon = box_.last_child().unwrap();
icon.set_opacity(
if listitem.item() == dropdown.selected_item() {
1.0
} else {
0.0
}
);
}
}
let selected_item_closure = closure_local!(@watch dropdown as _dropdown, @strong item as listitem =>
move |dropdown: &gtk::DropDown, _pspec: &glib::ParamSpec| {
selected_item_handler(dropdown, &listitem);
});
dropdown.connect_closure("notify::selected-item", true, selected_item_closure);
selected_item_handler(&dropdown, &item);
}));
self.outputdevice_dropdown.set_list_factory(Some(&listfactory));
// Or you know just do this which is much easier........
// Save the default factory implemenation
let default_dropdown_factory = self.outputdevice_dropdown.factory();
// Set out custom factory (which also impacts list-factory
self.outputdevice_dropdown.set_factory(Some(&factory));
// Reset list-factory to the default one after having set the main factory
self.outputdevice_dropdown.set_list_factory(default_dropdown_factory.as_ref());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment