From a6f4d0cdecbe5523717dc40a39cf7829c0dac7e0 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 3 Feb 2024 23:56:08 +0100 Subject: [PATCH] impr: Unfocus ImGui windows when main window loses focus --- .../include/hex/api/event_manager.hpp | 1 + main/gui/source/window/window.cpp | 4 ++++ plugins/builtin/source/content/events.cpp | 24 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/lib/libimhex/include/hex/api/event_manager.hpp b/lib/libimhex/include/hex/api/event_manager.hpp index 90f537c66..c5fd5863f 100644 --- a/lib/libimhex/include/hex/api/event_manager.hpp +++ b/lib/libimhex/include/hex/api/event_manager.hpp @@ -219,6 +219,7 @@ namespace hex { EVENT_DEF(EventAbnormalTermination, int); EVENT_DEF(EventThemeChanged); EVENT_DEF(EventOSThemeChanged); + EVENT_DEF(EventWindowFocused, bool); /** * @brief Called when the provider is created. diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index 7df11a024..14382863a 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -709,6 +709,10 @@ namespace hex { win->m_unlockFrameRate = true; }); + glfwSetWindowFocusCallback(m_window, [](GLFWwindow *, int focused) { + EventWindowFocused::post(focused == GLFW_TRUE); + }); + #if !defined(OS_WEB) // Register key press callback glfwSetInputMode(m_window, GLFW_LOCK_KEY_MODS, GLFW_TRUE); diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index 56f868598..ee171a6de 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -228,6 +228,30 @@ namespace hex::plugin::builtin { LocalizationManager::loadLanguage(it->second); }); + EventWindowFocused::subscribe([](bool focused) { + const auto ctx = ImGui::GetCurrentContext(); + if (ctx == nullptr) + return; + + // Get the currently focused window + const auto window = ctx->NavWindow; + if (window == nullptr) + return; + + static ImGuiWindow *lastFocusedWindow = window; + + if (focused) { + // If the main window gains focus again, restore the last focused window + ImGui::FocusWindow(lastFocusedWindow, ImGuiFocusRequestFlags_RestoreFocusedChild); + } else { + // If the main window loses focus, store the currently focused window + // and remove focus from it so it doesn't look like it's focused and + // cursor blink animations don't play + lastFocusedWindow = window; + ImGui::FocusWindow(nullptr); + } + }); + fs::setFileBrowserErrorCallback([](const std::string& errMsg){ #if defined(NFD_PORTAL) ui::PopupError::open(hex::format("hex.builtin.popup.error.file_dialog.portal"_lang, errMsg));