1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: MIT |
3 | |
4 | #![deny (unsafe_code)] |
5 | |
6 | #[cfg (target_arch = "wasm32" )] |
7 | use wasm_bindgen::prelude::*; |
8 | |
9 | slint::include_modules!(); |
10 | |
11 | use std::rc::Rc; |
12 | |
13 | use slint::{Model, StandardListViewItem, VecModel}; |
14 | |
15 | #[cfg_attr (target_arch = "wasm32" , wasm_bindgen(start))] |
16 | pub fn main() { |
17 | // This provides better error messages in debug mode. |
18 | // It's disabled in release mode so it doesn't bloat up the file size. |
19 | #[cfg (all(debug_assertions, target_arch = "wasm32" ))] |
20 | console_error_panic_hook::set_once(); |
21 | |
22 | slint::init_translations!(concat!(env!("CARGO_MANIFEST_DIR" ), "/lang/" )); |
23 | |
24 | let app = App::new().unwrap(); |
25 | |
26 | let row_data: Rc<VecModel<slint::ModelRc<StandardListViewItem>>> = Rc::new(VecModel::default()); |
27 | |
28 | for r in 1..101 { |
29 | let items = Rc::new(VecModel::default()); |
30 | |
31 | for c in 1..5 { |
32 | items.push(slint::format!("Item {r}. {c}" ).into()); |
33 | } |
34 | |
35 | row_data.push(items.into()); |
36 | } |
37 | |
38 | app.global::<TableViewPageAdapter>().set_row_data(row_data.clone().into()); |
39 | |
40 | app.global::<TableViewPageAdapter>().on_sort_ascending({ |
41 | let app_weak = app.as_weak(); |
42 | let row_data = row_data.clone(); |
43 | move |index| { |
44 | let row_data = row_data.clone(); |
45 | |
46 | let sort_model = Rc::new(row_data.sort_by(move |r_a, r_b| { |
47 | let c_a = r_a.row_data(index as usize).unwrap(); |
48 | let c_b = r_b.row_data(index as usize).unwrap(); |
49 | |
50 | c_a.text.cmp(&c_b.text) |
51 | })); |
52 | |
53 | app_weak.unwrap().global::<TableViewPageAdapter>().set_row_data(sort_model.into()); |
54 | } |
55 | }); |
56 | |
57 | app.global::<TableViewPageAdapter>().on_sort_descending({ |
58 | let app_weak = app.as_weak(); |
59 | move |index| { |
60 | let row_data = row_data.clone(); |
61 | |
62 | let sort_model = Rc::new(row_data.sort_by(move |r_a, r_b| { |
63 | let c_a = r_a.row_data(index as usize).unwrap(); |
64 | let c_b = r_b.row_data(index as usize).unwrap(); |
65 | |
66 | c_b.text.cmp(&c_a.text) |
67 | })); |
68 | |
69 | app_weak.unwrap().global::<TableViewPageAdapter>().set_row_data(sort_model.into()); |
70 | } |
71 | }); |
72 | |
73 | app.run().unwrap(); |
74 | } |
75 | |