aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/osx/gui/Window.hpp2
-rw-r--r--include/cru/platform/gui/Window.hpp2
-rw-r--r--include/cru/ui/controls/Popup.hpp1
-rw-r--r--include/cru/ui/controls/RootControl.hpp4
-rw-r--r--src/osx/gui/Window.mm11
-rw-r--r--src/ui/controls/Popup.cpp8
-rw-r--r--src/ui/controls/RootControl.cpp17
-rw-r--r--src/ui/controls/TextBox.cpp2
8 files changed, 43 insertions, 4 deletions
diff --git a/include/cru/osx/gui/Window.hpp b/include/cru/osx/gui/Window.hpp
index e5e77083..406c133f 100644
--- a/include/cru/osx/gui/Window.hpp
+++ b/include/cru/osx/gui/Window.hpp
@@ -42,6 +42,8 @@ class OsxWindow : public OsxGuiResource, public INativeWindow {
Rect GetWindowRect() override;
void SetWindowRect(const Rect& rect) override;
+ bool RequestFocus() override;
+
Point GetMousePosition() override;
bool CaptureMouse() override;
diff --git a/include/cru/platform/gui/Window.hpp b/include/cru/platform/gui/Window.hpp
index 3502868a..49cbe1a0 100644
--- a/include/cru/platform/gui/Window.hpp
+++ b/include/cru/platform/gui/Window.hpp
@@ -51,6 +51,8 @@ struct INativeWindow : virtual IPlatformResource {
// The lefttop of the rect is relative to screen lefttop.
virtual void SetWindowRect(const Rect& rect) = 0;
+ virtual bool RequestFocus() = 0;
+
// Relative to client lefttop.
virtual Point GetMousePosition() = 0;
diff --git a/include/cru/ui/controls/Popup.hpp b/include/cru/ui/controls/Popup.hpp
index d76e1211..321bbbc6 100644
--- a/include/cru/ui/controls/Popup.hpp
+++ b/include/cru/ui/controls/Popup.hpp
@@ -1,7 +1,6 @@
#pragma once
#include "RootControl.hpp"
-#include "cru/ui/Base.hpp"
#include "cru/platform/gui/Base.hpp"
#include <memory>
diff --git a/include/cru/ui/controls/RootControl.hpp b/include/cru/ui/controls/RootControl.hpp
index 53e69e7c..a795f322 100644
--- a/include/cru/ui/controls/RootControl.hpp
+++ b/include/cru/ui/controls/RootControl.hpp
@@ -32,6 +32,8 @@ class RootControl : public LayoutControl {
gsl::not_null<host::WindowHost*> host,
platform::gui::INativeWindow* parent) = 0;
+ void SetGainFocusOnCreateAndDestroyWhenLoseFocus(bool value);
+
private:
platform::gui::INativeWindow* GetNativeWindow(bool create);
@@ -41,5 +43,7 @@ class RootControl : public LayoutControl {
std::unique_ptr<render::StackLayoutRenderObject> render_object_;
Control* attached_control_;
+
+ bool gain_focus_on_create_and_destroy_when_lose_focus_ = false;
};
} // namespace cru::ui::controls
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);