From ee4bb33b0a3d746eedfcea2197c60fdbecf24d74 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 15 Dec 2024 11:54:10 +0100 Subject: [PATCH] impr: Display sub menus for shortcut settings --- .../include/hex/api/shortcut_manager.hpp | 4 +++- lib/libimhex/source/api/content_registry.cpp | 10 ++++++++-- lib/libimhex/source/api/shortcut_manager.cpp | 12 +++++++++-- .../source/content/settings_entries.cpp | 20 +++++++++++++++---- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/libimhex/include/hex/api/shortcut_manager.hpp b/lib/libimhex/include/hex/api/shortcut_manager.hpp index dc49b46a1..48cad1993 100644 --- a/lib/libimhex/include/hex/api/shortcut_manager.hpp +++ b/lib/libimhex/include/hex/api/shortcut_manager.hpp @@ -393,7 +393,7 @@ namespace hex { using Callback = std::function; struct ShortcutEntry { Shortcut shortcut; - UnlocalizedString unlocalizedName; + std::vector unlocalizedName; Callback callback; }; @@ -403,6 +403,7 @@ namespace hex { * @param unlocalizedName The unlocalized name of the shortcut * @param callback The callback to call when the shortcut is triggered. */ + static void addGlobalShortcut(const Shortcut &shortcut, const std::vector &unlocalizedName, const Callback &callback); static void addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const Callback &callback); /** @@ -412,6 +413,7 @@ namespace hex { * @param unlocalizedName The unlocalized name of the shortcut * @param callback The callback to call when the shortcut is triggered. */ + static void addShortcut(View *view, const Shortcut &shortcut, const std::vector &unlocalizedName, const Callback &callback); static void addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const Callback &callback); diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 7198aecd2..5962ce894 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -909,10 +909,16 @@ namespace hex { if (shortcut != Shortcut::None) { auto callbackIfEnabled = [enabledCallback, function]{ if (enabledCallback()) { function(); } }; + + const auto unlocalizedShortcutName = + unlocalizedMainMenuNames.size() == 1 ? + std::vector { unlocalizedMainMenuNames.back() } : + std::vector(unlocalizedMainMenuNames.begin() + 1, unlocalizedMainMenuNames.end()); + if (shortcut.isLocal() && view != nullptr) - ShortcutManager::addShortcut(view, shortcut, unlocalizedMainMenuNames.back(), callbackIfEnabled); + ShortcutManager::addShortcut(view, shortcut, unlocalizedShortcutName, callbackIfEnabled); else - ShortcutManager::addGlobalShortcut(shortcut, unlocalizedMainMenuNames.back(), callbackIfEnabled); + ShortcutManager::addGlobalShortcut(shortcut, unlocalizedShortcutName, callbackIfEnabled); } } diff --git a/lib/libimhex/source/api/shortcut_manager.cpp b/lib/libimhex/source/api/shortcut_manager.cpp index 4663bf3b4..4057124da 100644 --- a/lib/libimhex/source/api/shortcut_manager.cpp +++ b/lib/libimhex/source/api/shortcut_manager.cpp @@ -16,14 +16,22 @@ namespace hex { } - void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function &callback) { + void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::vector &unlocalizedName, const std::function &callback) { s_globalShortcuts->insert({ shortcut, { shortcut, unlocalizedName, callback } }); } - void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function &callback) { + void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function &callback) { + s_globalShortcuts->insert({ shortcut, { shortcut, { unlocalizedName }, callback } }); + } + + void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const std::vector &unlocalizedName, const std::function &callback) { view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, unlocalizedName, callback } }); } + void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function &callback) { + view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, { unlocalizedName }, callback } }); + } + static Shortcut getShortcut(bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { Shortcut pressedShortcut; diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 48062ce73..8b9cadf0c 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -246,9 +246,12 @@ namespace hex::plugin::builtin { class KeybindingWidget : public ContentRegistry::Settings::Widgets::Widget { public: - KeybindingWidget(View *view, const Shortcut &shortcut) : m_view(view), m_shortcut(shortcut), m_drawShortcut(shortcut), m_defaultShortcut(shortcut) {} + KeybindingWidget(View *view, const Shortcut &shortcut, const std::vector &fullName) + : m_view(view), m_shortcut(shortcut), m_drawShortcut(shortcut), m_defaultShortcut(shortcut), m_fullName(fullName) {} bool draw(const std::string &name) override { + std::ignore = name; + std::string label; if (!m_editing) @@ -300,7 +303,15 @@ namespace hex::plugin::builtin { ImGui::SameLine(); - ImGuiExt::TextFormatted("{}", name); + std::string fullName; + for (const auto &part : m_fullName) { + fullName += Lang(part).get(); + fullName += " -> "; + } + if (fullName.size() >= 4) + fullName = fullName.substr(0, fullName.size() - 4); + + ImGuiExt::TextFormatted("{}", fullName); ImGui::PopID(); @@ -376,6 +387,7 @@ namespace hex::plugin::builtin { private: View *m_view = nullptr; Shortcut m_shortcut, m_drawShortcut, m_defaultShortcut; + std::vector m_fullName; bool m_editing = false; bool m_hasDuplicate = false; }; @@ -934,12 +946,12 @@ namespace hex::plugin::builtin { { EventImHexStartupFinished::subscribe([]{ for (const auto &shortcutEntry : ShortcutManager::getGlobalShortcuts()) { - ContentRegistry::Settings::add("hex.builtin.setting.shortcuts", "hex.builtin.setting.shortcuts.global", shortcutEntry.unlocalizedName, nullptr, shortcutEntry.shortcut); + ContentRegistry::Settings::add("hex.builtin.setting.shortcuts", "hex.builtin.setting.shortcuts.global", shortcutEntry.unlocalizedName.back(), nullptr, shortcutEntry.shortcut, shortcutEntry.unlocalizedName); } for (auto &[viewName, view] : ContentRegistry::Views::impl::getEntries()) { for (const auto &shortcutEntry : ShortcutManager::getViewShortcuts(view.get())) { - ContentRegistry::Settings::add("hex.builtin.setting.shortcuts", viewName, shortcutEntry.unlocalizedName, view.get(), shortcutEntry.shortcut); + ContentRegistry::Settings::add("hex.builtin.setting.shortcuts", viewName, shortcutEntry.unlocalizedName.back(), view.get(), shortcutEntry.shortcut, shortcutEntry.unlocalizedName); } } });