Skip to content

Commit

Permalink
[Rust] Port the ListView demo (#198)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>

* Update src/List View/code.rs

Missed this one glib::clone

Co-authored-by: Hofer-Julian <[email protected]>

* Update src/List View/code.rs

Fixed comment

Co-authored-by: Hofer-Julian <[email protected]>

* Update src/List View/code.rs

English too hard

Co-authored-by: Hofer-Julian <[email protected]>

* Update src/List View/code.rs

Co-authored-by: Hofer-Julian <[email protected]>

* Update src/List View/code.rs

Remove unnecessary debugging output

Co-authored-by: Hofer-Julian <[email protected]>

* Apply suggestions from code review

Co-authored-by: Hofer-Julian <[email protected]>

* Strings_model => string_model

---------

Co-authored-by: Ondrej Kolin <[email protected]>
Co-authored-by: Hofer-Julian <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2024
1 parent b01dc34 commit be31107
Showing 1 changed file with 81 additions and 0 deletions.
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 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::<gtk::StringObject>() // 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::<gtk::StringList>()
.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::<gtk::StringList>()
.unwrap();
string_model.remove(selected_item);
}));

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

0 comments on commit be31107

Please sign in to comment.