WebEngine: Fake keypresses with modifiers instead of override_target

With QtWebEngine, handling a click (e.g. createWindow being called)
happens some time after a click event has been handled.

When setting override_target, that means we don't know when to set the
override target back, and we can't simply only unset it in createWindow
as elements not causing a new tab to be opened (like an input field) can
be hinted too.

Instead, we now only use override_target with QtWebKit, and simply
generate fake key events with the right modifiers to do the right thing
in createWindow for QtWebEngine.

Fixes #2119.
This commit is contained in:
Florian Bruhin 2016-11-14 23:03:36 +01:00
parent 81e8421f62
commit b30d37e3e0
3 changed files with 17 additions and 16 deletions

View File

@ -84,6 +84,7 @@ class TabData:
inspector: The QWebInspector used for this webview.
viewing_source: Set if we're currently showing a source view.
override_target: Override for open_target for fake clicks (like hints).
Only used for QtWebKit.
"""
def __init__(self):

View File

@ -338,6 +338,8 @@ class AbstractWebElement(collections.abc.MutableMapping):
"""Simulate a click on the element."""
# FIXME:qtwebengine do we need this?
# self._widget.setFocus()
# For QtWebKit
self._tab.data.override_target = click_target
pos = self._mouse_pos()
@ -345,20 +347,24 @@ class AbstractWebElement(collections.abc.MutableMapping):
log.webelem.debug("Sending fake click to {!r} at position {} with "
"target {}".format(self, pos, click_target))
if click_target in [usertypes.ClickTarget.tab,
usertypes.ClickTarget.tab_bg,
usertypes.ClickTarget.window]:
modifiers = Qt.ControlModifier
modifiers = {
usertypes.ClickTarget.normal: Qt.NoModifier,
usertypes.ClickTarget.window: Qt.AltModifier | Qt.ShiftModifier,
usertypes.ClickTarget.tab: Qt.ControlModifier,
usertypes.ClickTarget.tab_bg: Qt.ControlModifier,
}
if config.get('tabs', 'background-tabs'):
modifiers[usertypes.ClickTarget.tab] |= Qt.ShiftModifier
else:
modifiers = Qt.NoModifier
modifiers[usertypes.ClickTarget.tab_bg] |= Qt.ShiftModifier
events = [
QMouseEvent(QEvent.MouseMove, pos, Qt.NoButton, Qt.NoButton,
Qt.NoModifier),
QMouseEvent(QEvent.MouseButtonPress, pos, Qt.LeftButton,
Qt.LeftButton, modifiers),
Qt.LeftButton, modifiers[click_target]),
QMouseEvent(QEvent.MouseButtonRelease, pos, Qt.LeftButton,
Qt.NoButton, modifiers),
Qt.NoButton, modifiers[click_target]),
]
for evt in events:

View File

@ -74,18 +74,12 @@ class WebEngineView(QWebEngineView):
"""
debug_type = debug.qenum_key(QWebEnginePage, wintype)
background_tabs = config.get('tabs', 'background-tabs')
override_target = self._tabdata.override_target
log.webview.debug("createWindow with type {}, background_tabs "
"{}, override_target {}".format(
debug_type, background_tabs, override_target))
"{}".format(debug_type, background_tabs))
if override_target is not None:
target = override_target
self._tabdata.override_target = None
elif wintype == QWebEnginePage.WebBrowserWindow:
log.webview.debug("createWindow with WebBrowserWindow - when does "
"this happen?!")
if wintype == QWebEnginePage.WebBrowserWindow:
# Shift-Alt-Click
target = usertypes.ClickTarget.window
elif wintype == QWebEnginePage.WebDialog:
log.webview.warning("{} requested, but we don't support "