From 46ff47d2f47a66372ca0a8a09dd08c4fb04004f3 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 17 Oct 2020 15:57:53 +0800 Subject: Refactor timer. --- src/ui/UiHost.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui/UiHost.cpp') diff --git a/src/ui/UiHost.cpp b/src/ui/UiHost.cpp index 5451ebce..0fdf2f53 100644 --- a/src/ui/UiHost.cpp +++ b/src/ui/UiHost.cpp @@ -156,7 +156,7 @@ void UiHost::InvalidatePaint() { void UiHost::InvalidateLayout() { log::TagDebug(log_tag, u"A relayout is requested."); if (!need_layout_) { - platform::native::IUiApplication::GetInstance()->InvokeLater( + platform::native::IUiApplication::GetInstance()->SetImmediate( [resolver = this->CreateResolver()] { if (const auto host = resolver.Resolve()) { host->Relayout(); -- cgit v1.2.3 From f90650efb7175957892d18097954ffd3aa59dc95 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 18 Oct 2020 21:40:23 +0800 Subject: ... --- include/cru/ui/UiHost.hpp | 7 +++++++ src/ui/UiHost.cpp | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src/ui/UiHost.cpp') diff --git a/include/cru/ui/UiHost.hpp b/include/cru/ui/UiHost.hpp index b1658ef6..01791de6 100644 --- a/include/cru/ui/UiHost.hpp +++ b/include/cru/ui/UiHost.hpp @@ -59,6 +59,11 @@ class UiHost : public Object, public SelfResolvable { return &after_layout_event_; } + // 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); + void Relayout(); // Get current control that mouse hovers on. This ignores the mouse-capture @@ -168,5 +173,7 @@ class UiHost : public Object, public SelfResolvable { Control* focus_control_; // "focus_control_" can't be nullptr Control* mouse_captured_control_; + + bool layout_prefer_to_fill_window_ = true; }; } // namespace cru::ui diff --git a/src/ui/UiHost.cpp b/src/ui/UiHost.cpp index 48a4e06e..cd09907f 100644 --- a/src/ui/UiHost.cpp +++ b/src/ui/UiHost.cpp @@ -7,6 +7,7 @@ #include "cru/platform/native/UiApplication.hpp" #include "cru/platform/native/Window.hpp" #include "cru/ui/Window.hpp" +#include "cru/ui/render/MeasureRequirement.hpp" #include "cru/ui/render/WindowRenderObject.hpp" namespace cru::ui { @@ -168,14 +169,27 @@ void UiHost::InvalidateLayout() { } } +bool UiHost::IsLayoutPreferToFillWindow() const { + return layout_prefer_to_fill_window_; +} + +void UiHost::SetLayoutPreferToFillWindow(bool value) { + if (value == layout_prefer_to_fill_window_) return; + layout_prefer_to_fill_window_ = value; + InvalidateLayout(); +} + void UiHost::Relayout() { const auto native_window = native_window_resolver_->Resolve(); const auto client_size = native_window ? native_window->GetClientSize() : Size{100, 100}; // a reasonable assumed size + root_render_object_->Measure( render::MeasureRequirement{client_size, - render::MeasureSize::NotSpecified()}, + IsLayoutPreferToFillWindow() + ? render::MeasureSize(client_size) + : render::MeasureSize::NotSpecified()}, render::MeasureSize::NotSpecified()); root_render_object_->Layout(Point{}); after_layout_event_.Raise(AfterLayoutEventArgs{}); -- cgit v1.2.3 From b61ab6a39ed1637c65e83b7ff7ff0d20908baafb Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Oct 2020 00:10:49 +0800 Subject: ... --- src/ui/UiHost.cpp | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'src/ui/UiHost.cpp') diff --git a/src/ui/UiHost.cpp b/src/ui/UiHost.cpp index cd09907f..d3564619 100644 --- a/src/ui/UiHost.cpp +++ b/src/ui/UiHost.cpp @@ -45,10 +45,11 @@ bool IsAncestor(Control* control, Control* ancestor) { return false; } -std::list GetAncestorList(Control* control) { - std::list l; +// Ancestor at last. +std::vector GetAncestorList(Control* control) { + std::vector l; while (control != nullptr) { - l.push_front(control); + l.push_back(control); control = control->GetParent(); } return l; @@ -61,25 +62,25 @@ Control* FindLowestCommonAncestor(Control* left, Control* right) { auto&& right_list = GetAncestorList(right); // the root is different - if (left_list.front() != right_list.front()) return nullptr; + if (left_list.back() != right_list.back()) return nullptr; // find the last same control or the last control (one is ancestor of the // other) - auto left_i = left_list.cbegin(); - auto right_i = right_list.cbegin(); + auto left_iter = left_list.crbegin(); + auto right_iter = right_list.crbegin(); while (true) { - if (left_i == left_list.cend()) { - return *(--left_i); + if (left_iter == left_list.crend()) { + return left_list.front(); } - if (right_i == right_list.cend()) { - return *(--right_i); + if (right_iter == right_list.crend()) { + return right_list.front(); } - if (*left_i != *right_i) { - return *(--left_i); + if (*left_iter != *right_iter) { + return *(--left_iter); } - ++left_i; - ++right_i; + ++left_iter; + ++right_iter; } } } // namespace -- cgit v1.2.3 From 4e0a5338f15a247c33df863c4c619ffdb0a98aae Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Oct 2020 17:51:41 +0800 Subject: ... --- include/cru/ui/UiHost.hpp | 6 ++++++ src/ui/UiHost.cpp | 10 ++++++++++ src/ui/controls/TextControlService.hpp | 12 +++--------- 3 files changed, 19 insertions(+), 9 deletions(-) (limited to 'src/ui/UiHost.cpp') diff --git a/include/cru/ui/UiHost.hpp b/include/cru/ui/UiHost.hpp index 01791de6..2437b967 100644 --- a/include/cru/ui/UiHost.hpp +++ b/include/cru/ui/UiHost.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include "Base.hpp" #include "cru/common/Event.hpp" @@ -110,6 +111,10 @@ class UiHost : public Object, public SelfResolvable { void SetRetainAfterDestroy(bool destroy) { retain_after_destroy_ = destroy; } + // Is layout is invalid, wait for relayout and then run the action. Otherwist + // run it right now. + void RunAfterLayoutStable(std::function action); + private: //*************** region: native messages *************** void OnNativeDestroy(platform::native::INativeWindow* window, std::nullptr_t); @@ -148,6 +153,7 @@ class UiHost : public Object, public SelfResolvable { bool need_layout_ = false; Event after_layout_event_; + std::vector > after_layout_stable_action_; std::shared_ptr native_window_resolver_; diff --git a/src/ui/UiHost.cpp b/src/ui/UiHost.cpp index d3564619..ccc5fea0 100644 --- a/src/ui/UiHost.cpp +++ b/src/ui/UiHost.cpp @@ -193,6 +193,8 @@ void UiHost::Relayout() { : render::MeasureSize::NotSpecified()}, render::MeasureSize::NotSpecified()); root_render_object_->Layout(Point{}); + for (auto& action : after_layout_stable_action_) action(); + after_layout_stable_action_.clear(); after_layout_event_.Raise(AfterLayoutEventArgs{}); log::TagDebug(log_tag, u"A relayout is finished."); } @@ -249,6 +251,14 @@ bool UiHost::CaptureMouseFor(Control* control) { Control* UiHost::GetMouseCaptureControl() { return mouse_captured_control_; } +void UiHost::RunAfterLayoutStable(std::function action) { + if (need_layout_) { + after_layout_stable_action_.push_back(std::move(action)); + } else { + action(); + } +} + void UiHost::OnNativeDestroy(INativeWindow* window, std::nullptr_t) { CRU_UNUSED(window) native_window_destroyed_ = true; diff --git a/src/ui/controls/TextControlService.hpp b/src/ui/controls/TextControlService.hpp index 04807c30..376f9177 100644 --- a/src/ui/controls/TextControlService.hpp +++ b/src/ui/controls/TextControlService.hpp @@ -206,16 +206,12 @@ class TextControlService : public Object { SetSelection(GetSelection().GetStart() + text.size()); } - void ScrollToCaret(bool next_tick = true) { - if (next_tick) { - scroll_to_caret_timer_canceler_.Reset( - GetUiApplication()->GetInstance()->SetImmediate( - [this]() { this->ScrollToCaret(false); })); - } else { + void ScrollToCaret() { + this->control_->GetUiHost()->RunAfterLayoutStable([this]() { const auto caret_rect = this->GetTextRenderObject()->GetCaretRect(); this->GetScrollRenderObject()->ScrollToContain(caret_rect, Thickness{5.f}); - } + }); } private: @@ -453,8 +449,6 @@ class TextControlService : public Object { ShortcutHub shortcut_hub_; - platform::native::TimerAutoCanceler scroll_to_caret_timer_canceler_; - // nullopt means not selecting std::optional select_down_button_; -- cgit v1.2.3 From c1c5a185e4f4c4706e8a641f25add3885203f202 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Oct 2020 18:06:56 +0800 Subject: ... --- include/cru/ui/DebugFlags.hpp | 4 ++- src/ui/RoutedEventDispatch.hpp | 45 +++++++++------------------------- src/ui/UiHost.cpp | 7 ++++-- src/ui/controls/TextControlService.hpp | 13 ++++++---- src/ui/render/ScrollRenderObject.cpp | 1 - 5 files changed, 27 insertions(+), 43 deletions(-) (limited to 'src/ui/UiHost.cpp') diff --git a/include/cru/ui/DebugFlags.hpp b/include/cru/ui/DebugFlags.hpp index a0003e9d..fceef081 100644 --- a/include/cru/ui/DebugFlags.hpp +++ b/include/cru/ui/DebugFlags.hpp @@ -1,6 +1,8 @@ #pragma once namespace cru::ui::debug_flags { +constexpr bool routed_event = false; constexpr bool layout = false; -constexpr bool shortcut = true; +constexpr bool shortcut = false; +constexpr bool text_service = false; } // namespace cru::ui::debug_flags diff --git a/src/ui/RoutedEventDispatch.hpp b/src/ui/RoutedEventDispatch.hpp index 9337e9ec..b6e0999b 100644 --- a/src/ui/RoutedEventDispatch.hpp +++ b/src/ui/RoutedEventDispatch.hpp @@ -2,6 +2,7 @@ #include "cru/ui/Control.hpp" #include "cru/common/Logger.hpp" +#include "cru/ui/DebugFlags.hpp" #include @@ -24,25 +25,14 @@ void DispatchEvent(const std::u16string_view& event_name, Control* const original_sender, event::RoutedEvent* (Control::*event_ptr)(), Control* const last_receiver, Args&&... args) { -#ifndef CRU_DEBUG CRU_UNUSED(event_name) -#endif - -#ifdef CRU_DEBUG - bool do_log = true; - if (event_name == u"MouseMove") do_log = false; -#endif if (original_sender == last_receiver) { - /* - #ifdef CRU_DEBUG - if (do_log) - log::Debug( - "Routed event {} no need to dispatch (original_sender == " - "last_receiver). Original sender is {}.", - event_name, original_sender->GetControlType()); - #endif - */ + if constexpr (debug_flags::routed_event) + log::Debug( + "Routed event {} no need to dispatch (original_sender == " + "last_receiver). Original sender is {}.", + event_name, original_sender->GetControlType()); return; } @@ -54,8 +44,7 @@ void DispatchEvent(const std::u16string_view& event_name, parent = parent->GetParent(); } -#ifdef CRU_DEBUG - if (do_log) { + if constexpr (debug_flags::routed_event) { std::u16string log = u"Dispatch routed event "; log += event_name; log += u". Path (parent first): "; @@ -68,31 +57,24 @@ void DispatchEvent(const std::u16string_view& event_name, log += (*i)->GetControlType(); log::Debug(log); } -#endif auto handled = false; -#ifdef CRU_DEBUG int count = 0; -#endif // tunnel for (auto i = receive_list.crbegin(); i != receive_list.crend(); ++i) { -#ifdef CRU_DEBUG count++; -#endif EventArgs event_args(*i, original_sender, std::forward(args)...); static_cast*>(((*i)->*event_ptr)()->Tunnel()) ->Raise(event_args); if (event_args.IsHandled()) { handled = true; -#ifdef CRU_DEBUG - if (do_log) + if constexpr (debug_flags::routed_event) log::Debug( u"Routed event is short-circuit in TUNNEL at {}-st control (count " u"from parent).", count); -#endif break; } } @@ -100,20 +82,16 @@ void DispatchEvent(const std::u16string_view& event_name, // bubble if (!handled) { for (auto i : receive_list) { -#ifdef CRU_DEBUG count--; -#endif EventArgs event_args(i, original_sender, std::forward(args)...); static_cast*>((i->*event_ptr)()->Bubble()) ->Raise(event_args); if (event_args.IsHandled()) { -#ifdef CRU_DEBUG - if (do_log) + if constexpr (debug_flags::routed_event) log::Debug( u"Routed event is short-circuit in BUBBLE at {}-st control " u"(count from parent).", count); -#endif break; } } @@ -126,8 +104,7 @@ void DispatchEvent(const std::u16string_view& event_name, ->Raise(event_args); } -#ifdef CRU_DEBUG - if (do_log) log::Debug(u"Routed event dispatch finished."); -#endif + if constexpr (debug_flags::routed_event) + log::Debug(u"Routed event dispatch finished."); } } // namespace cru::ui diff --git a/src/ui/UiHost.cpp b/src/ui/UiHost.cpp index ccc5fea0..a881c16d 100644 --- a/src/ui/UiHost.cpp +++ b/src/ui/UiHost.cpp @@ -6,6 +6,7 @@ #include "cru/platform/native/InputMethod.hpp" #include "cru/platform/native/UiApplication.hpp" #include "cru/platform/native/Window.hpp" +#include "cru/ui/DebugFlags.hpp" #include "cru/ui/Window.hpp" #include "cru/ui/render/MeasureRequirement.hpp" #include "cru/ui/render/WindowRenderObject.hpp" @@ -156,7 +157,8 @@ void UiHost::InvalidatePaint() { } void UiHost::InvalidateLayout() { - log::TagDebug(log_tag, u"A relayout is requested."); + if constexpr (debug_flags::layout) + log::TagDebug(log_tag, u"A relayout is requested."); if (!need_layout_) { platform::native::IUiApplication::GetInstance()->SetImmediate( [resolver = this->CreateResolver()] { @@ -196,7 +198,8 @@ void UiHost::Relayout() { for (auto& action : after_layout_stable_action_) action(); after_layout_stable_action_.clear(); after_layout_event_.Raise(AfterLayoutEventArgs{}); - log::TagDebug(log_tag, u"A relayout is finished."); + if constexpr (debug_flags::layout) + log::TagDebug(log_tag, u"A relayout is finished."); } bool UiHost::RequestFocusFor(Control* control) { diff --git a/src/ui/controls/TextControlService.hpp b/src/ui/controls/TextControlService.hpp index fe33f459..b4102a49 100644 --- a/src/ui/controls/TextControlService.hpp +++ b/src/ui/controls/TextControlService.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include "../Helper.hpp" #include "cru/common/Logger.hpp" #include "cru/common/StringUtil.hpp" @@ -10,13 +9,15 @@ #include "cru/platform/native/Window.hpp" #include "cru/ui/Base.hpp" #include "cru/ui/Control.hpp" +#include "cru/ui/DebugFlags.hpp" #include "cru/ui/ShortcutHub.hpp" #include "cru/ui/UiEvent.hpp" #include "cru/ui/UiHost.hpp" #include "cru/ui/render/CanvasRenderObject.hpp" #include "cru/ui/render/ScrollRenderObject.hpp" #include "cru/ui/render/TextRenderObject.hpp" -#include "gsl/gsl_util" + +#include namespace cru::ui::controls { constexpr int k_default_caret_blink_duration = 500; @@ -262,15 +263,17 @@ class TextControlService : public Object { void StartSelection(Index start) { SetSelection(start); - log::TagDebug(log_tag, u"Text selection started, position: {}.", start); + if constexpr (debug_flags::text_service) + log::TagDebug(log_tag, u"Text selection started, position: {}.", start); } void UpdateSelection(Index new_end) { auto selection = GetSelection(); selection.AdjustEnd(new_end); this->SetSelection(selection); - log::TagDebug(log_tag, u"Text selection updated, range: {}, {}.", - selection.GetStart(), selection.GetEnd()); + if constexpr (debug_flags::text_service) + log::TagDebug(log_tag, u"Text selection updated, range: {}, {}.", + selection.GetStart(), selection.GetEnd()); } template diff --git a/src/ui/render/ScrollRenderObject.cpp b/src/ui/render/ScrollRenderObject.cpp index 08ce744b..24ea351a 100644 --- a/src/ui/render/ScrollRenderObject.cpp +++ b/src/ui/render/ScrollRenderObject.cpp @@ -138,7 +138,6 @@ Size ScrollRenderObject::OnMeasureContent(const MeasureRequirement& requirement, void ScrollRenderObject::OnLayoutContent(const Rect& content_rect) { if (const auto child = GetSingleChild()) { - const auto child_size = child->GetSize(); child->Layout(content_rect.GetLeftTop() - GetScrollOffset()); } } -- cgit v1.2.3 From 6ce70ed4b08c7db20b6b384be26c7fc4e11c98de Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Oct 2020 20:51:55 +0800 Subject: ... --- demos/main/main.cpp | 4 +- include/cru/ui/Base.hpp | 2 +- include/cru/ui/Control.hpp | 12 +- include/cru/ui/UiHost.hpp | 185 ------------ include/cru/ui/Window.hpp | 6 +- include/cru/ui/WindowHost.hpp | 185 ++++++++++++ include/cru/ui/render/RenderObject.hpp | 6 +- include/cru/ui/render/WindowRenderObject.hpp | 2 +- src/ui/CMakeLists.txt | 4 +- src/ui/ContentControl.cpp | 6 +- src/ui/Control.cpp | 20 +- src/ui/LayoutControl.cpp | 4 +- src/ui/UiHost.cpp | 416 -------------------------- src/ui/Window.cpp | 4 +- src/ui/WindowHost.cpp | 418 +++++++++++++++++++++++++++ src/ui/controls/TextControlService.hpp | 6 +- src/ui/render/RenderObject.cpp | 6 +- src/ui/render/WindowRenderObject.cpp | 4 +- 18 files changed, 646 insertions(+), 644 deletions(-) delete mode 100644 include/cru/ui/UiHost.hpp create mode 100644 include/cru/ui/WindowHost.hpp delete mode 100644 src/ui/UiHost.cpp create mode 100644 src/ui/WindowHost.cpp (limited to 'src/ui/UiHost.cpp') diff --git a/demos/main/main.cpp b/demos/main/main.cpp index 10b43b0f..f7635231 100644 --- a/demos/main/main.cpp +++ b/demos/main/main.cpp @@ -2,7 +2,7 @@ #include "cru/platform/native/UiApplication.hpp" #include "cru/platform/native/Window.hpp" #include "cru/ui/Base.hpp" -#include "cru/ui/UiHost.hpp" +#include "cru/ui/WindowHost.hpp" #include "cru/ui/Window.hpp" #include "cru/ui/controls/Button.hpp" #include "cru/ui/controls/FlexLayout.hpp" @@ -45,7 +45,7 @@ int main() { const auto text_box = TextBox::Create(); flex_layout->AddChild(text_box, 2); - window->GetUiHost()->GetNativeWindowResolver()->Resolve()->SetVisible(true); + window->GetWindowHost()->GetNativeWindowResolver()->Resolve()->SetVisible(true); return application->Run(); } diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index 6be359ab..dd93f187 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -29,7 +29,7 @@ namespace colors = cru::platform::colors; class Window; class Control; class ClickDetector; -class UiHost; +class WindowHost; namespace render { class RenderObject; diff --git a/include/cru/ui/Control.hpp b/include/cru/ui/Control.hpp index bd86bc2f..692dcc9b 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 UiHost; + friend WindowHost; protected: Control(); @@ -27,7 +27,7 @@ class Control : public Object { //*************** region: tree *************** public: // Get the ui host if attached, otherwise, return nullptr. - UiHost* GetUiHost() const { return ui_host_; } + WindowHost* GetWindowHost() const { return ui_host_; } Control* GetParent() const { return parent_; } @@ -37,7 +37,7 @@ class Control : public Object { void TraverseDescendants(const std::function& predicate); void _SetParent(Control* parent); - void _SetDescendantUiHost(UiHost* host); + void _SetDescendantWindowHost(WindowHost* host); private: static void _TraverseDescendants( @@ -135,13 +135,13 @@ class Control : public Object { //*************** region: tree *************** protected: virtual void OnParentChanged(Control* old_parent, Control* new_parent); - virtual void OnAttachToHost(UiHost* host); - virtual void OnDetachFromHost(UiHost* host); + virtual void OnAttachToHost(WindowHost* host); + virtual void OnDetachFromHost(WindowHost* host); virtual void OnMouseHoverChange(bool newHover) { CRU_UNUSED(newHover) } private: - UiHost* ui_host_ = nullptr; + WindowHost* ui_host_ = nullptr; Control* parent_ = nullptr; private: diff --git a/include/cru/ui/UiHost.hpp b/include/cru/ui/UiHost.hpp deleted file mode 100644 index 2437b967..00000000 --- a/include/cru/ui/UiHost.hpp +++ /dev/null @@ -1,185 +0,0 @@ -#pragma once -#include -#include "Base.hpp" - -#include "cru/common/Event.hpp" -#include "cru/common/SelfResolvable.hpp" -#include "render/Base.hpp" - -namespace cru::ui { -struct AfterLayoutEventArgs {}; - -// The host of all controls and render objects. -// -// 3 situations on destroy: -// 1. Native window destroyed, IsRetainAfterDestroy: false: -// OnNativeDestroy(set native_window_destroyed_ to true, call ~Window due to -// deleting_ is false and IsRetainAfterDestroy is false) -> ~Window -> -// ~UiHost(not destroy native window repeatedly due to native_window_destroyed_ -// is true) -// 2. Native window destroyed, IsRetainAfterDestroy: true: -// OnNativeDestroy(set native_window_destroyed_ to true, not call ~Window -// because deleting_ is false and IsRetainAfterDestroy is true) -// then, ~Window -> ~UiHost(not destroy native window repeatedly due to -// native_window_destroyed_ is true) -// 3. Native window not destroyed, ~Window is called: -// ~Window -> ~UiHost(set deleting_ to true, destroy native window -// due to native_window_destroyed is false) -> OnNativeDestroy(not call ~Window -// due to deleting_ is true and IsRetainAfterDestroy is whatever) -// In conclusion: -// 1. Set native_window_destroyed_ to true at the beginning of OnNativeDestroy. -// 2. Set deleting_ to true at the beginning of ~UiHost. -// 3. Destroy native window when native_window_destroy_ is false in ~Window. -// 4. Delete Window when deleting_ is false and IsRetainAfterDestroy is false in -// OnNativeDestroy. -class UiHost : public Object, public SelfResolvable { - CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::UiHost") - - public: - // This will create root window render object and attach it to window. - // It will also create and manage a native window. - UiHost(Window* window); - - CRU_DELETE_COPY(UiHost) - CRU_DELETE_MOVE(UiHost) - - ~UiHost() override; - - public: - // 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_; - } - - // 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); - - void Relayout(); - - // 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 *************** - - // Request focus for specified control. - bool RequestFocusFor(Control* control); - - // Get the control that has focus. - Control* GetFocusControl(); - - //*************** 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(); - - std::shared_ptr - GetNativeWindowResolver() { - return native_window_resolver_; - } - - bool IsRetainAfterDestroy() { return retain_after_destroy_; } - - void SetRetainAfterDestroy(bool destroy) { retain_after_destroy_ = destroy; } - - // Is layout is invalid, wait for relayout and then run the action. Otherwist - // run it right now. - void RunAfterLayoutStable(std::function action); - - private: - //*************** region: native messages *************** - void OnNativeDestroy(platform::native::INativeWindow* window, std::nullptr_t); - void OnNativePaint(platform::native::INativeWindow* window, std::nullptr_t); - void OnNativeResize(platform::native::INativeWindow* window, - const Size& size); - - void OnNativeFocus(platform::native::INativeWindow* window, - cru::platform::native::FocusChangeType focus); - - void OnNativeMouseEnterLeave( - platform::native::INativeWindow* window, - cru::platform::native::MouseEnterLeaveType enter); - void OnNativeMouseMove(platform::native::INativeWindow* window, - const Point& point); - void OnNativeMouseDown( - platform::native::INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args); - void OnNativeMouseUp( - platform::native::INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args); - - void OnNativeKeyDown(platform::native::INativeWindow* window, - const platform::native::NativeKeyEventArgs& args); - void OnNativeKeyUp(platform::native::INativeWindow* window, - const platform::native::NativeKeyEventArgs& args); - - //*************** region: event dispatcher helper *************** - - void DispatchMouseHoverControlChangeEvent(Control* old_control, - Control* new_control, - const Point& point, bool no_leave, - bool no_enter); - - private: - bool need_layout_ = false; - - Event after_layout_event_; - std::vector > after_layout_stable_action_; - - std::shared_ptr - native_window_resolver_; - - // See remarks of UiHost. - bool retain_after_destroy_ = false; - // See remarks of UiHost. - bool deleting_ = false; - - // We need this because calling Resolve on resolver in handler of destroy - // event is bad and will always get the dying window. But we need to label the - // window as destroyed so the destructor will not destroy native window - // repeatedly. See remarks of UiHost. - bool native_window_destroyed_ = false; - - std::vector event_revoker_guards_; - - Window* window_control_; - std::unique_ptr root_render_object_; - - Control* mouse_hover_control_; - - Control* focus_control_; // "focus_control_" can't be nullptr - - Control* mouse_captured_control_; - - bool layout_prefer_to_fill_window_ = true; -}; -} // namespace cru::ui diff --git a/include/cru/ui/Window.hpp b/include/cru/ui/Window.hpp index 450ea97b..5ea24855 100644 --- a/include/cru/ui/Window.hpp +++ b/include/cru/ui/Window.hpp @@ -3,7 +3,7 @@ namespace cru::ui { class Window final : public ContentControl { - friend UiHost; + friend WindowHost; public: static constexpr std::u16string_view control_type = u"Window"; @@ -32,9 +32,9 @@ class Window final : public ContentControl { void OnChildChanged(Control* old_child, Control* new_child) override; private: - std::unique_ptr managed_ui_host_; + std::unique_ptr managed_ui_host_; - // UiHost is responsible to take care of lifetime of this. + // WindowHost is responsible to take care of lifetime of this. render::WindowRenderObject* render_object_; }; } // namespace cru::ui diff --git a/include/cru/ui/WindowHost.hpp b/include/cru/ui/WindowHost.hpp new file mode 100644 index 00000000..64116590 --- /dev/null +++ b/include/cru/ui/WindowHost.hpp @@ -0,0 +1,185 @@ +#pragma once +#include +#include "Base.hpp" + +#include "cru/common/Event.hpp" +#include "cru/common/SelfResolvable.hpp" +#include "render/Base.hpp" + +namespace cru::ui { +struct AfterLayoutEventArgs {}; + +// The host of all controls and render objects. +// +// 3 situations on destroy: +// 1. Native window destroyed, IsRetainAfterDestroy: false: +// OnNativeDestroy(set native_window_destroyed_ to true, call ~Window due to +// deleting_ is false and IsRetainAfterDestroy is false) -> ~Window -> +// ~WindowHost(not destroy native window repeatedly due to native_window_destroyed_ +// is true) +// 2. Native window destroyed, IsRetainAfterDestroy: true: +// OnNativeDestroy(set native_window_destroyed_ to true, not call ~Window +// because deleting_ is false and IsRetainAfterDestroy is true) +// then, ~Window -> ~WindowHost(not destroy native window repeatedly due to +// native_window_destroyed_ is true) +// 3. Native window not destroyed, ~Window is called: +// ~Window -> ~WindowHost(set deleting_ to true, destroy native window +// due to native_window_destroyed is false) -> OnNativeDestroy(not call ~Window +// due to deleting_ is true and IsRetainAfterDestroy is whatever) +// In conclusion: +// 1. Set native_window_destroyed_ to true at the beginning of OnNativeDestroy. +// 2. Set deleting_ to true at the beginning of ~WindowHost. +// 3. Destroy native window when native_window_destroy_ is false in ~Window. +// 4. Delete Window when deleting_ is false and IsRetainAfterDestroy is false in +// OnNativeDestroy. +class WindowHost : public Object, public SelfResolvable { + CRU_DEFINE_CLASS_LOG_TAG(u"cru::ui::WindowHost") + + public: + // This will create root window render object and attach it to window. + // It will also create and manage a native window. + WindowHost(Window* window); + + CRU_DELETE_COPY(WindowHost) + CRU_DELETE_MOVE(WindowHost) + + ~WindowHost() override; + + public: + // 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_; + } + + // 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); + + void Relayout(); + + // 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 *************** + + // Request focus for specified control. + bool RequestFocusFor(Control* control); + + // Get the control that has focus. + Control* GetFocusControl(); + + //*************** 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(); + + std::shared_ptr + GetNativeWindowResolver() { + return native_window_resolver_; + } + + bool IsRetainAfterDestroy() { return retain_after_destroy_; } + + void SetRetainAfterDestroy(bool destroy) { retain_after_destroy_ = destroy; } + + // Is layout is invalid, wait for relayout and then run the action. Otherwist + // run it right now. + void RunAfterLayoutStable(std::function action); + + private: + //*************** region: native messages *************** + void OnNativeDestroy(platform::native::INativeWindow* window, std::nullptr_t); + void OnNativePaint(platform::native::INativeWindow* window, std::nullptr_t); + void OnNativeResize(platform::native::INativeWindow* window, + const Size& size); + + void OnNativeFocus(platform::native::INativeWindow* window, + cru::platform::native::FocusChangeType focus); + + void OnNativeMouseEnterLeave( + platform::native::INativeWindow* window, + cru::platform::native::MouseEnterLeaveType enter); + void OnNativeMouseMove(platform::native::INativeWindow* window, + const Point& point); + void OnNativeMouseDown( + platform::native::INativeWindow* window, + const platform::native::NativeMouseButtonEventArgs& args); + void OnNativeMouseUp( + platform::native::INativeWindow* window, + const platform::native::NativeMouseButtonEventArgs& args); + + void OnNativeKeyDown(platform::native::INativeWindow* window, + const platform::native::NativeKeyEventArgs& args); + void OnNativeKeyUp(platform::native::INativeWindow* window, + const platform::native::NativeKeyEventArgs& args); + + //*************** region: event dispatcher helper *************** + + void DispatchMouseHoverControlChangeEvent(Control* old_control, + Control* new_control, + const Point& point, bool no_leave, + bool no_enter); + + private: + bool need_layout_ = false; + + Event after_layout_event_; + std::vector > after_layout_stable_action_; + + std::shared_ptr + native_window_resolver_; + + // See remarks of WindowHost. + bool retain_after_destroy_ = false; + // See remarks of WindowHost. + bool deleting_ = false; + + // We need this because calling Resolve on resolver in handler of destroy + // event is bad and will always get the dying window. But we need to label the + // window as destroyed so the destructor will not destroy native window + // repeatedly. See remarks of WindowHost. + bool native_window_destroyed_ = false; + + std::vector event_revoker_guards_; + + Window* window_control_; + std::unique_ptr root_render_object_; + + Control* mouse_hover_control_; + + Control* focus_control_; // "focus_control_" can't be nullptr + + Control* mouse_captured_control_; + + 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 57251e3a..f052221e 100644 --- a/include/cru/ui/render/RenderObject.hpp +++ b/include/cru/ui/render/RenderObject.hpp @@ -64,7 +64,7 @@ class RenderObject : public Object { Control* GetAttachedControl() const { return control_; } void SetAttachedControl(Control* new_control) { control_ = new_control; } - UiHost* GetUiHost() const { return ui_host_; } + WindowHost* GetWindowHost() const { return ui_host_; } RenderObject* GetParent() const { return parent_; } @@ -198,11 +198,11 @@ class RenderObject : public Object { private: void SetParent(RenderObject* new_parent); - void SetRenderHostRecursive(UiHost* host); + void SetRenderHostRecursive(WindowHost* host); private: Control* control_ = nullptr; - UiHost* ui_host_ = nullptr; + WindowHost* ui_host_ = nullptr; RenderObject* parent_ = nullptr; std::vector children_{}; diff --git a/include/cru/ui/render/WindowRenderObject.hpp b/include/cru/ui/render/WindowRenderObject.hpp index d2ca5526..23fd8748 100644 --- a/include/cru/ui/render/WindowRenderObject.hpp +++ b/include/cru/ui/render/WindowRenderObject.hpp @@ -6,7 +6,7 @@ namespace cru::ui::render { class WindowRenderObject : public RenderObject { public: - WindowRenderObject(UiHost* host); + WindowRenderObject(WindowHost* host); WindowRenderObject(const WindowRenderObject& other) = delete; WindowRenderObject(WindowRenderObject&& other) = delete; WindowRenderObject& operator=(const WindowRenderObject& other) = delete; diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index a83ab1d7..d59fd7da 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -12,9 +12,9 @@ add_library(cru_ui STATIC NoChildControl.cpp ShortcutHub.cpp UiEvent.cpp - UiHost.cpp UiManager.cpp Window.cpp + WindowHost.cpp controls/Button.cpp controls/Container.cpp controls/FlexLayout.cpp @@ -42,9 +42,9 @@ target_sources(cru_ui PUBLIC ${CRU_UI_INCLUDE_DIR}/NoChildControl.hpp ${CRU_UI_INCLUDE_DIR}/ShortcutHub.hpp ${CRU_UI_INCLUDE_DIR}/UiEvent.hpp - ${CRU_UI_INCLUDE_DIR}/UiHost.hpp ${CRU_UI_INCLUDE_DIR}/UiManager.hpp ${CRU_UI_INCLUDE_DIR}/Window.hpp + ${CRU_UI_INCLUDE_DIR}/WindowHost.hpp ${CRU_UI_INCLUDE_DIR}/controls/Base.hpp ${CRU_UI_INCLUDE_DIR}/controls/Button.hpp ${CRU_UI_INCLUDE_DIR}/controls/Container.hpp diff --git a/src/ui/ContentControl.cpp b/src/ui/ContentControl.cpp index 8d1a17d2..60d944d6 100644 --- a/src/ui/ContentControl.cpp +++ b/src/ui/ContentControl.cpp @@ -12,16 +12,16 @@ void ContentControl::SetChild(Control* child) { Expects(!dynamic_cast(child)); // Can't add a window as child. if (child == child_) return; - const auto host = GetUiHost(); + const auto host = GetWindowHost(); const auto old_child = child_; child_ = child; if (old_child) { old_child->_SetParent(nullptr); - old_child->_SetDescendantUiHost(nullptr); + old_child->_SetDescendantWindowHost(nullptr); } if (child) { child->_SetParent(this); - child->_SetDescendantUiHost(host); + child->_SetDescendantWindowHost(host); } OnChildChanged(old_child, child); } diff --git a/src/ui/Control.cpp b/src/ui/Control.cpp index cd1367fe..cd7ed0dc 100644 --- a/src/ui/Control.cpp +++ b/src/ui/Control.cpp @@ -3,7 +3,7 @@ #include "cru/platform/native/Cursor.hpp" #include "cru/platform/native/UiApplication.hpp" #include "cru/ui/Base.hpp" -#include "cru/ui/UiHost.hpp" +#include "cru/ui/WindowHost.hpp" #include "RoutedEventDispatch.hpp" #include @@ -32,7 +32,7 @@ void Control::_SetParent(Control* parent) { if (old_parent != new_parent) OnParentChanged(old_parent, new_parent); } -void Control::_SetDescendantUiHost(UiHost* host) { +void Control::_SetDescendantWindowHost(WindowHost* host) { if (host == nullptr && ui_host_ == nullptr) return; // You can only attach or detach window. @@ -64,35 +64,35 @@ void Control::_TraverseDescendants( } bool Control::RequestFocus() { - auto host = GetUiHost(); + auto host = GetWindowHost(); if (host == nullptr) return false; return host->RequestFocusFor(this); } bool Control::HasFocus() { - auto host = GetUiHost(); + auto host = GetWindowHost(); if (host == nullptr) return false; return host->GetFocusControl() == this; } bool Control::CaptureMouse() { - auto host = GetUiHost(); + auto host = GetWindowHost(); if (host == nullptr) return false; return host->CaptureMouseFor(this); } bool Control::ReleaseMouse() { - auto host = GetUiHost(); + auto host = GetWindowHost(); if (host == nullptr) return false; return host->CaptureMouseFor(nullptr); } bool Control::IsMouseCaptured() { - auto host = GetUiHost(); + auto host = GetWindowHost(); if (host == nullptr) return false; return host->GetMouseCaptureControl() == this; @@ -113,7 +113,7 @@ std::shared_ptr Control::GetInheritedCursor() { void Control::SetCursor(std::shared_ptr cursor) { cursor_ = std::move(cursor); - const auto host = GetUiHost(); + const auto host = GetWindowHost(); if (host != nullptr) { host->UpdateCursor(); } @@ -124,7 +124,7 @@ void Control::OnParentChanged(Control* old_parent, Control* new_parent) { CRU_UNUSED(new_parent) } -void Control::OnAttachToHost(UiHost* host) { CRU_UNUSED(host) } +void Control::OnAttachToHost(WindowHost* host) { CRU_UNUSED(host) } -void Control::OnDetachFromHost(UiHost* host) { CRU_UNUSED(host) } +void Control::OnDetachFromHost(WindowHost* host) { CRU_UNUSED(host) } } // namespace cru::ui diff --git a/src/ui/LayoutControl.cpp b/src/ui/LayoutControl.cpp index 4813566b..05fcdc4a 100644 --- a/src/ui/LayoutControl.cpp +++ b/src/ui/LayoutControl.cpp @@ -19,7 +19,7 @@ void LayoutControl::AddChild(Control* control, const Index position) { children_.insert(this->children_.cbegin() + position, control); control->_SetParent(this); - control->_SetDescendantUiHost(GetUiHost()); + control->_SetDescendantWindowHost(GetWindowHost()); OnAddChild(control, position); } @@ -36,7 +36,7 @@ void LayoutControl::RemoveChild(const Index position) { children_.erase(i); child->_SetParent(nullptr); - child->_SetDescendantUiHost(nullptr); + child->_SetDescendantWindowHost(nullptr); OnRemoveChild(child, position); } diff --git a/src/ui/UiHost.cpp b/src/ui/UiHost.cpp deleted file mode 100644 index a881c16d..00000000 --- a/src/ui/UiHost.cpp +++ /dev/null @@ -1,416 +0,0 @@ -#include "cru/ui/UiHost.hpp" - -#include "RoutedEventDispatch.hpp" -#include "cru/common/Logger.hpp" -#include "cru/platform/graph/Painter.hpp" -#include "cru/platform/native/InputMethod.hpp" -#include "cru/platform/native/UiApplication.hpp" -#include "cru/platform/native/Window.hpp" -#include "cru/ui/DebugFlags.hpp" -#include "cru/ui/Window.hpp" -#include "cru/ui/render/MeasureRequirement.hpp" -#include "cru/ui/render/WindowRenderObject.hpp" - -namespace cru::ui { -using platform::native::INativeWindow; -using platform::native::IUiApplication; - -namespace event_names { -#ifdef CRU_DEBUG -// clang-format off -#define CRU_DEFINE_EVENT_NAME(name) constexpr const char16_t* name = CRU_MAKE_UNICODE_LITERAL(name); -// clang-format on -#else -#define CRU_DEFINE_EVENT_NAME(name) constexpr const char16_t* name = u""; -#endif - -CRU_DEFINE_EVENT_NAME(LoseFocus) -CRU_DEFINE_EVENT_NAME(GainFocus) -CRU_DEFINE_EVENT_NAME(MouseEnter) -CRU_DEFINE_EVENT_NAME(MouseLeave) -CRU_DEFINE_EVENT_NAME(MouseMove) -CRU_DEFINE_EVENT_NAME(MouseDown) -CRU_DEFINE_EVENT_NAME(MouseUp) -CRU_DEFINE_EVENT_NAME(KeyDown) -CRU_DEFINE_EVENT_NAME(KeyUp) - -#undef CRU_DEFINE_EVENT_NAME -} // namespace event_names - -namespace { -bool IsAncestor(Control* control, Control* ancestor) { - while (control != nullptr) { - if (control == ancestor) return true; - control = control->GetParent(); - } - return false; -} - -// Ancestor at last. -std::vector GetAncestorList(Control* control) { - std::vector l; - while (control != nullptr) { - l.push_back(control); - control = control->GetParent(); - } - return l; -} - -Control* FindLowestCommonAncestor(Control* left, Control* right) { - if (left == nullptr || right == nullptr) return nullptr; - - auto&& left_list = GetAncestorList(left); - auto&& right_list = GetAncestorList(right); - - // the root is different - if (left_list.back() != right_list.back()) return nullptr; - - // find the last same control or the last control (one is ancestor of the - // other) - auto left_iter = left_list.crbegin(); - auto right_iter = right_list.crbegin(); - - while (true) { - if (left_iter == left_list.crend()) { - return left_list.front(); - } - if (right_iter == right_list.crend()) { - return right_list.front(); - } - if (*left_iter != *right_iter) { - return *(--left_iter); - } - ++left_iter; - ++right_iter; - } -} -} // namespace - -namespace { -template -inline void BindNativeEvent( - UiHost* host, INativeWindow* native_window, IEvent* event, - void (UiHost::*handler)(INativeWindow*, typename IEvent::EventArgs), - std::vector& guard_pool) { - guard_pool.push_back(EventRevokerGuard(event->AddHandler( - std::bind(handler, host, native_window, std::placeholders::_1)))); -} -} // namespace - -UiHost::UiHost(Window* window) - : window_control_(window), - mouse_hover_control_(nullptr), - focus_control_(window), - mouse_captured_control_(nullptr) { - const auto ui_application = IUiApplication::GetInstance(); - native_window_resolver_ = ui_application->CreateWindow(nullptr); - - const auto native_window = native_window_resolver_->Resolve(); - - auto input_method_context = - ui_application->GetInputMethodManager()->GetContext(native_window); - input_method_context->DisableIME(); - - window->ui_host_ = this; - - root_render_object_ = std::make_unique(this); - root_render_object_->SetAttachedControl(window); - window->render_object_ = root_render_object_.get(); - - BindNativeEvent(this, native_window, native_window->DestroyEvent(), - &UiHost::OnNativeDestroy, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->PaintEvent(), - &UiHost::OnNativePaint, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->ResizeEvent(), - &UiHost::OnNativeResize, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->FocusEvent(), - &UiHost::OnNativeFocus, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->MouseEnterLeaveEvent(), - &UiHost::OnNativeMouseEnterLeave, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->MouseMoveEvent(), - &UiHost::OnNativeMouseMove, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->MouseDownEvent(), - &UiHost::OnNativeMouseDown, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->MouseUpEvent(), - &UiHost::OnNativeMouseUp, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->KeyDownEvent(), - &UiHost::OnNativeKeyDown, event_revoker_guards_); - BindNativeEvent(this, native_window, native_window->KeyUpEvent(), - &UiHost::OnNativeKeyUp, event_revoker_guards_); -} - -UiHost::~UiHost() { - deleting_ = true; - window_control_->TraverseDescendants( - [this](Control* control) { control->OnDetachFromHost(this); }); - if (!native_window_destroyed_) { - const auto native_window = native_window_resolver_->Resolve(); - if (native_window) { - native_window->Close(); - } - } -} - -void UiHost::InvalidatePaint() { - if (const auto native_window = native_window_resolver_->Resolve()) - native_window->RequestRepaint(); -} - -void UiHost::InvalidateLayout() { - if constexpr (debug_flags::layout) - log::TagDebug(log_tag, u"A relayout is requested."); - if (!need_layout_) { - platform::native::IUiApplication::GetInstance()->SetImmediate( - [resolver = this->CreateResolver()] { - if (const auto host = resolver.Resolve()) { - host->Relayout(); - host->need_layout_ = false; - host->InvalidatePaint(); - } - }); - need_layout_ = true; - } -} - -bool UiHost::IsLayoutPreferToFillWindow() const { - return layout_prefer_to_fill_window_; -} - -void UiHost::SetLayoutPreferToFillWindow(bool value) { - if (value == layout_prefer_to_fill_window_) return; - layout_prefer_to_fill_window_ = value; - InvalidateLayout(); -} - -void UiHost::Relayout() { - const auto native_window = native_window_resolver_->Resolve(); - const auto client_size = native_window - ? native_window->GetClientSize() - : Size{100, 100}; // a reasonable assumed size - - root_render_object_->Measure( - render::MeasureRequirement{client_size, - IsLayoutPreferToFillWindow() - ? render::MeasureSize(client_size) - : render::MeasureSize::NotSpecified()}, - render::MeasureSize::NotSpecified()); - root_render_object_->Layout(Point{}); - for (auto& action : after_layout_stable_action_) action(); - after_layout_stable_action_.clear(); - after_layout_event_.Raise(AfterLayoutEventArgs{}); - if constexpr (debug_flags::layout) - log::TagDebug(log_tag, u"A relayout is finished."); -} - -bool UiHost::RequestFocusFor(Control* control) { - Expects(control != nullptr); // The control to request focus can't be null. - // You can set it as the window. - - if (focus_control_ == control) return true; - - const auto old_focus_control = focus_control_; - - focus_control_ = control; - - DispatchEvent(event_names::LoseFocus, old_focus_control, - &Control::LoseFocusEvent, nullptr, false); - - DispatchEvent(event_names::GainFocus, control, &Control::GainFocusEvent, - nullptr, false); - - return true; -} - -Control* UiHost::GetFocusControl() { return focus_control_; } - -bool UiHost::CaptureMouseFor(Control* control) { - const auto native_window = native_window_resolver_->Resolve(); - if (!native_window) return false; - - if (control == mouse_captured_control_) return true; - - if (control == nullptr) { - const auto old_capture_control = mouse_captured_control_; - mouse_captured_control_ = - nullptr; // update this in case this is used in event handlers - if (old_capture_control != mouse_hover_control_) { - DispatchMouseHoverControlChangeEvent( - old_capture_control, mouse_hover_control_, - native_window->GetMousePosition(), true, false); - } - UpdateCursor(); - return true; - } - - if (mouse_captured_control_) return false; - - mouse_captured_control_ = control; - DispatchMouseHoverControlChangeEvent( - mouse_hover_control_, mouse_captured_control_, - native_window->GetMousePosition(), false, true); - UpdateCursor(); - return true; -} - -Control* UiHost::GetMouseCaptureControl() { return mouse_captured_control_; } - -void UiHost::RunAfterLayoutStable(std::function action) { - if (need_layout_) { - after_layout_stable_action_.push_back(std::move(action)); - } else { - action(); - } -} - -void UiHost::OnNativeDestroy(INativeWindow* window, std::nullptr_t) { - CRU_UNUSED(window) - native_window_destroyed_ = true; - if (!deleting_ && !retain_after_destroy_) delete window_control_; -} - -void UiHost::OnNativePaint(INativeWindow* window, std::nullptr_t) { - auto painter = window->BeginPaint(); - painter->Clear(colors::white); - root_render_object_->Draw(painter.get()); - painter->EndDraw(); -} - -void UiHost::OnNativeResize(INativeWindow* window, const Size& size) { - CRU_UNUSED(window) - CRU_UNUSED(size) - - InvalidateLayout(); -} - -void UiHost::OnNativeFocus(INativeWindow* window, - platform::native::FocusChangeType focus) { - CRU_UNUSED(window) - - focus == platform::native::FocusChangeType::Gain - ? DispatchEvent(event_names::GainFocus, focus_control_, - &Control::GainFocusEvent, nullptr, true) - : DispatchEvent(event_names::LoseFocus, focus_control_, - &Control::LoseFocusEvent, nullptr, true); -} - -void UiHost::OnNativeMouseEnterLeave( - INativeWindow* window, platform::native::MouseEnterLeaveType type) { - CRU_UNUSED(window) - - if (type == platform::native::MouseEnterLeaveType::Leave) { - DispatchEvent(event_names::MouseLeave, mouse_hover_control_, - &Control::MouseLeaveEvent, nullptr); - mouse_hover_control_ = nullptr; - } -} - -void UiHost::OnNativeMouseMove(INativeWindow* window, const Point& point) { - CRU_UNUSED(window) - - // Find the first control that hit test succeed. - const auto new_mouse_hover_control = HitTest(point); - const auto old_mouse_hover_control = mouse_hover_control_; - mouse_hover_control_ = new_mouse_hover_control; - - if (mouse_captured_control_) { - const auto n = FindLowestCommonAncestor(new_mouse_hover_control, - mouse_captured_control_); - const auto o = FindLowestCommonAncestor(old_mouse_hover_control, - mouse_captured_control_); - bool a = IsAncestor(o, n); - if (a) { - DispatchEvent(event_names::MouseLeave, o, &Control::MouseLeaveEvent, n); - } else { - DispatchEvent(event_names::MouseEnter, n, &Control::MouseEnterEvent, o, - point); - } - DispatchEvent(event_names::MouseMove, mouse_captured_control_, - &Control::MouseMoveEvent, nullptr, point); - UpdateCursor(); - return; - } - - DispatchMouseHoverControlChangeEvent( - old_mouse_hover_control, new_mouse_hover_control, point, false, false); - DispatchEvent(event_names::MouseMove, new_mouse_hover_control, - &Control::MouseMoveEvent, nullptr, point); - UpdateCursor(); -} - -void UiHost::OnNativeMouseDown( - INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args) { - CRU_UNUSED(window) - - Control* control = - mouse_captured_control_ ? mouse_captured_control_ : HitTest(args.point); - DispatchEvent(event_names::MouseDown, control, &Control::MouseDownEvent, - nullptr, args.point, args.button, args.modifier); -} - -void UiHost::OnNativeMouseUp( - INativeWindow* window, - const platform::native::NativeMouseButtonEventArgs& args) { - CRU_UNUSED(window) - - Control* control = - mouse_captured_control_ ? mouse_captured_control_ : HitTest(args.point); - DispatchEvent(event_names::MouseUp, control, &Control::MouseUpEvent, nullptr, - args.point, args.button, args.modifier); -} - -void UiHost::OnNativeKeyDown(INativeWindow* window, - const platform::native::NativeKeyEventArgs& args) { - CRU_UNUSED(window) - - DispatchEvent(event_names::KeyDown, focus_control_, &Control::KeyDownEvent, - nullptr, args.key, args.modifier); -} - -void UiHost::OnNativeKeyUp(INativeWindow* window, - const platform::native::NativeKeyEventArgs& args) { - CRU_UNUSED(window) - - DispatchEvent(event_names::KeyUp, focus_control_, &Control::KeyUpEvent, - nullptr, args.key, args.modifier); -} - -void UiHost::DispatchMouseHoverControlChangeEvent(Control* old_control, - Control* new_control, - const Point& point, - bool no_leave, - bool no_enter) { - if (new_control != old_control) // if the mouse-hover-on control changed - { - const auto lowest_common_ancestor = - FindLowestCommonAncestor(old_control, new_control); - if (!no_leave && old_control != nullptr) - DispatchEvent(event_names::MouseLeave, old_control, - &Control::MouseLeaveEvent, - lowest_common_ancestor); // dispatch mouse leave event. - if (!no_enter && new_control != nullptr) { - DispatchEvent(event_names::MouseEnter, new_control, - &Control::MouseEnterEvent, lowest_common_ancestor, - point); // dispatch mouse enter event. - } - } -} - -void UiHost::UpdateCursor() { - if (const auto native_window = native_window_resolver_->Resolve()) { - const auto capture = GetMouseCaptureControl(); - native_window->SetCursor( - (capture ? capture : GetMouseHoverControl())->GetInheritedCursor()); - } -} - -Control* UiHost::HitTest(const Point& point) { - const auto render_object = root_render_object_->HitTest(point); - if (render_object) { - const auto control = render_object->GetAttachedControl(); - Ensures(control); - return control; - } - return window_control_; -} -} // namespace cru::ui diff --git a/src/ui/Window.cpp b/src/ui/Window.cpp index dca95ebb..a8cb315b 100644 --- a/src/ui/Window.cpp +++ b/src/ui/Window.cpp @@ -1,7 +1,7 @@ #include "cru/ui/Window.hpp" #include "cru/ui/render/WindowRenderObject.hpp" -#include "cru/ui/UiHost.hpp" +#include "cru/ui/WindowHost.hpp" namespace cru::ui { Window* Window::CreateOverlapped() { @@ -9,7 +9,7 @@ Window* Window::CreateOverlapped() { } Window::Window(tag_overlapped_constructor) { - managed_ui_host_ = std::make_unique(this); + managed_ui_host_ = std::make_unique(this); } Window::~Window() { diff --git a/src/ui/WindowHost.cpp b/src/ui/WindowHost.cpp new file mode 100644 index 00000000..d3dec422 --- /dev/null +++ b/src/ui/WindowHost.cpp @@ -0,0 +1,418 @@ +#include "cru/ui/WindowHost.hpp" + +#include "RoutedEventDispatch.hpp" +#include "cru/common/Logger.hpp" +#include "cru/platform/graph/Painter.hpp" +#include "cru/platform/native/InputMethod.hpp" +#include "cru/platform/native/UiApplication.hpp" +#include "cru/platform/native/Window.hpp" +#include "cru/ui/DebugFlags.hpp" +#include "cru/ui/Window.hpp" +#include "cru/ui/render/MeasureRequirement.hpp" +#include "cru/ui/render/WindowRenderObject.hpp" + +namespace cru::ui { +using platform::native::INativeWindow; +using platform::native::IUiApplication; + +namespace event_names { +#ifdef CRU_DEBUG +// clang-format off +#define CRU_DEFINE_EVENT_NAME(name) constexpr const char16_t* name = CRU_MAKE_UNICODE_LITERAL(name); +// clang-format on +#else +#define CRU_DEFINE_EVENT_NAME(name) constexpr const char16_t* name = u""; +#endif + +CRU_DEFINE_EVENT_NAME(LoseFocus) +CRU_DEFINE_EVENT_NAME(GainFocus) +CRU_DEFINE_EVENT_NAME(MouseEnter) +CRU_DEFINE_EVENT_NAME(MouseLeave) +CRU_DEFINE_EVENT_NAME(MouseMove) +CRU_DEFINE_EVENT_NAME(MouseDown) +CRU_DEFINE_EVENT_NAME(MouseUp) +CRU_DEFINE_EVENT_NAME(KeyDown) +CRU_DEFINE_EVENT_NAME(KeyUp) + +#undef CRU_DEFINE_EVENT_NAME +} // namespace event_names + +namespace { +bool IsAncestor(Control* control, Control* ancestor) { + while (control != nullptr) { + if (control == ancestor) return true; + control = control->GetParent(); + } + return false; +} + +// Ancestor at last. +std::vector GetAncestorList(Control* control) { + std::vector l; + while (control != nullptr) { + l.push_back(control); + control = control->GetParent(); + } + return l; +} + +Control* FindLowestCommonAncestor(Control* left, Control* right) { + if (left == nullptr || right == nullptr) return nullptr; + + auto&& left_list = GetAncestorList(left); + auto&& right_list = GetAncestorList(right); + + // the root is different + if (left_list.back() != right_list.back()) return nullptr; + + // find the last same control or the last control (one is ancestor of the + // other) + auto left_iter = left_list.crbegin(); + auto right_iter = right_list.crbegin(); + + while (true) { + if (left_iter == left_list.crend()) { + return left_list.front(); + } + if (right_iter == right_list.crend()) { + return right_list.front(); + } + if (*left_iter != *right_iter) { + return *(--left_iter); + } + ++left_iter; + ++right_iter; + } +} +} // namespace + +namespace { +template +inline void BindNativeEvent( + WindowHost* host, INativeWindow* native_window, IEvent* event, + void (WindowHost::*handler)(INativeWindow*, typename IEvent::EventArgs), + std::vector& guard_pool) { + guard_pool.push_back(EventRevokerGuard(event->AddHandler( + std::bind(handler, host, native_window, std::placeholders::_1)))); +} +} // namespace + +WindowHost::WindowHost(Window* window) + : window_control_(window), + mouse_hover_control_(nullptr), + focus_control_(window), + mouse_captured_control_(nullptr) { + const auto ui_application = IUiApplication::GetInstance(); + native_window_resolver_ = ui_application->CreateWindow(nullptr); + + const auto native_window = native_window_resolver_->Resolve(); + + auto input_method_context = + ui_application->GetInputMethodManager()->GetContext(native_window); + input_method_context->DisableIME(); + + window->ui_host_ = this; + + root_render_object_ = std::make_unique(this); + root_render_object_->SetAttachedControl(window); + window->render_object_ = root_render_object_.get(); + + BindNativeEvent(this, native_window, native_window->DestroyEvent(), + &WindowHost::OnNativeDestroy, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->PaintEvent(), + &WindowHost::OnNativePaint, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->ResizeEvent(), + &WindowHost::OnNativeResize, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->FocusEvent(), + &WindowHost::OnNativeFocus, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->MouseEnterLeaveEvent(), + &WindowHost::OnNativeMouseEnterLeave, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->MouseMoveEvent(), + &WindowHost::OnNativeMouseMove, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->MouseDownEvent(), + &WindowHost::OnNativeMouseDown, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->MouseUpEvent(), + &WindowHost::OnNativeMouseUp, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->KeyDownEvent(), + &WindowHost::OnNativeKeyDown, event_revoker_guards_); + BindNativeEvent(this, native_window, native_window->KeyUpEvent(), + &WindowHost::OnNativeKeyUp, event_revoker_guards_); +} + +WindowHost::~WindowHost() { + deleting_ = true; + window_control_->TraverseDescendants( + [this](Control* control) { control->OnDetachFromHost(this); }); + if (!native_window_destroyed_) { + const auto native_window = native_window_resolver_->Resolve(); + if (native_window) { + native_window->Close(); + } + } +} + +void WindowHost::InvalidatePaint() { + if (const auto native_window = native_window_resolver_->Resolve()) + native_window->RequestRepaint(); +} + +void WindowHost::InvalidateLayout() { + if constexpr (debug_flags::layout) + log::TagDebug(log_tag, u"A relayout is requested."); + if (!need_layout_) { + platform::native::IUiApplication::GetInstance()->SetImmediate( + [resolver = this->CreateResolver()] { + if (const auto host = resolver.Resolve()) { + host->Relayout(); + host->need_layout_ = false; + host->InvalidatePaint(); + } + }); + need_layout_ = true; + } +} + +bool WindowHost::IsLayoutPreferToFillWindow() const { + return layout_prefer_to_fill_window_; +} + +void WindowHost::SetLayoutPreferToFillWindow(bool value) { + if (value == layout_prefer_to_fill_window_) return; + layout_prefer_to_fill_window_ = value; + InvalidateLayout(); +} + +void WindowHost::Relayout() { + const auto native_window = native_window_resolver_->Resolve(); + const auto client_size = native_window + ? native_window->GetClientSize() + : Size{100, 100}; // a reasonable assumed size + + root_render_object_->Measure( + render::MeasureRequirement{client_size, + IsLayoutPreferToFillWindow() + ? render::MeasureSize(client_size) + : render::MeasureSize::NotSpecified()}, + render::MeasureSize::NotSpecified()); + root_render_object_->Layout(Point{}); + for (auto& action : after_layout_stable_action_) action(); + after_layout_stable_action_.clear(); + after_layout_event_.Raise(AfterLayoutEventArgs{}); + if constexpr (debug_flags::layout) + log::TagDebug(log_tag, u"A relayout is finished."); +} + +bool WindowHost::RequestFocusFor(Control* control) { + Expects(control != nullptr); // The control to request focus can't be null. + // You can set it as the window. + + if (focus_control_ == control) return true; + + const auto old_focus_control = focus_control_; + + focus_control_ = control; + + DispatchEvent(event_names::LoseFocus, old_focus_control, + &Control::LoseFocusEvent, nullptr, false); + + DispatchEvent(event_names::GainFocus, control, &Control::GainFocusEvent, + nullptr, false); + + return true; +} + +Control* WindowHost::GetFocusControl() { return focus_control_; } + +bool WindowHost::CaptureMouseFor(Control* control) { + const auto native_window = native_window_resolver_->Resolve(); + if (!native_window) return false; + + if (control == mouse_captured_control_) return true; + + if (control == nullptr) { + const auto old_capture_control = mouse_captured_control_; + mouse_captured_control_ = + nullptr; // update this in case this is used in event handlers + if (old_capture_control != mouse_hover_control_) { + DispatchMouseHoverControlChangeEvent( + old_capture_control, mouse_hover_control_, + native_window->GetMousePosition(), true, false); + } + UpdateCursor(); + return true; + } + + if (mouse_captured_control_) return false; + + mouse_captured_control_ = control; + DispatchMouseHoverControlChangeEvent( + mouse_hover_control_, mouse_captured_control_, + native_window->GetMousePosition(), false, true); + UpdateCursor(); + return true; +} + +Control* WindowHost::GetMouseCaptureControl() { + return mouse_captured_control_; +} + +void WindowHost::RunAfterLayoutStable(std::function action) { + if (need_layout_) { + after_layout_stable_action_.push_back(std::move(action)); + } else { + action(); + } +} + +void WindowHost::OnNativeDestroy(INativeWindow* window, std::nullptr_t) { + CRU_UNUSED(window) + native_window_destroyed_ = true; + if (!deleting_ && !retain_after_destroy_) delete window_control_; +} + +void WindowHost::OnNativePaint(INativeWindow* window, std::nullptr_t) { + auto painter = window->BeginPaint(); + painter->Clear(colors::white); + root_render_object_->Draw(painter.get()); + painter->EndDraw(); +} + +void WindowHost::OnNativeResize(INativeWindow* window, const Size& size) { + CRU_UNUSED(window) + CRU_UNUSED(size) + + InvalidateLayout(); +} + +void WindowHost::OnNativeFocus(INativeWindow* window, + platform::native::FocusChangeType focus) { + CRU_UNUSED(window) + + focus == platform::native::FocusChangeType::Gain + ? DispatchEvent(event_names::GainFocus, focus_control_, + &Control::GainFocusEvent, nullptr, true) + : DispatchEvent(event_names::LoseFocus, focus_control_, + &Control::LoseFocusEvent, nullptr, true); +} + +void WindowHost::OnNativeMouseEnterLeave( + INativeWindow* window, platform::native::MouseEnterLeaveType type) { + CRU_UNUSED(window) + + if (type == platform::native::MouseEnterLeaveType::Leave) { + DispatchEvent(event_names::MouseLeave, mouse_hover_control_, + &Control::MouseLeaveEvent, nullptr); + mouse_hover_control_ = nullptr; + } +} + +void WindowHost::OnNativeMouseMove(INativeWindow* window, const Point& point) { + CRU_UNUSED(window) + + // Find the first control that hit test succeed. + const auto new_mouse_hover_control = HitTest(point); + const auto old_mouse_hover_control = mouse_hover_control_; + mouse_hover_control_ = new_mouse_hover_control; + + if (mouse_captured_control_) { + const auto n = FindLowestCommonAncestor(new_mouse_hover_control, + mouse_captured_control_); + const auto o = FindLowestCommonAncestor(old_mouse_hover_control, + mouse_captured_control_); + bool a = IsAncestor(o, n); + if (a) { + DispatchEvent(event_names::MouseLeave, o, &Control::MouseLeaveEvent, n); + } else { + DispatchEvent(event_names::MouseEnter, n, &Control::MouseEnterEvent, o, + point); + } + DispatchEvent(event_names::MouseMove, mouse_captured_control_, + &Control::MouseMoveEvent, nullptr, point); + UpdateCursor(); + return; + } + + DispatchMouseHoverControlChangeEvent( + old_mouse_hover_control, new_mouse_hover_control, point, false, false); + DispatchEvent(event_names::MouseMove, new_mouse_hover_control, + &Control::MouseMoveEvent, nullptr, point); + UpdateCursor(); +} + +void WindowHost::OnNativeMouseDown( + INativeWindow* window, + const platform::native::NativeMouseButtonEventArgs& args) { + CRU_UNUSED(window) + + Control* control = + mouse_captured_control_ ? mouse_captured_control_ : HitTest(args.point); + DispatchEvent(event_names::MouseDown, control, &Control::MouseDownEvent, + nullptr, args.point, args.button, args.modifier); +} + +void WindowHost::OnNativeMouseUp( + INativeWindow* window, + const platform::native::NativeMouseButtonEventArgs& args) { + CRU_UNUSED(window) + + Control* control = + mouse_captured_control_ ? mouse_captured_control_ : HitTest(args.point); + DispatchEvent(event_names::MouseUp, control, &Control::MouseUpEvent, nullptr, + args.point, args.button, args.modifier); +} + +void WindowHost::OnNativeKeyDown( + INativeWindow* window, const platform::native::NativeKeyEventArgs& args) { + CRU_UNUSED(window) + + DispatchEvent(event_names::KeyDown, focus_control_, &Control::KeyDownEvent, + nullptr, args.key, args.modifier); +} + +void WindowHost::OnNativeKeyUp( + INativeWindow* window, const platform::native::NativeKeyEventArgs& args) { + CRU_UNUSED(window) + + DispatchEvent(event_names::KeyUp, focus_control_, &Control::KeyUpEvent, + nullptr, args.key, args.modifier); +} + +void WindowHost::DispatchMouseHoverControlChangeEvent(Control* old_control, + Control* new_control, + const Point& point, + bool no_leave, + bool no_enter) { + if (new_control != old_control) // if the mouse-hover-on control changed + { + const auto lowest_common_ancestor = + FindLowestCommonAncestor(old_control, new_control); + if (!no_leave && old_control != nullptr) + DispatchEvent(event_names::MouseLeave, old_control, + &Control::MouseLeaveEvent, + lowest_common_ancestor); // dispatch mouse leave event. + if (!no_enter && new_control != nullptr) { + DispatchEvent(event_names::MouseEnter, new_control, + &Control::MouseEnterEvent, lowest_common_ancestor, + point); // dispatch mouse enter event. + } + } +} + +void WindowHost::UpdateCursor() { + if (const auto native_window = native_window_resolver_->Resolve()) { + const auto capture = GetMouseCaptureControl(); + native_window->SetCursor( + (capture ? capture : GetMouseHoverControl())->GetInheritedCursor()); + } +} + +Control* WindowHost::HitTest(const Point& point) { + const auto render_object = root_render_object_->HitTest(point); + if (render_object) { + const auto control = render_object->GetAttachedControl(); + Ensures(control); + return control; + } + return window_control_; +} +} // namespace cru::ui diff --git a/src/ui/controls/TextControlService.hpp b/src/ui/controls/TextControlService.hpp index 8419f35c..8c1bd32f 100644 --- a/src/ui/controls/TextControlService.hpp +++ b/src/ui/controls/TextControlService.hpp @@ -12,7 +12,7 @@ #include "cru/ui/DebugFlags.hpp" #include "cru/ui/ShortcutHub.hpp" #include "cru/ui/UiEvent.hpp" -#include "cru/ui/UiHost.hpp" +#include "cru/ui/WindowHost.hpp" #include "cru/ui/render/CanvasRenderObject.hpp" #include "cru/ui/render/ScrollRenderObject.hpp" #include "cru/ui/render/TextRenderObject.hpp" @@ -208,7 +208,7 @@ class TextControlService : public Object { } void ScrollToCaret() { - this->control_->GetUiHost()->RunAfterLayoutStable([this]() { + this->control_->GetWindowHost()->RunAfterLayoutStable([this]() { const auto caret_rect = this->GetTextRenderObject()->GetCaretRect(); this->GetScrollRenderObject()->ScrollToContain(caret_rect, Thickness{5.f}); @@ -408,7 +408,7 @@ class TextControlService : public Object { void GainFocusHandler(event::FocusChangeEventArgs& args) { CRU_UNUSED(args); if (editable_) { - UiHost* ui_host = this->control_->GetUiHost(); + WindowHost* ui_host = this->control_->GetWindowHost(); auto window = ui_host->GetNativeWindowResolver()->Resolve(); if (window == nullptr) return; input_method_context_ = diff --git a/src/ui/render/RenderObject.cpp b/src/ui/render/RenderObject.cpp index c85f8080..5266daaf 100644 --- a/src/ui/render/RenderObject.cpp +++ b/src/ui/render/RenderObject.cpp @@ -3,7 +3,7 @@ #include "cru/common/Logger.hpp" #include "cru/platform/graph/util/Painter.hpp" #include "cru/ui/DebugFlags.hpp" -#include "cru/ui/UiHost.hpp" +#include "cru/ui/WindowHost.hpp" #include #include @@ -24,7 +24,7 @@ void RenderObject::AddChild(RenderObject* render_object, const Index position) { children_.insert(children_.cbegin() + position, render_object); render_object->SetParent(this); - render_object->SetRenderHostRecursive(GetUiHost()); + render_object->SetRenderHostRecursive(GetWindowHost()); OnAddChild(render_object, position); } @@ -304,7 +304,7 @@ void RenderObject::NotifyAfterLayoutRecursive(RenderObject* render_object) { } } -void RenderObject::SetRenderHostRecursive(UiHost* host) { +void RenderObject::SetRenderHostRecursive(WindowHost* host) { ui_host_ = host; for (const auto child : GetChildren()) { child->SetRenderHostRecursive(host); diff --git a/src/ui/render/WindowRenderObject.cpp b/src/ui/render/WindowRenderObject.cpp index a136c1e9..23cca12e 100644 --- a/src/ui/render/WindowRenderObject.cpp +++ b/src/ui/render/WindowRenderObject.cpp @@ -2,10 +2,10 @@ #include "../Helper.hpp" #include "cru/platform/graph/util/Painter.hpp" -#include "cru/ui/UiHost.hpp" +#include "cru/ui/WindowHost.hpp" namespace cru::ui::render { -WindowRenderObject::WindowRenderObject(UiHost* host) { +WindowRenderObject::WindowRenderObject(WindowHost* host) { SetChildMode(ChildMode::Single); ui_host_ = host; after_layout_event_guard_.Reset(host->AfterLayoutEvent()->AddHandler( -- cgit v1.2.3