diff options
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/EventQueue.cpp | 31 | ||||
| -rw-r--r-- | src/EventQueue.hpp | 19 | ||||
| -rw-r--r-- | src/WindowActionRouter.cpp | 30 | ||||
| -rw-r--r-- | src/WindowActionRouter.hpp | 13 | ||||
| -rw-r--r-- | src/main.cpp | 7 | ||||
| -rwxr-xr-x | templates/class.sh | 12 |
7 files changed, 63 insertions, 52 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e1ceb3..ff74ded 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,8 @@ set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib") set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) # TODO: Please fix this. This sucks -add_compile_definitions(PROJECT_LUA_PATH="./lua/?.lua") +# Path to installed lua/ directory +add_compile_definitions(PROJECT_LUA_PATH="${CMAKE_CURRENT_LIST_DIR}/lua/?.lua") if(DEFINED ENV{RELEASE}) set(CMAKE_BUILD_TYPE Release) diff --git a/src/EventQueue.cpp b/src/EventQueue.cpp new file mode 100644 index 0000000..eebda76 --- /dev/null +++ b/src/EventQueue.cpp @@ -0,0 +1,31 @@ +#include "EventQueue.hpp" +#include "LuaRuntime.hpp" + +void EventQueue::dispatch_event(BrowserEvent *event) { + auto &runtime = LuaRuntime::instance(); + + runtime.queue_task([this, event]() { + std::unordered_map<QString, 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 EventQueue::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); + } +} diff --git a/src/EventQueue.hpp b/src/EventQueue.hpp new file mode 100644 index 0000000..c4a9f76 --- /dev/null +++ b/src/EventQueue.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include <QtCore> +#include <mutex> +#include <unordered_map> + +#include "events.hpp" + +class EventQueue { +public: + EventQueue() = default; + + void dispatch_event(BrowserEvent *event); + void register_event(const EventHandlerRequest &event); + +private: + std::mutex events_mutex; + std::unordered_map<QString, std::vector<EventHandlerRequest>> events; +}; diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp index e1d0664..8c80290 100644 --- a/src/WindowActionRouter.cpp +++ b/src/WindowActionRouter.cpp @@ -1,6 +1,5 @@ #include <QWidget> #include <QtCore> -#include <string> #include <unordered_map> #include "LuaRuntime.hpp" @@ -10,35 +9,6 @@ #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<QString, 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() { auto &runtime = LuaRuntime::instance(); diff --git a/src/WindowActionRouter.hpp b/src/WindowActionRouter.hpp index f7cb2b4..b72734c 100644 --- a/src/WindowActionRouter.hpp +++ b/src/WindowActionRouter.hpp @@ -4,11 +4,8 @@ #include <QtCore> #include <cstdint> #include <functional> -#include <mutex> -#include <string> -#include <unordered_map> -#include "events.hpp" +#include "EventQueue.hpp" #include "widgets/BrowserWindow.hpp" #include "widgets/WebViewStack.hpp" @@ -37,8 +34,8 @@ public: WebViewId fetch_current_tab_id(WindowId win_id = 0); QList<WebViewData> fetch_webview_data_list(WindowId win_id = 0); - void dispatch_event(BrowserEvent *event); - void register_event(const EventHandlerRequest &event); + DELEGATE((&event_queue), dispatch_event, dispatch_event); + DELEGATE((&event_queue), register_event, register_event) protected: WindowActionRouter() = default; @@ -54,7 +51,5 @@ private: uint64_t last_id = 1; Configuration *configuration; - // TODO: Split event handling to its own class - std::mutex events_mutex; - std::unordered_map<QString, std::vector<EventHandlerRequest>> events; + EventQueue event_queue; }; diff --git a/src/main.cpp b/src/main.cpp index 03fc0d8..066a612 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include "InstanceManager.hpp" #include "LuaRuntime.hpp" -#include "WindowActionRouter.hpp" #include "widgets/BrowserApp.hpp" QCommandLineParser *create_cli_parser() { @@ -35,13 +34,9 @@ int main(int argc, char *argv[]) { QObject::connect(&instance_manager, &InstanceManager::lua_eval_requested, &lua, &LuaRuntime::evaluate); QObject::connect(&instance_manager, &InstanceManager::urls_open_requested, - &lua, [browser](const QStringList &urls) { - qDebug() << urls; - browser->create_window(urls); - }); + browser, &BrowserApp::create_window); } else { qInfo() << "Using current instance"; - auto urls = parser->positionalArguments(); auto lua_expr = parser->value("expr"); diff --git a/templates/class.sh b/templates/class.sh index ad60acf..e49b99d 100755 --- a/templates/class.sh +++ b/templates/class.sh @@ -7,7 +7,9 @@ path="$2" [ -z "$class_name" ] && exit 1 -if ! [ -z "$path" ]; then +if [ -z "$path" ]; then + path="$path/" +else mkdir -p "./src/$path" fi @@ -15,17 +17,15 @@ fi echo "#pragma once class $class_name { - Q_OBJECT - public: $class_name(); }; -" > "./src/$path/$class_name.hpp" +" > "./src/$path$class_name.hpp" # Implementation echo " -#include \"$path/$class_name.hpp\" +#include \"$path$class_name.hpp\" $class_name::$class_name() {} -" > "./src/$path/$class_name.cpp" +" > "./src/$path$class_name.cpp" |
