fix: Pattern editor shortcuts being set to use CTRL on macOS

This commit is contained in:
WerWolv
2024-12-15 22:39:24 +01:00
parent 89090b25e3
commit a07c79efcb
4 changed files with 53 additions and 47 deletions

View File

@@ -138,6 +138,7 @@ namespace hex {
constexpr Key() = default;
constexpr Key(Keys key) : m_key(static_cast<u32>(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<Key>& getKeys() const { return m_keys; }

View File

@@ -181,22 +181,26 @@ namespace hex {
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::vector<UnlocalizedString> &unlocalizedName, const std::function<void()> &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<void()> &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<UnlocalizedString> &unlocalizedName, const std::function<void()> &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<void()> &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();
}
}
}