| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | #include "dashboard.h" |
| 5 | #include <chrono> |
| 6 | #include <slint-interpreter.h> |
| 7 | |
| 8 | #include <fmt/core.h> |
| 9 | #include <fmt/chrono.h> |
| 10 | #include <random> |
| 11 | #include <time.h> |
| 12 | |
| 13 | class PlaceholderWidget : public Widget |
| 14 | { |
| 15 | public: |
| 16 | PlaceholderWidget(std::string_view type_name) : m_type_name(type_name) { } |
| 17 | |
| 18 | std::string type_name() const override { return m_type_name; } |
| 19 | std::vector<PropertyDeclaration> properties() const override { return {}; } |
| 20 | |
| 21 | private: |
| 22 | std::string m_type_name; |
| 23 | }; |
| 24 | |
| 25 | class ClockWidget : public Widget |
| 26 | { |
| 27 | public: |
| 28 | ClockWidget(); |
| 29 | std::string type_name() const override { return "Clock" ; } |
| 30 | std::vector<PropertyDeclaration> properties() const override |
| 31 | { |
| 32 | return { PropertyDeclaration { .name: "time" , .type_name: "string" } }; |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | void update_clock(); |
| 37 | |
| 38 | slint::Timer clock_update_timer; |
| 39 | }; |
| 40 | |
| 41 | ClockWidget::ClockWidget() : clock_update_timer(std::chrono::seconds(1), [this] { update_clock(); }) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void ClockWidget::update_clock() |
| 46 | { |
| 47 | std::string current_time = fmt::format("{:%H:%M:%S}" , fmt::localtime(std::time(nullptr))); |
| 48 | set_property("time" , slint::SharedString(current_time)); |
| 49 | } |
| 50 | |
| 51 | class HumidityWidget : public Widget |
| 52 | { |
| 53 | public: |
| 54 | HumidityWidget(); |
| 55 | std::string type_name() const override { return "Humidity" ; } |
| 56 | std::vector<PropertyDeclaration> properties() const override |
| 57 | { |
| 58 | return { PropertyDeclaration { .name: "humidity_percent" , .type_name: "int" } }; |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | void update_fake_humidity(); |
| 63 | slint::Timer fake_humidity_update_timer; |
| 64 | std::default_random_engine rng; |
| 65 | }; |
| 66 | |
| 67 | HumidityWidget::HumidityWidget() |
| 68 | : fake_humidity_update_timer(std::chrono::seconds(5), [this] { update_fake_humidity(); }), |
| 69 | rng(std::chrono::system_clock::now().time_since_epoch().count()) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void HumidityWidget::update_fake_humidity() |
| 74 | { |
| 75 | std::uniform_int_distribution<> humidity_range(20, 150); |
| 76 | double humidity_percent = humidity_range(rng); |
| 77 | set_property("humidity_percent" , humidity_percent); |
| 78 | } |
| 79 | |
| 80 | int main() |
| 81 | { |
| 82 | DashboardBuilder builder; |
| 83 | |
| 84 | // The widgets and their position is hardcoded for now, but one could imagine getting this |
| 85 | // from a config file, and instantiating the widgets with a factory function |
| 86 | builder.add_top_bar_widget(std::make_shared<ClockWidget>()); |
| 87 | builder.add_grid_widget(widget: std::make_shared<PlaceholderWidget>(args: "Usage" ), location: { .row: 0, .column: 0, .row_span: 2 }); |
| 88 | builder.add_grid_widget(widget: std::make_shared<PlaceholderWidget>(args: "IndoorTemperature" ), location: { .row: 0, .column: 1 }); |
| 89 | builder.add_grid_widget(std::make_shared<HumidityWidget>(), { 1, 1 }); |
| 90 | builder.add_grid_widget(widget: std::make_shared<PlaceholderWidget>(args: "MyDevices" ), location: { .row: 0, .column: 2, .row_span: 2 }); |
| 91 | builder.add_grid_widget(widget: std::make_shared<PlaceholderWidget>(args: "UsageDiagram" ), location: { .row: 2, .column: 0, .row_span: {}, .col_span: 2 }); |
| 92 | builder.add_grid_widget(widget: std::make_shared<PlaceholderWidget>(args: "LightIntensity" ), location: { .row: 2, .column: 2 }); |
| 93 | |
| 94 | slint::interpreter::ComponentCompiler compiler; |
| 95 | compiler.set_include_paths({ SOURCE_DIR }); |
| 96 | auto dashboard = builder.build(compiler); |
| 97 | |
| 98 | if (!dashboard) { |
| 99 | return EXIT_FAILURE; |
| 100 | } |
| 101 | |
| 102 | (*dashboard)->run(); |
| 103 | } |
| 104 | |