1
0
mirror of synced 2025-02-08 23:09:36 +01:00

fix: Properly trigger shortcuts on selected view on macOS

This commit is contained in:
WerWolv 2025-02-06 22:13:06 +01:00
parent f17d0d3ae1
commit 96afa650d1
5 changed files with 36 additions and 9 deletions

View File

@ -597,6 +597,12 @@ namespace hex {
* @return The view if it exists, nullptr otherwise * @return The view if it exists, nullptr otherwise
*/ */
View* getViewByName(const UnlocalizedString &unlocalizedName); 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 */ /* Tools Registry. Allows adding new entries to the tools window */

View File

@ -135,7 +135,13 @@ namespace hex {
*/ */
static void processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode); 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 * @brief Clear all shortcuts

View File

@ -736,6 +736,15 @@ namespace hex {
return nullptr; return nullptr;
} }
View* getFocusedView() {
for (const auto &[unlocalizedName, view] : *impl::s_views) {
if (view->isFocused())
return view.get();
}
return nullptr;
}
} }
namespace ContentRegistry::Tools { namespace ContentRegistry::Tools {

View File

@ -315,11 +315,12 @@ namespace hex {
return pressedShortcut; return pressedShortcut;
} }
static void processShortcut(Shortcut shortcut, const std::map<Shortcut, ShortcutManager::ShortcutEntry> &shortcuts) { static bool processShortcut(Shortcut shortcut, const std::map<Shortcut, ShortcutManager::ShortcutEntry> &shortcuts) {
if (s_paused) return; if (s_paused)
return true;
if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId))
return; return true;
const bool currentlyTyping = ImGui::GetIO().WantTextInput; const bool currentlyTyping = ImGui::GetIO().WantTextInput;
@ -338,15 +339,19 @@ namespace hex {
if (!entry.unlocalizedName.empty()) { if (!entry.unlocalizedName.empty()) {
s_lastShortcutMainMenu = entry.unlocalizedName.front(); 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) if (view == nullptr)
processShortcut(shortcut, s_globalShortcuts); return processShortcut(shortcut, s_globalShortcuts);
else 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) { void ShortcutManager::process(const View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {

View File

@ -51,8 +51,9 @@ namespace hex::plugin::builtin {
if (menu::menuItemEx(Lang(name), icon, shortcut, selectedCallback(), enabledCallback())) { if (menu::menuItemEx(Lang(name), icon, shortcut, selectedCallback(), enabledCallback())) {
if (shortcut == Shortcut::None) if (shortcut == Shortcut::None)
callback(); callback();
else else {
ShortcutManager::runShortcut(shortcut, view); ShortcutManager::runShortcut(shortcut, ContentRegistry::Views::getFocusedView());
}
} }
} else { } else {
bool isSubmenu = (menuItems.begin() + 1)->get() == ContentRegistry::Interface::impl::SubMenuValue; bool isSubmenu = (menuItems.begin() + 1)->get() == ContentRegistry::Interface::impl::SubMenuValue;