diff --git a/lib/libimhex/include/hex/ui/view.hpp b/lib/libimhex/include/hex/ui/view.hpp index 5b5bef1ac..b52e4ac56 100644 --- a/lib/libimhex/include/hex/ui/view.hpp +++ b/lib/libimhex/include/hex/ui/view.hpp @@ -98,6 +98,14 @@ namespace hex { [[nodiscard]] static std::string toWindowName(const UnlocalizedString &unlocalizedName); + [[nodiscard]] bool isFocused() const { return m_focused; } + + /** + * @brief Used for focus handling. Don't use this directly + * @param focused Whether this view is focused + */ + void setFocused(bool focused) { m_focused = focused; } + public: class Window; class Special; @@ -110,6 +118,7 @@ namespace hex { std::map m_shortcuts; bool m_windowJustOpened = false; const char *m_icon; + bool m_focused = false; friend class ShortcutManager; }; diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index 830bb3b27..938a0d65e 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -728,6 +728,7 @@ namespace hex { // Detect if the window is focused const bool focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_NoPopupHierarchy); + view->setFocused(focused); // Dock the window if it's not already docked if (view->didWindowJustOpen() && !ImGui::IsWindowDocked()) { diff --git a/plugins/builtin/source/content/window_decoration.cpp b/plugins/builtin/source/content/window_decoration.cpp index f94a523a8..a86d493e9 100644 --- a/plugins/builtin/source/content/window_decoration.cpp +++ b/plugins/builtin/source/content/window_decoration.cpp @@ -33,7 +33,7 @@ namespace hex::plugin::builtin { bool s_displayShortcutHighlights = true; bool s_useNativeMenuBar = false; - void createNestedMenu(std::span menuItems, const char *icon, const Shortcut &shortcut, const ContentRegistry::Interface::impl::MenuCallback &callback, const ContentRegistry::Interface::impl::EnabledCallback &enabledCallback, const ContentRegistry::Interface::impl::SelectedCallback &selectedCallback) { + void createNestedMenu(std::span menuItems, const char *icon, const Shortcut &shortcut, View *view, const ContentRegistry::Interface::impl::MenuCallback &callback, const ContentRegistry::Interface::impl::EnabledCallback &enabledCallback, const ContentRegistry::Interface::impl::SelectedCallback &selectedCallback) { const auto &name = menuItems.front(); if (name.get() == ContentRegistry::Interface::impl::SeparatorValue) { @@ -46,13 +46,13 @@ namespace hex::plugin::builtin { callback(); } } else if (menuItems.size() == 1) { - if (menu::menuItemEx(Lang(name), icon, shortcut, selectedCallback(), enabledCallback())) + if (menu::menuItemEx(Lang(name), icon, shortcut, selectedCallback(), enabledCallback() && (view == nullptr || view->isFocused()))) callback(); } else { bool isSubmenu = (menuItems.begin() + 1)->get() == ContentRegistry::Interface::impl::SubMenuValue; if (menu::beginMenuEx(Lang(name), std::next(menuItems.begin())->get() == ContentRegistry::Interface::impl::SubMenuValue ? icon : nullptr, isSubmenu ? enabledCallback() : true)) { - createNestedMenu({ std::next(menuItems.begin()), menuItems.end() }, icon, shortcut, callback, enabledCallback, selectedCallback); + createNestedMenu({ std::next(menuItems.begin()), menuItems.end() }, icon, shortcut, view, callback, enabledCallback, selectedCallback); menu::endMenu(); } } @@ -287,7 +287,7 @@ namespace hex::plugin::builtin { toolbarIndex ] = menuItem; - createNestedMenu(unlocalizedNames | std::views::drop(1), icon.glyph.c_str(), shortcut, callback, enabledCallback, selectedCallack); + createNestedMenu(unlocalizedNames | std::views::drop(1), icon.glyph.c_str(), shortcut, view, callback, enabledCallback, selectedCallack); } } @@ -572,7 +572,7 @@ namespace hex::plugin::builtin { const auto &[unlocalizedNames, icon, shortcut, view, callback, enabledCallback, selectedCallback, toolbarIndex] = menuItem; if (ImGui::BeginPopup(unlocalizedNames.front().get().c_str())) { - createNestedMenu({ unlocalizedNames.begin() + 1, unlocalizedNames.end() }, icon.glyph.c_str(), shortcut, callback, enabledCallback, selectedCallback); + createNestedMenu({ unlocalizedNames.begin() + 1, unlocalizedNames.end() }, icon.glyph.c_str(), shortcut, view, callback, enabledCallback, selectedCallback); ImGui::EndPopup(); } }