1
0
mirror of synced 2025-02-12 08:43:03 +01:00

sys: Fixed global shortcuts not working correctly

Fixes #606
This commit is contained in:
WerWolv 2022-07-30 22:01:49 +02:00
parent a097f162a7
commit 3d5568f65f
14 changed files with 63 additions and 30 deletions

View File

@ -102,7 +102,6 @@ namespace hex {
/* Default Events */ /* Default Events */
EVENT_DEF(EventFileLoaded, std::fs::path); EVENT_DEF(EventFileLoaded, std::fs::path);
EVENT_DEF(EventFileUnloaded);
EVENT_DEF(EventDataChanged); EVENT_DEF(EventDataChanged);
EVENT_DEF(EventHighlightingChanged); EVENT_DEF(EventHighlightingChanged);
EVENT_DEF(EventWindowClosing, GLFWwindow *); EVENT_DEF(EventWindowClosing, GLFWwindow *);
@ -114,6 +113,7 @@ namespace hex {
EVENT_DEF(EventOSThemeChanged); EVENT_DEF(EventOSThemeChanged);
EVENT_DEF(EventProviderCreated, prv::Provider *); EVENT_DEF(EventProviderCreated, prv::Provider *);
EVENT_DEF(EventProviderChanged, prv::Provider *, prv::Provider *); EVENT_DEF(EventProviderChanged, prv::Provider *, prv::Provider *);
EVENT_DEF(EventProviderDeleted, prv::Provider *);
EVENT_DEF(EventFrameBegin); EVENT_DEF(EventFrameBegin);
EVENT_DEF(EventFrameEnd); EVENT_DEF(EventFrameEnd);
EVENT_DEF(EventWindowInitialized); EVENT_DEF(EventWindowInitialized);

View File

@ -188,7 +188,9 @@ namespace hex {
public: public:
static void addGlobalShortcut(const Shortcut &shortcut, const std::function<void()> &callback); static void addGlobalShortcut(const Shortcut &shortcut, const std::function<void()> &callback);
static void addShortcut(View *view, const Shortcut &shortcut, const std::function<void()> &callback); static void addShortcut(View *view, const Shortcut &shortcut, const std::function<void()> &callback);
static void process(View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode); static void process(View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode);
static void processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode);
static void clearShortcuts(); static void clearShortcuts();

View File

@ -251,10 +251,17 @@ namespace hex {
} }
void remove(prv::Provider *provider) { void remove(prv::Provider *provider) {
if (provider == nullptr)
return;
if (Task::getRunningTaskCount() > 0) if (Task::getRunningTaskCount() > 0)
return; return;
auto it = std::find(s_providers.begin(), s_providers.end(), provider); auto it = std::find(s_providers.begin(), s_providers.end(), provider);
if (it == s_providers.end())
return;
EventManager::post<EventProviderDeleted>(provider);
s_providers.erase(it); s_providers.erase(it);

View File

@ -15,7 +15,7 @@ namespace hex {
view->m_shortcuts.insert({ shortcut, callback }); view->m_shortcuts.insert({ shortcut, callback });
} }
void ShortcutManager::process(View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) { static Shortcut getShortcut(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) {
Shortcut pressedShortcut; Shortcut pressedShortcut;
if (ctrl) if (ctrl)
@ -29,9 +29,20 @@ namespace hex {
pressedShortcut += static_cast<Keys>(keyCode); pressedShortcut += static_cast<Keys>(keyCode);
return pressedShortcut;
}
void ShortcutManager::process(View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, keyCode);
if (focused && currentView->m_shortcuts.contains(pressedShortcut)) if (focused && currentView->m_shortcuts.contains(pressedShortcut))
currentView->m_shortcuts[pressedShortcut](); currentView->m_shortcuts[pressedShortcut]();
else if (ShortcutManager::s_globalShortcuts.contains(pressedShortcut)) }
void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) {
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, keyCode);
if (ShortcutManager::s_globalShortcuts.contains(pressedShortcut))
ShortcutManager::s_globalShortcuts[pressedShortcut](); ShortcutManager::s_globalShortcuts[pressedShortcut]();
} }

View File

@ -87,7 +87,7 @@ namespace hex {
EventManager::post<EventWindowClosing>(this->m_window); EventManager::post<EventWindowClosing>(this->m_window);
}); });
EventManager::subscribe<EventFileUnloaded>(this, [] { EventManager::subscribe<EventProviderDeleted>(this, [](const auto*) {
EventManager::post<RequestChangeWindowTitle>(""); EventManager::post<RequestChangeWindowTitle>("");
}); });
@ -163,7 +163,7 @@ namespace hex {
this->exitImGui(); this->exitImGui();
this->exitGLFW(); this->exitGLFW();
EventManager::unsubscribe<EventFileUnloaded>(this); EventManager::unsubscribe<EventProviderDeleted>(this);
EventManager::unsubscribe<RequestCloseImHex>(this); EventManager::unsubscribe<RequestCloseImHex>(this);
EventManager::unsubscribe<RequestChangeWindowTitle>(this); EventManager::unsubscribe<RequestChangeWindowTitle>(this);
EventManager::unsubscribe<EventAbnormalTermination>(this); EventManager::unsubscribe<EventAbnormalTermination>(this);
@ -416,6 +416,7 @@ namespace hex {
calls.clear(); calls.clear();
} }
auto &io = ImGui::GetIO();
for (auto &[name, view] : ContentRegistry::Views::getEntries()) { for (auto &[name, view] : ContentRegistry::Views::getEntries()) {
ImGui::GetCurrentContext()->NextWindowData.ClearFlags(); ImGui::GetCurrentContext()->NextWindowData.ClearFlags();
@ -442,13 +443,16 @@ namespace hex {
ImGui::End(); ImGui::End();
} }
auto &io = ImGui::GetIO();
for (const auto &key : this->m_pressedKeys) { for (const auto &key : this->m_pressedKeys) {
ShortcutManager::process(view, io.KeyCtrl, io.KeyAlt, io.KeyShift, io.KeySuper, focused, key); ShortcutManager::process(view, io.KeyCtrl, io.KeyAlt, io.KeyShift, io.KeySuper, focused, key);
} }
} }
} }
for (const auto &key : this->m_pressedKeys) {
ShortcutManager::processGlobals(io.KeyCtrl, io.KeyAlt, io.KeyShift, io.KeySuper, key);
}
this->m_pressedKeys.clear(); this->m_pressedKeys.clear();
} }

View File

@ -23,6 +23,7 @@ add_library(${PROJECT_NAME} SHARED
source/content/data_visualizers.cpp source/content/data_visualizers.cpp
source/content/events.cpp source/content/events.cpp
source/content/hashes.cpp source/content/hashes.cpp
source/content/shortcuts.cpp
source/content/providers/file_provider.cpp source/content/providers/file_provider.cpp
source/content/providers/gdb_provider.cpp source/content/providers/gdb_provider.cpp

View File

@ -46,7 +46,6 @@ namespace hex::plugin::builtin {
bool taskRunning = Task::getRunningTaskCount() > 0; bool taskRunning = Task::getRunningTaskCount() > 0;
if (ImGui::MenuItem("hex.builtin.menu.file.close"_lang, "CTRL + W", false, providerValid && !taskRunning)) { if (ImGui::MenuItem("hex.builtin.menu.file.close"_lang, "CTRL + W", false, providerValid && !taskRunning)) {
EventManager::post<EventFileUnloaded>();
ImHexApi::Provider::remove(ImHexApi::Provider::get()); ImHexApi::Provider::remove(ImHexApi::Provider::get());
} }

View File

@ -0,0 +1,20 @@
#include <hex/api/keybinding.hpp>
#include <hex/api/event.hpp>
namespace hex::plugin::builtin {
void registerShortcuts() {
// Open file
ShortcutManager::addGlobalShortcut(CTRL + Keys::O, [] {
fs::openFileBrowser(fs::DialogMode::Open, {}, [](const auto &path) {
EventManager::post<RequestOpenFile>(path);
});
});
// Close file
ShortcutManager::addGlobalShortcut(CTRL + Keys::W, [] {
ImHexApi::Provider::remove(ImHexApi::Provider::get());
});
}
}

View File

@ -37,7 +37,7 @@ namespace hex::plugin::builtin {
ProjectFile::setBookmarks(this->m_bookmarks); ProjectFile::setBookmarks(this->m_bookmarks);
}); });
EventManager::subscribe<EventFileUnloaded>(this, [this] { EventManager::subscribe<EventProviderDeleted>(this, [this](const auto*) {
this->m_bookmarks.clear(); this->m_bookmarks.clear();
}); });
@ -115,7 +115,7 @@ namespace hex::plugin::builtin {
EventManager::unsubscribe<RequestAddBookmark>(this); EventManager::unsubscribe<RequestAddBookmark>(this);
EventManager::unsubscribe<EventProjectFileLoad>(this); EventManager::unsubscribe<EventProjectFileLoad>(this);
EventManager::unsubscribe<EventProjectFileStore>(this); EventManager::unsubscribe<EventProjectFileStore>(this);
EventManager::unsubscribe<EventFileUnloaded>(this); EventManager::unsubscribe<EventProviderDeleted>(this);
this->m_bookmarks.clear(); this->m_bookmarks.clear();
} }

View File

@ -22,7 +22,7 @@ namespace hex::plugin::builtin {
} }
}); });
EventManager::subscribe<EventFileUnloaded>(this, [this] { EventManager::subscribe<EventProviderDeleted>(this, [this](const auto*) {
this->m_disassembly.clear(); this->m_disassembly.clear();
}); });
} }
@ -30,7 +30,7 @@ namespace hex::plugin::builtin {
ViewDisassembler::~ViewDisassembler() { ViewDisassembler::~ViewDisassembler() {
EventManager::unsubscribe<EventDataChanged>(this); EventManager::unsubscribe<EventDataChanged>(this);
EventManager::unsubscribe<EventRegionSelected>(this); EventManager::unsubscribe<EventRegionSelected>(this);
EventManager::unsubscribe<EventFileUnloaded>(this); EventManager::unsubscribe<EventProviderDeleted>(this);
} }
void ViewDisassembler::disassemble() { void ViewDisassembler::disassemble() {

View File

@ -1243,19 +1243,6 @@ namespace hex::plugin::builtin {
pasteBytes(selection); pasteBytes(selection);
}); });
// Open file
ShortcutManager::addGlobalShortcut(CTRL + Keys::O, [] {
fs::openFileBrowser(fs::DialogMode::Open, {}, [](const auto &path) {
EventManager::post<RequestOpenFile>(path);
});
});
// Close file
ShortcutManager::addGlobalShortcut(CTRL + Keys::W, [] {
EventManager::post<EventFileUnloaded>();
ImHexApi::Provider::remove(ImHexApi::Provider::get());
});
// Undo / Redo // Undo / Redo
ShortcutManager::addShortcut(this, CTRL + Keys::Z, [] { ShortcutManager::addShortcut(this, CTRL + Keys::Z, [] {
if (ImHexApi::Provider::isValid()) if (ImHexApi::Provider::isValid())

View File

@ -42,7 +42,7 @@ namespace hex::plugin::builtin {
this->m_entropyHandlePosition = region.address / this->m_blockSize; this->m_entropyHandlePosition = region.address / this->m_blockSize;
}); });
EventManager::subscribe<EventFileUnloaded>(this, [this] { EventManager::subscribe<EventProviderDeleted>(this, [this](const auto*) {
this->m_dataValid = false; this->m_dataValid = false;
}); });
@ -61,7 +61,7 @@ namespace hex::plugin::builtin {
ViewInformation::~ViewInformation() { ViewInformation::~ViewInformation() {
EventManager::unsubscribe<EventDataChanged>(this); EventManager::unsubscribe<EventDataChanged>(this);
EventManager::unsubscribe<EventRegionSelected>(this); EventManager::unsubscribe<EventRegionSelected>(this);
EventManager::unsubscribe<EventFileUnloaded>(this); EventManager::unsubscribe<EventProviderDeleted>(this);
} }
static float calculateEntropy(std::array<ImU64, 256> &valueCounts, size_t blockSize) { static float calculateEntropy(std::array<ImU64, 256> &valueCounts, size_t blockSize) {

View File

@ -150,8 +150,8 @@ namespace hex::plugin::builtin {
} }
}); });
EventManager::subscribe<EventFileUnloaded>(this, [] { EventManager::subscribe<EventProviderDeleted>(this, [](auto *provider) {
ImHexApi::Provider::get()->getPatternLanguageRuntime().abort(); provider->getPatternLanguageRuntime().abort();
}); });
EventManager::subscribe<EventProviderChanged>(this, [this](prv::Provider *oldProvider, prv::Provider *newProvider) { EventManager::subscribe<EventProviderChanged>(this, [this](prv::Provider *oldProvider, prv::Provider *newProvider) {
@ -283,7 +283,7 @@ namespace hex::plugin::builtin {
EventManager::unsubscribe<EventProjectFileLoad>(this); EventManager::unsubscribe<EventProjectFileLoad>(this);
EventManager::unsubscribe<RequestSetPatternLanguageCode>(this); EventManager::unsubscribe<RequestSetPatternLanguageCode>(this);
EventManager::unsubscribe<EventFileLoaded>(this); EventManager::unsubscribe<EventFileLoaded>(this);
EventManager::unsubscribe<EventFileUnloaded>(this); EventManager::unsubscribe<EventProviderDeleted>(this);
EventManager::unsubscribe<RequestChangeTheme>(this); EventManager::unsubscribe<RequestChangeTheme>(this);
EventManager::unsubscribe<EventProviderChanged>(this); EventManager::unsubscribe<EventProviderChanged>(this);
} }

View File

@ -19,6 +19,7 @@ namespace hex::plugin::builtin {
void registerMainMenuEntries(); void registerMainMenuEntries();
void createWelcomeScreen(); void createWelcomeScreen();
void registerViews(); void registerViews();
void registerShortcuts();
void addFooterItems(); void addFooterItems();
void addToolbarItems(); void addToolbarItems();
@ -62,6 +63,7 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {
registerDataFormatters(); registerDataFormatters();
createWelcomeScreen(); createWelcomeScreen();
registerViews(); registerViews();
registerShortcuts();
addFooterItems(); addFooterItems();
addToolbarItems(); addToolbarItems();