sys: Replace the terrible event manager with a much better one

This commit is contained in:
WerWolv
2021-03-27 11:36:36 +01:00
parent 688ca01b1b
commit d805d976a6
27 changed files with 206 additions and 227 deletions

View File

@@ -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() {

View File

@@ -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;
}

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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;