forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
98 lines (83 loc) · 3.36 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
use slint::{FilterModel, Model, SortModel};
use std::rc::Rc;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
slint::include_modules!();
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn main() {
// This provides better error messages in debug mode.
// It's disabled in release mode so it doesn't bloat up the file size.
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
console_error_panic_hook::set_once();
let todo_model = Rc::new(slint::VecModel::<TodoItem>::from(vec![
TodoItem { checked: true, title: "Implement the .slint file".into() },
TodoItem { checked: true, title: "Do the Rust part".into() },
TodoItem { checked: false, title: "Make the C++ code".into() },
TodoItem { checked: false, title: "Write some JavaScript code".into() },
TodoItem { checked: false, title: "Test the application".into() },
TodoItem { checked: false, title: "Ship to customer".into() },
TodoItem { checked: false, title: "???".into() },
TodoItem { checked: false, title: "Profit".into() },
]));
let main_window = MainWindow::new().unwrap();
main_window.on_todo_added({
let todo_model = todo_model.clone();
move |text| todo_model.push(TodoItem { checked: false, title: text })
});
main_window.on_remove_done({
let todo_model = todo_model.clone();
move || {
let mut offset = 0;
for i in 0..todo_model.row_count() {
if todo_model.row_data(i - offset).unwrap().checked {
todo_model.remove(i - offset);
offset += 1;
}
}
}
});
let weak_window = main_window.as_weak();
main_window.on_popup_confirmed(move || {
let window = weak_window.unwrap();
window.hide().unwrap();
});
{
let weak_window = main_window.as_weak();
let todo_model = todo_model.clone();
main_window.window().on_close_requested(move || {
let window = weak_window.unwrap();
if todo_model.iter().any(|t| !t.checked) {
window.invoke_show_confirm_popup();
slint::CloseRequestResponse::KeepWindowShown
} else {
slint::CloseRequestResponse::HideWindow
}
});
}
main_window.on_apply_sorting_and_filtering({
let weak_window = main_window.as_weak();
let todo_model = todo_model.clone();
move || {
let window = weak_window.unwrap();
window.set_todo_model(todo_model.clone().into());
if window.get_hide_done_items() {
window.set_todo_model(
Rc::new(FilterModel::new(window.get_todo_model(), |e| !e.checked)).into(),
);
}
if window.get_is_sort_by_name() {
window.set_todo_model(
Rc::new(SortModel::new(window.get_todo_model(), |lhs, rhs| {
lhs.title.to_lowercase().cmp(&rhs.title.to_lowercase())
}))
.into(),
);
}
}
});
main_window.set_show_header(true);
main_window.set_todo_model(todo_model.into());
main_window.run().unwrap();
}