From 313e59d7f981068a2ebf482d9797b72807fde949 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 16 Dec 2024 20:14:26 +0100 Subject: [PATCH] fix: Shortcuts applying to multiple views at once --- .../include/hex/api/shortcut_manager.hpp | 66 ++++--------------- lib/libimhex/source/api/shortcut_manager.cpp | 57 +++++++++++++--- 2 files changed, 61 insertions(+), 62 deletions(-) diff --git a/lib/libimhex/include/hex/api/shortcut_manager.hpp b/lib/libimhex/include/hex/api/shortcut_manager.hpp index e1120a02b..940170646 100644 --- a/lib/libimhex/include/hex/api/shortcut_manager.hpp +++ b/lib/libimhex/include/hex/api/shortcut_manager.hpp @@ -163,61 +163,24 @@ namespace hex { class Shortcut { public: Shortcut() = default; - Shortcut(Keys key) : m_keys({ key }) { } - explicit Shortcut(std::set keys) : m_keys(std::move(keys)) { } + Shortcut(Keys key); + explicit Shortcut(std::set keys); Shortcut(const Shortcut &other) = default; Shortcut(Shortcut &&) noexcept = default; - Shortcut& operator=(const Shortcut &other) = default; - - Shortcut& operator=(Shortcut &&) noexcept = default; - constexpr static auto None = Keys(0); - Shortcut operator+(const Key &other) const { - Shortcut result = *this; - result.m_keys.insert(other); + Shortcut& operator=(const Shortcut &other) = default; + Shortcut& operator=(Shortcut &&) noexcept = default; - return result; - } - - Shortcut &operator+=(const Key &other) { - m_keys.insert(other); - - return *this; - } - - bool operator<(const Shortcut &other) const { - return m_keys < other.m_keys; - } - - bool operator==(const Shortcut &other) const { - return m_keys == other.m_keys; - } - - 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; - } - - bool isLocal() const { - return m_keys.contains(CurrentView); - } - - bool allowWhileTyping() const { - return m_keys.contains(AllowWhileTyping); - } + Shortcut operator+(const Key &other) const; + Shortcut &operator+=(const Key &other); + bool operator<(const Shortcut &other) const; + bool operator==(const Shortcut &other) const; + bool isLocal() const; std::string toString() const; - - const std::set& getKeys() const { return m_keys; } + const std::set& getKeys() const; private: friend Shortcut operator+(const Key &lhs, const Key &rhs); @@ -225,12 +188,7 @@ namespace hex { std::set m_keys; }; - inline Shortcut operator+(const Key &lhs, const Key &rhs) { - Shortcut result; - result.m_keys = { lhs, rhs }; - - return result; - } + Shortcut operator+(const Key &lhs, const Key &rhs); /** * @brief The ShortcutManager handles global and view-specific shortcuts. @@ -300,7 +258,7 @@ namespace hex { [[nodiscard]] static std::vector getGlobalShortcuts(); [[nodiscard]] static std::vector getViewShortcuts(const View *view); - [[nodiscard]] static bool updateShortcut(const Shortcut &oldShortcut, const Shortcut &newShortcut, View *view = nullptr); + [[nodiscard]] static bool updateShortcut(Shortcut oldShortcut, Shortcut newShortcut, View *view = nullptr); }; } \ No newline at end of file diff --git a/lib/libimhex/source/api/shortcut_manager.cpp b/lib/libimhex/source/api/shortcut_manager.cpp index 4574bd26b..6aa644fa1 100644 --- a/lib/libimhex/source/api/shortcut_manager.cpp +++ b/lib/libimhex/source/api/shortcut_manager.cpp @@ -15,6 +15,47 @@ namespace hex { } + Shortcut operator+(const Key &lhs, const Key &rhs) { + Shortcut result; + result.m_keys = { lhs, rhs }; + + return result; + } + + Shortcut::Shortcut(Keys key) : m_keys({ key }) { + + } + Shortcut::Shortcut(std::set keys) : m_keys(std::move(keys)) { + + } + + Shortcut Shortcut::operator+(const Key &other) const { + Shortcut result = *this; + result.m_keys.insert(other); + + return result; + } + + Shortcut& Shortcut::operator+=(const Key &other) { + m_keys.insert(other); + + return *this; + } + + bool Shortcut::operator<(const Shortcut &other) const { + return m_keys < other.m_keys; + } + + bool Shortcut::operator==(const Shortcut &other) const { + return m_keys == other.m_keys; + } + + bool Shortcut::isLocal() const { + return m_keys.contains(CurrentView); + } + + const std::set& Shortcut::getKeys() const { return m_keys; } + std::string Shortcut::toString() const { std::string result; @@ -169,11 +210,11 @@ namespace hex { continue; } - result += " + "; + result += Concatination; } - if (result.ends_with(" + ")) - result = result.substr(0, result.size() - 3); + if (result.ends_with(Concatination)) + result = result.substr(0, result.size() - strlen(Concatination)); return result; } @@ -216,6 +257,8 @@ namespace hex { pressedShortcut += SUPER; if (focused) pressedShortcut += CurrentView; + if (ImGui::GetIO().WantTextInput) + pressedShortcut += AllowWhileTyping; pressedShortcut += static_cast(keyCode); @@ -228,11 +271,9 @@ namespace hex { if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) return; - for (const auto &[potentialShortcut, entry] : shortcuts) { - if (potentialShortcut.match(shortcut)) { - if (!ImGui::GetIO().WantTextInput || potentialShortcut.allowWhileTyping()) - entry.callback(); - } + if (auto it = shortcuts.find(shortcut); it != shortcuts.end()) { + const auto &[foundShortcut, entry] = *it; + entry.callback(); } }