Esp32-RBGridUI
Library for creating UIs for the RBController app
Loading...
Searching...
No Matches
widget.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <string>
6#include <type_traits>
7
8#include <stdint.h>
9
10#include "rbjson.h"
11
12#include "../widgets/widget.h"
13
24namespace gridui {
25
26class _GridUi;
27
28extern _GridUi UI;
29
30namespace builder {
31
32template <typename Self, typename Constructed>
34 // The Self is not fully defined yet here, so the check won't compile.
35 //static_assert(std::is_base_of<Widget, Self>::value, "Self must inherit from Widget.");
36 static_assert(std::is_base_of<gridui::Widget, Constructed>::value, "Constructed must inherit from gridui::Widget.");
37
38 friend class gridui::_GridUi;
39
40public:
41 typedef std::function<void(Constructed&)> callback_t;
42
43 Self& css(const std::string& key, const std::string& value) {
44 auto& s = self();
45 s.style().set(key, value);
46 return s;
47 }
48
49 Constructed finish() {
50 return Constructed(&self().m_state);
51 }
52
53protected:
54 void addCallback(const std::string& name, const callback_t& cb) {
55 auto* cbHeap = static_cast<void*>(new callback_t(cb)); // fuj
56 self().m_state.addCallback(&callbackTrampoline, &callbackDeleter, name, cbHeap);
57 }
58
59private:
60 Self& self() { return *static_cast<Self*>(this); }
61 const Self& self() const { return *static_cast<Self*>(this); }
62
63 static void callbackTrampoline(void* cb, WidgetState* state) {
64 Constructed w(state);
65 (*static_cast<callback_t*>(cb))(w);
66 }
67
68 static void callbackDeleter(void *cb) {
69 auto *cb_typed = static_cast<callback_t*>(cb);
70 delete cb_typed;
71 }
72};
73
74class Widget {
75 friend class gridui::_GridUi;
76
77 template <typename Self, typename Finished>
78 friend class BuilderMixin;
79
80public:
81 Widget(Widget&& o) noexcept;
82
83 const char *widgetTypeName() const { return m_type; }
84
85protected:
86 Widget(const char* type, WidgetState& state);
87
88 void serialize(std::ostream& ss);
89
90 rbjson::Object& extra();
91 rbjson::Object& style();
92
94
95private:
96 Widget(const Widget&) = delete;
97 Widget& operator=(const Widget&) = delete;
98
99 const char* m_type;
100};
101
102};
103};
Self & css(const std::string &key, const std::string &value)
Definition widget.h:43
std::function< void(Constructed &)> callback_t
Definition widget.h:41
void addCallback(const std::string &name, const callback_t &cb)
Definition widget.h:54
rbjson::Object & extra()
Definition widget.cpp:16
WidgetState & m_state
Definition widget.h:93
Widget(Widget &&o) noexcept
void serialize(std::ostream &ss)
Definition widget.cpp:29
rbjson::Object & style()
Definition widget.cpp:20
const char * widgetTypeName() const
Definition widget.h:83
Definition arm.h:8
_GridUi UI
Definition gridui.cpp:14