CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
window.cppm
Go to the documentation of this file.
1
5
6module;
7#include <cyd_fabric_modules/headers/macros/async_events.h>
8#include <tracy/Tracy.hpp>
9#define SDL_MAIN_HANDLED
10#include <SDL3/SDL.h>
11
12export module cydui:window;
13
14import std;
15import fabric.logging;
16import fabric.async;
17import fabric.profiling;
18
19export import cydui.graphics;
20export import cydui.window_events;
21export import cydui.styling;
22export import cydui.application;
23export import cydui.animations;
24
25export namespace cydui {
26 class CWindow;
27 class Layout;
28}
29
30namespace cydui {
31 void bind_layout(cydui::Layout* layout, const std::shared_ptr<cydui::CWindow> &window);
32}
33
34export namespace cydui {
35 class CWindow: public fabric::async::async_bus_t {
36 CWindow(
38 std::string title,
39 int x,
40 int y,
41 int width,
42 int height
43 );
44
45 public:
46 using sptr = std::shared_ptr<CWindow>;
47
48 ~CWindow();
49
50 template <typename... Args>
51 auto run_async(auto&& fun, Args&&... args) {
52 ZoneScopedN("Application:run_async");
53 return get_executor()->schedule(
54 [=](Args... argss) -> fabric::task<decltype(fun(std::forward<Args>(argss)...))> {
55 ZoneScopedN("Application:run_async:()");
56 co_return fun(std::forward<Args>(argss)...);
57 },
58 std::forward<Args>(args)...
59 );
60 }
61
62 template <typename... Args>
63 auto run(auto&& fun, Args&&... args) {
64 ZoneScopedN("Application:run");
65 return run_async(fun, std::forward<Args>(args)...).get();
66 }
67
68 template <typename... Args>
69 auto schedule(auto&& fun, Args&&... args) {
70 ZoneScopedN("Application:run_async");
71 return get_executor()->schedule(fun, std::forward<Args>(args)...);
72 }
73
74 template <typename... Args>
75 auto schedule(fabric::tasks::time_point tp, auto&& fun, Args&&... args) {
76 ZoneScopedN("Application:run_async");
77 return get_executor()->schedule(tp, fun, std::forward<Args>(args)...);
78 }
79
80 template <typename... Args>
81 auto schedule(fabric::tasks::duration duration, auto&& fun, Args&&... args) {
82 ZoneScopedN("Application:run_async");
83 return get_executor()->schedule(duration, fun, std::forward<Args>(args)...);
84 }
85
86
87 struct builder_t {
89 }
90
91 builder_t(const builder_t &) = delete;
92
93 builder_t &position(int x, int y) {
94 x_ = x;
95 y_ = y;
96 return *this;
97 }
98
99 builder_t &size(int width, int height) {
100 width_ = width;
101 height_ = height;
102 return *this;
103 }
104
105 builder_t &title(const std::string &title) {
106 title_ = title;
107 return *this;
108 }
109
110 builder_t &style(const std::string &style) {
111 styles_.push_back(style);
112 return *this;
113 }
114
115 builder_t &stylesheet(const std::filesystem::path &stylesheet) {
116 stylesheets_.push_back(stylesheet);
117 return *this;
118 }
119
120
121 private:
122 void configure_layout_style();
123
124 public:
126 ZoneScopedN("CWindow:builder:show");
127 configure_layout_style();
128 auto ptr = std::shared_ptr<CWindow>(new CWindow(layout_, title_, x_, y_, width_, height_));
129 std::string t = title_;
130 ptr->run([=] {
131 tracy::SetThreadNameWithHint(std::format("window[{}]", t).c_str(), 1);
132 });
133 ptr->schedule([](Layout* lyt, sptr win) -> fabric::task<> {
134 bind_layout(lyt, win);
135 LOG::print {INFO}("Layout bound to window");
136 co_return;
137 }, layout_, ptr);
138 return ptr;
139 }
140
141 private:
142 Layout* layout_ = nullptr;
143 int x_ = SDL_WINDOWPOS_UNDEFINED;
144 int y_ = SDL_WINDOWPOS_UNDEFINED;
145 int width_ = 1280;
146 int height_ = 720;
147 std::string title_ = "CYD-UI";
148 std::vector<std::string> styles_ = {};
149 std::vector<std::filesystem::path> stylesheets_ = {};
150 };
151
153 return builder_t(layout);
154 }
155
156 template<typename Component>
157 static builder_t make(typename Component::props_t props = { });
158
159 graphics::window_t* native();
160
161 std::unique_ptr<graphics::window_t> win_ref;
163
164 prof::context_t profiling_ctx { };
165
166 void terminate();
167
168 bool is_open() const;
169
170 std::pair<int, int> get_position();
171
172 std::pair<int, int> get_size();
173
174
175
177 };
178
179 //* Implementation
180
181 CWindow::CWindow(
182 Layout* layout,
183 std::string title,
184 int x,
185 int y,
186 int width,
187 int height
188 ): win_ref(std::make_unique<graphics::window_t>(this, nullptr, width, height)),
189 layout(layout) {
190 using namespace std::chrono_literals;
191 ZoneScopedN("CWindow{}");
192
193 get_executor()->get_spawn_context()->set_resource<AnimationSystem>(std::make_shared<AnimationSystem>(get_executor()));
194
195 run([=,this] {
196 Application::run([=](CWindow* self) {
197 ZoneScopedN("CWindow{}:init");
198 SDL_CreateWindowAndRenderer(
199 title.c_str(),
200 width,
201 height,
202 SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL,
203 &self->win_ref->window,
204 &self->win_ref->renderer
205 );
206 SDL_SetWindowPosition(self->win_ref->window, x, y);
207 // SDL_FlashWindow(win_ref->window, SDL_FLASH_UNTIL_FOCUSED);
208
209 Application::register_window(self->win_ref->window_id(), self);
210
211 self->compositor.set_render_target(self->win_ref.get(), &self->profiling_ctx);
212 LOG::print {INFO}("SDL3 Window initialized.");
213 }, this);
214 });
215 }
216
220
222 return win_ref.get();
223 }
224
226 emit<fabric::async::StopBusEvent>();
227 Application::run([&]() {
228 SDL_DestroyWindow(win_ref->window);
229 SDL_DestroyRenderer(win_ref->renderer);
230 win_ref.reset();
231 });
232 }
233
234 bool CWindow::is_open() const {
235 return win_ref != nullptr;
236 }
237
238 std::pair<int, int> CWindow::get_position() {
239 int x, y;
240 Application::run([](int* x, int* y, CWindow* self) {
241 SDL_GetWindowPosition(self->win_ref->window, x, y);
242 }, &x, &y, this);
243 return {x, y};
244 }
245
246 std::pair<int, int> CWindow::get_size() {
247 int w, h;
248 Application::run([](int* w, int* h, CWindow* self) {
249 SDL_GetCurrentRenderOutputSize(self->win_ref->renderer, w, h);
250 }, &w, &h, this);
251 return {w, h};
252 }
253}
static auto run(auto &&fun, Args &&... args)
static void register_window(std::size_t id, fabric::async::async_bus_t *window)
static void unregister_window(std::size_t id)
Layout * layout
Definition window.cppm:162
std::shared_ptr< CWindow > sptr
Definition window.cppm:46
static builder_t make(Layout *layout)
Definition window.cppm:152
auto schedule(fabric::tasks::duration duration, auto &&fun, Args &&... args)
Definition window.cppm:81
graphics::window_t * native()
Definition window.cppm:221
std::unique_ptr< graphics::window_t > win_ref
Definition window.cppm:161
auto schedule(fabric::tasks::time_point tp, auto &&fun, Args &&... args)
Definition window.cppm:75
prof::context_t profiling_ctx
Definition window.cppm:164
bool is_open() const
Definition window.cppm:234
std::pair< int, int > get_size()
Definition window.cppm:246
compositing::LayoutCompositor compositor
Definition window.cppm:176
auto run(auto &&fun, Args &&... args)
Definition window.cppm:63
void terminate()
Definition window.cppm:225
auto run_async(auto &&fun, Args &&... args)
Definition window.cppm:51
std::pair< int, int > get_position()
Definition window.cppm:238
auto schedule(auto &&fun, Args &&... args)
Definition window.cppm:69
void bind_layout(Layout *layout, const CWindow::sptr &window)
Definition layout.cppm:461
builder_t & position(int x, int y)
Definition window.cppm:93
builder_t & size(int width, int height)
Definition window.cppm:99
builder_t(Layout *layout)
Definition window.cppm:88
builder_t(const builder_t &)=delete
builder_t & stylesheet(const std::filesystem::path &stylesheet)
Definition window.cppm:115
builder_t & title(const std::string &title)
Definition window.cppm:105
builder_t & style(const std::string &style)
Definition window.cppm:110