forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
103 lines (84 loc) · 3.02 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
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
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
#include "dashboard.h"
#include <chrono>
#include <slint-interpreter.h>
#include <fmt/core.h>
#include <fmt/chrono.h>
#include <random>
#include <time.h>
class PlaceholderWidget : public Widget
{
public:
PlaceholderWidget(std::string_view type_name) : m_type_name(type_name) { }
std::string type_name() const override { return m_type_name; }
std::vector<PropertyDeclaration> properties() const override { return {}; }
private:
std::string m_type_name;
};
class ClockWidget : public Widget
{
public:
ClockWidget();
std::string type_name() const override { return "Clock"; }
std::vector<PropertyDeclaration> properties() const override
{
return { PropertyDeclaration { "time", "string" } };
}
private:
void update_clock();
slint::Timer clock_update_timer;
};
ClockWidget::ClockWidget() : clock_update_timer(std::chrono::seconds(1), [this] { update_clock(); })
{
}
void ClockWidget::update_clock()
{
std::string current_time = fmt::format("{:%H:%M:%S}", fmt::localtime(std::time(nullptr)));
set_property("time", slint::SharedString(current_time));
}
class HumidityWidget : public Widget
{
public:
HumidityWidget();
std::string type_name() const override { return "Humidity"; }
std::vector<PropertyDeclaration> properties() const override
{
return { PropertyDeclaration { "humidity_percent", "int" } };
}
private:
void update_fake_humidity();
slint::Timer fake_humidity_update_timer;
std::default_random_engine rng;
};
HumidityWidget::HumidityWidget()
: fake_humidity_update_timer(std::chrono::seconds(5), [this] { update_fake_humidity(); }),
rng(std::chrono::system_clock::now().time_since_epoch().count())
{
}
void HumidityWidget::update_fake_humidity()
{
std::uniform_int_distribution<> humidity_range(20, 150);
double humidity_percent = humidity_range(rng);
set_property("humidity_percent", humidity_percent);
}
int main()
{
DashboardBuilder builder;
// The widgets and their position is hardcoded for now, but one could imagine getting this
// from a config file, and instantiating the widgets with a factory function
builder.add_top_bar_widget(std::make_shared<ClockWidget>());
builder.add_grid_widget(std::make_shared<PlaceholderWidget>("Usage"), { 0, 0, 2 });
builder.add_grid_widget(std::make_shared<PlaceholderWidget>("IndoorTemperature"), { 0, 1 });
builder.add_grid_widget(std::make_shared<HumidityWidget>(), { 1, 1 });
builder.add_grid_widget(std::make_shared<PlaceholderWidget>("MyDevices"), { 0, 2, 2 });
builder.add_grid_widget(std::make_shared<PlaceholderWidget>("UsageDiagram"), { 2, 0, {}, 2 });
builder.add_grid_widget(std::make_shared<PlaceholderWidget>("LightIntensity"), { 2, 2 });
slint::interpreter::ComponentCompiler compiler;
compiler.set_include_paths({ SOURCE_DIR });
auto dashboard = builder.build(compiler);
if (!dashboard) {
return EXIT_FAILURE;
}
(*dashboard)->run();
}