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