1
0
mirror of synced 2025-01-11 05:42:15 +01:00

fix: Shortcuts applying to multiple views at once

This commit is contained in:
WerWolv 2024-12-16 20:14:26 +01:00
parent e34e0e62f2
commit 313e59d7f9
2 changed files with 61 additions and 62 deletions

View File

@ -163,61 +163,24 @@ namespace hex {
class Shortcut { class Shortcut {
public: public:
Shortcut() = default; Shortcut() = default;
Shortcut(Keys key) : m_keys({ key }) { } Shortcut(Keys key);
explicit Shortcut(std::set<Key> keys) : m_keys(std::move(keys)) { } explicit Shortcut(std::set<Key> keys);
Shortcut(const Shortcut &other) = default; Shortcut(const Shortcut &other) = default;
Shortcut(Shortcut &&) noexcept = default; Shortcut(Shortcut &&) noexcept = default;
Shortcut& operator=(const Shortcut &other) = default;
Shortcut& operator=(Shortcut &&) noexcept = default;
constexpr static auto None = Keys(0); constexpr static auto None = Keys(0);
Shortcut operator+(const Key &other) const { Shortcut& operator=(const Shortcut &other) = default;
Shortcut result = *this; Shortcut& operator=(Shortcut &&) noexcept = default;
result.m_keys.insert(other);
return result; Shortcut operator+(const Key &other) const;
} Shortcut &operator+=(const Key &other);
bool operator<(const Shortcut &other) const;
Shortcut &operator+=(const Key &other) { bool operator==(const Shortcut &other) const;
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);
}
bool isLocal() const;
std::string toString() const; std::string toString() const;
const std::set<Key>& getKeys() const;
const std::set<Key>& getKeys() const { return m_keys; }
private: private:
friend Shortcut operator+(const Key &lhs, const Key &rhs); friend Shortcut operator+(const Key &lhs, const Key &rhs);
@ -225,12 +188,7 @@ namespace hex {
std::set<Key> m_keys; std::set<Key> m_keys;
}; };
inline Shortcut operator+(const Key &lhs, const Key &rhs) { Shortcut operator+(const Key &lhs, const Key &rhs);
Shortcut result;
result.m_keys = { lhs, rhs };
return result;
}
/** /**
* @brief The ShortcutManager handles global and view-specific shortcuts. * @brief The ShortcutManager handles global and view-specific shortcuts.
@ -300,7 +258,7 @@ namespace hex {
[[nodiscard]] static std::vector<ShortcutEntry> getGlobalShortcuts(); [[nodiscard]] static std::vector<ShortcutEntry> getGlobalShortcuts();
[[nodiscard]] static std::vector<ShortcutEntry> getViewShortcuts(const View *view); [[nodiscard]] static std::vector<ShortcutEntry> 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);
}; };
} }

View File

@ -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<Key> 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<Key>& Shortcut::getKeys() const { return m_keys; }
std::string Shortcut::toString() const { std::string Shortcut::toString() const {
std::string result; std::string result;
@ -169,11 +210,11 @@ namespace hex {
continue; continue;
} }
result += " + "; result += Concatination;
} }
if (result.ends_with(" + ")) if (result.ends_with(Concatination))
result = result.substr(0, result.size() - 3); result = result.substr(0, result.size() - strlen(Concatination));
return result; return result;
} }
@ -216,6 +257,8 @@ namespace hex {
pressedShortcut += SUPER; pressedShortcut += SUPER;
if (focused) if (focused)
pressedShortcut += CurrentView; pressedShortcut += CurrentView;
if (ImGui::GetIO().WantTextInput)
pressedShortcut += AllowWhileTyping;
pressedShortcut += static_cast<Keys>(keyCode); pressedShortcut += static_cast<Keys>(keyCode);
@ -228,11 +271,9 @@ namespace hex {
if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId))
return; return;
for (const auto &[potentialShortcut, entry] : shortcuts) { if (auto it = shortcuts.find(shortcut); it != shortcuts.end()) {
if (potentialShortcut.match(shortcut)) { const auto &[foundShortcut, entry] = *it;
if (!ImGui::GetIO().WantTextInput || potentialShortcut.allowWhileTyping()) entry.callback();
entry.callback();
}
} }
} }