forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.cpp
224 lines (179 loc) · 6.52 KB
/
dashboard.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT
#include "dashboard.h"
#include <fmt/core.h>
void Widget::set_property(std::string_view name, const slint::interpreter::Value &value)
{
if (m_ui)
(*m_ui)->set_property(qualified_property_name(name), value);
}
std::optional<slint::interpreter::Value> Widget::property(std::string_view name) const
{
if (m_ui)
return (*m_ui)->get_property(qualified_property_name(name));
return {};
}
void Widget::connect_ui(const slint::ComponentHandle<slint::interpreter::ComponentInstance> &ui,
std::string_view properties_prefix)
{
m_ui = ui;
m_properties_prefix = properties_prefix;
}
std::string Widget::qualified_property_name(std::string_view name) const
{
std::string qname(m_properties_prefix);
qname += name;
return qname;
}
std::string WidgetLocation::location_bindings() const
{
auto maybe_binding = [](std::string_view name, const auto &opt_value) -> std::string {
if (opt_value.has_value()) {
return fmt::format(" {}: {};\n", name, *opt_value);
} else {
return "";
}
};
return fmt::format(
R"slint(row: {};
col: {};
{}{})slint",
row, column, maybe_binding("rowspan", row_span), maybe_binding("colspan", col_span));
}
void DashboardBuilder::add_grid_widget(WidgetPtr widget, const WidgetLocation &location)
{
auto widget_id = register_widget(widget);
grid_widgets.push_back({ widget_id, location });
}
void DashboardBuilder::add_top_bar_widget(WidgetPtr widget)
{
auto widget_id = register_widget(widget);
top_bar_widgets.push_back(widget_id);
}
int DashboardBuilder::register_widget(WidgetPtr widget)
{
auto widget_type_name = widget->type_name();
widgets_used.insert(widget_type_name);
auto widget_id = int(widgets.size());
auto widget_name = fmt::format("widget_{}", widget_id);
widgets.push_back({ widget_name, widget });
return widget_id;
}
std::optional<slint::ComponentHandle<slint::interpreter::ComponentInstance>>
DashboardBuilder::build(slint::interpreter::ComponentCompiler &compiler) const
{
std::string widget_imports;
for (const auto &widget : widgets_used) {
if (widget_imports.size() > 0) {
widget_imports.append(", ");
}
widget_imports.append(widget);
}
if (widget_imports.size() > 0) {
widget_imports =
fmt::format("import {{ {} }} from \"iot-dashboard.slint\";", widget_imports);
}
// Vector of name/type_name of properties forwarded through the MainContent {} element.
std::string main_content_properties;
std::string main_grid;
std::string top_bar;
std::string exposed_properties;
for (const auto &[widget_id, location] : grid_widgets) {
const auto &[widget_name, widget_ptr] = widgets[widget_id];
main_grid.append(fmt::format(
R"slint(
{0} := {1} {{
{2}
}}
)slint",
widget_name, widget_ptr->type_name(), location.location_bindings()));
std::string properties_prefix = widget_name;
properties_prefix.append("__");
for (const auto &property : widget_ptr->properties()) {
std::string forwarded_property_name = properties_prefix;
forwarded_property_name.append(property.name);
main_content_properties.append(
fmt::format(" in-out property <{0}> {1} <=> {2}.{3};\n", property.type_name,
forwarded_property_name, widget_name, property.name));
exposed_properties.append(
fmt::format(" in-out property <{0}> {1} <=> main_content.{1};\n",
property.type_name, forwarded_property_name));
}
}
for (const auto widget_id : top_bar_widgets) {
const auto &[widget_name, widget_ptr] = widgets[widget_id];
top_bar.append(fmt::format(
R"slint(
{0} := {1} {{
}}
)slint",
widget_name, widget_ptr->type_name()));
std::string properties_prefix = widget_name;
properties_prefix.append("__");
for (const auto &property : widget_ptr->properties()) {
std::string forwarded_property_name = properties_prefix;
forwarded_property_name.append(property.name);
exposed_properties.append(fmt::format(" in-out property <{0}> {1} <=> {2}.{3};\n",
property.type_name, forwarded_property_name,
widget_name, property.name));
}
}
auto source_code = fmt::format(
R"slint(
{0}
component MainContent inherits VerticalLayout {{
{4}
spacing: 24px;
TopBar {{
@children
}}
GridLayout {{
spacing: 6px;
padding-left: 19px;
padding-top: 0px;
padding-right: 17px;
padding-bottom: 24px;
{2}
}}
}}
export component MainWindow inherits Window {{
title: "IOT dashboard";
{3}
HorizontalLayout {{
padding: 0; spacing: 0;
MenuBar {{
}}
main_content := MainContent {{
{1}
}}
}}
}}
)slint",
widget_imports, top_bar, main_grid, exposed_properties, main_content_properties);
auto definition = compiler.build_from_source(source_code, SOURCE_DIR);
for (auto diagnostic : compiler.diagnostics()) {
std::cerr << (diagnostic.level == slint::interpreter::DiagnosticLevel::Warning ? "warning: "
: "error: ")
<< diagnostic.message << std::endl;
std::cerr << "location: " << diagnostic.source_file;
if (diagnostic.line > 0)
std::cerr << ":" << diagnostic.line;
if (diagnostic.column > 0)
std::cerr << ":" << diagnostic.column;
std::cerr << std::endl;
}
if (!definition) {
std::cerr << "compilation failure!" << std::endl;
std::cerr << "generated source:" << std::endl << source_code << std::endl;
return {};
}
// std::cerr << source_code << std::endl;
auto ui = definition->create();
for (const auto &entry : widgets) {
auto [widget_name, widget_ptr] = entry;
std::string properties_prefix = widget_name;
properties_prefix += "__";
widget_ptr->connect_ui(ui, properties_prefix);
}
return ui;
}