From 376d5bfe0f3b9658cbf2d4ca9b00c0600341ee85 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 25 Nov 2025 17:15:16 +0800 Subject: Clean code. Clean events of native window. --- include/cru/base/Base.h | 7 ++++- include/cru/base/Event.h | 7 +++++ include/cru/platform/gui/Window.h | 21 +++++++++++++++ include/cru/platform/gui/win/Window.h | 50 ++--------------------------------- src/base/Base.cpp | 2 +- src/platform/gui/Window.cpp | 5 ++++ src/platform/gui/win/Window.cpp | 39 +++++++++++++++------------ src/ui/render/TextRenderObject.cpp | 2 ++ 8 files changed, 66 insertions(+), 67 deletions(-) diff --git a/include/cru/base/Base.h b/include/cru/base/Base.h index fda45f66..3cd04f8c 100644 --- a/include/cru/base/Base.h +++ b/include/cru/base/Base.h @@ -137,12 +137,17 @@ class CRU_BASE_API Exception : public std::exception { std::shared_ptr inner_; }; +class CRU_BASE_API NotImplementedException : public Exception { + public: + using Exception::Exception; // inherit constructors +}; + class CRU_BASE_API PlatformException : public Exception { public: using Exception::Exception; // inherit constructors }; -class ErrnoException : public Exception { +class CRU_BASE_API ErrnoException : public Exception { public: ErrnoException(); explicit ErrnoException(int error_code); diff --git a/include/cru/base/Event.h b/include/cru/base/Event.h index bdaf3ea6..71c0ac6b 100644 --- a/include/cru/base/Event.h +++ b/include/cru/base/Event.h @@ -141,6 +141,13 @@ class Event : public EventBase, public IEvent { public: \ ::cru::IEvent* name##Event() { return &name##Event_; } +#define CRU_DEFINE_EVENT_OVERRIDE(name, arg_type) \ + private: \ + ::cru::Event name##Event_; \ + \ + public: \ + ::cru::IEvent* name##Event() override { return &name##Event_; } + namespace details { struct EventHandlerRevokerCaller { void operator()(const EventHandlerRevoker& revoker) { revoker(); } diff --git a/include/cru/platform/gui/Window.h b/include/cru/platform/gui/Window.h index b8973c5a..e04db448 100644 --- a/include/cru/platform/gui/Window.h +++ b/include/cru/platform/gui/Window.h @@ -23,6 +23,10 @@ struct WindowStyleFlags { enum class WindowVisibilityType { Show, Hide, Minimize }; +struct NativePaintEventArgs { + Rect repaint_area; +}; + enum class FocusChangeType { Gain, Lose }; enum class MouseEnterLeaveType { Enter, Leave }; @@ -100,6 +104,7 @@ struct CRU_PLATFORM_GUI_API INativeWindow : virtual IPlatformResource { virtual IEvent* CreateEvent() = 0; virtual IEvent* DestroyEvent() = 0; virtual IEvent* PaintEvent() = 0; + virtual IEvent* Paint1Event(); virtual IEvent* VisibilityChangeEvent() = 0; virtual IEvent* ResizeEvent() = 0; @@ -116,3 +121,19 @@ struct CRU_PLATFORM_GUI_API INativeWindow : virtual IPlatformResource { virtual IInputMethodContext* GetInputMethodContext() = 0; }; } // namespace cru::platform::gui + +#define CRU_DEFINE_CRU_PLATFORM_GUI_I_NATIVE_WINDOW_OVERRIDE_EVENTS() \ + CRU_DEFINE_EVENT_OVERRIDE(Create, std::nullptr_t) \ + CRU_DEFINE_EVENT_OVERRIDE(Destroy, std::nullptr_t) \ + CRU_DEFINE_EVENT_OVERRIDE(Paint, std::nullptr_t) \ + CRU_DEFINE_EVENT_OVERRIDE(Paint1, const NativePaintEventArgs&) \ + CRU_DEFINE_EVENT_OVERRIDE(VisibilityChange, WindowVisibilityType) \ + CRU_DEFINE_EVENT_OVERRIDE(Resize, const Size&) \ + CRU_DEFINE_EVENT_OVERRIDE(Focus, FocusChangeType) \ + CRU_DEFINE_EVENT_OVERRIDE(MouseEnterLeave, MouseEnterLeaveType) \ + CRU_DEFINE_EVENT_OVERRIDE(MouseMove, const Point&) \ + CRU_DEFINE_EVENT_OVERRIDE(MouseDown, const NativeMouseButtonEventArgs&) \ + CRU_DEFINE_EVENT_OVERRIDE(MouseUp, const NativeMouseButtonEventArgs&) \ + CRU_DEFINE_EVENT_OVERRIDE(MouseWheel, const NativeMouseWheelEventArgs&) \ + CRU_DEFINE_EVENT_OVERRIDE(KeyDown, const NativeKeyEventArgs&) \ + CRU_DEFINE_EVENT_OVERRIDE(KeyUp, const NativeKeyEventArgs&) diff --git a/include/cru/platform/gui/win/Window.h b/include/cru/platform/gui/win/Window.h index c7d48ea9..e690ad4e 100644 --- a/include/cru/platform/gui/win/Window.h +++ b/include/cru/platform/gui/win/Window.h @@ -61,38 +61,8 @@ class CRU_WIN_GUI_API WinNativeWindow : public WinNativeResource, void SetToForeground() override; - IEvent* CreateEvent() override { return &create_event_; } - IEvent* DestroyEvent() override { return &destroy_event_; } - IEvent* PaintEvent() override { return &paint_event_; } - IEvent* VisibilityChangeEvent() override { - return &visibility_change_event_; - } - IEvent* ResizeEvent() override { return &resize_event_; } - IEvent* FocusEvent() override { return &focus_event_; } - IEvent* MouseEnterLeaveEvent() override { - return &mouse_enter_leave_event_; - } - IEvent* MouseMoveEvent() override { return &mouse_move_event_; } - IEvent* MouseDownEvent() override { - return &mouse_down_event_; - } - IEvent* MouseUpEvent() override { - return &mouse_up_event_; - } - IEvent* MouseWheelEvent() override { - return &mouse_wheel_event_; - } - - IEvent* KeyDownEvent() override { - return &key_down_event_; - } - IEvent* KeyUpEvent() override { - return &key_up_event_; - } - - IEvent* NativeMessageEvent() { - return &native_message_event_; - } + CRU_DEFINE_CRU_PLATFORM_GUI_I_NATIVE_WINDOW_OVERRIDE_EVENTS() + CRU_DEFINE_EVENT(NativeMessage, WindowNativeMessageEventArgs&) IInputMethodContext* GetInputMethodContext() override; @@ -193,21 +163,5 @@ class CRU_WIN_GUI_API WinNativeWindow : public WinNativeResource, std::shared_ptr cursor_; std::unique_ptr input_method_context_; - - Event create_event_; - Event destroy_event_; - Event paint_event_; - Event resize_event_; - Event visibility_change_event_; - Event focus_event_; - Event mouse_enter_leave_event_; - Event mouse_move_event_; - Event mouse_down_event_; - Event mouse_up_event_; - Event mouse_wheel_event_; - Event key_down_event_; - Event key_up_event_; - - Event native_message_event_; }; } // namespace cru::platform::gui::win diff --git a/src/base/Base.cpp b/src/base/Base.cpp index 7dd6337d..c97c53a5 100644 --- a/src/base/Base.cpp +++ b/src/base/Base.cpp @@ -7,7 +7,7 @@ namespace cru { void UnreachableCode() { std::terminate(); } -void NotImplemented() { std::terminate(); } +void NotImplemented() { throw NotImplementedException(); } Exception::Exception(std::string message, std::shared_ptr inner) : message_(std::move(message)), inner_(std::move(inner)) {} diff --git a/src/platform/gui/Window.cpp b/src/platform/gui/Window.cpp index fdcfbae4..15a49b06 100644 --- a/src/platform/gui/Window.cpp +++ b/src/platform/gui/Window.cpp @@ -3,4 +3,9 @@ namespace cru::platform::gui { bool INativeWindow::IsCreated() { NotImplemented(); } + +IEvent* INativeWindow::Paint1Event() { + NotImplemented(); +} + } // namespace cru::platform::gui diff --git a/src/platform/gui/win/Window.cpp b/src/platform/gui/win/Window.cpp index 2c0bc5a1..fb2ce024 100644 --- a/src/platform/gui/win/Window.cpp +++ b/src/platform/gui/win/Window.cpp @@ -312,7 +312,7 @@ bool WinNativeWindow::HandleNativeWindowMessage(HWND hwnd, UINT msg, LRESULT* result) { WindowNativeMessageEventArgs args{ WindowNativeMessage{hwnd, msg, w_param, l_param}}; - native_message_event_.Raise(args); + NativeMessageEvent_.Raise(args); if (args.IsHandled()) { *result = args.GetResult(); return true; @@ -510,13 +510,13 @@ void WinNativeWindow::RecreateWindow() { void WinNativeWindow::OnCreateInternal() { CRU_LOG_TAG_DEBUG("A native window is created, hwnd {}.", static_cast(GetWindowHandle())); - create_event_.Raise(nullptr); + CreateEvent_.Raise(nullptr); } void WinNativeWindow::OnDestroyInternal() { CRU_LOG_TAG_DEBUG("A native window is destroying, hwnd {}.", static_cast(GetWindowHandle())); - destroy_event_.Raise(nullptr); + DestroyEvent_.Raise(nullptr); hwnd_ = nullptr; if (application_->IsQuitOnAllWindowClosed() && @@ -529,8 +529,14 @@ void WinNativeWindow::OnDestroyInternal() { } void WinNativeWindow::OnPaintInternal() { - paint_event_.Raise(nullptr); - ValidateRect(hwnd_, nullptr); + PaintEvent_.Raise(nullptr); + NativePaintEventArgs args; + ::RECT rect; + if (::GetUpdateRect(hwnd_, &rect, FALSE)) { + args.repaint_area = PixelToDip(rect); + } + Paint1Event_.Raise(args); + ::ValidateRect(hwnd_, nullptr); CRU_LOG_TAG_DEBUG("A repaint is finished."); } @@ -545,18 +551,18 @@ void WinNativeWindow::OnResizeInternal(const int new_width, client_rect_.height = PixelToDip(new_height); if (!(new_width == 0 && new_height == 0)) { window_render_target_->ResizeBuffer(new_width, new_height); - resize_event_.Raise(Size{PixelToDip(new_width), PixelToDip(new_height)}); + ResizeEvent_.Raise(Size{PixelToDip(new_width), PixelToDip(new_height)}); } } void WinNativeWindow::OnSetFocusInternal() { has_focus_ = true; - focus_event_.Raise(FocusChangeType::Gain); + FocusEvent_.Raise(FocusChangeType::Gain); } void WinNativeWindow::OnKillFocusInternal() { has_focus_ = false; - focus_event_.Raise(FocusChangeType::Lose); + FocusEvent_.Raise(FocusChangeType::Lose); } void WinNativeWindow::OnMouseMoveInternal(const POINT point) { @@ -571,43 +577,42 @@ void WinNativeWindow::OnMouseMoveInternal(const POINT point) { TrackMouseEvent(&tme); is_mouse_in_ = true; - mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Enter); + MouseEnterLeaveEvent_.Raise(MouseEnterLeaveType::Enter); } - mouse_move_event_.Raise(PixelToDip(point)); + MouseMoveEvent_.Raise(PixelToDip(point)); } void WinNativeWindow::OnMouseLeaveInternal() { is_mouse_in_ = false; - mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Leave); + MouseEnterLeaveEvent_.Raise(MouseEnterLeaveType::Leave); } void WinNativeWindow::OnMouseDownInternal(platform::gui::MouseButton button, POINT point) { const auto dip_point = PixelToDip(point); - mouse_down_event_.Raise({button, dip_point, RetrieveKeyModifier()}); + MouseDownEvent_.Raise({button, dip_point, RetrieveKeyModifier()}); } void WinNativeWindow::OnMouseUpInternal(platform::gui::MouseButton button, POINT point) { const auto dip_point = PixelToDip(point); - mouse_up_event_.Raise({button, dip_point, RetrieveKeyModifier()}); + MouseUpEvent_.Raise({button, dip_point, RetrieveKeyModifier()}); } void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) { const auto dip_point = PixelToDip(point); const float d = -((float)delta / 120.f); - mouse_wheel_event_.Raise({d, dip_point, RetrieveKeyModifier()}); + MouseWheelEvent_.Raise({d, dip_point, RetrieveKeyModifier()}); } void WinNativeWindow::OnKeyDownInternal(int virtual_code) { - key_down_event_.Raise( + KeyDownEvent_.Raise( {VirtualKeyToKeyCode(virtual_code), RetrieveKeyModifier()}); } void WinNativeWindow::OnKeyUpInternal(int virtual_code) { - key_up_event_.Raise( - {VirtualKeyToKeyCode(virtual_code), RetrieveKeyModifier()}); + KeyUpEvent_.Raise({VirtualKeyToKeyCode(virtual_code), RetrieveKeyModifier()}); } void WinNativeWindow::OnActivatedInternal() {} diff --git a/src/ui/render/TextRenderObject.cpp b/src/ui/render/TextRenderObject.cpp index 44dee1aa..1fe8e652 100644 --- a/src/ui/render/TextRenderObject.cpp +++ b/src/ui/render/TextRenderObject.cpp @@ -54,12 +54,14 @@ void TextRenderObject::SetFont( std::shared_ptr font) { Expects(font); text_layout_->SetFont(std::move(font)); + InvalidateLayout(); } bool TextRenderObject::IsEditMode() { return text_layout_->IsEditMode(); } void TextRenderObject::SetEditMode(bool enable) { text_layout_->SetEditMode(enable); + InvalidateLayout(); } Index TextRenderObject::GetLineCount() { return text_layout_->GetLineCount(); } -- cgit v1.2.3