NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
element.hpp
浏览该文件的文档.
1#ifndef NEFORCE_TUI_DOM_ELEMENT_HPP__
2#define NEFORCE_TUI_DOM_ELEMENT_HPP__
3
11
16NEFORCE_BEGIN_NAMESPACE__
17NEFORCE_BEGIN_TUI__
18
23
25template <typename T>
26class state;
28
29
37class NEFORCE_API element {
38public:
42 enum class kind : uint8_t {
43 empty,
44 vbox,
45 hbox,
46 zstack,
47 text,
48 spacer,
49 separator,
50 button,
51 text_input,
52 checkbox,
53 scroll_view,
59 };
60
61 element();
62 ~element();
63
64 element(const element& other);
65 element& operator=(const element& other);
66 element(element&& other) noexcept;
67 element& operator=(element&& other) noexcept;
68
74 element& with_key(size_t k);
75
76 NEFORCE_NODISCARD kind kind() const noexcept;
77 NEFORCE_NODISCARD size_t key() const noexcept;
78 NEFORCE_NODISCARD const vector<element>& children() const;
79 NEFORCE_NODISCARD vector<element>& children();
80 NEFORCE_NODISCARD const tui::style& style() const;
81 NEFORCE_NODISCARD const box_props& layout() const;
82 NEFORCE_NODISCARD const string& text() const;
83 NEFORCE_NODISCARD const function<void()>& on_click() const;
84 NEFORCE_NODISCARD int flex() const noexcept;
85 NEFORCE_NODISCARD void* state_ref() const noexcept;
86 NEFORCE_NODISCARD style::variant variant() const noexcept;
87 NEFORCE_NODISCARD const function<void(int, int, int, int)>& draw_function() const;
88
89 NEFORCE_NODISCARD void* owner() const noexcept;
90 void set_owner(void* o);
91
92 NEFORCE_NODISCARD int scroll_x() const noexcept;
93 NEFORCE_NODISCARD int scroll_y() const noexcept;
94
95 NEFORCE_NODISCARD int grid_columns() const noexcept;
96
97 NEFORCE_NODISCARD const string& placeholder() const noexcept;
98
99 NEFORCE_NODISCARD bool cursor_visible() const noexcept;
100 element& with_cursor_visible(bool v);
101
102 NEFORCE_NODISCARD size_t cursor_pos() const noexcept;
103 element& with_cursor_pos(size_t pos);
104
105 NEFORCE_NODISCARD style::wrap_mode wrap_mode() const noexcept;
106
107 NEFORCE_NODISCARD void* scroll_x_state() const noexcept;
108 element& with_scroll_x_state(void* s);
109 NEFORCE_NODISCARD void* scroll_y_state() const noexcept;
110 element& with_scroll_y_state(void* s);
111
112 NEFORCE_NODISCARD bool is_layout_dirty() const noexcept;
113 void set_layout_dirty(bool dirty);
114
115 NEFORCE_NODISCARD const vector<layout_rect>& cached_layout() const noexcept;
116 void set_cached_layout(const vector<layout_rect>& layout, int constraint_w, int constraint_h) const;
117 NEFORCE_NODISCARD int cached_constraint_w() const noexcept;
118 NEFORCE_NODISCARD int cached_constraint_h() const noexcept;
119
120 static element empty();
121 static element vbox(vector<element> children, box_props props = {});
122 static element hbox(vector<element> children, box_props props = {});
123 static element zstack(vector<element> children);
124 static element text(string content, tui::style style = {}, style::wrap_mode wrap = style::wrap_mode::none);
125 static element spacer(int flex = 1);
126 static element separator();
127 static element button(string label, function<void()> on_click, tui::style style = {},
128 tui::style::variant variant = tui::style::variant::default_);
129 static element text_input(state<string>& text, tui::style style = {}, string placeholder = "");
130 static element checkbox(string label, state<bool>& checked, tui::style style = {});
131 static element scroll_view(element child, tui::style style = {}, int scroll_x = 0, int scroll_y = 0);
132
138 element& with_style(const tui::style& s);
139
141 static element canvas(function<void(int, int, int, int)> draw_function, tui::style style = {});
142
149 static element flexbox(vector<element> children, box_props config = {});
150
156 static element gridbox(vector<vector<element>> grid);
157
165 static element
166 when(bool cond, function<element()> then, function<element()> otherwise = []() { return element::empty(); });
167
176 template <typename T>
177 static element each(const vector<T>& items, function<element(const T&, size_t)> render) {
178 element el;
179 el.init_kind(kind::each);
180 el.reserve_children(items.size());
181 for (size_t i = 0; i < items.size(); ++i) {
182 el.add_child(render(items[i], i));
183 }
184 return el;
185 }
186
187private:
188 struct node;
189 shared_ptr<node> node_;
190
191 void ensure_node();
192 void init_kind(enum kind k);
193 void reserve_children(size_t n);
194 void add_child(element child);
195};
196
203
208
209
216inline element operator|(element e, const decorator& d) { return d(move(e)); }
217
224inline element& operator|=(element& e, const decorator& d) {
225 e = d(move(e));
226 return e;
227}
228
236 vector<element> result;
237 result.reserve(es.size());
238 for (const auto& e: es) {
239 result.push_back(d(e));
240 }
241 return result;
242}
243
251 return [a = move(a), b = move(b)](element e) { return a(b(move(e))); };
252}
253
254
256inline decorator bold() {
257 return [](element e) {
258 style s;
259 s.bold = true;
260 return e.with_style(s);
261 };
262}
263
265inline decorator dim() {
266 return [](element e) {
267 style s;
268 s.dim = true;
269 return e.with_style(s);
270 };
271}
272
275 return [](element e) {
276 style s;
277 s.italic = true;
278 return e.with_style(s);
279 };
280}
281
284 return [](element e) {
285 style s;
286 s.underline = true;
287 return e.with_style(s);
288 };
289}
290
293 return [](element e) {
294 style s;
295 s.underlined_double = true;
296 return e.with_style(s);
297 };
298}
299
301inline decorator blink() {
302 return [](element e) {
303 style s;
304 s.blink = true;
305 return e.with_style(s);
306 };
307}
308
311 return [](element e) {
312 style s;
313 s.strikethrough = true;
314 return e.with_style(s);
315 };
316}
317
320 return [](element e) {
321 style s;
322 s.reverse = true;
323 return e.with_style(s);
324 };
325}
326
330 return [c = move(c)](element e) {
331 style s;
332 s.fg = c;
333 return e.with_style(s);
334 };
335}
336
339inline decorator bgcolor(_NEFORCE color c) {
340 return [c = move(c)](element e) {
341 style s;
342 s.bg = c;
343 return e.with_style(s);
344 };
345}
346
350 return [b](element e) {
351 style s;
352 s.border = b;
353 return e.with_style(s);
354 };
355}
356
359inline decorator flex_grow_el(float factor) {
360 return [factor](element e) {
361 style s;
362 s.flex_grow = factor;
363 return e.with_style(s);
364 };
365}
366
369inline decorator flex_shrink_el(float factor) {
370 return [factor](element e) {
371 style s;
372 s.flex_shrink = factor;
373 return e.with_style(s);
374 };
375}
376
380inline decorator flex_el(float grow, float shrink) {
381 return [grow, shrink](element e) {
382 style s;
383 s.flex_grow = grow;
384 s.flex_shrink = shrink;
385 return e.with_style(s);
386 };
387}
388
392 return [wm](element e) {
393 style s;
394 s.text_wrap = wm;
395 return e.with_style(s);
396 };
397}
398 // TUI
400
401NEFORCE_END_TUI__
402NEFORCE_END_NAMESPACE__
403#endif // NEFORCE_TUI_DOM_ELEMENT_HPP__
函数包装器主模板声明
共享智能指针类模板
element & with_style(const tui::style &s)
合并样式
static element canvas(function< void(int, int, int, int)> draw_function, tui::style style={})
自定义绘制区域
kind
元素类型枚举
static element when(bool cond, function< element()> then, function< element()> otherwise=[]() { return element::empty();})
条件渲染
static element each(const vector< T > &items, function< element(const T &, size_t)> render)
列表渲染
element & with_key(size_t k)
分配列表 key,帮助 Reconciler 追踪元素身份
static element gridbox(vector< vector< element > > grid)
网格布局容器
static element flexbox(vector< element > children, box_props config={})
CSS-flexbox 弹性容器
动态大小数组容器
constexpr size_type size() const noexcept
获取当前元素数量
constexpr void reserve(const size_type n)
预留容量
constexpr void push_back(const T &value)
在末尾拷贝插入元素
通用函数包装器
unique_ptr< component_base > scroll_view(scroll_view_option opt)
创建滚动视图组件
unique_ptr< component_base > text_input(text_input_option opt)
创建文本输入组件
unsigned char uint8_t
8位无符号整数类型
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
decorator flex_grow_el(float factor)
弹性增长装饰器
decorator border(enum style::border b)
边框装饰器
decorator italic()
斜体装饰器
decorator bold()
粗体装饰器
decorator bgcolor(_NEFORCE color c)
背景色装饰器
decorator strikethrough()
删除线装饰器
decorator blink()
闪烁装饰器
function< element(element)> decorator
元素装饰器
decorator color(color c)
前景色装饰器
decorator underlined_double()
双下划线装饰器
vector< element > elements
元素列表类型
decorator dim()
暗色装饰器
decorator inverted()
反色装饰器
decorator text_wrap(style::wrap_mode wm)
文本换行装饰器
decorator underlined()
单下划线装饰器
element & operator|=(element &e, const decorator &d)
就地装饰元素
decorator flex_shrink_el(float factor)
弹性收缩装饰器
element operator|(element e, const decorator &d)
将装饰器应用于元素
decorator flex_el(float grow, float shrink)
弹性装饰器
布局基础类型
共享智能指针实现
布局容器属性
optional< bool > underline
单下划线
optional< color > fg
前景色
optional< float > flex_grow
弹性增长权重
optional< bool > underlined_double
双下划线
optional< bool > blink
闪烁
optional< bool > dim
暗色
optional< bool > reverse
反色
wrap_mode
文本换行模式
optional< float > flex_shrink
弹性收缩权重
optional< wrap_mode > text_wrap
文本换行模式
optional< bool > strikethrough
删除线
optional< bool > italic
斜体
optional< color > bg
背景色
optional< bool > bold
粗体
TUI样式与主题系统