From 96afa650d1d9027bd6b94c147e7f0975c64ab679 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 6 Feb 2025 22:13:06 +0100 Subject: [PATCH] fix: Properly trigger shortcuts on selected view on macOS --- .../include/hex/api/content_registry.hpp | 6 ++++++ .../include/hex/api/shortcut_manager.hpp | 8 +++++++- lib/libimhex/source/api/content_registry.cpp | 9 +++++++++ lib/libimhex/source/api/shortcut_manager.cpp | 17 +++++++++++------ .../source/content/window_decoration.cpp | 5 +++-- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index c14a40384..09e838bf0 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -597,6 +597,12 @@ namespace hex { * @return The view if it exists, nullptr otherwise */ View* getViewByName(const UnlocalizedString &unlocalizedName); + + /** + * @brief Gets the currently focused view + * @return The view that is focused right now. nullptr if none is focused + */ + View* getFocusedView(); } /* Tools Registry. Allows adding new entries to the tools window */ diff --git a/lib/libimhex/include/hex/api/shortcut_manager.hpp b/lib/libimhex/include/hex/api/shortcut_manager.hpp index fa5d255d7..fdd952a11 100644 --- a/lib/libimhex/include/hex/api/shortcut_manager.hpp +++ b/lib/libimhex/include/hex/api/shortcut_manager.hpp @@ -135,7 +135,13 @@ namespace hex { */ static void processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode); - static void runShortcut(const Shortcut &shortcut, const View *view = nullptr); + /** + * @brief Runs the callback of a shortcut as if it was pressed on the keyboard + * @param shortcut Shortcut to run + * @param view View the shortcut belongs to or nullptr to run a global shortcut + * @return True if a callback was executed, false if not + */ + static bool runShortcut(const Shortcut &shortcut, const View *view = nullptr); /** * @brief Clear all shortcuts diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index ffc43a346..43625c2be 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -736,6 +736,15 @@ namespace hex { return nullptr; } + View* getFocusedView() { + for (const auto &[unlocalizedName, view] : *impl::s_views) { + if (view->isFocused()) + return view.get(); + } + + return nullptr; + } + } namespace ContentRegistry::Tools { diff --git a/lib/libimhex/source/api/shortcut_manager.cpp b/lib/libimhex/source/api/shortcut_manager.cpp index a2c79de22..8d3f941c6 100644 --- a/lib/libimhex/source/api/shortcut_manager.cpp +++ b/lib/libimhex/source/api/shortcut_manager.cpp @@ -315,11 +315,12 @@ namespace hex { return pressedShortcut; } - static void processShortcut(Shortcut shortcut, const std::map &shortcuts) { - if (s_paused) return; + static bool processShortcut(Shortcut shortcut, const std::map &shortcuts) { + if (s_paused) + return true; if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) - return; + return true; const bool currentlyTyping = ImGui::GetIO().WantTextInput; @@ -338,15 +339,19 @@ namespace hex { if (!entry.unlocalizedName.empty()) { s_lastShortcutMainMenu = entry.unlocalizedName.front(); } + + return true; } } + + return false; } - void ShortcutManager::runShortcut(const Shortcut &shortcut, const View *view) { + bool ShortcutManager::runShortcut(const Shortcut &shortcut, const View *view) { if (view == nullptr) - processShortcut(shortcut, s_globalShortcuts); + return processShortcut(shortcut, s_globalShortcuts); else - processShortcut(shortcut, view->m_shortcuts); + return processShortcut(shortcut, view->m_shortcuts); } void ShortcutManager::process(const View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { diff --git a/plugins/builtin/source/content/window_decoration.cpp b/plugins/builtin/source/content/window_decoration.cpp index 1c92d8c80..c7f48f4b2 100644 --- a/plugins/builtin/source/content/window_decoration.cpp +++ b/plugins/builtin/source/content/window_decoration.cpp @@ -51,8 +51,9 @@ namespace hex::plugin::builtin { if (menu::menuItemEx(Lang(name), icon, shortcut, selectedCallback(), enabledCallback())) { if (shortcut == Shortcut::None) callback(); - else - ShortcutManager::runShortcut(shortcut, view); + else { + ShortcutManager::runShortcut(shortcut, ContentRegistry::Views::getFocusedView()); + } } } else { bool isSubmenu = (menuItems.begin() + 1)->get() == ContentRegistry::Interface::impl::SubMenuValue;