aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-11-27 17:39:31 +0800
committerYuqian Yang <crupest@crupest.life>2025-11-27 17:39:31 +0800
commit97ca464fc9e9da4e20e3320559a6f4ac3203fe84 (patch)
treefa23dd3d31fd6c86814ef4505ae2f7966b840ed2
parent6e8d570e3d63b5bc4e24d258d2dd383530ace2a3 (diff)
downloadcru-97ca464fc9e9da4e20e3320559a6f4ac3203fe84.tar.gz
cru-97ca464fc9e9da4e20e3320559a6f4ac3203fe84.tar.bz2
cru-97ca464fc9e9da4e20e3320559a6f4ac3203fe84.zip
Clean code. Use event define macro of native window on linux.
-rw-r--r--include/cru/platform/gui/sdl/UiApplication.h3
-rw-r--r--include/cru/platform/gui/sdl/Window.h34
-rw-r--r--include/cru/platform/gui/xcb/Window.h32
-rw-r--r--src/platform/gui/sdl/UiApplication.cpp14
-rw-r--r--src/platform/gui/sdl/Window.cpp56
-rw-r--r--src/platform/gui/xcb/Window.cpp83
6 files changed, 48 insertions, 174 deletions
diff --git a/include/cru/platform/gui/sdl/UiApplication.h b/include/cru/platform/gui/sdl/UiApplication.h
index c300568c..be5ae102 100644
--- a/include/cru/platform/gui/sdl/UiApplication.h
+++ b/include/cru/platform/gui/sdl/UiApplication.h
@@ -38,6 +38,8 @@ class SdlUiApplication : public SdlResource, public virtual IUiApplication {
std::function<void()> action) override;
void CancelTimer(long long id) override;
+ void DeleteLater(Object* object) override;
+
std::vector<INativeWindow*> GetAllWindow() override;
INativeWindow* CreateWindow() override;
@@ -64,6 +66,7 @@ class SdlUiApplication : public SdlResource, public virtual IUiApplication {
bool release_graphics_factory_;
std::uint32_t empty_event_type_;
+ DeleteLaterPool delete_later_pool_;
TimerRegistry<std::function<void()>> timers_;
std::atomic_int quit_code_;
std::vector<std::function<void()>> quit_handlers_;
diff --git a/include/cru/platform/gui/sdl/Window.h b/include/cru/platform/gui/sdl/Window.h
index 8ffadd02..78fbaa93 100644
--- a/include/cru/platform/gui/sdl/Window.h
+++ b/include/cru/platform/gui/sdl/Window.h
@@ -1,11 +1,9 @@
#pragma once
-#include <cru/base/Base.h>
#include "../../GraphicsBase.h"
#include "../Window.h"
#include "Base.h"
#include <SDL3/SDL_video.h>
-#include <cstddef>
#include <optional>
namespace cru::platform::gui::sdl {
@@ -59,21 +57,7 @@ class SdlWindow : public SdlResource, public virtual INativeWindow {
std::unique_ptr<graphics::IPainter> BeginPaint() override;
- IEvent<std::nullptr_t>* CreateEvent() override;
- IEvent<std::nullptr_t>* DestroyEvent() override;
- IEvent<std::nullptr_t>* PaintEvent() override;
-
- IEvent<WindowVisibilityType>* VisibilityChangeEvent() override;
- IEvent<const Size&>* ResizeEvent() override;
- IEvent<FocusChangeType>* FocusEvent() override;
-
- IEvent<MouseEnterLeaveType>* MouseEnterLeaveEvent() override;
- IEvent<const Point&>* MouseMoveEvent() override;
- IEvent<const NativeMouseButtonEventArgs&>* MouseDownEvent() override;
- IEvent<const NativeMouseButtonEventArgs&>* MouseUpEvent() override;
- IEvent<const NativeMouseWheelEventArgs&>* MouseWheelEvent() override;
- IEvent<const NativeKeyEventArgs&>* KeyDownEvent() override;
- IEvent<const NativeKeyEventArgs&>* KeyUpEvent() override;
+ CRU_DEFINE_CRU_PLATFORM_GUI_I_NATIVE_WINDOW_OVERRIDE_EVENTS()
IInputMethodContext* GetInputMethodContext() override;
@@ -86,19 +70,5 @@ class SdlWindow : public SdlResource, public virtual INativeWindow {
std::optional<SDL_Window*> sdl_window_;
SdlWindow* parent_;
WindowStyleFlag style_;
-
- Event<std::nullptr_t> create_event_;
- Event<std::nullptr_t> destroy_event_;
- Event<std::nullptr_t> paint_event_;
- Event<WindowVisibilityType> visibility_change_event_;
- Event<const Size&> resize_event_;
- Event<FocusChangeType> focus_event_;
- Event<MouseEnterLeaveType> mouse_enter_leave_event_;
- Event<const Point&> mouse_move_event_;
- Event<const NativeMouseButtonEventArgs&> mouse_down_event_;
- Event<const NativeMouseButtonEventArgs&> mouse_up_event_;
- Event<const NativeMouseWheelEventArgs&> mouse_wheel_event_;
- Event<const NativeKeyEventArgs&> key_down_event_;
- Event<const NativeKeyEventArgs&> key_up_event_;
};
-} // namespace cru::platform::gui::xcb
+} // namespace cru::platform::gui::sdl
diff --git a/include/cru/platform/gui/xcb/Window.h b/include/cru/platform/gui/xcb/Window.h
index 293732c5..6820e7a9 100644
--- a/include/cru/platform/gui/xcb/Window.h
+++ b/include/cru/platform/gui/xcb/Window.h
@@ -7,7 +7,6 @@
#include <cairo.h>
#include <xcb/xcb.h>
-#include <cstddef>
#include <optional>
namespace cru::platform::gui::xcb {
@@ -63,21 +62,7 @@ class XcbWindow : public XcbResource, public virtual INativeWindow {
std::unique_ptr<graphics::IPainter> BeginPaint() override;
- IEvent<std::nullptr_t>* CreateEvent() override;
- IEvent<std::nullptr_t>* DestroyEvent() override;
- IEvent<std::nullptr_t>* PaintEvent() override;
-
- IEvent<WindowVisibilityType>* VisibilityChangeEvent() override;
- IEvent<const Size&>* ResizeEvent() override;
- IEvent<FocusChangeType>* FocusEvent() override;
-
- IEvent<MouseEnterLeaveType>* MouseEnterLeaveEvent() override;
- IEvent<const Point&>* MouseMoveEvent() override;
- IEvent<const NativeMouseButtonEventArgs&>* MouseDownEvent() override;
- IEvent<const NativeMouseButtonEventArgs&>* MouseUpEvent() override;
- IEvent<const NativeMouseWheelEventArgs&>* MouseWheelEvent() override;
- IEvent<const NativeKeyEventArgs&>* KeyDownEvent() override;
- IEvent<const NativeKeyEventArgs&>* KeyUpEvent() override;
+ CRU_DEFINE_CRU_PLATFORM_GUI_I_NATIVE_WINDOW_OVERRIDE_EVENTS()
IInputMethodContext* GetInputMethodContext() override;
@@ -121,20 +106,5 @@ class XcbWindow : public XcbResource, public virtual INativeWindow {
XcbXimInputMethodContext* input_method_;
TimerAutoCanceler repaint_canceler_;
-
- Event<std::nullptr_t> create_event_;
- Event<std::nullptr_t> destroy_event_;
- Event<std::nullptr_t> paint_event_;
-
- Event<WindowVisibilityType> visibility_change_event_;
- Event<const Size&> resize_event_;
- Event<FocusChangeType> focus_event_;
- Event<MouseEnterLeaveType> mouse_enter_leave_event_;
- Event<const Point&> mouse_move_event_;
- Event<const NativeMouseButtonEventArgs&> mouse_down_event_;
- Event<const NativeMouseButtonEventArgs&> mouse_up_event_;
- Event<const NativeMouseWheelEventArgs&> mouse_wheel_event_;
- Event<const NativeKeyEventArgs&> key_down_event_;
- Event<const NativeKeyEventArgs&> key_up_event_;
};
} // namespace cru::platform::gui::xcb
diff --git a/src/platform/gui/sdl/UiApplication.cpp b/src/platform/gui/sdl/UiApplication.cpp
index b448d8f0..c876ad19 100644
--- a/src/platform/gui/sdl/UiApplication.cpp
+++ b/src/platform/gui/sdl/UiApplication.cpp
@@ -25,6 +25,8 @@ SdlUiApplication::SdlUiApplication(graphics::IGraphicsFactory* graphics_factory,
SdlUiApplication::~SdlUiApplication() {
SDL_Quit();
+ delete_later_pool_.Clean();
+
if (release_graphics_factory_) {
delete graphics_factory_;
}
@@ -32,14 +34,12 @@ SdlUiApplication::~SdlUiApplication() {
int SdlUiApplication::Run() {
while (true) {
- auto now = std::chrono::steady_clock::now();
-
- if (auto result = timers_.Update(now)) {
+ if (auto result = timers_.Update(std::chrono::steady_clock::now())) {
result->data();
continue;
}
- auto timeout = timers_.NextTimeout(now);
+ auto timeout = timers_.NextTimeout(std::chrono::steady_clock::now());
SDL_Event event;
CheckSdlReturn(timeout ? SDL_WaitEventTimeout(&event, timeout->count())
@@ -47,6 +47,8 @@ int SdlUiApplication::Run() {
if (event.type == SDL_EVENT_QUIT) {
break;
}
+
+ delete_later_pool_.Clean();
}
for (const auto& handler : this->quit_handlers_) {
@@ -95,6 +97,10 @@ void SdlUiApplication::CancelTimer(long long id) {
return timers_.Remove(static_cast<int>(id));
}
+void SdlUiApplication::DeleteLater(Object* object) {
+ delete_later_pool_.Add(object);
+}
+
std::vector<INativeWindow*> SdlUiApplication::GetAllWindow() {
std::vector<INativeWindow*> windows(windows_.size());
std::ranges::copy(windows_, windows.begin());
diff --git a/src/platform/gui/sdl/Window.cpp b/src/platform/gui/sdl/Window.cpp
index 72ccc873..654ac4cd 100644
--- a/src/platform/gui/sdl/Window.cpp
+++ b/src/platform/gui/sdl/Window.cpp
@@ -15,7 +15,7 @@
namespace cru::platform::gui::sdl {
-SdlWindow::SdlWindow(SdlUiApplication *application)
+SdlWindow::SdlWindow(SdlUiApplication* application)
: application_(application), parent_(nullptr) {
application->RegisterWindow(this);
}
@@ -30,9 +30,9 @@ void SdlWindow::Close() {
}
}
-INativeWindow *SdlWindow::GetParent() { return parent_; }
+INativeWindow* SdlWindow::GetParent() { return parent_; }
-void SdlWindow::SetParent(INativeWindow *parent) {
+void SdlWindow::SetParent(INativeWindow* parent) {
parent_ = CheckPlatform<SdlWindow>(parent, GetPlatformId());
NotImplemented();
}
@@ -56,7 +56,7 @@ void SdlWindow::SetVisibility(WindowVisibilityType visibility) {
Size SdlWindow::GetClientSize() { return GetClientRect().GetSize(); }
-void SdlWindow::SetClientSize(const Size &size) {
+void SdlWindow::SetClientSize(const Size& size) {
auto rect = GetClientRect();
SetClientRect(Rect(rect.GetLeftTop(), size));
}
@@ -66,7 +66,7 @@ Rect SdlWindow::GetClientRect() {
NotImplemented();
}
-void SdlWindow::SetClientRect(const Rect &rect) {
+void SdlWindow::SetClientRect(const Rect& rect) {
if (!sdl_window_) return;
NotImplemented();
}
@@ -76,7 +76,7 @@ Rect SdlWindow::GetWindowRect() {
NotImplemented();
}
-void SdlWindow::SetWindowRect(const Rect &rect) {
+void SdlWindow::SetWindowRect(const Rect& rect) {
if (!sdl_window_) return;
NotImplemented();
}
@@ -121,48 +121,10 @@ std::unique_ptr<graphics::IPainter> SdlWindow::BeginPaint() {
NotImplemented();
}
-IEvent<std::nullptr_t> *SdlWindow::CreateEvent() { return &create_event_; }
+IInputMethodContext* SdlWindow::GetInputMethodContext() { NotImplemented(); }
-IEvent<std::nullptr_t> *SdlWindow::DestroyEvent() { return &destroy_event_; }
+std::optional<SDL_Window*> SdlWindow::GetSdlWindow() { return sdl_window_; }
-IEvent<std::nullptr_t> *SdlWindow::PaintEvent() { return &paint_event_; }
-
-IEvent<WindowVisibilityType> *SdlWindow::VisibilityChangeEvent() {
- return &visibility_change_event_;
-}
-
-IEvent<const Size&> *SdlWindow::ResizeEvent() { return &resize_event_; }
-
-IEvent<FocusChangeType> *SdlWindow::FocusEvent() { return &focus_event_; }
-
-IEvent<MouseEnterLeaveType> *SdlWindow::MouseEnterLeaveEvent() {
- return &mouse_enter_leave_event_;
-}
-
-IEvent<const Point&> *SdlWindow::MouseMoveEvent() { return &mouse_move_event_; }
-
-IEvent<const NativeMouseButtonEventArgs&> *SdlWindow::MouseDownEvent() {
- return &mouse_down_event_;
-}
-
-IEvent<const NativeMouseButtonEventArgs&> *SdlWindow::MouseUpEvent() {
- return &mouse_up_event_;
-}
-
-IEvent<const NativeMouseWheelEventArgs&> *SdlWindow::MouseWheelEvent() {
- return &mouse_wheel_event_;
-}
-
-IEvent<const NativeKeyEventArgs&> *SdlWindow::KeyDownEvent() {
- return &key_down_event_;
-}
-
-IEvent<const NativeKeyEventArgs&> *SdlWindow::KeyUpEvent() { return &key_up_event_; }
-
-IInputMethodContext *SdlWindow::GetInputMethodContext() { NotImplemented(); }
-
-std::optional<SDL_Window *> SdlWindow::GetSdlWindow() { return sdl_window_; }
-
-SdlUiApplication *SdlWindow::GetSdlUiApplication() { return application_; }
+SdlUiApplication* SdlWindow::GetSdlUiApplication() { return application_; }
} // namespace cru::platform::gui::sdl
diff --git a/src/platform/gui/xcb/Window.cpp b/src/platform/gui/xcb/Window.cpp
index f85ec634..50f2c9aa 100644
--- a/src/platform/gui/xcb/Window.cpp
+++ b/src/platform/gui/xcb/Window.cpp
@@ -6,6 +6,7 @@
#include "cru/platform/graphics/NullPainter.h"
#include "cru/platform/graphics/Painter.h"
#include "cru/platform/graphics/cairo/CairoPainter.h"
+#include "cru/platform/gui/Window.h"
#include "cru/platform/gui/xcb/Cursor.h"
#include "cru/platform/gui/xcb/Input.h"
#include "cru/platform/gui/xcb/InputMethod.h"
@@ -46,7 +47,7 @@ XcbWindow::XcbWindow(XcbUiApplication* application)
input_method_ = new XcbXimInputMethodContext(
application->GetXcbXimInputMethodManager(), this);
- paint_event_.AddSpyOnlyHandler([this] {
+ PaintEvent_.AddSpyOnlyHandler([this] {
if (xcb_window_)
CRU_LOG_TAG_DEBUG("{:#x} Paint event triggered.", *xcb_window_);
});
@@ -288,8 +289,10 @@ void XcbWindow::RequestRepaint() {
if (!xcb_window_.has_value()) return;
CRU_LOG_TAG_DEBUG("{:#x} Repaint requested.", *xcb_window_);
// TODO: true throttle
- repaint_canceler_.Reset(
- application_->SetImmediate([this] { paint_event_.Raise(nullptr); }));
+ repaint_canceler_.Reset(application_->SetImmediate([this] {
+ PaintEvent_.Raise(nullptr);
+ Paint1Event_.Raise({{{}, GetClientSize()}});
+ }));
}
std::unique_ptr<graphics::IPainter> XcbWindow::BeginPaint() {
@@ -304,46 +307,6 @@ std::unique_ptr<graphics::IPainter> XcbWindow::BeginPaint() {
return std::unique_ptr<graphics::IPainter>(painter);
}
-IEvent<std::nullptr_t>* XcbWindow::CreateEvent() { return &create_event_; }
-
-IEvent<std::nullptr_t>* XcbWindow::DestroyEvent() { return &destroy_event_; }
-
-IEvent<std::nullptr_t>* XcbWindow::PaintEvent() { return &paint_event_; }
-
-IEvent<WindowVisibilityType>* XcbWindow::VisibilityChangeEvent() {
- return &visibility_change_event_;
-}
-
-IEvent<const Size&>* XcbWindow::ResizeEvent() { return &resize_event_; }
-
-IEvent<FocusChangeType>* XcbWindow::FocusEvent() { return &focus_event_; }
-
-IEvent<MouseEnterLeaveType>* XcbWindow::MouseEnterLeaveEvent() {
- return &mouse_enter_leave_event_;
-}
-
-IEvent<const Point&>* XcbWindow::MouseMoveEvent() { return &mouse_move_event_; }
-
-IEvent<const NativeMouseButtonEventArgs&>* XcbWindow::MouseDownEvent() {
- return &mouse_down_event_;
-}
-
-IEvent<const NativeMouseButtonEventArgs&>* XcbWindow::MouseUpEvent() {
- return &mouse_up_event_;
-}
-
-IEvent<const NativeMouseWheelEventArgs&>* XcbWindow::MouseWheelEvent() {
- return &mouse_wheel_event_;
-}
-
-IEvent<const NativeKeyEventArgs&>* XcbWindow::KeyDownEvent() {
- return &key_down_event_;
-}
-
-IEvent<const NativeKeyEventArgs&>* XcbWindow::KeyUpEvent() {
- return &key_up_event_;
-}
-
IInputMethodContext* XcbWindow::GetInputMethodContext() {
return input_method_;
}
@@ -415,8 +378,8 @@ xcb_window_t XcbWindow::DoCreateWindow() {
cairo_surface_ =
cairo_xcb_surface_create(connection, window, visual_type, width, height);
- create_event_.Raise(nullptr);
- resize_event_.Raise(current_size_);
+ CreateEvent_.Raise(nullptr);
+ ResizeEvent_.Raise(current_size_);
RequestRepaint();
return window;
@@ -429,7 +392,7 @@ void XcbWindow::HandleEvent(xcb_generic_event_t* event) {
break;
}
case XCB_DESTROY_NOTIFY: {
- destroy_event_.Raise(nullptr);
+ DestroyEvent_.Raise(nullptr);
cairo_surface_destroy(cairo_surface_);
cairo_surface_ = nullptr;
@@ -454,26 +417,26 @@ void XcbWindow::HandleEvent(xcb_generic_event_t* event) {
current_size_ = Size(width, height);
assert(cairo_surface_);
cairo_xcb_surface_set_size(cairo_surface_, width, height);
- resize_event_.Raise(current_size_);
+ ResizeEvent_.Raise(current_size_);
}
break;
}
case XCB_MAP_NOTIFY: {
- visibility_change_event_.Raise(WindowVisibilityType::Show);
+ VisibilityChangeEvent_.Raise(WindowVisibilityType::Show);
mapped_ = true;
break;
}
case XCB_UNMAP_NOTIFY: {
- visibility_change_event_.Raise(WindowVisibilityType::Hide);
+ VisibilityChangeEvent_.Raise(WindowVisibilityType::Hide);
mapped_ = false;
break;
}
case XCB_FOCUS_IN: {
- focus_event_.Raise(FocusChangeType::Gain);
+ FocusEvent_.Raise(FocusChangeType::Gain);
break;
}
case XCB_FOCUS_OUT: {
- focus_event_.Raise(FocusChangeType::Lose);
+ FocusEvent_.Raise(FocusChangeType::Lose);
break;
}
case XCB_BUTTON_PRESS: {
@@ -489,14 +452,14 @@ void XcbWindow::HandleEvent(xcb_generic_event_t* event) {
if (bp->detail == 6 || bp->detail == 7) {
args.horizontal = true;
}
- mouse_wheel_event_.Raise(std::move(args));
+ MouseWheelEvent_.Raise(std::move(args));
break;
}
NativeMouseButtonEventArgs args(ConvertMouseButton(bp->detail),
Point(bp->event_x, bp->event_y),
ConvertModifiersOfEvent(bp->state));
- mouse_down_event_.Raise(std::move(args));
+ MouseDownEvent_.Raise(std::move(args));
break;
}
case XCB_BUTTON_RELEASE: {
@@ -504,20 +467,20 @@ void XcbWindow::HandleEvent(xcb_generic_event_t* event) {
NativeMouseButtonEventArgs args(ConvertMouseButton(br->detail),
Point(br->event_x, br->event_y),
ConvertModifiersOfEvent(br->state));
- mouse_up_event_.Raise(std::move(args));
+ MouseUpEvent_.Raise(std::move(args));
break;
}
case XCB_MOTION_NOTIFY: {
xcb_motion_notify_event_t* motion = (xcb_motion_notify_event_t*)event;
Point point(motion->event_x, motion->event_y);
- mouse_move_event_.Raise(point);
+ MouseMoveEvent_.Raise(point);
break;
}
case XCB_ENTER_NOTIFY: {
xcb_enter_notify_event_t* enter = (xcb_enter_notify_event_t*)event;
- mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Enter);
+ MouseEnterLeaveEvent_.Raise(MouseEnterLeaveType::Enter);
Point point(enter->event_x, enter->event_y);
- mouse_move_event_.Raise(point);
+ MouseMoveEvent_.Raise(point);
break;
}
case XCB_LEAVE_NOTIFY: {
@@ -525,21 +488,21 @@ void XcbWindow::HandleEvent(xcb_generic_event_t* event) {
// xcb_leave_notify_event_t *leave = (xcb_leave_notify_event_t *)event;
// Point point(leave->event_x, leave->event_y);
// mouse_move_event_.Raise(point);
- mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Leave);
+ MouseEnterLeaveEvent_.Raise(MouseEnterLeaveType::Leave);
break;
}
case XCB_KEY_PRESS: {
xcb_key_press_event_t* kp = (xcb_key_press_event_t*)event;
NativeKeyEventArgs args(XorgKeycodeToCruKeyCode(application_, kp->detail),
ConvertModifiersOfEvent(kp->state));
- key_down_event_.Raise(std::move(args));
+ KeyDownEvent_.Raise(std::move(args));
break;
}
case XCB_KEY_RELEASE: {
xcb_key_release_event_t* kr = (xcb_key_release_event_t*)event;
NativeKeyEventArgs args(XorgKeycodeToCruKeyCode(application_, kr->detail),
ConvertModifiersOfEvent(kr->state));
- key_up_event_.Raise(std::move(args));
+ KeyUpEvent_.Raise(std::move(args));
break;
}
case XCB_CLIENT_MESSAGE: {