Permalink
Browse files

Move auto-insert-mode handling to browsertab

This means auto-insert-mode now works correctly with QtWebEngine.
  • Loading branch information...
The-Compiler committed Sep 7, 2016
1 parent 2ef6423 commit ee5a97206902cd44776966538b8296b92f38c4fe
@@ -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 @@ def __init__(self, win_id, parent=None):
# 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 @@ def __init__(self, win_id, parent=None):
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 @@ def _on_load_started(self):
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 @@ def _on_load_finished(self, ok):
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):
@@ -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)
@@ -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)
@@ -70,24 +70,16 @@ def __init__(self, win_id, tab_id, tab, parent=None):
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 @@ def add_js_bridge(self):
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.
View
@@ -244,7 +244,7 @@ def __init__(self, url=FakeUrl(), title='', tab_id=0, *,
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
@@ -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,

0 comments on commit ee5a972

Please sign in to comment.