CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
component_macros.h
Go to the documentation of this file.
1// Copyright (c) 2024, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#ifndef CYD_UI_COMPONENT_MACROS_H
5#define CYD_UI_COMPONENT_MACROS_H
6
7#include <SDL3/SDL_keycode.h>
8#include <cyd_fabric_modules/headers/macros/async_events.h>
10
11#define STATE \
12 ; \
13 \
14public: \
15 struct state_type: public cydui::components::component_state_t
16
17#define EXTENDS(...) , __VA_ARGS__
18
19#define COMPONENT_DECL(NAME, ...) \
20 struct NAME: public cydui::components::component_t<NAME> { \
21 struct event_handler_t; \
22 struct state_type; \
23 struct style_type; \
24 struct props_t __VA_ARGS__; \
25 \
26 public: \
27 static constexpr std::source_location declaration_loc = std::source_location::current(); \
28 props_t props; \
29 using window_type = cydui::CWindow; \
30 using state_t = std::conditional_t< \
31 is_type_complete_v<struct state_type>, \
32 state_type, \
33 cydui::components::component_state_t>; \
34 using style_t = std::conditional_t< \
35 is_type_complete_v<struct style_type>, \
36 style_type, \
37 cydui::components::simple_style_t>; \
38 template <typename P = props_t> \
39 explicit NAME( \
40 std::enable_if_t<std::is_default_constructible_v<P>, props_t> props = {}, \
41 cydui::components::identifier_t identifier = {} \
42 ) \
43 : cydui::components::component_t<NAME>(identifier), \
44 props(std::move(props)) {} \
45 explicit NAME(props_t props, cydui::components::identifier_t identifier = {}) \
46 : cydui::components::component_t<NAME>(identifier), \
47 props(std::move(props)) {} \
48 ~NAME() override = default; \
49 void* get_props() override { \
50 return (void*)&(this->props); \
51 } \
52 auto& style(const std::function<void(style_t&)>& style_transform) { \
53 this->set_style_transform(style_transform); \
54 return *this; \
55 } \
56 friend struct event_handler_t; \
57 friend struct cydui::components::event_handler_data_t<NAME>; \
58 }
59
60#define COMPONENT_IMPL(NAME) \
61 struct NAME::event_handler_t: public cydui::components::event_handler_data_t<NAME>
62
63#define COMPONENT(NAME, ...) \
64 COMPONENT_DECL(NAME, __VA_ARGS__); \
65 COMPONENT_IMPL(NAME)
66
67
68#define TCOMPONENT(NAME, ...) \
69 template NAME##_TEMPLATE struct NAME \
70 : public cydui::components::component_t<NAME NAME##_TEMPLATE_SHORT> { \
71 struct event_handler_t; \
72 struct state_type; \
73 struct style_type; \
74 struct props_t __VA_ARGS__; \
75 \
76 public: \
77 props_t props; \
78 using window_type = cydui::CWindow; \
79 using state_t = std::conditional_t< \
80 is_type_complete_v<struct state_type>, \
81 state_type, \
82 cydui::components::component_state_t>; \
83 using style_t = std::conditional_t< \
84 is_type_complete_v<struct style_type>, \
85 style_type, \
86 cydui::components::simple_style_t>; \
87 template <typename P = props_t> \
88 explicit NAME(std::enable_if_t<std::is_default_constructible_v<P>, props_t> props = {}) \
89 : cydui::components::component_t<NAME NAME##_TEMPLATE_SHORT>(), \
90 props(std::move(props)) {} \
91 explicit NAME(props_t props) \
92 : cydui::components::component_t<NAME NAME##_TEMPLATE_SHORT>(), \
93 props(std::move(props)) {} \
94 ~NAME() override = default; \
95 void* get_props() override { \
96 return (void*)&(this->props); \
97 } \
98 auto& style(std::function<void(style_t&)> style_transform) { \
99 this->set_style_transform(style_transform); \
100 return *this; \
101 } \
102 friend struct event_handler_t; \
103 friend struct cydui::components::event_handler_data_t<NAME NAME##_TEMPLATE_SHORT>; \
104 }; \
105 template NAME##_TEMPLATE struct NAME NAME##_TEMPLATE_SHORT::event_handler_t \
106 : public cydui::components::event_handler_data_t<NAME NAME##_TEMPLATE_SHORT>
107
108
109#define CYDUI_INTERNAL_EV_HANDLER_IMPL(NAME) void on_##NAME CYDUI_INTERNAL_EV_##NAME##_ARGS
110
111#define CYDUI_INTERNAL_EV_HANDLER_IMPL_W_RET(NAME) \
112 CYDUI_INTERNAL_EV_##NAME##_RETURN on_##NAME CYDUI_INTERNAL_EV_##NAME##_ARGS
113
114#define ON_REDRAW CYDUI_INTERNAL_EV_HANDLER_IMPL_W_RET(redraw)
115#define CHILDREN CYDUI_INTERNAL_EV_HANDLER_IMPL_W_RET(redraw)
116#define ON_BUTTON_PRESS CYDUI_INTERNAL_EV_HANDLER_IMPL(button_press)
117#define ON_BUTTON_RELEASE CYDUI_INTERNAL_EV_HANDLER_IMPL(button_release)
118#define ON_MOUSE_ENTER CYDUI_INTERNAL_EV_HANDLER_IMPL(mouse_enter)
119#define ON_MOUSE_EXIT CYDUI_INTERNAL_EV_HANDLER_IMPL(mouse_exit)
120#define ON_MOUSE_MOTION CYDUI_INTERNAL_EV_HANDLER_IMPL(mouse_motion)
121#define ON_SCROLL CYDUI_INTERNAL_EV_HANDLER_IMPL(scroll)
122#define ON_KEY_PRESS CYDUI_INTERNAL_EV_HANDLER_IMPL(key_press)
123#define ON_KEY_RELEASE CYDUI_INTERNAL_EV_HANDLER_IMPL(key_release)
124#define ON_FOCUS_CHANGED CYDUI_INTERNAL_EV_HANDLER_IMPL(focus_changed)
125#define ON_TEXT_INPUT \
126 static constexpr bool handles_text_input = true; \
127 CYDUI_INTERNAL_EV_HANDLER_IMPL(text_input)
128
129#define ON_EVENT(EVENT, ...) \
130 custom_event_listener<EVENT> on_##EVENT{ \
131 &this->window, \
132 [&](const EVENT& event) -> fabric::task<> { \
133 __VA_ARGS__; \
134 co_return; \
135 }, \
136 [&]() -> fabric::task<> { \
137 this->state.force_redraw(); \
138 co_return; \
139 } \
140 };
141
142#define FRAGMENT void draw_fragment CYDUI_INTERNAL_EV_fragment_ARGS
143
144#define SIGNAL(NAME, ...) \
145private: \
146 fabric::wiring::signal<__VA_ARGS__> NAME{}; \
147 \
148public: \
149 auto& on_##NAME(auto&& fun) { \
150 NAME.connect(fun); \
151 return *this; \
152 }
153
154#define ATTRIBUTE(NAME, ...) \
155public: \
156 auto& NAME(const __VA_ARGS__& value) { \
157 NAME##_ = value; \
158 return *this; \
159 } \
160 \
161private: \
162 __VA_ARGS__ NAME##_
163
164#define STYLE struct style_type: cydui::components::style_base_t
165
166
168
169#define ANONYMOUS_STRUCT(...) \
170 decltype([&] { \
171 struct _anon_ __VA_ARGS__; \
172 return _anon_{}; \
173 }())
174
175#define KEYFRAME(POS, ...) cydui::keyframe::make(POS, __VA_ARGS__)
176#define AUTO_KEYFRAME(POS, ...) cydui::keyframe::make(POS, ANONYMOUS_STRUCT(__VA_ARGS__){})
177
178#endif // CYD_UI_COMPONENT_MACROS_H