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
28namespace builder {
29
30template <typename Self, typename Constructed>
32 // The Self is not fully defined yet here, so the check won't compile.
33 //static_assert(std::is_base_of<Widget, Self>::value, "Self must inherit from Widget.");
34 static_assert(std::is_base_of<gridui::Widget, Constructed>::value, "Constructed must inherit from gridui::Widget.");
35
36 friend class gridui::_GridUi;
37
38public:
39 typedef std::function<void(Constructed&)> callback_t;
40
41 Self& css(const std::string& key, const std::string& value) {
42 auto& s = self();
43 s.style().set(key, value);
44 return s;
45 }
46
47 Constructed finish() {
48 return Constructed(&self().m_state);
49 }
50
51protected:
52 void addCallback(const std::string& name, callback_t cb) {
53 auto* cbHeap = static_cast<void*>(new callback_t(cb)); // fuj
54 self().m_state.addCallback(&callbackTrampoline, &callbackDeleter, name, cbHeap);
55 }
56
57private:
58 Self& self() { return *static_cast<Self*>(this); }
59 const Self& self() const { return *static_cast<Self*>(this); }
60
61 static void callbackTrampoline(void* cb, WidgetState* state) {
62 Constructed w(state);
63 (*static_cast<callback_t*>(cb))(w);
64 }
65
66 static void callbackDeleter(void *cb) {
67 auto *cb_typed = static_cast<callback_t*>(cb);
68 delete cb_typed;
69 }
70};
71
72class Widget {
73 friend class gridui::_GridUi;
74
75 template <typename Self, typename Finished>
76 friend class BuilderMixin;
77
78public:
79 Widget(Widget&& o) noexcept;
80 virtual ~Widget();
81
82protected:
83 Widget(const char* type, WidgetState& state);
84
85 virtual void serialize(std::stringstream& ss);
86
87 rbjson::Object& extra();
88 rbjson::Object& style();
89
91
92private:
93 Widget(const Widget&) = delete;
94 Widget& operator=(const Widget&) = delete;
95
96 const char* m_type;
97 rbjson::Object* m_style;
98};
99
100};
101};
Self & css(const std::string &key, const std::string &value)
Definition widget.h:41
void addCallback(const std::string &name, callback_t cb)
Definition widget.h:52
std::function< void(Constructed &)> callback_t
Definition widget.h:39
rbjson::Object & extra()
Definition widget.cpp:20
WidgetState & m_state
Definition widget.h:90
virtual void serialize(std::stringstream &ss)
Definition widget.cpp:33
Widget(Widget &&o) noexcept
rbjson::Object & style()
Definition widget.cpp:24
Definition arm.h:8