diff --git a/lib/libimhex/source/api/keybinding.cpp b/lib/libimhex/source/api/keybinding.cpp index 1f2c776b6..6854648cf 100644 --- a/lib/libimhex/source/api/keybinding.cpp +++ b/lib/libimhex/source/api/keybinding.cpp @@ -107,23 +107,40 @@ namespace hex { return result; } - void ShortcutManager::updateShortcut(const Shortcut &oldShortcut, const Shortcut &newShortcut, View *view) { - if (oldShortcut == newShortcut) - return; + static bool updateShortcutImpl(const Shortcut &oldShortcut, const Shortcut &newShortcut, std::map &shortcuts) { + if (shortcuts.contains(oldShortcut + CurrentView)) { + if (shortcuts.contains(newShortcut + CurrentView)) + return false; + shortcuts[newShortcut + CurrentView] = shortcuts[oldShortcut + CurrentView]; + shortcuts[newShortcut + CurrentView].shortcut = newShortcut + CurrentView; + shortcuts.erase(oldShortcut + CurrentView); + } + + return true; + } + + bool ShortcutManager::updateShortcut(const Shortcut &oldShortcut, const Shortcut &newShortcut, View *view) { + if (oldShortcut == newShortcut) + return true; + + bool result; if (view != nullptr) { - if (view->m_shortcuts.contains(oldShortcut + CurrentView)) { - view->m_shortcuts[newShortcut + CurrentView] = view->m_shortcuts[oldShortcut + CurrentView]; - view->m_shortcuts[newShortcut + CurrentView].shortcut = newShortcut + CurrentView; - view->m_shortcuts.erase(oldShortcut + CurrentView); - } + result = updateShortcutImpl(oldShortcut, newShortcut, view->m_shortcuts); } else { - if (s_globalShortcuts.contains(oldShortcut)) { - s_globalShortcuts[newShortcut] = s_globalShortcuts[oldShortcut]; - s_globalShortcuts[newShortcut].shortcut = newShortcut; - s_globalShortcuts.erase(oldShortcut); + result = updateShortcutImpl(oldShortcut, newShortcut, s_globalShortcuts); + } + + if (result) { + for (auto &[priority, menuItem] : ContentRegistry::Interface::impl::getMenuItems()) { + if (menuItem.view == view && *menuItem.shortcut == oldShortcut) { + *menuItem.shortcut = newShortcut; + break; + } } } + + return result; } } \ No newline at end of file