diff options
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/components/Menu.cpp | 5 | ||||
-rw-r--r-- | src/ui/controls/Popup.cpp | 16 | ||||
-rw-r--r-- | src/ui/controls/RootControl.cpp | 60 | ||||
-rw-r--r-- | src/ui/controls/Window.cpp | 9 | ||||
-rw-r--r-- | src/ui/host/WindowHost.cpp | 35 |
5 files changed, 34 insertions, 91 deletions
diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp index af54c46c..964eead9 100644 --- a/src/ui/components/Menu.cpp +++ b/src/ui/components/Menu.cpp @@ -1,4 +1,5 @@ #include "cru/ui/components/Menu.hpp" +#include "cru/platform/gui/Window.hpp" #include "cru/ui/UiManager.hpp" #include "cru/ui/controls/Button.hpp" #include "cru/ui/controls/Control.hpp" @@ -82,11 +83,11 @@ PopupMenu::~PopupMenu() { controls::Control* PopupMenu::GetRootControl() { return popup_; } void PopupMenu::SetPosition(const Point& position) { - popup_->SetRect(Rect{position, {}}); + popup_->GetWindowHost()->GetNativeWindow()->SetClientRect(Rect{position, {}}); } void PopupMenu::Show() { popup_->GetWindowHost()->RelayoutWithSize(Size::Infinate(), true); - popup_->Show(); + popup_->GetWindowHost()->GetNativeWindow()->SetVisible(true); } } // namespace cru::ui::components diff --git a/src/ui/controls/Popup.cpp b/src/ui/controls/Popup.cpp index 515c7b31..ae6ac1d2 100644 --- a/src/ui/controls/Popup.cpp +++ b/src/ui/controls/Popup.cpp @@ -8,19 +8,13 @@ #include <memory> namespace cru::ui::controls { -Popup::Popup(Control* attached_control) : RootControl(attached_control) { +Popup::Popup(Control* attached_control) + : RootControl( + attached_control, + host::CreateWindowParams{ + nullptr, platform::gui::CreateWindowFlags::NoCaptionAndBorder}) { SetGainFocusOnCreateAndDestroyWhenLoseFocus(true); } Popup::~Popup() = default; - -gsl::not_null<platform::gui::INativeWindow*> Popup::CreateNativeWindow( - gsl::not_null<host::WindowHost*> host, - platform::gui::INativeWindow* parent) { - auto window = host->CreateNativeWindow( - {parent, platform::gui::CreateWindowFlags::NoCaptionAndBorder}); - - return window; -} - } // namespace cru::ui::controls diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index 007a0ac1..4ae41c80 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -11,57 +11,35 @@ #include <memory> namespace cru::ui::controls { -RootControl::RootControl(Control* attached_control) +RootControl::RootControl(Control* attached_control, + host::CreateWindowParams params) : attached_control_(attached_control) { render_object_ = std::make_unique<render::StackLayoutRenderObject>(); render_object_->SetAttachedControl(this); SetContainerRenderObject(render_object_.get()); - window_host_ = std::make_unique<host::WindowHost>(this); -} - -RootControl::~RootControl() {} - -render::RenderObject* RootControl::GetRenderObject() const { - return render_object_.get(); -} - -void RootControl::EnsureWindowCreated() { this->GetNativeWindow(true); } + window_host_ = std::make_unique<host::WindowHost>(this, params); -Rect RootControl::GetRect() { return window_host_->GetWindowRect(); } - -void RootControl::SetRect(const Rect& rect) { - window_host_->SetWindowRect(rect); -} - -void RootControl::Show(bool create) { - platform::gui::INativeWindow* native_window = GetNativeWindow(create); - if (!native_window) return; - native_window->SetVisible(true); if (gain_focus_on_create_and_destroy_when_lose_focus_) { - native_window->RequestFocus(); + auto native_window = window_host_->GetNativeWindow(); + native_window->CreateEvent()->AddHandler( + [](platform::gui::INativeWindow* window) { + window->CreateEvent()->AddHandler( + [window](std::nullptr_t) { window->RequestFocus(); }); + }); + + native_window->FocusEvent()->AddHandler( + [native_window](platform::gui::FocusChangeType type) { + if (type == platform::gui::FocusChangeType::Lost) { + native_window->Close(); + } + }); } } -platform::gui::INativeWindow* RootControl::GetNativeWindow(bool create) { - const auto host = GetWindowHost(); - platform::gui::INativeWindow* native_window = host->GetNativeWindow(); - if (!create) return native_window; - if (!native_window) { - native_window = this->CreateNativeWindow( - host, attached_control_ - ? attached_control_->GetWindowHost()->GetNativeWindow() - : nullptr); +RootControl::~RootControl() {} - if (gain_focus_on_create_and_destroy_when_lose_focus_) { - native_window->FocusEvent()->AddHandler( - [native_window](platform::gui::FocusChangeType type) { - if (type == platform::gui::FocusChangeType::Lost) { - native_window->Close(); - } - }); - } - } - return native_window; +render::RenderObject* RootControl::GetRenderObject() const { + return render_object_.get(); } void RootControl::SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value) { diff --git a/src/ui/controls/Window.cpp b/src/ui/controls/Window.cpp index ba66f42e..00fab8e9 100644 --- a/src/ui/controls/Window.cpp +++ b/src/ui/controls/Window.cpp @@ -12,13 +12,8 @@ Window* Window::Create(Control* attached_control) { return new Window(attached_control); } -Window::Window(Control* attached_control) : RootControl(attached_control) {} +Window::Window(Control* attached_control) + : RootControl(attached_control, host::CreateWindowParams{}) {} Window::~Window() {} - -gsl::not_null<platform::gui::INativeWindow*> Window::CreateNativeWindow( - gsl::not_null<host::WindowHost*> host, - platform::gui::INativeWindow* parent) { - return host->CreateNativeWindow({parent}); -} } // namespace cru::ui::controls diff --git a/src/ui/host/WindowHost.cpp b/src/ui/host/WindowHost.cpp index d26b43ab..3f3976b4 100644 --- a/src/ui/host/WindowHost.cpp +++ b/src/ui/host/WindowHost.cpp @@ -104,7 +104,8 @@ inline void BindNativeEvent( } } // namespace -WindowHost::WindowHost(controls::Control* root_control) +WindowHost::WindowHost(controls::Control* root_control, + CreateWindowParams params) : root_control_(root_control), focus_control_(root_control) { root_control_->TraverseDescendants([this](controls::Control* control) { control->window_host_ = this; @@ -115,14 +116,14 @@ WindowHost::WindowHost(controls::Control* root_control) root_render_object_->SetWindowHostRecursive(this); this->layout_paint_cycler_ = std::make_unique<LayoutPaintCycler>(this); + + CreateNativeWindow(params); } WindowHost::~WindowHost() {} gsl::not_null<platform::gui::INativeWindow*> WindowHost::CreateNativeWindow( CreateWindowParams create_window_params) { - if (native_window_ != nullptr) return native_window_; - const auto ui_application = IUiApplication::GetInstance(); auto native_window = ui_application->CreateWindow(create_window_params.parent, @@ -153,10 +154,6 @@ 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_; @@ -194,8 +191,7 @@ void WindowHost::RelayoutWithSize(const Size& available_size, render::MeasureSize::NotSpecified()); if (set_window_size_to_fit_content) { - auto rect = GetWindowRect(); - SetWindowRect({rect.GetLeftTop(), root_render_object_->GetSize()}); + native_window_->SetClientSize(root_render_object_->GetSize()); } root_render_object_->Layout(Point{}); @@ -277,29 +273,8 @@ 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(); - - native_window_change_event_.Raise(nullptr); } void WindowHost::OnNativePaint(INativeWindow* window, std::nullptr_t) { |