1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: MIT |
3 | |
4 | #pragma once |
5 | |
6 | #include <slint-interpreter.h> |
7 | |
8 | #include <optional> |
9 | #include <string_view> |
10 | #include <vector> |
11 | #include <unordered_set> |
12 | |
13 | struct PropertyDeclaration |
14 | { |
15 | std::string name; |
16 | std::string type_name; |
17 | }; |
18 | |
19 | /** |
20 | The Widget base class is a wrapper around slint::interpreter::ComponentInstance that allows |
21 | conveniently reading and writing properties of an element of which the properties have been |
22 | forwarded via two-way bindings. |
23 | |
24 | When an instance of a Widget sub-class is added to the DashboardBuilder, the value of |
25 | type_name() is used to create an element declaration in the generated .slint code ("SomeElement { |
26 | ... }"), the element is given an automatically generated name and all properties returned by the |
27 | properties() function are forwarded. For example two instances of a "Clock" element become this |
28 | in .slint: |
29 | |
30 | export component MainWindow inherits Window { |
31 | ... |
32 | widget_1 := Clock { |
33 | } |
34 | widget_2 := Clock { |
35 | } |
36 | |
37 | in-out property <string> widget_1__time <=> widget_1.time; |
38 | in-out property <string> widget_2__time <=> widget_2.time; |
39 | } |
40 | |
41 | The DashboardBuilder calls connect_ui() to inform the instance about the "widget_1__" and |
42 | "widget_2__" prefix and passes a reference to the MainWindow as ComponentInstance. Subsequently |
43 | calls to set_property("time", some_value) translate to setting "widget_1__time" or |
44 | "widget_2__time", depending on the Widget instance. |
45 | */ |
46 | class Widget |
47 | { |
48 | public: |
49 | virtual ~Widget() { } |
50 | virtual std::string type_name() const = 0; |
51 | virtual std::vector<PropertyDeclaration> properties() const = 0; |
52 | |
53 | void set_property(std::string_view name, const slint::interpreter::Value &value); |
54 | |
55 | std::optional<slint::interpreter::Value> property(std::string_view name) const; |
56 | |
57 | void connect_ui(const slint::ComponentHandle<slint::interpreter::ComponentInstance> &ui, |
58 | std::string_view properties_prefix); |
59 | |
60 | std::pair<std::string, std::vector<PropertyDeclaration>> |
61 | generate_forwarding_two_way_property_bindings(std::string_view widget_name) const; |
62 | |
63 | private: |
64 | std::string qualified_property_name(std::string_view name) const; |
65 | |
66 | std::optional<slint::ComponentHandle<slint::interpreter::ComponentInstance>> m_ui; |
67 | std::string m_properties_prefix; |
68 | }; |
69 | |
70 | using WidgetPtr = std::shared_ptr<Widget>; |
71 | |
72 | struct WidgetLocation |
73 | { |
74 | int row = 0; |
75 | int column = 0; |
76 | std::optional<int> row_span; |
77 | std::optional<int> col_span; |
78 | |
79 | std::string location_bindings() const; |
80 | }; |
81 | |
82 | /** |
83 | The DashboardBuilder is dynamically builds the .slint code that represents the IOT-Dashboard demo |
84 | and allows placing widgets into the top-bar or the main grid. All the properties of the added |
85 | widgets are forwarded and their name prefix is registered with the individual widget instances. |
86 | */ |
87 | struct DashboardBuilder |
88 | { |
89 | void add_grid_widget(WidgetPtr widget, const WidgetLocation &location); |
90 | void add_top_bar_widget(WidgetPtr widget); |
91 | |
92 | std::optional<slint::ComponentHandle<slint::interpreter::ComponentInstance>> |
93 | build(slint::interpreter::ComponentCompiler &compiler) const; |
94 | |
95 | private: |
96 | int register_widget(WidgetPtr widget); |
97 | |
98 | std::unordered_set<std::string> widgets_used = { "TopBar" , "MenuBar" }; |
99 | std::vector<int> top_bar_widgets; |
100 | std::vector<std::pair<int, WidgetLocation>> grid_widgets; |
101 | |
102 | std::vector<std::pair<std::string, WidgetPtr>> widgets; |
103 | }; |
104 | |