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));