Skip to content

Commit be31107

Browse files
ondrejkolinOndrej KolinHofer-Julian
authored
[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 <[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]>
1 parent b01dc34 commit be31107

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/List View/code.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use crate::workbench;
2+
use gtk;
3+
use gtk::glib;
4+
use gtk::prelude::*;
5+
use std::cell::Cell;
6+
7+
pub fn main() {
8+
// Required by gtk::StringList::new(), otherwise crashes
9+
gtk::init().unwrap();
10+
11+
let list_view: gtk::ListView = workbench::builder().object("list_view").unwrap();
12+
let add: gtk::Button = workbench::builder().object("add").unwrap();
13+
let remove: gtk::Button = workbench::builder().object("remove").unwrap();
14+
15+
// https://doc.rust-lang.org/std/cell/struct.Cell.html
16+
// "A mutable memory location."
17+
// Makes it possible to access and change values from within signal handlers
18+
let item = Cell::new(1);
19+
20+
// Model
21+
let string_model =
22+
gtk::StringList::new(&["Default Item 1", "Default Item 2", "Default Item 3"]);
23+
let model = gtk::SingleSelection::new(Some(string_model.clone()));
24+
25+
// View
26+
string_model.connect_items_changed(move |_model, position, removed, added| {
27+
println!(
28+
"position: {}, Item removed? {}, Item added? {}",
29+
position,
30+
removed > 0,
31+
added > 0
32+
)
33+
});
34+
35+
model.connect_selection_changed(move |model, _position, _n_items| {
36+
let selected_item = model.selected();
37+
println!(
38+
"Model item selected from view: {}",
39+
model
40+
.item(selected_item) // Get the item
41+
.unwrap() // Make sure it exists
42+
.downcast::<gtk::StringObject>() // It's a member of GStringList
43+
.unwrap() // Make sure it's really a StringObject
44+
.string() // Read the string value
45+
)
46+
});
47+
48+
add.connect_clicked(glib::clone!(
49+
// Copy the reference, so it's accessible from the closure
50+
// https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/macro.clone.html
51+
@weak model => move |_| {
52+
// Get the item counter value
53+
let value = item.get();
54+
// Access the underlying gtk::StringList
55+
let string_model = model
56+
.model()
57+
.unwrap()
58+
.downcast::<gtk::StringList>()
59+
.unwrap();
60+
string_model.append(format!("New item {}", item.get()).as_str());
61+
// Increase the counter
62+
item.set(value + 1);
63+
}));
64+
65+
remove.connect_clicked(gtk::glib::clone!(
66+
// Copy the reference, so it's accessible from the closure
67+
// https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/macro.clone.html
68+
@weak model => move |_| {
69+
let selected_item = model.selected();
70+
// In order to delete values we need to access the
71+
// actual StringList model, we've created.
72+
let string_model = model
73+
.model()
74+
.unwrap()
75+
.downcast::<gtk::StringList>()
76+
.unwrap();
77+
string_model.remove(selected_item);
78+
}));
79+
80+
list_view.set_model(Some(&model));
81+
}

0 commit comments

Comments
 (0)