aboutsummaryrefslogtreecommitdiff
path: root/src/ui/controls
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-11-21 15:18:28 +0800
committercrupest <crupest@outlook.com>2021-11-21 15:18:28 +0800
commiteeb1f55d5a483720441c3f44e83d02cc882a3bc0 (patch)
treee8d8a49d26602ac3f8097aa4d2fff65d70bbfba5 /src/ui/controls
parent2b28ec296fdc72050569b9fedc1664ada7791497 (diff)
downloadcru-eeb1f55d5a483720441c3f44e83d02cc882a3bc0.tar.gz
cru-eeb1f55d5a483720441c3f44e83d02cc882a3bc0.tar.bz2
cru-eeb1f55d5a483720441c3f44e83d02cc882a3bc0.zip
...
Diffstat (limited to 'src/ui/controls')
-rw-r--r--src/ui/controls/Popup.cpp8
-rw-r--r--src/ui/controls/RootControl.cpp42
-rw-r--r--src/ui/controls/Window.cpp3
3 files changed, 26 insertions, 27 deletions
diff --git a/src/ui/controls/Popup.cpp b/src/ui/controls/Popup.cpp
index ae6ac1d2..b386165b 100644
--- a/src/ui/controls/Popup.cpp
+++ b/src/ui/controls/Popup.cpp
@@ -8,11 +8,9 @@
#include <memory>
namespace cru::ui::controls {
-Popup::Popup(Control* attached_control)
- : RootControl(
- attached_control,
- host::CreateWindowParams{
- nullptr, platform::gui::CreateWindowFlags::NoCaptionAndBorder}) {
+Popup::Popup(Control* attached_control) : RootControl(attached_control) {
+ GetWindowHost()->GetNativeWindow()->SetStyleFlag(
+ cru::platform::gui::WindowStyleFlags::NoCaptionAndBorder);
SetGainFocusOnCreateAndDestroyWhenLoseFocus(true);
}
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 <memory>
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::StackLayoutRenderObject>();
render_object_->SetAttachedControl(this);
SetContainerRenderObject(render_object_.get());
- window_host_ = std::make_unique<host::WindowHost>(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<host::WindowHost>(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
diff --git a/src/ui/controls/Window.cpp b/src/ui/controls/Window.cpp
index 00fab8e9..76203f22 100644
--- a/src/ui/controls/Window.cpp
+++ b/src/ui/controls/Window.cpp
@@ -12,8 +12,7 @@ Window* Window::Create(Control* attached_control) {
return new Window(attached_control);
}
-Window::Window(Control* attached_control)
- : RootControl(attached_control, host::CreateWindowParams{}) {}
+Window::Window(Control* attached_control) : RootControl(attached_control) {}
Window::~Window() {}
} // namespace cru::ui::controls