Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] Port the ListView demo #198

Merged
merged 11 commits into from
Aug 6, 2024
81 changes: 81 additions & 0 deletions src/List View/code.rs
Original file line number Diff line number Diff line change
@@ -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 possible accessing and changing values from signal handlers
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
let item = Cell::new(1);

// Model
let strings_model =
gtk::StringList::new(&["Default Item 1", "Default Item 2", "Default Item 3"]);
let model = gtk::SingleSelection::new(Some(strings_model));

//View
model.connect_items_changed(move |_model, position, removed, added| {
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
println!(
"position: {}, Item removed? {} Item added? {}",
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
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: {:?}",
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
model
.item(selected_item) // Get the item
.unwrap() // Make sure it exists
.downcast::<gtk::StringObject>() // It's a member of GStringList
.unwrap() // Make sure it's really a StringObject
.string() // Read the string value
)
});

add.connect_clicked(gtk::glib::clone!(
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
// 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 strings_model: gtk::StringList = model
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
.model()
.unwrap()
.downcast::<gtk::StringList>()
.unwrap();
strings_model.append(format!("New item {}", item.get()).as_str());
//Increase the counter
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
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
// actuall Stringlist model, we've created.
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
let strings_model: gtk::StringList = model
ondrejkolin marked this conversation as resolved.
Show resolved Hide resolved
.model()
.unwrap()
.downcast::<gtk::StringList>()
.unwrap();
strings_model.remove(selected_item);
}));

list_view.set_model(Some(&model));
}
Loading