From 8fe04d6db4f5e6927028bb877dff602afbb35d49 Mon Sep 17 00:00:00 2001 From: mafiesto4 Date: Tue, 9 Feb 2021 21:52:34 +0100 Subject: [PATCH] Fix window auto-focus on Linux --- .../Engine/Platform/Linux/LinuxPlatform.cpp | 9 +++++++++ Source/Engine/Platform/Linux/LinuxWindow.cpp | 19 ++++++++++++++----- Source/Engine/Platform/Linux/LinuxWindow.h | 3 ++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Source/Engine/Platform/Linux/LinuxPlatform.cpp b/Source/Engine/Platform/Linux/LinuxPlatform.cpp index e594e8e75..4e3f9013e 100644 --- a/Source/Engine/Platform/Linux/LinuxPlatform.cpp +++ b/Source/Engine/Platform/Linux/LinuxPlatform.cpp @@ -1691,6 +1691,15 @@ void LinuxPlatform::Tick() } } break; + case MapNotify: + // Auto-focus shown windows + window = WindowsManager::GetByNativePtr((void*)event.xmap.window); + if (window && window->_focusOnMapped) + { + window->_focusOnMapped = false; + window->Focus(); + } + break; case FocusIn: // Update input context focus X11::XSetICFocus(IC); diff --git a/Source/Engine/Platform/Linux/LinuxWindow.cpp b/Source/Engine/Platform/Linux/LinuxWindow.cpp index bad1c8132..b2d5a26c4 100644 --- a/Source/Engine/Platform/Linux/LinuxWindow.cpp +++ b/Source/Engine/Platform/Linux/LinuxWindow.cpp @@ -242,10 +242,7 @@ void LinuxWindow::Show() LINUX_WINDOW_PROLOG; X11::XMapWindow(display, window); X11::XFlush(display); - if (_settings.AllowInput && _settings.ActivateWhenFirstShown) - { - Focus(); - } + _focusOnMapped = _settings.AllowInput && _settings.ActivateWhenFirstShown; // Base WindowBase::Show(); @@ -306,6 +303,11 @@ bool LinuxWindow::IsClosed() const return _isClosing; } +bool LinuxWindow::IsForegroundWindow() const +{ + return _focused || _focusOnMapped; +} + void LinuxWindow::SetClientBounds(const Rectangle& clientArea) { int32 x = Math::TruncToInt(clientArea.GetX()); @@ -673,7 +675,14 @@ void LinuxWindow::SetOpacity(const float opacity) void LinuxWindow::Focus() { - // TODO: impl this + if (_focused || !IsWindowMapped()) + return; + + LINUX_WINDOW_PROLOG; + + X11::XSetInputFocus(display, window, RevertToPointerRoot, CurrentTime); + X11::XFlush(display); + X11::XSync(display, 0); } void LinuxWindow::SetTitle(const StringView& title) diff --git a/Source/Engine/Platform/Linux/LinuxWindow.h b/Source/Engine/Platform/Linux/LinuxWindow.h index 22755874f..845fd587e 100644 --- a/Source/Engine/Platform/Linux/LinuxWindow.h +++ b/Source/Engine/Platform/Linux/LinuxWindow.h @@ -20,7 +20,7 @@ public: private: - bool _resizeDisabled; + bool _resizeDisabled, _focusOnMapped = false; float _opacity = 1.0f; HandleType _window; @@ -74,6 +74,7 @@ public: void Maximize() override; void Restore() override; bool IsClosed() const override; + bool IsForegroundWindow() const override; void BringToFront(bool force = false) override; void SetClientBounds(const Rectangle& clientArea) override; void SetPosition(const Vector2& position) override;