From eeb1f55d5a483720441c3f44e83d02cc882a3bc0 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 21 Nov 2021 15:18:28 +0800 Subject: ... --- src/ui/controls/RootControl.cpp | 42 +++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'src/ui/controls/RootControl.cpp') diff --git a/src/ui/controls/RootControl.cpp b/src/ui/controls/RootControl.cpp index 4ae41c80..d89982c5 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -11,29 +11,12 @@ #include namespace cru::ui::controls { -RootControl::RootControl(Control* attached_control, - host::CreateWindowParams params) +RootControl::RootControl(Control* attached_control) : attached_control_(attached_control) { render_object_ = std::make_unique(); render_object_->SetAttachedControl(this); SetContainerRenderObject(render_object_.get()); - window_host_ = std::make_unique(this, params); - - if (gain_focus_on_create_and_destroy_when_lose_focus_) { - 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(); - } - }); - } + window_host_ = std::make_unique(this); } RootControl::~RootControl() {} @@ -43,6 +26,25 @@ render::RenderObject* RootControl::GetRenderObject() const { } void RootControl::SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value) { - gain_focus_on_create_and_destroy_when_lose_focus_ = value; + gain_focus_on_create_and_destroy_when_lose_focus_event_guard_.Clear(); + if (value) { + auto native_window = window_host_->GetNativeWindow(); + + gain_focus_on_create_and_destroy_when_lose_focus_event_guard_ += + native_window->VisibilityChangeEvent()->AddHandler( + [native_window](platform::gui::WindowVisibilityType type) { + if (type == platform::gui::WindowVisibilityType::Show) { + native_window->RequestFocus(); + } + }); + + gain_focus_on_create_and_destroy_when_lose_focus_event_guard_ += + native_window->FocusEvent()->AddHandler( + [native_window](platform::gui::FocusChangeType type) { + if (type == platform::gui::FocusChangeType::Lost) { + native_window->Close(); + } + }); + } } } // namespace cru::ui::controls -- cgit v1.2.3