Refactor key_press into _repeated_key_press

Also split off generic key pressing ability from WebKitScroller to WebKitTab
This commit is contained in:
Jay Kamat 2017-05-08 23:11:50 -07:00
parent 63cffaf558
commit 5bdd291d28
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
2 changed files with 24 additions and 18 deletions

View File

@ -311,6 +311,10 @@ class WebEngineScroller(browsertab.AbstractScroller):
page = widget.page() page = widget.page()
page.scrollPositionChanged.connect(self._update_pos) page.scrollPositionChanged.connect(self._update_pos)
def _repeated_key_press(self, key, count=1, modifier=Qt.NoModifier):
for _ in range(min(count, 5000)):
self._tab.key_press(key, modifier)
@pyqtSlot() @pyqtSlot()
def _update_pos(self): def _update_pos(self):
"""Update the scroll position attributes when it changed.""" """Update the scroll position attributes when it changed."""
@ -366,16 +370,16 @@ class WebEngineScroller(browsertab.AbstractScroller):
self._tab.run_js_async(js_code) self._tab.run_js_async(js_code)
def up(self, count=1): def up(self, count=1):
self._tab.key_press(Qt.Key_Up, count) self._repeated_key_press(Qt.Key_Up, count)
def down(self, count=1): def down(self, count=1):
self._tab.key_press(Qt.Key_Down, count) self._repeated_key_press(Qt.Key_Down, count)
def left(self, count=1): def left(self, count=1):
self._tab.key_press(Qt.Key_Left, count) self._repeated_key_press(Qt.Key_Left, count)
def right(self, count=1): def right(self, count=1):
self._tab.key_press(Qt.Key_Right, count) self._repeated_key_press(Qt.Key_Right, count)
def top(self): def top(self):
self._tab.key_press(Qt.Key_Home) self._tab.key_press(Qt.Key_Home)
@ -384,10 +388,10 @@ class WebEngineScroller(browsertab.AbstractScroller):
self._tab.key_press(Qt.Key_End) self._tab.key_press(Qt.Key_End)
def page_up(self, count=1): def page_up(self, count=1):
self._tab.key_press(Qt.Key_PageUp, count) self._repeated_key_press(Qt.Key_PageUp, count)
def page_down(self, count=1): def page_down(self, count=1):
self._tab.key_press(Qt.Key_PageDown, count) self._repeated_key_press(Qt.Key_PageDown, count)
def at_top(self): def at_top(self):
return self.pos_px().y() == 0 return self.pos_px().y() == 0
@ -647,13 +651,12 @@ class WebEngineTab(browsertab.AbstractTab):
def clear_ssl_errors(self): def clear_ssl_errors(self):
raise browsertab.UnsupportedOperationError raise browsertab.UnsupportedOperationError
def key_press(self, key, count=1, modifier=Qt.NoModifier): def key_press(self, key, modifier=Qt.NoModifier):
for _ in range(min(count, 5000)): press_evt = QKeyEvent(QEvent.KeyPress, key, modifier, 0, 0, 0)
press_evt = QKeyEvent(QEvent.KeyPress, key, modifier, 0, 0, 0) release_evt = QKeyEvent(QEvent.KeyRelease, key, modifier,
release_evt = QKeyEvent(QEvent.KeyRelease, key, modifier, 0, 0, 0)
0, 0, 0) self.send_event(press_evt)
self.send_event(press_evt) self.send_event(release_evt)
self.send_event(release_evt)
@pyqtSlot() @pyqtSlot()
def _on_history_trigger(self): def _on_history_trigger(self):

View File

@ -468,15 +468,11 @@ class WebKitScroller(browsertab.AbstractScroller):
# self._widget.setFocus() # self._widget.setFocus()
for _ in range(min(count, 5000)): for _ in range(min(count, 5000)):
press_evt = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier, 0, 0, 0)
release_evt = QKeyEvent(QEvent.KeyRelease, key, Qt.NoModifier,
0, 0, 0)
# Abort scrolling if the minimum/maximum was reached. # Abort scrolling if the minimum/maximum was reached.
if (getter is not None and if (getter is not None and
frame.scrollBarValue(direction) == getter(direction)): frame.scrollBarValue(direction) == getter(direction)):
return return
self._widget.keyPressEvent(press_evt) self._tab.key_press(key)
self._widget.keyReleaseEvent(release_evt)
def up(self, count=1): def up(self, count=1):
self._key_press(Qt.Key_Up, count, 'scrollBarMinimum', Qt.Vertical) self._key_press(Qt.Key_Up, count, 'scrollBarMinimum', Qt.Vertical)
@ -702,6 +698,13 @@ class WebKitTab(browsertab.AbstractTab):
def clear_ssl_errors(self): def clear_ssl_errors(self):
self.networkaccessmanager().clear_all_ssl_errors() self.networkaccessmanager().clear_all_ssl_errors()
def key_press(self, key, modifier=Qt.NoModifier):
press_evt = QKeyEvent(QEvent.KeyPress, key, modifier, 0, 0, 0)
release_evt = QKeyEvent(QEvent.KeyRelease, key, modifier,
0, 0, 0)
self.send_event(press_evt)
self.send_event(release_evt)
@pyqtSlot() @pyqtSlot()
def _on_history_trigger(self): def _on_history_trigger(self):
url = self.url() url = self.url()