diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index f1b61b320..d15f53df5 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -520,7 +520,7 @@ class AbstractTab(QWidget): WIDGET_CLASS = None - def __init__(self, win_id, parent=None): + def __init__(self, win_id, mode_manager, parent=None): self.win_id = win_id self.tab_id = next(tab_id_gen) super().__init__(parent) @@ -533,8 +533,8 @@ class AbstractTab(QWidget): # self.history = AbstractHistory(self) # self.scroller = AbstractScroller(self, parent=self) - # self.caret = AbstractCaret(win_id=win_id, tab=self, mode_manager=..., - # parent=self) + # self.caret = AbstractCaret(win_id=win_id, tab=self, + # mode_manager=mode_manager, parent=self) # self.zoom = AbstractZoom(win_id=win_id) # self.search = AbstractSearch(parent=self) # self.printing = AbstractPrinting() @@ -545,6 +545,7 @@ class AbstractTab(QWidget): self._widget = None self._progress = 0 self._has_ssl_errors = False + self._mode_manager = mode_manager self._load_status = usertypes.LoadStatus.none self._mouse_event_filter = mouse.MouseEventFilter( self, widget_class=self.WIDGET_CLASS, parent=self) @@ -650,6 +651,26 @@ class AbstractTab(QWidget): self._set_load_status(usertypes.LoadStatus.loading) self.load_started.emit() + def _handle_auto_insert_mode(self, ok): + """Handle auto-insert-mode after loading finished.""" + if not config.get('input', 'auto-insert-mode') or not ok: + return + + cur_mode = self._mode_manager.mode + if cur_mode == usertypes.KeyMode.insert: + return + + def _auto_insert_mode_cb(elem): + """Called from JS after finding the focused element.""" + if elem is None: + log.webview.debug("No focused element!") + return + if elem.is_editable(): + modeman.enter(self.win_id, usertypes.KeyMode.insert, + 'load finished', only_if_normal=True) + + self.elements.find_focused(_auto_insert_mode_cb) + @pyqtSlot(bool) def _on_load_finished(self, ok): if ok and not self._has_ssl_errors: @@ -665,6 +686,7 @@ class AbstractTab(QWidget): self.load_finished.emit(ok) if not self.title(): self.title_changed.emit(self.url().toDisplayString()) + self._handle_auto_insert_mode(ok) @pyqtSlot() def _on_history_trigger(self): diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 0abf327bf..79fcc1dfc 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -390,7 +390,8 @@ class WebEngineTab(browsertab.AbstractTab): WIDGET_CLASS = QOpenGLWidget def __init__(self, win_id, mode_manager, parent=None): - super().__init__(win_id) + super().__init__(win_id=win_id, mode_manager=mode_manager, + parent=parent) widget = webview.WebEngineView(tabdata=self.data, win_id=win_id) self.history = WebEngineHistory(self) self.scroller = WebEngineScroller(self, parent=self) diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index b4973ad48..59b049ffb 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -577,7 +577,8 @@ class WebKitTab(browsertab.AbstractTab): WIDGET_CLASS = webview.WebView def __init__(self, win_id, mode_manager, parent=None): - super().__init__(win_id) + super().__init__(win_id=win_id, mode_manager=mode_manager, + parent=parent) widget = webview.WebView(win_id, self.tab_id, tab=self) self.history = WebKitHistory(self) self.scroller = WebKitScroller(self, parent=self) diff --git a/qutebrowser/browser/webkit/webview.py b/qutebrowser/browser/webkit/webview.py index 239557358..8438d9890 100644 --- a/qutebrowser/browser/webkit/webview.py +++ b/qutebrowser/browser/webkit/webview.py @@ -70,24 +70,16 @@ class WebView(QWebView): self._set_bg_color() self._tab_id = tab_id - self._init_page(tab.data) + page = webpage.BrowserPage(self.win_id, self._tab_id, tab.data, + parent=self) + self.setPage(page) + mode_manager = objreg.get('mode-manager', scope='window', window=win_id) mode_manager.entered.connect(self.on_mode_entered) mode_manager.left.connect(self.on_mode_left) objreg.get('config').changed.connect(self._set_bg_color) - def _init_page(self, tabdata): - """Initialize the QWebPage used by this view. - - Args: - tabdata: The TabData object for this tab. - """ - page = webpage.BrowserPage(self.win_id, self._tab_id, tabdata, - parent=self) - self.setPage(page) - page.mainFrame().loadFinished.connect(self.on_load_finished) - def __repr__(self): url = utils.elide(self.url().toDisplayString(QUrl.EncodeUnicode), 100) return utils.get_repr(self, tab_id=self._tab_id, url=url) @@ -154,37 +146,6 @@ class WebView(QWebView): bridge = objreg.get('js-bridge') frame.addToJavaScriptWindowObject('qute', bridge) - @pyqtSlot() - def on_load_finished(self): - """Handle a finished page load. - - We don't take loadFinished's ok argument here as it always seems to be - true when the QWebPage has an ErrorPageExtension implemented. - See https://github.com/The-Compiler/qutebrowser/issues/84 - """ - ok = not self.page().error_occurred - self._handle_auto_insert_mode(ok) - - def _handle_auto_insert_mode(self, ok): - """Handle auto-insert-mode after loading finished.""" - if not config.get('input', 'auto-insert-mode'): - return - mode_manager = objreg.get('mode-manager', scope='window', - window=self.win_id) - cur_mode = mode_manager.mode - if cur_mode == usertypes.KeyMode.insert or not ok: - return - frame = self.page().currentFrame() - try: - elem = webkitelem.focus_elem(frame) - except webkitelem.IsNullError: - log.webview.debug("Focused element is null!") - return - log.modes.debug("focus element: {}".format(repr(elem))) - if elem.is_editable(): - modeman.enter(self.win_id, usertypes.KeyMode.insert, - 'load finished', only_if_normal=True) - @pyqtSlot(usertypes.KeyMode) def on_mode_entered(self, mode): """Ignore attempts to focus the widget if in any status-input mode. diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 169190853..85f73d98c 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -244,7 +244,7 @@ class FakeWebTab(browsertab.AbstractTab): scroll_pos_perc=(0, 0), load_status=usertypes.LoadStatus.success, progress=0): - super().__init__(win_id=0) + super().__init__(win_id=0, mode_manager=None) self._load_status = load_status self._title = title self._url = url diff --git a/tests/unit/browser/test_tab.py b/tests/unit/browser/test_tab.py index 67911a09e..1fb984feb 100644 --- a/tests/unit/browser/test_tab.py +++ b/tests/unit/browser/test_tab.py @@ -90,7 +90,8 @@ class Tab(browsertab.AbstractTab): # pylint: disable=abstract-method def __init__(self, win_id, mode_manager, parent=None): - super().__init__(win_id, parent) + super().__init__(win_id=win_id, mode_manager=mode_manager, + parent=parent) self.history = browsertab.AbstractHistory(self) self.scroller = browsertab.AbstractScroller(self, parent=self) self.caret = browsertab.AbstractCaret(win_id=self.win_id,