Esp32-RBGridUI
Library for creating UIs for the RBController app
Loading...
Searching...
No Matches
builder
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
24
namespace
gridui
{
25
26
class
_GridUi;
27
28
extern
_GridUi
UI
;
29
30
namespace
builder {
31
32
template
<
typename
Self,
typename
Constructed>
33
class
BuilderMixin
{
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
40
public
:
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
53
protected
:
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
59
private
:
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
74
class
Widget
{
75
friend
class
gridui::_GridUi
;
76
77
template
<
typename
Self,
typename
Finished>
78
friend
class
BuilderMixin
;
79
80
public
:
81
Widget
(
Widget
&& o)
noexcept
;
82
83
const
char
*
widgetTypeName
()
const
{
return
m_type; }
84
85
protected
:
86
Widget
(
const
char
* type,
WidgetState
& state);
87
88
void
serialize
(std::ostream& ss);
89
90
rbjson::Object&
extra
();
91
rbjson::Object&
style
();
92
93
WidgetState
&
m_state
;
94
95
private
:
96
Widget
(
const
Widget
&) =
delete
;
97
Widget
& operator=(
const
Widget
&) =
delete
;
98
99
const
char
* m_type;
100
};
101
102
};
103
};
gridui::WidgetState
Definition
widget.h:99
gridui::_GridUi
Definition
gridui.h:42
gridui::builder::BuilderMixin
Definition
widget.h:33
gridui::builder::BuilderMixin::css
Self & css(const std::string &key, const std::string &value)
Definition
widget.h:43
gridui::builder::BuilderMixin::callback_t
std::function< void(Constructed &)> callback_t
Definition
widget.h:41
gridui::builder::BuilderMixin::addCallback
void addCallback(const std::string &name, const callback_t &cb)
Definition
widget.h:54
gridui::builder::BuilderMixin::finish
Constructed finish()
Definition
widget.h:49
gridui::builder::Widget
Definition
widget.h:74
gridui::builder::Widget::extra
rbjson::Object & extra()
Definition
widget.cpp:16
gridui::builder::Widget::m_state
WidgetState & m_state
Definition
widget.h:93
gridui::builder::Widget::Widget
Widget(Widget &&o) noexcept
gridui::builder::Widget::serialize
void serialize(std::ostream &ss)
Definition
widget.cpp:29
gridui::builder::Widget::style
rbjson::Object & style()
Definition
widget.cpp:20
gridui::builder::Widget::widgetTypeName
const char * widgetTypeName() const
Definition
widget.h:83
gridui
Definition
arm.h:8
gridui::UI
_GridUi UI
Definition
gridui.cpp:14
Generated on Tue Jul 9 2024 10:04:33 for Esp32-RBGridUI by
1.9.8