From a176c40ba0f913f98e966f11bad557833ae6dc57 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Oct 2020 11:25:57 +0800 Subject: ... --- include/cru/ui/Base.hpp | 3 + include/cru/ui/Control.hpp | 10 +-- include/cru/ui/Window.hpp | 2 +- include/cru/ui/WindowHost.hpp | 142 --------------------------------- include/cru/ui/host/WindowHost.hpp | 142 +++++++++++++++++++++++++++++++++ include/cru/ui/render/RenderObject.hpp | 12 +-- 6 files changed, 157 insertions(+), 154 deletions(-) delete mode 100644 include/cru/ui/WindowHost.hpp create mode 100644 include/cru/ui/host/WindowHost.hpp (limited to 'include/cru/ui') diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index 36d0eb78..39fbb035 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -29,7 +29,10 @@ namespace colors = cru::platform::colors; class Window; class Control; class ClickDetector; + +namespace host { class WindowHost; +} namespace render { class RenderObject; diff --git a/include/cru/ui/Control.hpp b/include/cru/ui/Control.hpp index 5f381965..fe50624a 100644 --- a/include/cru/ui/Control.hpp +++ b/include/cru/ui/Control.hpp @@ -9,7 +9,7 @@ namespace cru::ui { class Control : public Object { - friend WindowHost; + friend host::WindowHost; protected: Control(); @@ -26,7 +26,7 @@ class Control : public Object { //*************** region: tree *************** public: - WindowHost* GetWindowHost() const; + host::WindowHost* GetWindowHost() const; Control* GetParent() const { return parent_; } @@ -131,8 +131,8 @@ class Control : public Object { virtual void OnAddChild(Control* child, Index position); virtual void OnRemoveChild(Control* child, Index position); virtual void OnParentChanged(Control* old_parent, Control* new_parent); - virtual void OnAttachToHost(WindowHost* host); - virtual void OnDetachFromHost(WindowHost* host); + virtual void OnAttachToHost(host::WindowHost* host); + virtual void OnDetachFromHost(host::WindowHost* host); protected: virtual void OnMouseHoverChange(bool newHover) { CRU_UNUSED(newHover) } @@ -141,7 +141,7 @@ class Control : public Object { Control* parent_ = nullptr; std::vector children_; - WindowHost* window_host_ = nullptr; + host::WindowHost* window_host_ = nullptr; private: bool is_mouse_over_ = false; diff --git a/include/cru/ui/Window.hpp b/include/cru/ui/Window.hpp index 0739e3dc..70423a14 100644 --- a/include/cru/ui/Window.hpp +++ b/include/cru/ui/Window.hpp @@ -29,7 +29,7 @@ class Window final : public LayoutControl { void OnRemoveChild(Control* child, Index position) override; private: - std::unique_ptr window_host_; + std::unique_ptr window_host_; std::unique_ptr render_object_; }; diff --git a/include/cru/ui/WindowHost.hpp b/include/cru/ui/WindowHost.hpp deleted file mode 100644 index 7f7d62f0..00000000 --- a/include/cru/ui/WindowHost.hpp +++ /dev/null @@ -1,142 +0,0 @@ -#pragma once -#include "Base.hpp" - -#include "cru/common/Event.hpp" -#include "cru/platform/gui/UiApplication.hpp" -#include "cru/platform/gui/Window.hpp" -#include "render/Base.hpp" - -#include - -namespace cru::ui { -struct AfterLayoutEventArgs {}; - -// The bridge between control tree and native window. -class WindowHost : public Object { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::WindowHost") - - public: - WindowHost(Control* root_control); - - CRU_DELETE_COPY(WindowHost) - CRU_DELETE_MOVE(WindowHost) - - ~WindowHost() override; - - public: - platform::gui::INativeWindow* GetNativeWindow() { return native_window_; } - - // Mark the layout as invalid, and arrange a re-layout later. - // This method could be called more than one times in a message cycle. But - // layout only takes place once. - void InvalidateLayout(); - - // Mark the paint as invalid, and arrange a re-paint later. - // This method could be called more than one times in a message cycle. But - // paint only takes place once. - void InvalidatePaint(); - - IEvent* AfterLayoutEvent() { - return &after_layout_event_; - } - - void Relayout(); - void Relayout(const Size& available_size); - - // Is layout is invalid, wait for relayout and then run the action. Otherwist - // run it right now. - void RunAfterLayoutStable(std::function action); - - // If true, preferred size of root render object is set to window size when - // measure. Default is true. - bool IsLayoutPreferToFillWindow() const; - void SetLayoutPreferToFillWindow(bool value); - - // Get current control that mouse hovers on. This ignores the mouse-capture - // control. Even when mouse is captured by another control, this function - // return the control under cursor. You can use `GetMouseCaptureControl` to - // get more info. - Control* GetMouseHoverControl() const { return mouse_hover_control_; } - - //*************** region: focus *************** - - Control* GetFocusControl(); - - void SetFocusControl(Control* control); - - //*************** region: focus *************** - - // Pass nullptr to release capture. If mouse is already capture by a control, - // this capture will fail and return false. If control is identical to the - // capturing control, capture is not changed and this function will return - // true. - // - // When capturing control changes, - // appropriate event will be sent. If mouse is not on the capturing control - // and capture is released, mouse enter event will be sent to the mouse-hover - // control. If mouse is not on the capturing control and capture is set, mouse - // leave event will be sent to the mouse-hover control. - bool CaptureMouseFor(Control* control); - - // Return null if not captured. - Control* GetMouseCaptureControl(); - - Control* HitTest(const Point& point); - - void UpdateCursor(); - - private: - //*************** region: native messages *************** - void OnNativeDestroy(platform::gui::INativeWindow* window, std::nullptr_t); - void OnNativePaint(platform::gui::INativeWindow* window, std::nullptr_t); - void OnNativeResize(platform::gui::INativeWindow* window, - const Size& size); - - void OnNativeFocus(platform::gui::INativeWindow* window, - cru::platform::gui::FocusChangeType focus); - - void OnNativeMouseEnterLeave( - platform::gui::INativeWindow* window, - cru::platform::gui::MouseEnterLeaveType enter); - void OnNativeMouseMove(platform::gui::INativeWindow* window, - const Point& point); - void OnNativeMouseDown( - platform::gui::INativeWindow* window, - const platform::gui::NativeMouseButtonEventArgs& args); - void OnNativeMouseUp( - platform::gui::INativeWindow* window, - const platform::gui::NativeMouseButtonEventArgs& args); - - void OnNativeKeyDown(platform::gui::INativeWindow* window, - const platform::gui::NativeKeyEventArgs& args); - void OnNativeKeyUp(platform::gui::INativeWindow* window, - const platform::gui::NativeKeyEventArgs& args); - - //*************** region: event dispatcher helper *************** - - void DispatchMouseHoverControlChangeEvent(Control* old_control, - Control* new_control, - const Point& point, bool no_leave, - bool no_enter); - - private: - Control* root_control_ = nullptr; - render::RenderObject* root_render_object_ = nullptr; - - platform::gui::INativeWindow* native_window_ = nullptr; - - bool need_layout_ = false; - Event after_layout_event_; - std::vector > after_layout_stable_action_; - - std::vector event_revoker_guards_; - - Control* mouse_hover_control_ = nullptr; - - Control* focus_control_; - - Control* mouse_captured_control_ = nullptr; - - bool layout_prefer_to_fill_window_ = true; -}; -} // namespace cru::ui diff --git a/include/cru/ui/host/WindowHost.hpp b/include/cru/ui/host/WindowHost.hpp new file mode 100644 index 00000000..77ed937f --- /dev/null +++ b/include/cru/ui/host/WindowHost.hpp @@ -0,0 +1,142 @@ +#pragma once +#include "../Base.hpp" + +#include "cru/common/Event.hpp" +#include "cru/platform/gui/UiApplication.hpp" +#include "cru/platform/gui/Window.hpp" +#include "../render/Base.hpp" + +#include + +namespace cru::ui::host { +struct AfterLayoutEventArgs {}; + +// The bridge between control tree and native window. +class WindowHost : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::host::WindowHost") + + public: + WindowHost(Control* root_control); + + CRU_DELETE_COPY(WindowHost) + CRU_DELETE_MOVE(WindowHost) + + ~WindowHost() override; + + public: + platform::gui::INativeWindow* GetNativeWindow() { return native_window_; } + + // Mark the layout as invalid, and arrange a re-layout later. + // This method could be called more than one times in a message cycle. But + // layout only takes place once. + void InvalidateLayout(); + + // Mark the paint as invalid, and arrange a re-paint later. + // This method could be called more than one times in a message cycle. But + // paint only takes place once. + void InvalidatePaint(); + + IEvent* AfterLayoutEvent() { + return &after_layout_event_; + } + + void Relayout(); + void Relayout(const Size& available_size); + + // Is layout is invalid, wait for relayout and then run the action. Otherwist + // run it right now. + void RunAfterLayoutStable(std::function action); + + // If true, preferred size of root render object is set to window size when + // measure. Default is true. + bool IsLayoutPreferToFillWindow() const; + void SetLayoutPreferToFillWindow(bool value); + + // Get current control that mouse hovers on. This ignores the mouse-capture + // control. Even when mouse is captured by another control, this function + // return the control under cursor. You can use `GetMouseCaptureControl` to + // get more info. + Control* GetMouseHoverControl() const { return mouse_hover_control_; } + + //*************** region: focus *************** + + Control* GetFocusControl(); + + void SetFocusControl(Control* control); + + //*************** region: focus *************** + + // Pass nullptr to release capture. If mouse is already capture by a control, + // this capture will fail and return false. If control is identical to the + // capturing control, capture is not changed and this function will return + // true. + // + // When capturing control changes, + // appropriate event will be sent. If mouse is not on the capturing control + // and capture is released, mouse enter event will be sent to the mouse-hover + // control. If mouse is not on the capturing control and capture is set, mouse + // leave event will be sent to the mouse-hover control. + bool CaptureMouseFor(Control* control); + + // Return null if not captured. + Control* GetMouseCaptureControl(); + + Control* HitTest(const Point& point); + + void UpdateCursor(); + + private: + //*************** region: native messages *************** + void OnNativeDestroy(platform::gui::INativeWindow* window, std::nullptr_t); + void OnNativePaint(platform::gui::INativeWindow* window, std::nullptr_t); + void OnNativeResize(platform::gui::INativeWindow* window, + const Size& size); + + void OnNativeFocus(platform::gui::INativeWindow* window, + cru::platform::gui::FocusChangeType focus); + + void OnNativeMouseEnterLeave( + platform::gui::INativeWindow* window, + cru::platform::gui::MouseEnterLeaveType enter); + void OnNativeMouseMove(platform::gui::INativeWindow* window, + const Point& point); + void OnNativeMouseDown( + platform::gui::INativeWindow* window, + const platform::gui::NativeMouseButtonEventArgs& args); + void OnNativeMouseUp( + platform::gui::INativeWindow* window, + const platform::gui::NativeMouseButtonEventArgs& args); + + void OnNativeKeyDown(platform::gui::INativeWindow* window, + const platform::gui::NativeKeyEventArgs& args); + void OnNativeKeyUp(platform::gui::INativeWindow* window, + const platform::gui::NativeKeyEventArgs& args); + + //*************** region: event dispatcher helper *************** + + void DispatchMouseHoverControlChangeEvent(Control* old_control, + Control* new_control, + const Point& point, bool no_leave, + bool no_enter); + + private: + Control* root_control_ = nullptr; + render::RenderObject* root_render_object_ = nullptr; + + platform::gui::INativeWindow* native_window_ = nullptr; + + bool need_layout_ = false; + Event after_layout_event_; + std::vector > after_layout_stable_action_; + + std::vector event_revoker_guards_; + + Control* mouse_hover_control_ = nullptr; + + Control* focus_control_; + + Control* mouse_captured_control_ = nullptr; + + bool layout_prefer_to_fill_window_ = true; +}; +} // namespace cru::ui diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp index 436cf6b2..635a541e 100644 --- a/include/cru/ui/render/RenderObject.hpp +++ b/include/cru/ui/render/RenderObject.hpp @@ -39,7 +39,7 @@ namespace cru::ui::render { // Size OnMeasureContent(const MeasureRequirement& requirement) override; // void OnLayoutContent(const Rect& content_rect) override; class RenderObject : public Object { - friend WindowHost; + friend host::WindowHost; CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::render::RenderObject") @@ -65,7 +65,7 @@ class RenderObject : public Object { Control* GetAttachedControl() const { return control_; } void SetAttachedControl(Control* new_control) { control_ = new_control; } - WindowHost* GetWindowHost() const { return window_host_; } + host::WindowHost* GetWindowHost() const { return window_host_; } RenderObject* GetParent() const { return parent_; } @@ -135,7 +135,7 @@ class RenderObject : public Object { // Add offset before pass point to children. virtual RenderObject* HitTest(const Point& point) = 0; - IEvent* AttachToHostEvent() { return &attach_to_host_event_; } + IEvent* AttachToHostEvent() { return &attach_to_host_event_; } IEvent* DetachFromHostEvent() { return &detach_from_host_event_; } @@ -205,11 +205,11 @@ class RenderObject : public Object { private: void SetParent(RenderObject* new_parent); - void SetWindowHostRecursive(WindowHost* host); + void SetWindowHostRecursive(host::WindowHost* host); private: Control* control_ = nullptr; - WindowHost* window_host_ = nullptr; + host::WindowHost* window_host_ = nullptr; RenderObject* parent_ = nullptr; std::vector children_{}; @@ -225,7 +225,7 @@ class RenderObject : public Object { Thickness margin_{}; Thickness padding_{}; - Event attach_to_host_event_; + Event attach_to_host_event_; Event detach_from_host_event_; }; } // namespace cru::ui::render -- cgit v1.2.3