From 68fc33443981fcd499dfe263c228787e213ae943 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 9 Nov 2020 20:06:04 +0800 Subject: ... --- include/cru/ui/controls/RootControl.hpp | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 include/cru/ui/controls/RootControl.hpp (limited to 'include/cru/ui/controls/RootControl.hpp') diff --git a/include/cru/ui/controls/RootControl.hpp b/include/cru/ui/controls/RootControl.hpp new file mode 100644 index 00000000..ff1b545a --- /dev/null +++ b/include/cru/ui/controls/RootControl.hpp @@ -0,0 +1,40 @@ +#pragma once +#include "LayoutControl.hpp" + +#include "cru/common/Base.hpp" +#include "cru/platform/gui/Base.hpp" +#include "cru/ui/Base.hpp" + +namespace cru::ui::controls { +class RootControl : public LayoutControl { + protected: + explicit RootControl(Control* attached_control); + + public: + CRU_DELETE_COPY(RootControl) + CRU_DELETE_MOVE(RootControl) + ~RootControl() override; + + public: + render::RenderObject* GetRenderObject() const override; + + // If create is false and native window is not create, it will not be created + // and shown. + void Show(bool create = true); + + protected: + virtual gsl::not_null CreateNativeWindow( + gsl::not_null host, + platform::gui::INativeWindow* parent) = 0; + + private: + platform::gui::INativeWindow* GetNativeWindow(bool create); + + private: + std::unique_ptr window_host_; + + std::unique_ptr render_object_; + + Control* attached_control_; +}; +} // namespace cru::ui::controls -- cgit v1.2.3 From 014aaf773fd95a7f0dd1a766023a21fb6b54a1a1 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 9 Nov 2020 20:28:39 +0800 Subject: ... --- include/cru/ui/controls/RootControl.hpp | 7 +++++++ src/ui/controls/RootControl.cpp | 9 +++++++++ 2 files changed, 16 insertions(+) (limited to 'include/cru/ui/controls/RootControl.hpp') diff --git a/include/cru/ui/controls/RootControl.hpp b/include/cru/ui/controls/RootControl.hpp index ff1b545a..d41f02d8 100644 --- a/include/cru/ui/controls/RootControl.hpp +++ b/include/cru/ui/controls/RootControl.hpp @@ -18,10 +18,17 @@ class RootControl : public LayoutControl { public: render::RenderObject* GetRenderObject() const override; + void EnsureWindowCreated(); + // If create is false and native window is not create, it will not be created // and shown. void Show(bool create = true); + // If native window does not exist, nothing will be done. It will not save it + // and use it when creating window. So call this after ensuring window + // created. + void SetRect(const Rect& rect); + protected: virtual gsl::not_null CreateNativeWindow( gsl::not_null host, diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index 61d272f8..c6d9a577 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -24,6 +24,15 @@ render::RenderObject* RootControl::GetRenderObject() const { return render_object_.get(); } +void RootControl::EnsureWindowCreated() { this->GetNativeWindow(true); } + +void RootControl::SetRect(const Rect& rect) { + auto native_window = GetNativeWindow(false); + if (!native_window) return; + + native_window->SetWindowRect(rect); +} + void RootControl::Show(bool create) { platform::gui::INativeWindow* native_window = GetNativeWindow(create); if (!native_window) return; -- cgit v1.2.3 From bed29872d2a7befff9d4cb7313cb40060326a95e Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 9 Nov 2020 20:45:27 +0800 Subject: ... --- include/cru/ui/controls/RootControl.hpp | 3 --- include/cru/ui/host/WindowHost.hpp | 11 +++++++++++ src/ui/controls/RootControl.cpp | 5 +---- src/ui/host/WindowHost.cpp | 21 +++++++++++++++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) (limited to 'include/cru/ui/controls/RootControl.hpp') diff --git a/include/cru/ui/controls/RootControl.hpp b/include/cru/ui/controls/RootControl.hpp index d41f02d8..b3fa6101 100644 --- a/include/cru/ui/controls/RootControl.hpp +++ b/include/cru/ui/controls/RootControl.hpp @@ -24,9 +24,6 @@ class RootControl : public LayoutControl { // and shown. void Show(bool create = true); - // If native window does not exist, nothing will be done. It will not save it - // and use it when creating window. So call this after ensuring window - // created. void SetRect(const Rect& rect); protected: diff --git a/include/cru/ui/host/WindowHost.hpp b/include/cru/ui/host/WindowHost.hpp index 9fc24eb2..bd2f7c16 100644 --- a/include/cru/ui/host/WindowHost.hpp +++ b/include/cru/ui/host/WindowHost.hpp @@ -8,6 +8,7 @@ #include #include +#include namespace cru::ui::host { class LayoutPaintCycler; @@ -109,6 +110,14 @@ class WindowHost : public Object { return &native_window_change_event_; } + // If window exist, return window actual size. Otherwise if saved rect exists, + // return it. Otherwise return 0. + Rect GetWindowRect(); + + void SetSavedWindowRect(std::optional rect); + + void SetWindowRect(const Rect& rect); + private: //*************** region: native messages *************** void OnNativeDestroy(platform::gui::INativeWindow* window, std::nullptr_t); @@ -161,5 +170,7 @@ class WindowHost : public Object { bool layout_prefer_to_fill_window_ = true; Event native_window_change_event_; + + std::optional saved_rect_; }; } // namespace cru::ui::host diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index c6d9a577..1ebcb6e7 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -27,10 +27,7 @@ render::RenderObject* RootControl::GetRenderObject() const { void RootControl::EnsureWindowCreated() { this->GetNativeWindow(true); } void RootControl::SetRect(const Rect& rect) { - auto native_window = GetNativeWindow(false); - if (!native_window) return; - - native_window->SetWindowRect(rect); + window_host_->SetWindowRect(rect); } void RootControl::Show(bool create) { diff --git a/src/ui/host/WindowHost.cpp b/src/ui/host/WindowHost.cpp index 1550361b..5e107733 100644 --- a/src/ui/host/WindowHost.cpp +++ b/src/ui/host/WindowHost.cpp @@ -150,6 +150,10 @@ gsl::not_null WindowHost::CreateNativeWindow( BindNativeEvent(this, native_window, native_window->KeyUpEvent(), &WindowHost::OnNativeKeyUp, event_revoker_guards_); + if (saved_rect_) { + native_window->SetWindowRect(saved_rect_.value()); + } + native_window_change_event_.Raise(native_window); return native_window_; @@ -263,8 +267,25 @@ void WindowHost::RunAfterLayoutStable(std::function action) { } } +Rect WindowHost::GetWindowRect() { + if (native_window_) return native_window_->GetWindowRect(); + return saved_rect_.value_or(Rect{}); +} + +void WindowHost::SetSavedWindowRect(std::optional rect) { + saved_rect_ = std::move(rect); +} + +void WindowHost::SetWindowRect(const Rect& rect) { + SetSavedWindowRect(rect); + if (native_window_) native_window_->SetWindowRect(rect); +} + void WindowHost::OnNativeDestroy(INativeWindow* window, std::nullptr_t) { CRU_UNUSED(window) + + saved_rect_ = this->native_window_->GetWindowRect(); + this->native_window_ = nullptr; event_revoker_guards_.clear(); -- cgit v1.2.3 From c5fe6fc72e9bca222e6d7b94fd47523089083fdd Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 9 Nov 2020 20:48:13 +0800 Subject: ... --- include/cru/ui/controls/RootControl.hpp | 1 + src/ui/controls/RootControl.cpp | 2 ++ 2 files changed, 3 insertions(+) (limited to 'include/cru/ui/controls/RootControl.hpp') diff --git a/include/cru/ui/controls/RootControl.hpp b/include/cru/ui/controls/RootControl.hpp index b3fa6101..53e69e7c 100644 --- a/include/cru/ui/controls/RootControl.hpp +++ b/include/cru/ui/controls/RootControl.hpp @@ -24,6 +24,7 @@ class RootControl : public LayoutControl { // and shown. void Show(bool create = true); + Rect GetRect(); void SetRect(const Rect& rect); protected: diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index 1ebcb6e7..015703c3 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -26,6 +26,8 @@ render::RenderObject* RootControl::GetRenderObject() const { void RootControl::EnsureWindowCreated() { this->GetNativeWindow(true); } +Rect RootControl::GetRect() { return window_host_->GetWindowRect(); } + void RootControl::SetRect(const Rect& rect) { window_host_->SetWindowRect(rect); } -- cgit v1.2.3