From a07c79efcb8be775561c762ab701997c2c452360 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 15 Dec 2024 22:39:24 +0100 Subject: [PATCH] fix: Pattern editor shortcuts being set to use CTRL on macOS --- .../include/hex/api/shortcut_manager.hpp | 33 +++++++-------- lib/libimhex/source/api/shortcut_manager.cpp | 22 +++++----- .../source/content/settings_entries.cpp | 5 ++- .../content/views/view_pattern_editor.cpp | 40 +++++++++---------- 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/lib/libimhex/include/hex/api/shortcut_manager.hpp b/lib/libimhex/include/hex/api/shortcut_manager.hpp index 7354d492c..e1120a02b 100644 --- a/lib/libimhex/include/hex/api/shortcut_manager.hpp +++ b/lib/libimhex/include/hex/api/shortcut_manager.hpp @@ -138,6 +138,7 @@ namespace hex { constexpr Key() = default; constexpr Key(Keys key) : m_key(static_cast(key)) { } + bool operator==(const Key &) const = default; auto operator<=>(const Key &) const = default; [[nodiscard]] constexpr u32 getKeyCode() const { return m_key; } @@ -171,7 +172,7 @@ namespace hex { Shortcut& operator=(Shortcut &&) noexcept = default; - constexpr static inline auto None = Keys(0); + constexpr static auto None = Keys(0); Shortcut operator+(const Key &other) const { Shortcut result = *this; @@ -187,25 +188,21 @@ namespace hex { } bool operator<(const Shortcut &other) const { - u64 left = 0; - for (const auto &key : m_keys) - left |= key.getKeyCode(); - - u64 right = 0; - for (const auto &key : other.m_keys) - right |= key.getKeyCode(); - - return left < right; + return m_keys < other.m_keys; } bool operator==(const Shortcut &other) const { - u64 left = 0; - for (const auto &key : m_keys) - left |= key.getKeyCode(); + return m_keys == other.m_keys; + } - u64 right = 0; - for (const auto &key : other.m_keys) - right |= key.getKeyCode(); + bool match(const Shortcut &other) const { + auto left = m_keys; + auto right = other.m_keys; + + left.erase(AllowWhileTyping); + right.erase(AllowWhileTyping); + left.erase(CurrentView); + right.erase(CurrentView); return left == right; } @@ -214,6 +211,10 @@ namespace hex { return m_keys.contains(CurrentView); } + bool allowWhileTyping() const { + return m_keys.contains(AllowWhileTyping); + } + std::string toString() const; const std::set& getKeys() const { return m_keys; } diff --git a/lib/libimhex/source/api/shortcut_manager.cpp b/lib/libimhex/source/api/shortcut_manager.cpp index 968b3de63..4574bd26b 100644 --- a/lib/libimhex/source/api/shortcut_manager.cpp +++ b/lib/libimhex/source/api/shortcut_manager.cpp @@ -181,22 +181,26 @@ namespace hex { void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::vector &unlocalizedName, const std::function &callback) { log::debug("Adding global shortcut {} for {}", shortcut.toString(), unlocalizedName.back().get()); - s_globalShortcuts->insert({ shortcut, { shortcut, unlocalizedName, callback } }); + auto [it, inserted] = s_globalShortcuts->insert({ shortcut, { shortcut, unlocalizedName, callback } }); + if (!inserted) log::error("Failed to add shortcut!"); } void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function &callback) { log::debug("Adding global shortcut {} for {}", shortcut.toString(), unlocalizedName.get()); - s_globalShortcuts->insert({ shortcut, { shortcut, { unlocalizedName }, callback } }); + auto [it, inserted] = s_globalShortcuts->insert({ shortcut, { shortcut, { unlocalizedName }, callback } }); + if (!inserted) log::error("Failed to add shortcut!"); } void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const std::vector &unlocalizedName, const std::function &callback) { log::debug("Adding shortcut {} for {}", shortcut.toString(), unlocalizedName.back().get()); - view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, unlocalizedName, callback } }); + auto [it, inserted] = view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, unlocalizedName, callback } }); + if (!inserted) log::error("Failed to add shortcut!"); } void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function &callback) { log::debug("Adding shortcut {} for {}", shortcut.toString(), unlocalizedName.get()); - view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, { unlocalizedName }, callback } }); + auto [it, inserted] = view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, { unlocalizedName }, callback } }); + if (!inserted) log::error("Failed to add shortcut!"); } static Shortcut getShortcut(bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { @@ -224,11 +228,11 @@ namespace hex { if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) return; - if (shortcuts.contains(shortcut + AllowWhileTyping)) { - shortcuts.at(shortcut + AllowWhileTyping).callback(); - } else if (shortcuts.contains(shortcut)) { - if (!ImGui::GetIO().WantTextInput) - shortcuts.at(shortcut).callback(); + for (const auto &[potentialShortcut, entry] : shortcuts) { + if (potentialShortcut.match(shortcut)) { + if (!ImGui::GetIO().WantTextInput || potentialShortcut.allowWhileTyping()) + entry.callback(); + } } } diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 8b9cadf0c..436b2f0f1 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -283,7 +283,7 @@ namespace hex::plugin::builtin { bool settingChanged = false; - ImGui::BeginDisabled(m_drawShortcut == m_defaultShortcut); + ImGui::BeginDisabled(m_drawShortcut.match(m_defaultShortcut)); if (ImGuiExt::IconButton(ICON_VS_X, ImGui::GetStyleColorVec4(ImGuiCol_Text))) { m_hasDuplicate = !ShortcutManager::updateShortcut(m_shortcut, m_defaultShortcut, m_view); @@ -320,8 +320,9 @@ namespace hex::plugin::builtin { m_editing = false; ShortcutManager::resumeShortcuts(); - settingChanged = true; + settingChanged = true; if (!m_hasDuplicate) { + } } } diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index b09c6d3ac..9ebc4b3e2 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -2294,16 +2294,16 @@ namespace hex::plugin::builtin { } }); - ShortcutManager::addShortcut(this, CTRL + Keys::G + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.goto_line", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::G + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.goto_line", [this] { m_openGotoLinePopUp = true; }); - ShortcutManager::addShortcut(this, CTRL + Keys::F + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.find", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::F + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.find", [this] { m_openFindReplacePopUp = true; m_replaceMode = false; }); - ShortcutManager::addShortcut(this, CTRL + Keys::H + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.replace", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::H + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.replace", [this] { if (m_focusedSubWindowName.contains(textEditorView)) { m_openFindReplacePopUp = true; m_replaceMode = true; @@ -2358,11 +2358,11 @@ namespace hex::plugin::builtin { hex::plugin::builtin::saveProjectAs(); }); - // ShortcutManager::addShortcut(this, CTRL + Keys::Insert + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.copy", [this] { + // ShortcutManager::addShortcut(this, CTRLCMD + Keys::Insert + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.copy", [this] { // m_textEditor.Copy(); // }); - ShortcutManager::addShortcut(this, CTRL + Keys::C + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.copy", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::C + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.copy", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->Copy(); }); @@ -2371,12 +2371,12 @@ namespace hex::plugin::builtin { // m_textEditor.Paste(); // }); - ShortcutManager::addShortcut(this, CTRL + Keys::V + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.paste", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::V + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.paste", [this] { if (m_focusedSubWindowName.contains(textEditorView)) m_textEditor.Paste(); }); - ShortcutManager::addShortcut(this, CTRL + Keys::X + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.cut", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::X + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.cut", [this] { if (m_focusedSubWindowName.contains(textEditorView)) m_textEditor.Cut(); }); @@ -2385,7 +2385,7 @@ namespace hex::plugin::builtin { // m_textEditor.Cut(); // }); - ShortcutManager::addShortcut(this, CTRL + Keys::Z + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.undo", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::Z + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.undo", [this] { if (m_focusedSubWindowName.contains(textEditorView)) m_textEditor.Undo(); }); @@ -2399,12 +2399,12 @@ namespace hex::plugin::builtin { m_textEditor.Delete(); }); - ShortcutManager::addShortcut(this, CTRL + Keys::Y + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.redo", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::Y + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.redo", [this] { if (m_focusedSubWindowName.contains(textEditorView)) m_textEditor.Redo(); }); - ShortcutManager::addShortcut(this, CTRL + Keys::A + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_all", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::A + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_all", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->SelectAll(); }); @@ -2414,7 +2414,7 @@ namespace hex::plugin::builtin { editor->MoveRight(1, true, false); }); - ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::Right + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_word_right", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + SHIFT + Keys::Right + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_word_right", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveRight(1, true, true); }); @@ -2424,7 +2424,7 @@ namespace hex::plugin::builtin { editor->MoveLeft(1, true, false); }); - ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::Left + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_word_left", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + SHIFT + Keys::Left + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_word_left", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveLeft(1, true, true); }); @@ -2449,12 +2449,12 @@ namespace hex::plugin::builtin { editor->MoveDown(editor->GetPageSize()-4, true); }); - ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::Home + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_top", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + SHIFT + Keys::Home + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_top", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveTop(true); }); - ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::End + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_bottom", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + SHIFT + Keys::End + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.select_bottom", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveBottom(true); }); @@ -2469,12 +2469,12 @@ namespace hex::plugin::builtin { editor->MoveEnd(true); }); - ShortcutManager::addShortcut(this, CTRL + Keys::Delete + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.delete_word_right", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::Delete + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.delete_word_right", [this] { if (m_focusedSubWindowName.contains(textEditorView)) m_textEditor.DeleteWordRight(); }); - ShortcutManager::addShortcut(this, CTRL + Keys::Backspace + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.delete_word_left", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::Backspace + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.delete_word_left", [this] { if (m_focusedSubWindowName.contains(textEditorView)) m_textEditor.DeleteWordLeft(); }); @@ -2489,7 +2489,7 @@ namespace hex::plugin::builtin { m_textEditor.SetOverwrite(!m_textEditor.IsOverwrite()); }); - ShortcutManager::addShortcut(this, CTRL + Keys::Right + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_word_right", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::Right + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_word_right", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveRight(1, false, true); }); @@ -2499,7 +2499,7 @@ namespace hex::plugin::builtin { editor->MoveRight(1, false, false); }); - ShortcutManager::addShortcut(this, CTRL + Keys::Left + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_word_left", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::Left + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_word_left", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveLeft(1, false, true); }); @@ -2529,12 +2529,12 @@ namespace hex::plugin::builtin { editor->MoveDown(editor->GetPageSize()-4, false); }); - ShortcutManager::addShortcut(this, CTRL + Keys::Home + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_top", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::Home + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_top", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveTop(false); }); - ShortcutManager::addShortcut(this, CTRL + Keys::End + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_bottom", [this] { + ShortcutManager::addShortcut(this, CTRLCMD + Keys::End + AllowWhileTyping, "hex.builtin.view.pattern_editor.shortcut.move_bottom", [this] { if (auto editor = getEditorFromFocusedWindow(); editor != nullptr) editor->MoveBottom(false); });