forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.rs
88 lines (74 loc) · 2.81 KB
/
crud.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
use slint::{Model, ModelExt, SharedString, StandardListViewItem, VecModel};
use std::cell::RefCell;
use std::rc::Rc;
slint::slint!(import { MainWindow } from "crud.slint";);
#[derive(Clone)]
struct Name {
first: String,
last: String,
}
pub fn main() {
let main_window = MainWindow::new().unwrap();
let prefix = Rc::new(RefCell::new(SharedString::from("")));
let prefix_for_wrapper = prefix.clone();
let model = Rc::new(VecModel::from(vec![
Name { first: "Hans".to_string(), last: "Emil".to_string() },
Name { first: "Max".to_string(), last: "Mustermann".to_string() },
Name { first: "Roman".to_string(), last: "Tisch".to_string() },
]));
let filtered_model = Rc::new(
model
.clone()
.map(|n| StandardListViewItem::from(slint::format!("{}, {}", n.last, n.first)))
.filter(move |e| e.text.starts_with(prefix_for_wrapper.borrow().as_str())),
);
main_window.set_names_list(filtered_model.clone().into());
{
let main_window_weak = main_window.as_weak();
let model = model.clone();
main_window.on_createClicked(move || {
let main_window = main_window_weak.unwrap();
let new_entry = Name {
first: main_window.get_name().to_string(),
last: main_window.get_surname().to_string(),
};
model.push(new_entry);
});
}
{
let main_window_weak = main_window.as_weak();
let model = model.clone();
let filtered_model = filtered_model.clone();
main_window.on_updateClicked(move || {
let main_window = main_window_weak.unwrap();
let updated_entry = Name {
first: main_window.get_name().to_string(),
last: main_window.get_surname().to_string(),
};
let row = filtered_model.unfiltered_row(main_window.get_current_item() as usize);
model.set_row_data(row, updated_entry);
});
}
{
let main_window_weak = main_window.as_weak();
let model = model.clone();
let filtered_model = filtered_model.clone();
main_window.on_deleteClicked(move || {
let main_window = main_window_weak.unwrap();
let index = filtered_model.unfiltered_row(main_window.get_current_item() as usize);
model.remove(index);
});
}
{
let main_window_weak = main_window.as_weak();
let filtered_model = filtered_model.clone();
main_window.on_prefixEdited(move || {
let main_window = main_window_weak.unwrap();
*prefix.borrow_mut() = main_window.get_prefix();
filtered_model.reset();
});
}
main_window.run().unwrap();
}