forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
72 lines (60 loc) · 2.54 KB
/
main.cpp
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
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
#include "todo.h"
int main()
{
auto demo = MainWindow::create();
auto todo_model = std::make_shared<slint::VectorModel<TodoItem>>(std::vector {
TodoItem { "Implement the .slint file", true },
TodoItem { "Do the Rust part", false },
TodoItem { "Make the C++ code", true },
TodoItem { "Write some JavaScript code", false },
TodoItem { "Test the application", false },
TodoItem { "Ship to customer", false },
TodoItem { "???", false },
TodoItem { "Profit", false },
});
demo->set_todo_model(todo_model);
demo->on_todo_added([todo_model](const slint::SharedString &s) {
todo_model->push_back(TodoItem { s, false });
});
demo->on_remove_done([todo_model] {
int offset = 0;
int count = todo_model->row_count();
for (int i = 0; i < count; ++i) {
if (todo_model->row_data(i - offset)->checked) {
todo_model->erase(i - offset);
offset += 1;
}
}
});
demo->on_popup_confirmed(
[demo = slint::ComponentWeakHandle(demo)] { (*demo.lock())->window().hide(); });
demo->window().on_close_requested([todo_model, demo = slint::ComponentWeakHandle(demo)] {
int count = todo_model->row_count();
for (int i = 0; i < count; ++i) {
if (!todo_model->row_data(i)->checked) {
(*demo.lock())->invoke_show_confirm_popup();
return slint::CloseRequestResponse::KeepWindowShown;
}
}
return slint::CloseRequestResponse::HideWindow;
});
demo->set_show_header(true);
demo->on_apply_sorting_and_filtering([todo_model, demo = slint::ComponentWeakHandle(demo)] {
auto demo_lock = demo.lock();
(*demo_lock)->set_todo_model(todo_model);
if ((*demo_lock)->get_hide_done_items()) {
(*demo_lock)
->set_todo_model(std::make_shared<slint::FilterModel<TodoItem>>(
(*demo_lock)->get_todo_model(), [](auto e) { return !e.checked; }));
}
if ((*demo_lock)->get_is_sort_by_name()) {
(*demo_lock)
->set_todo_model(std::make_shared<slint::SortModel<TodoItem>>(
(*demo_lock)->get_todo_model(),
[](auto lhs, auto rhs) { return lhs.title < rhs.title; }));
}
});
demo->run();
}