aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/ui/controls/RootControl.hpp3
-rw-r--r--include/cru/ui/host/WindowHost.hpp11
-rw-r--r--src/ui/controls/RootControl.cpp5
-rw-r--r--src/ui/host/WindowHost.cpp21
4 files changed, 33 insertions, 7 deletions
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 <functional>
#include <memory>
+#include <optional>
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> 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<platform::gui::INativeWindow*> native_window_change_event_;
+
+ std::optional<Rect> 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<platform::gui::INativeWindow*> 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<void()> action) {
}
}
+Rect WindowHost::GetWindowRect() {
+ if (native_window_) return native_window_->GetWindowRect();
+ return saved_rect_.value_or(Rect{});
+}
+
+void WindowHost::SetSavedWindowRect(std::optional<Rect> 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();