mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 13:37:42 -05:00
sys: Replace the terrible event manager with a much better one
This commit is contained in:
@@ -133,21 +133,6 @@ namespace hex {
|
||||
}
|
||||
|
||||
|
||||
/* Events */
|
||||
|
||||
auto ContentRegistry::Events::get(std::string_view name) {
|
||||
auto &customEvents = SharedData::customEvents;
|
||||
auto &lastId = SharedData::customEventsLastId;
|
||||
|
||||
if (!customEvents.contains(name.data())) {
|
||||
customEvents[name.data()] = static_cast<hex::Events>(lastId);
|
||||
lastId++;
|
||||
}
|
||||
|
||||
return customEvents[name.data()];
|
||||
}
|
||||
|
||||
|
||||
/* Command Palette Commands */
|
||||
|
||||
void ContentRegistry::CommandPaletteCommands::add(ContentRegistry::CommandPaletteCommands::Type type, std::string_view command, std::string_view unlocalizedDescription, const std::function<std::string(std::string)> &displayCallback, const std::function<void(std::string)> &executeCallback) {
|
||||
@@ -225,7 +210,7 @@ namespace hex {
|
||||
void ContentRegistry::Language::addLocalizations(std::string_view languageCode, const LanguageDefinition &definition) {
|
||||
getLanguageDefinitions()[languageCode.data()].push_back(definition);
|
||||
|
||||
EventManager::post(hex::Events::SettingsChanged, {});
|
||||
EventManager::post<EventSettingsChanged>();
|
||||
}
|
||||
|
||||
std::map<std::string, std::string>& ContentRegistry::Language::getLanguages() {
|
||||
|
||||
@@ -1,30 +1,8 @@
|
||||
#include <hex/api/event.hpp>
|
||||
|
||||
#include <hex/helpers/shared_data.hpp>
|
||||
|
||||
namespace hex {
|
||||
|
||||
std::vector<std::any> EventManager::post(Events eventType, const std::any &userData) {
|
||||
std::vector<std::any> results;
|
||||
for (auto &handler : SharedData::eventHandlers)
|
||||
if (eventType == handler.eventType)
|
||||
results.push_back(handler.callback(userData));
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
void EventManager::subscribe(Events eventType, void *owner, std::function<std::any(const std::any&)> callback) {
|
||||
for (auto &handler : SharedData::eventHandlers)
|
||||
if (eventType == handler.eventType && owner == handler.owner)
|
||||
return;
|
||||
|
||||
SharedData::eventHandlers.push_back(EventHandler { owner, eventType, callback });
|
||||
}
|
||||
|
||||
void EventManager::unsubscribe(Events eventType, void *sender) {
|
||||
std::erase_if(SharedData::eventHandlers, [&eventType, &sender](EventHandler handler) {
|
||||
return eventType == handler.eventType && sender == handler.owner;
|
||||
});
|
||||
}
|
||||
std::map<void*, EventManager::EventList::iterator> EventManager::s_tokenStore;
|
||||
EventManager::EventList EventManager::s_events;
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ namespace hex {
|
||||
|
||||
entry.color = color;
|
||||
|
||||
EventManager::post(Events::AddBookmark, entry);
|
||||
EventManager::post<RequestAddBookmark>(entry);
|
||||
}
|
||||
|
||||
void ImHexApi::Bookmarks::add(u64 addr, size_t size, std::string_view name, std::string_view comment, u32 color) {
|
||||
|
||||
@@ -2,13 +2,10 @@
|
||||
|
||||
namespace hex {
|
||||
|
||||
std::vector<EventHandler> SharedData::eventHandlers;
|
||||
std::vector<std::function<void()>> SharedData::deferredCalls;
|
||||
prv::Provider *SharedData::currentProvider;
|
||||
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>> SharedData::settingsEntries;
|
||||
nlohmann::json SharedData::settingsJson;
|
||||
std::map<std::string, Events> SharedData::customEvents;
|
||||
u32 SharedData::customEventsLastId;
|
||||
std::vector<ContentRegistry::CommandPaletteCommands::Entry> SharedData::commandPaletteCommands;
|
||||
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function> SharedData::patternLanguageFunctions;
|
||||
std::vector<View*> SharedData::views;
|
||||
|
||||
@@ -24,10 +24,6 @@ namespace hex {
|
||||
return SharedData::deferredCalls;
|
||||
}
|
||||
|
||||
std::vector<std::any> View::postEvent(Events eventType, const std::any &userData) {
|
||||
return EventManager::post(eventType, userData);
|
||||
}
|
||||
|
||||
void View::openFileBrowser(std::string_view title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::string)> &callback) {
|
||||
NFD::Init();
|
||||
|
||||
@@ -93,18 +89,6 @@ namespace hex {
|
||||
return this->m_unlocalizedViewName;
|
||||
}
|
||||
|
||||
void View::subscribeEvent(Events eventType, const std::function<std::any(const std::any&)> &callback) {
|
||||
EventManager::subscribe(eventType, this, callback);
|
||||
}
|
||||
|
||||
void View::subscribeEvent(Events eventType, const std::function<void(const std::any&)> &callback) {
|
||||
EventManager::subscribe(eventType, this, [callback](auto userData) -> std::any { callback(userData); return { }; });
|
||||
}
|
||||
|
||||
void View::unsubscribeEvent(Events eventType) {
|
||||
EventManager::unsubscribe(eventType, this);
|
||||
}
|
||||
|
||||
void View::discardNavigationRequests() {
|
||||
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows))
|
||||
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard;
|
||||
|
||||
Reference in New Issue
Block a user