Skip to content

Truly independent web browser

UI/Qt: Focus the tab a clicked notification came from

sideshowbarker8b37c7b6ee64
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index a44b04219a6..96fb483e3d0 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -7,9 +7,12 @@
#include <LibCore/ArgsParser.h>
#include <LibURL/InternalURLs.h>
#include <LibURL/URL.h>
+#include <LibWebView/CanonicalTraversable.h>
+#include <LibWebView/NotificationBackend.h>
#include <LibWebView/SessionStore.h>
#include <LibWebView/URL.h>
#include <LibWebView/Utilities.h>
+#include <LibWebView/WebContentPage.h>
#include <UI/Qt/Application.h>
#include <UI/Qt/BookmarkDialog.h>
#include <UI/Qt/ChromeLayout.h>
@@ -1287,6 +1290,48 @@ OwnPtr<WebView::PictureInPictureWindow> Application::create_picture_in_picture_w
return Ladybird::create_picture_in_picture_window(traversable, requesting_page.view(), video_size);
}
+// The window that shows this view in one of its tabs, if any.
+static BrowserWindow* window_for_view(WebView::ViewImplementation const& view)
+{
+ for (auto* widget : QApplication::topLevelWidgets()) {
+ if (auto* window = as_if<BrowserWindow>(widget); window && window->tab_for_view(view))
+ return window;
+ }
+ return nullptr;
+}
+
+NonnullOwnPtr<WebView::NotificationBackend> Application::create_notification_backend()
+{
+ // The platform backends follow; until then, pages see every show fail.
+ return WebView::Application::create_notification_backend();
+}
+
+void Application::focus_page_from_notification(WebView::WebContentPage& page)
+{
+ auto* window = window_for_view(page.view());
+ if (!window)
+ return;
+ window->activate_tab(window->tab_index(window->tab_for_view(page.view())));
+ window->bring_to_front();
+}
+
+void Application::open_url_in_new_tab_from_notification(URL::URL const& url, Web::PageId, WebView::CanonicalTraversable* traversable)
+{
+ BrowserWindow* window = nullptr;
+ if (traversable) {
+ if (auto view = traversable->view(); view.has_value())
+ window = window_for_view(*view);
+ }
+ if (!window)
+ window = m_active_window;
+ if (!window) {
+ new_window({ url });
+ return;
+ }
+ window->new_tab_from_url(url, Web::HTML::ActivateTab::Yes, BrowserWindow::TabLocation::end());
+ window->bring_to_front();
+}
+
Optional<WebView::ViewImplementation&> Application::active_web_view() const
{
if (auto* active_tab = this->active_tab())
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index 78a2c8633e7..03df8ba6125 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -145,6 +145,10 @@ private:
virtual Optional<String> system_font_family() const override;
#endif
+ virtual NonnullOwnPtr<WebView::NotificationBackend> create_notification_backend() override;
+ virtual void focus_page_from_notification(WebView::WebContentPage&) override;
+ virtual void open_url_in_new_tab_from_notification(URL::URL const&, Web::PageId originating_page, WebView::CanonicalTraversable*) override;
+
virtual bool supports_picture_in_picture() const override { return true; }
virtual OwnPtr<WebView::PictureInPictureWindow> create_picture_in_picture_window(WebView::WebContentPage& requesting_page, WebView::CanonicalTraversable&, Gfx::IntSize video_size) override;
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 1522f064455..827b827c3b6 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -2042,12 +2042,27 @@ void BrowserWindow::raise_for_close_prompt()
// A page's beforeunload prompt shows inside this window, which may sit behind other windows or in the Dock while a
// close check asks it, so the window comes to the front first, where the user can see the prompt.
+ bring_to_front();
+}
+
+void BrowserWindow::bring_to_front()
+{
if (isMinimized())
setWindowState(windowState() & ~Qt::WindowMinimized);
raise();
activateWindow();
}
+Tab* BrowserWindow::tab_for_view(WebView::ViewImplementation const& view) const
+{
+ for (int index = 0; index < m_tabs_container->count(); ++index) {
+ auto* tab = m_tabs_container->tab(index);
+ if (tab && &tab->view() == &view)
+ return tab;
+ }
+ return nullptr;
+}
+
void BrowserWindow::report_activation_to_session_store()
{
if (m_session_window_id.has_value())
diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.h
index 67db8c7ed9f..471577a2187 100644
--- a/UI/Qt/BrowserWindow.h
+++ b/UI/Qt/BrowserWindow.h
@@ -186,6 +186,10 @@ public:
void request_close_preflight(Function<void(bool approved)> on_complete);
// Brings the window forward for a page's beforeunload prompt during an application-wide close check.
void raise_for_close_prompt();
+ // Unminimizes, raises and activates the window, so the user sees it in front.
+ void bring_to_front();
+ // The tab showing this view, if it's one of this window's.
+ Tab* tab_for_view(WebView::ViewImplementation const&) const;
bool is_window_close_approved() const { return m_window_close_approved; }
void report_activation_to_session_store();
void cancel_window_close_preflight();