|
| 1 | +// Variation on ListBox model example to show use of StringList and StringSorter. |
| 2 | + |
| 3 | +use gtk::{glib, prelude::*}; |
| 4 | + |
| 5 | +fn main() -> glib::ExitCode { |
| 6 | + let application = gtk::Application::builder() |
| 7 | + .application_id("com.github.gtk-rs.examples.SortedStringList") |
| 8 | + .build(); |
| 9 | + |
| 10 | + application.connect_activate(build_ui); |
| 11 | + |
| 12 | + application.run() |
| 13 | +} |
| 14 | + |
| 15 | +fn build_ui(application: >k::Application) { |
| 16 | + let window = gtk::ApplicationWindow::builder() |
| 17 | + .default_width(320) |
| 18 | + .default_height(200) |
| 19 | + .application(application) |
| 20 | + .title("Sorted StringList") |
| 21 | + .build(); |
| 22 | + |
| 23 | + // Create a StringSorter with a property expression to sort |
| 24 | + // StringObjects in a StringList. StringObject has a "string" property. |
| 25 | + let expression = gtk::PropertyExpression::new( |
| 26 | + gtk::StringObject::static_type(), |
| 27 | + None::<gtk::Expression>, |
| 28 | + "string", |
| 29 | + ); |
| 30 | + let sorter = gtk::StringSorter::new(Some(expression)); |
| 31 | + sorter.set_ignore_case(true); |
| 32 | + |
| 33 | + // Create our list store as a StringList and populate with some strings. |
| 34 | + let model = gtk::StringList::new(&["zoo", "abba", "donkey", "sunrise", "river", "phoenix"]); |
| 35 | + |
| 36 | + // Create a sort model and bind it to the ListStore and the sorter. |
| 37 | + let sort_model = gtk::SortListModel::new(Some(model.clone()), Some(sorter)); |
| 38 | + |
| 39 | + // And then create the UI part, the listbox and bind the sort |
| 40 | + // model to it. Whenever the UI needs to show a new row, e.g. because |
| 41 | + // it was notified that the model changed, it will call the callback |
| 42 | + // with the corresponding item from the model and will ask for a new |
| 43 | + // gtk::ListBoxRow that should be displayed. |
| 44 | + // |
| 45 | + // The gtk::ListBoxRow can contain any possible widgets. |
| 46 | + // Here we use an Inscription. |
| 47 | + |
| 48 | + let listbox = gtk::ListBox::new(); |
| 49 | + listbox.bind_model(Some(&sort_model), move |obj| { |
| 50 | + let list_object = obj |
| 51 | + .downcast_ref::<gtk::StringObject>() |
| 52 | + .expect("The object should be of type `StringObject`."); |
| 53 | + gtk::Inscription::builder() |
| 54 | + .xalign(0.0) |
| 55 | + .text(list_object.string()) |
| 56 | + .build() |
| 57 | + .upcast() |
| 58 | + }); |
| 59 | + |
| 60 | + let scrolled_window = gtk::ScrolledWindow::builder() |
| 61 | + .hscrollbar_policy(gtk::PolicyType::Never) // Disable horizontal scrolling |
| 62 | + .min_content_height(200) |
| 63 | + .min_content_width(360) |
| 64 | + .build(); |
| 65 | + |
| 66 | + scrolled_window.set_child(Some(&listbox)); |
| 67 | + |
| 68 | + window.set_child(Some(&scrolled_window)); |
| 69 | + |
| 70 | + for i in 0..10 { |
| 71 | + model.append(&format!("Name {i}")); |
| 72 | + } |
| 73 | + |
| 74 | + window.present(); |
| 75 | +} |
0 commit comments