#include #include #include #include "LuaRuntime.hpp" #include "WindowActionRouter.hpp" #include "WindowMediator.hpp" #include "keymap/KeymapEvaluator.hpp" #include "widgets/BrowserWindow.hpp" #include "widgets/WebViewStack.hpp" void WindowActionRouter::initialize() { auto &runtime = LuaRuntime::instance(); connect(&runtime, &LuaRuntime::keymap_added, this, &WindowActionRouter::add_keymap); connect(&runtime, &LuaRuntime::history_back_requested, this, [this](WebViewId webview_id, qsizetype history_index) { WITH_WEBVIEW_WINDOW(webview_id, window, { emit window->mediator()->history_back_requested(webview_id, history_index); }); }); connect(&runtime, &LuaRuntime::history_forward_requested, this, [this](WebViewId webview_id, qsizetype history_index) { WITH_WEBVIEW_WINDOW(webview_id, window, { emit window->mediator()->history_forward_requested(webview_id, history_index); }); }); connect(&runtime, &LuaRuntime::url_opened, this, [this](const QString &url, OpenType open_type, WebViewId webview_id) { WITH_WEBVIEW_WINDOW(webview_id, window, { emit window->mediator()->url_opened(url, open_type, webview_id); }); }); connect(&runtime, &LuaRuntime::webview_closed, this, [this](WebViewId webview_id) { WITH_WEBVIEW_WINDOW(webview_id, window, { emit window->mediator()->webview_closed(webview_id); }); }); connect(&runtime, &LuaRuntime::webview_selected, this, [this](WebViewId webview_id) { WITH_WEBVIEW_WINDOW(webview_id, window, { emit window->mediator()->webview_selected(webview_id); }); }); } void WindowActionRouter::add_window(BrowserWindow *window) { auto win_id = last_id; last_id++; window->set_id(win_id); { const std::lock_guard lock(window_map_mutex); window_map.insert({win_id, window}); } connect(window, &BrowserWindow::closed, this, [this, window]() { window->disconnect(); LuaRuntime::instance().queue_task([this, window]() { const std::lock_guard lock(window_map_mutex); window_map.erase(window->get_id()); }); }); connect(window->mediator(), &WindowMediator::close_window_requested, window, [window]() { window->close(); }); connect(window->mediator(), &WindowMediator::new_window_requested, this, &WindowActionRouter::new_window_requested); } const WindowMap &WindowActionRouter::windows() { return window_map; } void WindowActionRouter::add_keymap(const QString &mode_string, const QString &keyseq, std::function action) { auto &keymap_evaluator = KeymapEvaluator::instance(); const KeyMode mode = keymap_evaluator.mode_from_string(mode_string); keymap_evaluator.add_keymap(mode, keyseq, std::move(action)); } WebViewId WindowActionRouter::fetch_current_tab_id(WindowId win_id) { const std::lock_guard lock(window_map_mutex); for (auto &pair : window_map) { auto *win = pair.second; auto is_current_window = win_id == win->get_id() || (win_id == 0 && win->isActiveWindow()); if (is_current_window) { return win->mediator()->current_webview_id(); } } return 0; } QList WindowActionRouter::fetch_webview_data_list(WindowId win_id) { const std::lock_guard lock(window_map_mutex); for (auto &pair : window_map) { auto *win = pair.second; auto is_current_window = win_id == win->get_id() || (win_id == 0 && win->isActiveWindow()); if (is_current_window) { return win->mediator()->get_webview_list(); } } return {}; }