From 6121673c1a54df8fceb3d597efa4e362eda59ff0 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Thu, 29 Jul 2021 17:21:37 +0200 Subject: Switch to tab when clicking on notification --- qutebrowser/browser/webengine/notification.py | 11 ++++++++++- tests/end2end/features/notifications.feature | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/webengine/notification.py b/qutebrowser/browser/webengine/notification.py index e40b3e736..abee863b4 100644 --- a/qutebrowser/browser/webengine/notification.py +++ b/qutebrowser/browser/webengine/notification.py @@ -65,7 +65,7 @@ if TYPE_CHECKING: from qutebrowser.config import config from qutebrowser.misc import objects -from qutebrowser.utils import qtutils, log, utils, debug, message, version +from qutebrowser.utils import qtutils, log, utils, debug, message, version, objreg from qutebrowser.qt import sip @@ -362,6 +362,7 @@ class NotificationBridgePresenter(QObject): return try: + self._focus_first_matching_tab(notification) notification.click() except RuntimeError: # WORKAROUND for @@ -369,6 +370,14 @@ class NotificationBridgePresenter(QObject): log.misc.debug(f"Ignoring click request for notification {notification_id} " "due to PyQt bug") + def _focus_first_matching_tab(self, notification: "QWebEngineNotification") -> None: + for win_id in objreg.window_registry: + tabbedbrowser = objreg.get("tabbed-browser", window=win_id, scope="window") + for idx, tab in enumerate(tabbedbrowser.widgets()): + if tab.url().matches(notification.origin(), QUrl.RemovePath): + tabbedbrowser.widget.setCurrentIndex(idx) + return + def _drop_adapter(self) -> None: """Drop the currently active adapter (if any). diff --git a/tests/end2end/features/notifications.feature b/tests/end2end/features/notifications.feature index 08c5f725f..cd34793ef 100644 --- a/tests/end2end/features/notifications.feature +++ b/tests/end2end/features/notifications.feature @@ -4,7 +4,8 @@ Feature: Notifications HTML5 notification API interaction Background: - Given I open data/javascript/notifications.html + Given I clean up open tabs + And I open data/javascript/notifications.html And I set content.notifications.enabled to true And I run :click-element id button And I clean up the notification server @@ -120,8 +121,13 @@ Feature: Notifications Scenario: User clicks presented notification When I run :click-element id show-button And I wait for the javascript message "notification shown" + And I open about:blank in a new tab And I click the notification Then the javascript message "notification clicked" should be logged + And the following tabs should be open: + - about:blank + - data/javascript/notifications.html (active) + - about:blank @pyqtwebengine<5.15.0 Scenario: User clicks presented notification (old Qt) -- cgit v1.2.3-54-g00ecf From 69ebc6b9e65a6e20bd7a079fe4fb1bdd62610cd1 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Thu, 29 Jul 2021 19:27:32 +0200 Subject: Move focus out of except --- qutebrowser/browser/webengine/notification.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/browser/webengine/notification.py b/qutebrowser/browser/webengine/notification.py index abee863b4..2231c8e54 100644 --- a/qutebrowser/browser/webengine/notification.py +++ b/qutebrowser/browser/webengine/notification.py @@ -361,8 +361,8 @@ class NotificationBridgePresenter(QObject): log.misc.debug("Did not find matching notification, ignoring") return + self._focus_first_matching_tab(notification) try: - self._focus_first_matching_tab(notification) notification.click() except RuntimeError: # WORKAROUND for -- cgit v1.2.3-54-g00ecf From d7753009172d0ee0aac7898a01e61509ec24e8de Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Thu, 29 Jul 2021 19:27:55 +0200 Subject: Log message if no matching tab was found --- qutebrowser/browser/webengine/notification.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qutebrowser/browser/webengine/notification.py b/qutebrowser/browser/webengine/notification.py index 2231c8e54..31f86f26e 100644 --- a/qutebrowser/browser/webengine/notification.py +++ b/qutebrowser/browser/webengine/notification.py @@ -377,6 +377,7 @@ class NotificationBridgePresenter(QObject): if tab.url().matches(notification.origin(), QUrl.RemovePath): tabbedbrowser.widget.setCurrentIndex(idx) return + log.misc.debug(f"No matching tab found for {notification.origin()}") def _drop_adapter(self) -> None: """Drop the currently active adapter (if any). -- cgit v1.2.3-54-g00ecf From 887488f870424663917fd51f06ce3c202f0b2773 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sat, 9 Oct 2021 13:58:15 +0200 Subject: Move focus call to avoid crash --- qutebrowser/browser/webengine/notification.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/webengine/notification.py b/qutebrowser/browser/webengine/notification.py index 31f86f26e..a32981f64 100644 --- a/qutebrowser/browser/webengine/notification.py +++ b/qutebrowser/browser/webengine/notification.py @@ -361,7 +361,6 @@ class NotificationBridgePresenter(QObject): log.misc.debug("Did not find matching notification, ignoring") return - self._focus_first_matching_tab(notification) try: notification.click() except RuntimeError: @@ -369,6 +368,8 @@ class NotificationBridgePresenter(QObject): # https://www.riverbankcomputing.com/pipermail/pyqt/2020-May/042918.html log.misc.debug(f"Ignoring click request for notification {notification_id} " "due to PyQt bug") + return + self._focus_first_matching_tab(notification) def _focus_first_matching_tab(self, notification: "QWebEngineNotification") -> None: for win_id in objreg.window_registry: -- cgit v1.2.3-54-g00ecf