From 7bac33d5830072e1bf696ccdd162cafa50855bb6 Mon Sep 17 00:00:00 2001 From: lpogic Date: Sun, 19 May 2024 20:55:54 +0200 Subject: [PATCH 1/2] Window focus signals added --- include/TGUI/Backend/Window/BackendGui.hpp | 7 +++++++ src/Backend/Window/BackendGui.cpp | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/TGUI/Backend/Window/BackendGui.hpp b/include/TGUI/Backend/Window/BackendGui.hpp index 45e9535f0..9a0e7a96d 100644 --- a/include/TGUI/Backend/Window/BackendGui.hpp +++ b/include/TGUI/Backend/Window/BackendGui.hpp @@ -700,6 +700,11 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// bool handleTwoFingerScroll(bool wasAlreadyScrolling); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Updates internal window focus flag and emits signal if flag state was changed. + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void updateWindowFocusState(bool windowFocused); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Updates the view and changes the size of the root container when needed. // Derived classes should update m_framebufferSize in this function and then call this function from the base class. @@ -711,6 +716,8 @@ TGUI_MODULE_EXPORT namespace tgui public: SignalFloatRect onViewChange = {"ViewChanged"}; //!< The view was changed. Optional parameter: new view rectangle + Signal onWindowFocus = {"Focused"}; //!< The window was focused + Signal onWindowUnfocus = {"Unfocused"}; //!< The window was unfocused ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/Backend/Window/BackendGui.cpp b/src/Backend/Window/BackendGui.cpp index 657d83cd9..8311f37ef 100644 --- a/src/Backend/Window/BackendGui.cpp +++ b/src/Backend/Window/BackendGui.cpp @@ -186,12 +186,12 @@ namespace tgui } case Event::Type::LostFocus: { - m_windowFocused = false; + updateWindowFocusState(false); break; } case Event::Type::GainedFocus: { - m_windowFocused = true; + updateWindowFocusState(true); break; } case Event::Type::Resized: @@ -626,6 +626,20 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void BackendGui::updateWindowFocusState(bool windowFocused) + { + if(m_windowFocused != windowFocused) { + m_windowFocused = windowFocused; + if(windowFocused) { + onWindowFocus.emit(m_container.get()); + } else { + onWindowUnfocus.emit(m_container.get()); + } + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void BackendGui::updateContainerSize() { m_viewport.updateParentSize({static_cast(m_framebufferSize.x), static_cast(m_framebufferSize.y)}); From 60a40806119241d7cb81be3dc7cc6901bb37d8a8 Mon Sep 17 00:00:00 2001 From: lpogic Date: Sun, 19 May 2024 21:46:40 +0200 Subject: [PATCH 2/2] Ensure gui update on window focus lost --- src/Backend/Window/BackendGui.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Backend/Window/BackendGui.cpp b/src/Backend/Window/BackendGui.cpp index 8311f37ef..03544cd6f 100644 --- a/src/Backend/Window/BackendGui.cpp +++ b/src/Backend/Window/BackendGui.cpp @@ -187,12 +187,12 @@ namespace tgui case Event::Type::LostFocus: { updateWindowFocusState(false); - break; + return true; } case Event::Type::GainedFocus: { updateWindowFocusState(true); - break; + return true; } case Event::Type::Resized: {