From be31107a87bd124cd0d8911c57d560b603bbe82e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Kol=C3=ADn?= Date: Tue, 6 Aug 2024 21:25:30 +0200 Subject: [PATCH] [Rust] Port the ListView demo (#198) * [Rust] Ported the listview demo * Improve the comment on Cell usage * small comment change * Update src/List View/code.rs Fix the language Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Update src/List View/code.rs Missed this one glib::clone Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Update src/List View/code.rs Fixed comment Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Update src/List View/code.rs English too hard Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Update src/List View/code.rs Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Update src/List View/code.rs Remove unnecessary debugging output Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Strings_model => string_model --------- Co-authored-by: Ondrej Kolin Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> --- src/List View/code.rs | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/List View/code.rs diff --git a/src/List View/code.rs b/src/List View/code.rs new file mode 100644 index 00000000..e55bc145 --- /dev/null +++ b/src/List View/code.rs @@ -0,0 +1,81 @@ +use crate::workbench; +use gtk; +use gtk::glib; +use gtk::prelude::*; +use std::cell::Cell; + +pub fn main() { + // Required by gtk::StringList::new(), otherwise crashes + gtk::init().unwrap(); + + let list_view: gtk::ListView = workbench::builder().object("list_view").unwrap(); + let add: gtk::Button = workbench::builder().object("add").unwrap(); + let remove: gtk::Button = workbench::builder().object("remove").unwrap(); + + // https://doc.rust-lang.org/std/cell/struct.Cell.html + // "A mutable memory location." + // Makes it possible to access and change values from within signal handlers + let item = Cell::new(1); + + // Model + let string_model = + gtk::StringList::new(&["Default Item 1", "Default Item 2", "Default Item 3"]); + let model = gtk::SingleSelection::new(Some(string_model.clone())); + + // View + string_model.connect_items_changed(move |_model, position, removed, added| { + println!( + "position: {}, Item removed? {}, Item added? {}", + position, + removed > 0, + added > 0 + ) + }); + + model.connect_selection_changed(move |model, _position, _n_items| { + let selected_item = model.selected(); + println!( + "Model item selected from view: {}", + model + .item(selected_item) // Get the item + .unwrap() // Make sure it exists + .downcast::() // It's a member of GStringList + .unwrap() // Make sure it's really a StringObject + .string() // Read the string value + ) + }); + + add.connect_clicked(glib::clone!( + // Copy the reference, so it's accessible from the closure + // https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/macro.clone.html + @weak model => move |_| { + // Get the item counter value + let value = item.get(); + // Access the underlying gtk::StringList + let string_model = model + .model() + .unwrap() + .downcast::() + .unwrap(); + string_model.append(format!("New item {}", item.get()).as_str()); + // Increase the counter + item.set(value + 1); + })); + + remove.connect_clicked(gtk::glib::clone!( + // Copy the reference, so it's accessible from the closure + // https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/macro.clone.html + @weak model => move |_| { + let selected_item = model.selected(); + // In order to delete values we need to access the + // actual StringList model, we've created. + let string_model = model + .model() + .unwrap() + .downcast::() + .unwrap(); + string_model.remove(selected_item); + })); + + list_view.set_model(Some(&model)); +}