diff options
author | crupest <crupest@outlook.com> | 2021-11-20 19:47:53 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-11-20 19:47:53 +0800 |
commit | b0b86b6b9d6798d04a320060e90bd8497a7f50f9 (patch) | |
tree | a6e3f3315a538791e1cbd4e799bc6268262feedb /src | |
parent | 2ad748a0fcb66398299886970cc9c40cc50b3cd0 (diff) | |
download | cru-b0b86b6b9d6798d04a320060e90bd8497a7f50f9.tar.gz cru-b0b86b6b9d6798d04a320060e90bd8497a7f50f9.tar.bz2 cru-b0b86b6b9d6798d04a320060e90bd8497a7f50f9.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/osx/gui/Window.mm | 11 | ||||
-rw-r--r-- | src/ui/controls/Popup.cpp | 8 | ||||
-rw-r--r-- | src/ui/controls/RootControl.cpp | 17 | ||||
-rw-r--r-- | src/ui/controls/TextBox.cpp | 2 |
4 files changed, 35 insertions, 3 deletions
diff --git a/src/osx/gui/Window.mm b/src/osx/gui/Window.mm index a026a455..0112fd43 100644 --- a/src/osx/gui/Window.mm +++ b/src/osx/gui/Window.mm @@ -241,6 +241,11 @@ void OsxWindow::CreateWindow() { [p_->window_ setDelegate:p_->window_delegate_]; + if (p_->parent_) { + auto parent = CheckPlatform<OsxWindow>(p_->parent_, GetPlatformId()); + [p_->window_ setParentWindow:parent->p_->window_]; + } + NSView* content_view = [[CruView alloc] init:p_.get() input_context_p:p_->input_method_context_->p_.get() frame:Rect(Point{}, p_->content_rect_.GetSize())]; @@ -254,6 +259,12 @@ void OsxWindow::CreateWindow() { RequestRepaint(); } +bool OsxWindow::RequestFocus() { + if (!p_->window_) return false; + [p_->window_ makeKeyWindow]; + return true; +} + Point OsxWindow::GetMousePosition() { auto p = [p_->window_ mouseLocationOutsideOfEventStream]; return Point(p.x, p.y); diff --git a/src/ui/controls/Popup.cpp b/src/ui/controls/Popup.cpp index bc217bf5..515c7b31 100644 --- a/src/ui/controls/Popup.cpp +++ b/src/ui/controls/Popup.cpp @@ -8,15 +8,19 @@ #include <memory> namespace cru::ui::controls { -Popup::Popup(Control* attached_control) : RootControl(attached_control) {} +Popup::Popup(Control* attached_control) : RootControl(attached_control) { + SetGainFocusOnCreateAndDestroyWhenLoseFocus(true); +} Popup::~Popup() = default; gsl::not_null<platform::gui::INativeWindow*> Popup::CreateNativeWindow( gsl::not_null<host::WindowHost*> host, platform::gui::INativeWindow* parent) { - return host->CreateNativeWindow( + 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 015703c3..007a0ac1 100644 --- a/src/ui/controls/RootControl.cpp +++ b/src/ui/controls/RootControl.cpp @@ -2,6 +2,7 @@ #include "cru/common/Base.hpp" #include "cru/platform/gui/Base.hpp" +#include "cru/platform/gui/Window.hpp" #include "cru/ui/host/WindowHost.hpp" #include "cru/ui/render/Base.hpp" #include "cru/ui/render/StackLayoutRenderObject.hpp" @@ -36,6 +37,9 @@ 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(); + } } platform::gui::INativeWindow* RootControl::GetNativeWindow(bool create) { @@ -47,7 +51,20 @@ platform::gui::INativeWindow* RootControl::GetNativeWindow(bool create) { host, attached_control_ ? attached_control_->GetWindowHost()->GetNativeWindow() : nullptr); + + 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; } + +void RootControl::SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value) { + gain_focus_on_create_and_destroy_when_lose_focus_ = value; +} } // namespace cru::ui::controls diff --git a/src/ui/controls/TextBox.cpp b/src/ui/controls/TextBox.cpp index 622401c4..c4912307 100644 --- a/src/ui/controls/TextBox.cpp +++ b/src/ui/controls/TextBox.cpp @@ -28,7 +28,7 @@ TextBox::TextBox() border_render_object_->SetAttachedControl(this); scroll_render_object_->SetAttachedControl(this); text_render_object_->SetAttachedControl(this); - text_render_object_->SetMinSize(Size{100, 24}); + text_render_object_->SetMinSize(Size{100, 0}); text_render_object_->SetMeasureIncludingTrailingSpace(true); service_ = std::make_unique<TextHostControlService>(this); |