diff options
-rw-r--r-- | include/cru/platform/native/window.hpp | 1 | ||||
-rw-r--r-- | include/cru/ui/window.hpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/ui/render/window_render_object.cpp | 9 | ||||
-rw-r--r-- | src/ui/window.cpp | 19 |
5 files changed, 24 insertions, 11 deletions
diff --git a/include/cru/platform/native/window.hpp b/include/cru/platform/native/window.hpp index 84193a13..9f2e2e71 100644 --- a/include/cru/platform/native/window.hpp +++ b/include/cru/platform/native/window.hpp @@ -69,6 +69,7 @@ struct INativeWindow : virtual INativeResource { // See INativeWindow for more info. struct INativeWindowResolver : virtual INativeResource { + // Think twice before you save the return value. virtual INativeWindow* Resolve() = 0; }; } // namespace cru::platform::native diff --git a/include/cru/ui/window.hpp b/include/cru/ui/window.hpp index 105063d9..9f37c9d5 100644 --- a/include/cru/ui/window.hpp +++ b/include/cru/ui/window.hpp @@ -17,6 +17,8 @@ namespace render { class WindowRenderObject; } +// TODO: Make Window able to be invalid and handle operations in invalidity +// situation. class Window final : public ContentControl, public SelfResolvable<Window> { friend class Control; @@ -43,7 +45,7 @@ class Window final : public ContentControl, public SelfResolvable<Window> { render::RenderObject* GetRenderObject() const override; - platform::native::INativeWindow* GetNativeWindow(); + platform::native::INativeWindow* ResolveNativeWindow(); // Get current control that mouse hovers on. This ignores the mouse-capture // control. Even when mouse is captured by another control, this function diff --git a/src/main.cpp b/src/main.cpp index 07aff285..ece75367 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,7 @@ int main() { text_block2->SetText("Hello World!"); flex_layout->AddChild(text_block2, 1); - window->GetNativeWindow()->SetVisible(true); + window->ResolveNativeWindow()->SetVisible(true); return application->Run(); } diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp index 1001be87..43269d09 100644 --- a/src/ui/render/window_render_object.cpp +++ b/src/ui/render/window_render_object.cpp @@ -21,7 +21,9 @@ class WindowRenderHost : public IRenderHost, void InvalidateLayout() override; void InvalidatePaint() override { - render_object_->GetWindow()->GetNativeWindow()->RequestRepaint(); + if (const auto native_window = + render_object_->GetWindow()->ResolveNativeWindow()) + native_window->RequestRepaint(); } IEvent<AfterLayoutEventArgs>* AfterLayoutEvent() override { @@ -61,7 +63,10 @@ WindowRenderObject::WindowRenderObject(Window* window) } void WindowRenderObject::Relayout() { - const auto client_size = window_->GetNativeWindow()->GetClientSize(); + const auto native_window = window_->ResolveNativeWindow(); + const auto client_size = native_window + ? native_window->GetClientSize() + : Size{100, 100}; // a reasonable assumed size Measure(client_size); Layout(Rect{Point{}, client_size}); } diff --git a/src/ui/window.cpp b/src/ui/window.cpp index 35463778..7579786a 100644 --- a/src/ui/window.cpp +++ b/src/ui/window.cpp @@ -145,7 +145,7 @@ render::RenderObject* Window::GetRenderObject() const { return render_object_.get(); } -platform::native::INativeWindow* Window::GetNativeWindow() { +platform::native::INativeWindow* Window::ResolveNativeWindow() { return native_window_resolver_->Resolve(); } @@ -169,6 +169,9 @@ bool Window::RequestFocusFor(Control* control) { Control* Window::GetFocusControl() { return focus_control_; } bool Window::CaptureMouseFor(Control* control) { + const auto native_window = ResolveNativeWindow(); + if (!native_window) return false; + if (control == mouse_captured_control_) return true; if (control == nullptr) { @@ -178,7 +181,7 @@ bool Window::CaptureMouseFor(Control* control) { if (old_capture_control != mouse_hover_control_) { DispatchMouseHoverControlChangeEvent( old_capture_control, mouse_hover_control_, - GetNativeWindow()->GetMousePosition(), true, false); + native_window->GetMousePosition(), true, false); } UpdateCursor(); return true; @@ -189,7 +192,7 @@ bool Window::CaptureMouseFor(Control* control) { mouse_captured_control_ = control; DispatchMouseHoverControlChangeEvent( mouse_hover_control_, mouse_captured_control_, - GetNativeWindow()->GetMousePosition(), false, true); + native_window->GetMousePosition(), false, true); UpdateCursor(); return true; } @@ -207,7 +210,7 @@ Control* Window::HitTest(const Point& point) { void Window::OnNativeDestroy(INativeWindow* window, std::nullptr_t) { CRU_UNUSED(window) - delete this; + delete this; // TODO: Finally change this. } void Window::OnNativePaint(INativeWindow* window, std::nullptr_t) { @@ -335,8 +338,10 @@ void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, } void Window::UpdateCursor() { - const auto capture = GetMouseCaptureControl(); - GetNativeWindow()->SetCursor( - (capture ? capture : GetMouseHoverControl())->GetInheritedCursor()); + if (const auto native_window = ResolveNativeWindow()) { + const auto capture = GetMouseCaptureControl(); + native_window->SetCursor( + (capture ? capture : GetMouseHoverControl())->GetInheritedCursor()); + } } } // namespace cru::ui |