From eade305965b68627d4313e0664979692a7025994 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 25 Feb 2018 15:02:38 +0100 Subject: [PATCH] Add a predicted_navigation signal This is emitted when we know that we're going to visit some URL, but Qt doesn't know yet. This way, we can change the settings early, and since we know which settings have actually changed, prevent a change needing a reload in _on_navigation_request. --- qutebrowser/browser/browsertab.py | 7 ++++++- qutebrowser/browser/webengine/webenginetab.py | 10 +++++++++- qutebrowser/browser/webkit/webkittab.py | 3 ++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 3fb700420..bc1b6cb5c 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -616,6 +616,7 @@ class AbstractTab(QWidget): process terminated. arg 0: A TerminationStatus member. arg 1: The exit code. + predicted_navigation: Emitted before we tell Qt to open a URL. """ window_close_requested = pyqtSignal() @@ -633,6 +634,7 @@ class AbstractTab(QWidget): add_history_item = pyqtSignal(QUrl, QUrl, str) # url, requested url, title fullscreen_requested = pyqtSignal(bool) renderer_process_terminated = pyqtSignal(TerminationStatus, int) + predicted_navigation = pyqtSignal(QUrl) def __init__(self, *, win_id, mode_manager, private, parent=None): self.private = private @@ -663,6 +665,9 @@ class AbstractTab(QWidget): objreg.register('hintmanager', hintmanager, scope='tab', window=self.win_id, tab=self.tab_id) + self.predicted_navigation.connect( + lambda url: self.title_changed.emit(url.toDisplayString())) + def _set_widget(self, widget): # pylint: disable=protected-access self._widget = widget @@ -811,7 +816,7 @@ class AbstractTab(QWidget): def _openurl_prepare(self, url): qtutils.ensure_valid(url) - self.title_changed.emit(url.toDisplayString()) + self.predicted_navigation.emit(url) def openurl(self, url): raise NotImplementedError diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index c3f493ba1..4fcb6d279 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -471,7 +471,8 @@ class WebEngineHistory(browsertab.AbstractHistory): return self._history.itemAt(i) def _go_to_item(self, item): - return self._history.goToItem(item) + self._tab.predicted_navigation.emit(item.url()) + self._history.goToItem(item) def serialize(self): if not qtutils.version_check('5.9', compiled=False): @@ -903,6 +904,11 @@ class WebEngineTab(browsertab.AbstractTab): QTimer.singleShot(100, lambda url=self._reload_url: self.openurl(url)) self._reload_url = None + @pyqtSlot(QUrl) + def _on_predicted_navigation(self, url): + """If we know we're going to visit an URL soon, change the settings.""" + self.settings.update_for_url(url) + @pyqtSlot(usertypes.NavigationRequest) def _on_navigation_request(self, navigation): super()._on_navigation_request(navigation) @@ -946,5 +952,7 @@ class WebEngineTab(browsertab.AbstractTab): page.loadFinished.connect(self._restore_zoom) page.loadFinished.connect(self._on_load_finished) + self.predicted_navigation.connect(self._on_predicted_navigation) + def event_target(self): return self._widget.focusProxy() diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 5184550cd..7af0b474d 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -518,7 +518,8 @@ class WebKitHistory(browsertab.AbstractHistory): return self._history.itemAt(i) def _go_to_item(self, item): - return self._history.goToItem(item) + self._tab.predicted_navigation.emit(item.url()) + self._history.goToItem(item) def serialize(self): return qtutils.serialize(self._history)