Move auto-insert-mode handling to browsertab

This means auto-insert-mode now works correctly with QtWebEngine.
This commit is contained in:
Florian Bruhin 2016-09-07 10:24:27 +02:00
parent 2ef6423cf2
commit ee5a972069
6 changed files with 36 additions and 50 deletions

View File

@ -520,7 +520,7 @@ class AbstractTab(QWidget):
WIDGET_CLASS = None 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.win_id = win_id
self.tab_id = next(tab_id_gen) self.tab_id = next(tab_id_gen)
super().__init__(parent) super().__init__(parent)
@ -533,8 +533,8 @@ class AbstractTab(QWidget):
# self.history = AbstractHistory(self) # self.history = AbstractHistory(self)
# self.scroller = AbstractScroller(self, parent=self) # self.scroller = AbstractScroller(self, parent=self)
# self.caret = AbstractCaret(win_id=win_id, tab=self, mode_manager=..., # self.caret = AbstractCaret(win_id=win_id, tab=self,
# parent=self) # mode_manager=mode_manager, parent=self)
# self.zoom = AbstractZoom(win_id=win_id) # self.zoom = AbstractZoom(win_id=win_id)
# self.search = AbstractSearch(parent=self) # self.search = AbstractSearch(parent=self)
# self.printing = AbstractPrinting() # self.printing = AbstractPrinting()
@ -545,6 +545,7 @@ class AbstractTab(QWidget):
self._widget = None self._widget = None
self._progress = 0 self._progress = 0
self._has_ssl_errors = False self._has_ssl_errors = False
self._mode_manager = mode_manager
self._load_status = usertypes.LoadStatus.none self._load_status = usertypes.LoadStatus.none
self._mouse_event_filter = mouse.MouseEventFilter( self._mouse_event_filter = mouse.MouseEventFilter(
self, widget_class=self.WIDGET_CLASS, parent=self) self, widget_class=self.WIDGET_CLASS, parent=self)
@ -650,6 +651,26 @@ class AbstractTab(QWidget):
self._set_load_status(usertypes.LoadStatus.loading) self._set_load_status(usertypes.LoadStatus.loading)
self.load_started.emit() 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) @pyqtSlot(bool)
def _on_load_finished(self, ok): def _on_load_finished(self, ok):
if ok and not self._has_ssl_errors: if ok and not self._has_ssl_errors:
@ -665,6 +686,7 @@ class AbstractTab(QWidget):
self.load_finished.emit(ok) self.load_finished.emit(ok)
if not self.title(): if not self.title():
self.title_changed.emit(self.url().toDisplayString()) self.title_changed.emit(self.url().toDisplayString())
self._handle_auto_insert_mode(ok)
@pyqtSlot() @pyqtSlot()
def _on_history_trigger(self): def _on_history_trigger(self):

View File

@ -390,7 +390,8 @@ class WebEngineTab(browsertab.AbstractTab):
WIDGET_CLASS = QOpenGLWidget WIDGET_CLASS = QOpenGLWidget
def __init__(self, win_id, mode_manager, parent=None): 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) widget = webview.WebEngineView(tabdata=self.data, win_id=win_id)
self.history = WebEngineHistory(self) self.history = WebEngineHistory(self)
self.scroller = WebEngineScroller(self, parent=self) self.scroller = WebEngineScroller(self, parent=self)

View File

@ -577,7 +577,8 @@ class WebKitTab(browsertab.AbstractTab):
WIDGET_CLASS = webview.WebView WIDGET_CLASS = webview.WebView
def __init__(self, win_id, mode_manager, parent=None): 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) widget = webview.WebView(win_id, self.tab_id, tab=self)
self.history = WebKitHistory(self) self.history = WebKitHistory(self)
self.scroller = WebKitScroller(self, parent=self) self.scroller = WebKitScroller(self, parent=self)

View File

@ -70,24 +70,16 @@ class WebView(QWebView):
self._set_bg_color() self._set_bg_color()
self._tab_id = tab_id 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', mode_manager = objreg.get('mode-manager', scope='window',
window=win_id) window=win_id)
mode_manager.entered.connect(self.on_mode_entered) mode_manager.entered.connect(self.on_mode_entered)
mode_manager.left.connect(self.on_mode_left) mode_manager.left.connect(self.on_mode_left)
objreg.get('config').changed.connect(self._set_bg_color) 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): def __repr__(self):
url = utils.elide(self.url().toDisplayString(QUrl.EncodeUnicode), 100) url = utils.elide(self.url().toDisplayString(QUrl.EncodeUnicode), 100)
return utils.get_repr(self, tab_id=self._tab_id, url=url) return utils.get_repr(self, tab_id=self._tab_id, url=url)
@ -154,37 +146,6 @@ class WebView(QWebView):
bridge = objreg.get('js-bridge') bridge = objreg.get('js-bridge')
frame.addToJavaScriptWindowObject('qute', 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) @pyqtSlot(usertypes.KeyMode)
def on_mode_entered(self, mode): def on_mode_entered(self, mode):
"""Ignore attempts to focus the widget if in any status-input mode. """Ignore attempts to focus the widget if in any status-input mode.

View File

@ -244,7 +244,7 @@ class FakeWebTab(browsertab.AbstractTab):
scroll_pos_perc=(0, 0), scroll_pos_perc=(0, 0),
load_status=usertypes.LoadStatus.success, load_status=usertypes.LoadStatus.success,
progress=0): progress=0):
super().__init__(win_id=0) super().__init__(win_id=0, mode_manager=None)
self._load_status = load_status self._load_status = load_status
self._title = title self._title = title
self._url = url self._url = url

View File

@ -90,7 +90,8 @@ class Tab(browsertab.AbstractTab):
# pylint: disable=abstract-method # pylint: disable=abstract-method
def __init__(self, win_id, mode_manager, parent=None): 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.history = browsertab.AbstractHistory(self)
self.scroller = browsertab.AbstractScroller(self, parent=self) self.scroller = browsertab.AbstractScroller(self, parent=self)
self.caret = browsertab.AbstractCaret(win_id=self.win_id, self.caret = browsertab.AbstractCaret(win_id=self.win_id,