diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-04-02 20:49:49 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-04-02 20:49:49 +0530 |
| commit | 96727d6e63ca927f3c7b68d4baa4fe672a4dcd0b (patch) | |
| tree | 2e5b99bb3324a35a0bd9ecdaa2caefcf9260767a /src/WindowActionRouter.cpp | |
| parent | 4f945367ebc8e34263acbfca0416e3f75653924e (diff) | |
| download | null-browser-96727d6e63ca927f3c7b68d4baa4fe672a4dcd0b.tar.gz null-browser-96727d6e63ca927f3c7b68d4baa4fe672a4dcd0b.zip | |
Add events system for lua runtime to dispatch and register events
Diffstat (limited to 'src/WindowActionRouter.cpp')
| -rw-r--r-- | src/WindowActionRouter.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp index 1c5164e..89f92c7 100644 --- a/src/WindowActionRouter.cpp +++ b/src/WindowActionRouter.cpp @@ -1,12 +1,49 @@ #include <QWidget> #include <QtCore> +#include <string> +#include <unordered_map> #include "LuaRuntime.hpp" #include "WindowActionRouter.hpp" #include "keymap/KeymapEvaluator.hpp" +#include "widgets/BrowserWindow.hpp" #include "widgets/WebViewStack.hpp" +void WindowActionRouter::dispatch_event(BrowserEvent &event) { + auto &runtime = LuaRuntime::instance(); + runtime.queue_task([this, &event]() { + std::unordered_map<std::string, std::vector<EventHandlerRequest>> event_map; + { + const std::lock_guard<std::mutex> lock(events_mutex); + event_map = events; + } + + if (!event_map.contains(event.kind)) + return; + + for (auto &event_handler : event_map.at(event.kind)) { + // TODO: Pattern filter + event_handler.handler(event); + } + }); +} + +void WindowActionRouter::register_event(const EventHandlerRequest &event) { + const std::lock_guard<std::mutex> lock(events_mutex); + for (const auto &event_name : event.event_names) { + if (!events.contains(event_name)) + events.insert({event_name, {}}); + events.at(event_name).emplace_back(event); + } +} + void WindowActionRouter::initialize() { + UrlChangedEvent url_changed_event("https://googa.com", 2, 1); + WindowActionRouter::instance().dispatch_event(url_changed_event); + + UrlChangedEvent url_changed_event_1("https://duckduckgo.com", 5, 1); + WindowActionRouter::instance().dispatch_event(url_changed_event_1); + auto &runtime = LuaRuntime::instance(); connect(&runtime, &LuaRuntime::keymap_added, this, |
