1
0
mirror of synced 2025-01-10 21:41:53 +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 {
public:
Shortcut() = default;
Shortcut(Keys key) : m_keys({ key }) { }
explicit Shortcut(std::set<Key> keys) : m_keys(std::move(keys)) { }
Shortcut(Keys key);
explicit Shortcut(std::set<Key> 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<Key>& getKeys() const { return m_keys; }
const std::set<Key>& getKeys() const;
private:
friend Shortcut operator+(const Key &lhs, const Key &rhs);
@ -225,12 +188,7 @@ namespace hex {
std::set<Key> 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<ShortcutEntry> getGlobalShortcuts();
[[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 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<Keys>(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();
}
}