From 26504893e75fb33e9d3f01abdebf1d61f362f1b1 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 5 Apr 2025 19:07:03 +0530 Subject: Set url for windows synchronously + handle title change in webview --- src/widgets/BrowserWindow.cpp | 27 +++++++++++++++------------ src/widgets/BrowserWindow.hpp | 1 - src/widgets/WebViewStack.cpp | 11 +++++++---- src/widgets/WebViewStack.hpp | 2 +- 4 files changed, 23 insertions(+), 18 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp index ff05b0e..2867379 100644 --- a/src/widgets/BrowserWindow.cpp +++ b/src/widgets/BrowserWindow.cpp @@ -4,7 +4,6 @@ #include #include "Configuration.hpp" -#include "LuaRuntime.hpp" #include "WindowMediator.hpp" #include "keymap/KeymapEvaluator.hpp" #include "widgets/BrowserWindow.hpp" @@ -22,9 +21,19 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, const QStringLi centralWidget()->setLayout(layout); // Web engine - auto *web_view_stack = new WebViewStack(&configuration, new QWebEngineProfile("null-browser")); - layout->addWidget(web_view_stack); + auto *webview_stack = new WebViewStack(&configuration, new QWebEngineProfile("null-browser")); + layout->addWidget(webview_stack); + // Open webviews for given urls + if (urls.isEmpty()) { + webview_stack->open_url(configuration.new_tab_url.toString(), OpenType::OpenUrlInTab); + } else { + for (const auto &url : urls) { + webview_stack->open_url(url, OpenType::OpenUrlInTab); + } + } + + // Default keymaps auto &keymap_evaluator = KeymapEvaluator::instance(); // TODO: remove @@ -36,22 +45,16 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, const QStringLi }); keymap_evaluator.add_keymap(KeyMode::Normal, "a", []() { qDebug() << "Stuff"; }); - win_mediator = new WindowMediator(web_view_stack); + win_mediator = new WindowMediator(webview_stack); - connect(web_view_stack, &WebViewStack::current_webview_changed, this, [this](int index) { + // Update window title when webview changes + connect(webview_stack, &WebViewStack::current_webview_title_changed, this, [this](int index) { auto webviews = win_mediator->get_webview_list(); if (index >= 0 && index < webviews.count()) { const auto &webview = webviews.at(index); setWindowTitle(webview.title); } }); - - auto &lua = LuaRuntime::instance(); - lua.queue_task([this, urls]() { - for (const auto &url : urls) { - emit win_mediator->url_opened(url, OpenType::OpenUrlInTab, 0); - } - }); } void BrowserWindow::closeEvent(QCloseEvent * /*event*/) { emit closed(); } diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp index e657970..638d8de 100644 --- a/src/widgets/BrowserWindow.hpp +++ b/src/widgets/BrowserWindow.hpp @@ -24,7 +24,6 @@ public: signals: void closed(); - // void new_window_requested(const QUrl &url); private: WindowMediator *win_mediator; diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp index 5134101..f5e9765 100644 --- a/src/widgets/WebViewStack.cpp +++ b/src/widgets/WebViewStack.cpp @@ -19,9 +19,8 @@ WebViewStack::WebViewStack(const Configuration *configuration, QWebEngineProfile layout->setContentsMargins(0, 0, 0, 0); layout->setStackingMode(QStackedLayout::StackOne); - connect(layout, &QStackedLayout::currentChanged, this, &WebViewStack::current_webview_changed); - - create_new_webview(configuration->new_tab_url, true); + connect(layout, &QStackedLayout::currentChanged, this, + &WebViewStack::current_webview_title_changed); } void WebViewStack::open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) { @@ -54,11 +53,15 @@ WebView *WebViewStack::create_new_webview(const QUrl &url, bool focus) { WindowActionRouter::instance().dispatch_event( new UrlChangedEvent(url.toString(), webview->get_id(), 0)); }); + connect(webview->page(), &QWebEnginePage::titleChanged, this, + [this](const QString & /* title */) { + emit current_webview_title_changed(layout->currentIndex()); + }); if (focus) focus_webview(webview->get_id()); - emit current_webview_changed(layout->currentIndex()); + emit current_webview_title_changed(layout->currentIndex()); return webview; } diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp index 5d09aaf..23b9382 100644 --- a/src/widgets/WebViewStack.hpp +++ b/src/widgets/WebViewStack.hpp @@ -45,7 +45,7 @@ public: uint32_t current_webview_index(); signals: - void current_webview_changed(int index); + void current_webview_title_changed(int index); void new_window_requested(const QUrl &url); protected: -- cgit v1.3.1