From 61f04110a62815fc7c7e6165f0e2b9037592cd0f Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Tue, 21 Jan 2025 15:51:12 +0200 Subject: [PATCH] Add `Window.IsAlwaysOnTop` property --- Source/Engine/Platform/Base/WindowBase.cpp | 9 +++++++++ Source/Engine/Platform/Base/WindowBase.h | 11 +++++++++++ Source/Engine/Platform/SDL/SDLPlatform.cpp | 3 ++- Source/Engine/Platform/SDL/SDLWindow.cpp | 19 ++++++++++++++++--- Source/Engine/Platform/SDL/SDLWindow.h | 2 ++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Platform/Base/WindowBase.cpp b/Source/Engine/Platform/Base/WindowBase.cpp index 6d23b04eb..b2aa7981a 100644 --- a/Source/Engine/Platform/Base/WindowBase.cpp +++ b/Source/Engine/Platform/Base/WindowBase.cpp @@ -164,6 +164,15 @@ void WindowBase::SetIsVisible(bool isVisible) } } +bool WindowBase::IsAlwaysOnTop() const +{ + return false; +} + +void WindowBase::SetIsAlwaysOnTop(bool isAlwaysOnTop) +{ +} + String WindowBase::ToString() const { return GetTitle(); diff --git a/Source/Engine/Platform/Base/WindowBase.h b/Source/Engine/Platform/Base/WindowBase.h index ef069cb84..b35d56ad4 100644 --- a/Source/Engine/Platform/Base/WindowBase.h +++ b/Source/Engine/Platform/Base/WindowBase.h @@ -157,6 +157,17 @@ public: return _maximized; } + /// + /// Gets a value that indicates whether a window is always on top of other windows. + /// + API_PROPERTY() virtual bool IsAlwaysOnTop() const; + + /// + /// Sets a value that indicates whether a window is always on top of other windows. + /// + /// True if always on top. + API_PROPERTY() virtual void SetIsAlwaysOnTop(bool isAlwaysOnTop); + /// /// Gets the native window handle. /// diff --git a/Source/Engine/Platform/SDL/SDLPlatform.cpp b/Source/Engine/Platform/SDL/SDLPlatform.cpp index 20517a10c..a39feaa95 100644 --- a/Source/Engine/Platform/SDL/SDLPlatform.cpp +++ b/Source/Engine/Platform/SDL/SDLPlatform.cpp @@ -306,7 +306,8 @@ void SDLPlatform::Tick() draggedWindow->HandleEvent(buttonUpEvent); //SDL_PushEvent(&buttonUpEvent); - SDL_SetWindowAlwaysOnTop(draggedWindow->GetSDLWindow(), false); + if (!draggedWindow->GetSettings().IsTopmost) + draggedWindow->SetIsAlwaysOnTop(false); draggedWindow->BringToFront(); draggedWindow = nullptr; diff --git a/Source/Engine/Platform/SDL/SDLWindow.cpp b/Source/Engine/Platform/SDL/SDLWindow.cpp index 4f5ed569e..fd25a5836 100644 --- a/Source/Engine/Platform/SDL/SDLWindow.cpp +++ b/Source/Engine/Platform/SDL/SDLWindow.cpp @@ -825,7 +825,7 @@ void SDLWindow::Show() // Reused top-most windows (DockHintWindow) doesn't stay on top for some reason if (_settings.IsTopmost && _settings.Type != WindowType::Tooltip) - SDL_SetWindowAlwaysOnTop(_window, true); + SetIsAlwaysOnTop(true); if (_isTrackingMouse) { @@ -1051,12 +1051,25 @@ void SDLWindow::SetIsFullscreen(bool isFullscreen) if (!isFullscreen) { // The window is set to always-on-top for some reason when leaving fullscreen - SDL_SetWindowAlwaysOnTop(_window, false); + SetIsAlwaysOnTop(false); } WindowBase::SetIsFullscreen(isFullscreen); } +bool SDLWindow::IsAlwaysOnTop() const +{ + SDL_WindowFlags flags = SDL_GetWindowFlags(_window); + return (flags & SDL_WINDOW_ALWAYS_ON_TOP) != 0; +} + +void SDLWindow::SetIsAlwaysOnTop(bool isAlwaysOnTop) +{ + if (!SDL_SetWindowAlwaysOnTop(_window, isAlwaysOnTop)) + LOG(Warning, "SDL_SetWindowAlwaysOnTop failed: {0}", String(SDL_GetError())); + // Not sure if this should change _settings.IsTopmost to reflect the new value? +} + Float2 SDLWindow::GetPosition() const { Int2 topLeftBorder; @@ -1332,7 +1345,7 @@ DragDropEffect SDLWindow::DoDragDrop(const StringView& data, const Float2& offse else #endif { - SDL_SetWindowAlwaysOnTop(_window, true); + SetIsAlwaysOnTop(true); Show(); //draggingActive = true; diff --git a/Source/Engine/Platform/SDL/SDLWindow.h b/Source/Engine/Platform/SDL/SDLWindow.h index ef5339f29..c859e3da1 100644 --- a/Source/Engine/Platform/SDL/SDLWindow.h +++ b/Source/Engine/Platform/SDL/SDLWindow.h @@ -91,6 +91,8 @@ public: void SetPosition(const Float2& position) override; void SetClientPosition(const Float2& position) override; void SetIsFullscreen(bool isFullscreen) override; + bool IsAlwaysOnTop() const override; + void SetIsAlwaysOnTop(bool isAlwaysOnTop) override; Float2 GetPosition() const override; Float2 GetSize() const override; Float2 GetClientSize() const override;