1
0
mirror of synced 2024-12-21 20:15:55 +01:00
ImHex/lib/libimhex/source/api/shortcut_manager.cpp

148 lines
4.9 KiB
C++
Raw Normal View History

#include <hex/api/shortcut_manager.hpp>
#include <imgui.h>
2023-11-17 14:46:21 +01:00
#include <hex/api/content_registry.hpp>
2024-01-30 15:50:00 +01:00
#include <hex/helpers/auto_reset.hpp>
#include <hex/ui/view.hpp>
namespace hex {
namespace {
AutoReset<std::map<Shortcut, ShortcutManager::ShortcutEntry>> s_globalShortcuts;
2023-11-17 14:46:21 +01:00
std::atomic<bool> s_paused;
std::optional<Shortcut> s_prevShortcut;
}
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
s_globalShortcuts->insert({ shortcut, { shortcut, unlocalizedName, callback } });
}
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
2023-11-17 14:46:21 +01:00
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 14:46:21 +01:00
if (s_paused) return;
if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId))
return;
2023-11-17 14:46:21 +01: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 View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
const Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, focused, keyCode);
2023-11-17 14:46:21 +01:00
if (keyCode != 0)
s_prevShortcut = Shortcut(pressedShortcut.getKeys());
2023-11-17 14:46:21 +01:00
processShortcut(pressedShortcut, currentView->m_shortcuts);
}
2023-11-17 14:46:21 +01:00
void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) {
const Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, false, keyCode);
if (keyCode != 0)
s_prevShortcut = Shortcut(pressedShortcut.getKeys());
2023-11-17 14:46:21 +01:00
processShortcut(pressedShortcut, s_globalShortcuts);
}
void ShortcutManager::clearShortcuts() {
s_globalShortcuts->clear();
}
2023-11-17 14:46:21 +01: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<ShortcutEntry> result;
2023-11-17 14:46:21 +01:00
for (auto &[shortcut, entry] : *s_globalShortcuts)
2023-11-17 14:46:21 +01:00
result.push_back(entry);
return result;
}
2023-12-27 16:33:49 +01:00
std::vector<ShortcutManager::ShortcutEntry> ShortcutManager::getViewShortcuts(const View *view) {
std::vector<ShortcutEntry> result;
2023-11-17 14:46:21 +01:00
result.reserve(view->m_shortcuts.size());
2023-11-17 14:46:21 +01:00
for (auto &[shortcut, entry] : view->m_shortcuts)
result.push_back(entry);
return result;
}
static bool updateShortcutImpl(const Shortcut &oldShortcut, const Shortcut &newShortcut, std::map<Shortcut, ShortcutManager::ShortcutEntry> &shortcuts) {
if (shortcuts.contains(oldShortcut)) {
if (shortcuts.contains(newShortcut))
return false;
shortcuts[newShortcut] = shortcuts[oldShortcut];
shortcuts[newShortcut].shortcut = newShortcut;
shortcuts.erase(oldShortcut);
}
return true;
}
bool ShortcutManager::updateShortcut(const Shortcut &oldShortcut, const Shortcut &newShortcut, View *view) {
2023-11-17 14:46:21 +01:00
if (oldShortcut == newShortcut)
return true;
2023-11-17 14:46:21 +01:00
bool result;
2023-11-17 14:46:21 +01:00
if (view != nullptr) {
result = updateShortcutImpl(oldShortcut + CurrentView, newShortcut + CurrentView , view->m_shortcuts);
2023-11-17 14:46:21 +01:00
} else {
result = updateShortcutImpl(oldShortcut, newShortcut, s_globalShortcuts);
}
if (result) {
for (auto &[priority, menuItem] : ContentRegistry::Interface::impl::getMenuItems()) {
if (menuItem.view == view && *menuItem.shortcut == oldShortcut) {
*menuItem.shortcut = newShortcut;
break;
}
2023-11-17 14:46:21 +01:00
}
}
return result;
2023-11-17 14:46:21 +01:00
}
}