From 96727d6e63ca927f3c7b68d4baa4fe672a4dcd0b Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Wed, 2 Apr 2025 20:49:49 +0530 Subject: Add events system for lua runtime to dispatch and register events --- src/WindowActionRouter.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/WindowActionRouter.cpp') 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 #include +#include +#include #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> event_map; + { + const std::lock_guard 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 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, -- cgit v1.3.1