sys: Moved to a better shortcut handling system

This commit is contained in:
WerWolv
2021-12-23 15:11:38 +01:00
parent 8db0305c83
commit 936d1d6072
16 changed files with 453 additions and 208 deletions

View File

@@ -0,0 +1,37 @@
#include <hex/api/keybinding.hpp>
#include <hex/helpers/shared_data.hpp>
#include <imgui.h>
#include <hex/views/view.hpp>
namespace hex {
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::function<void()> &callback) {
SharedData::globalShortcuts.insert({ shortcut, callback });
}
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const std::function<void()> &callback) {
view->m_shortcuts.insert({ shortcut, callback });
}
void ShortcutManager::process(View *currentView, 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;
pressedShortcut += static_cast<Keys>(keyCode);
if (focused && currentView->m_shortcuts.contains(pressedShortcut))
currentView->m_shortcuts[pressedShortcut]();
else if (SharedData::globalShortcuts.contains(pressedShortcut))
SharedData::globalShortcuts[pressedShortcut]();
}
}

View File

@@ -29,6 +29,8 @@ namespace hex {
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::footerItems;
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::toolbarItems;
std::map<Shortcut, std::function<void()>> SharedData::globalShortcuts;
std::mutex SharedData::tasksMutex;
std::list<Task*> SharedData::runningTasks;

View File

@@ -13,7 +13,6 @@ namespace hex {
View::View(std::string unlocalizedName) : m_unlocalizedViewName(unlocalizedName) { }
void View::drawMenu() { }
bool View::handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt) { return false; }
bool View::isAvailable() {
return ImHexApi::Provider::isValid() && ImHexApi::Provider::get()->isAvailable();