ImHex/lib/libimhex/source/api/keybinding.cpp

129 lines
4.4 KiB
C++
Raw Normal View History

#include <hex/api/keybinding.hpp>
#include <imgui.h>
2023-11-17 13:46:21 +00:00
#include <hex/api/content_registry.hpp>
#include <hex/ui/view.hpp>
namespace hex {
namespace {
2023-11-17 13:46:21 +00:00
std::map<Shortcut, ShortcutManager::ShortcutEntry> s_globalShortcuts;
std::atomic<bool> s_paused;
std::optional<Shortcut> s_prevShortcut;
}
2023-11-17 13:46:21 +00:00
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::string &unlocalizedName, const std::function<void()> &callback) {
s_globalShortcuts.insert({ shortcut, { shortcut, unlocalizedName, callback } });
}
2023-11-17 13:46:21 +00:00
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const std::string &unlocalizedName, const std::function<void()> &callback) {
view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, unlocalizedName, callback } });
}
static Shortcut getShortcut(bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
Shortcut pressedShortcut;
if (ctrl)
pressedShortcut += CTRL;
if (alt)
pressedShortcut += ALT;
if (shift)
pressedShortcut += SHIFT;
if (super)
pressedShortcut += SUPER;
if (focused)
pressedShortcut += CurrentView;
pressedShortcut += static_cast<Keys>(keyCode);
return pressedShortcut;
}
static void processShortcut(const Shortcut &shortcut, const std::map<Shortcut, ShortcutManager::ShortcutEntry> &shortcuts) {
2023-11-17 13:46:21 +00:00
if (s_paused) return;
if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId))
return;
2023-11-17 13:46:21 +00:00
if (shortcuts.contains(shortcut + AllowWhileTyping)) {
shortcuts.at(shortcut + AllowWhileTyping).callback();
} else if (shortcuts.contains(shortcut)) {
if (!ImGui::GetIO().WantTextInput)
shortcuts.at(shortcut).callback();
}
}
void ShortcutManager::process(const std::unique_ptr<View> &currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, focused, keyCode);
2023-11-17 13:46:21 +00:00
if (keyCode != 0)
s_prevShortcut = Shortcut(pressedShortcut.getKeys());
2023-11-17 13:46:21 +00:00
processShortcut(pressedShortcut, currentView->m_shortcuts);
}
2023-11-17 13:46:21 +00:00
void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) {
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, false, keyCode);
if (keyCode != 0)
s_prevShortcut = Shortcut(pressedShortcut.getKeys());
2023-11-17 13:46:21 +00:00
processShortcut(pressedShortcut, s_globalShortcuts);
}
void ShortcutManager::clearShortcuts() {
s_globalShortcuts.clear();
}
2023-11-17 13:46:21 +00:00
void ShortcutManager::resumeShortcuts() {
s_paused = false;
}
void ShortcutManager::pauseShortcuts() {
s_paused = true;
s_prevShortcut.reset();
}
std::optional<Shortcut> ShortcutManager::getPreviousShortcut() {
return s_prevShortcut;
}
std::vector<ShortcutManager::ShortcutEntry> ShortcutManager::getGlobalShortcuts() {
std::vector<ShortcutManager::ShortcutEntry> result;
for (auto &[shortcut, entry] : s_globalShortcuts)
result.push_back(entry);
return result;
}
std::vector<ShortcutManager::ShortcutEntry> ShortcutManager::getViewShortcuts(View *view) {
std::vector<ShortcutManager::ShortcutEntry> result;
for (auto &[shortcut, entry] : view->m_shortcuts)
result.push_back(entry);
return result;
}
void ShortcutManager::updateShortcut(const Shortcut &oldShortcut, const Shortcut &newShortcut, View *view) {
if (oldShortcut == newShortcut)
return;
if (view != nullptr) {
if (view->m_shortcuts.contains(oldShortcut + CurrentView)) {
view->m_shortcuts[newShortcut + CurrentView] = view->m_shortcuts[oldShortcut + CurrentView];
view->m_shortcuts[newShortcut + CurrentView].shortcut = newShortcut + CurrentView;
view->m_shortcuts.erase(oldShortcut + CurrentView);
}
} else {
if (s_globalShortcuts.contains(oldShortcut)) {
s_globalShortcuts[newShortcut] = s_globalShortcuts[oldShortcut];
s_globalShortcuts[newShortcut].shortcut = newShortcut;
s_globalShortcuts.erase(oldShortcut);
}
}
}
}