Accept arbitrary QEvent subscriptions via a set

symbol_search
Tyler Goodlet 2021-05-28 12:55:11 -04:00
parent 7fa9f3f542
commit ea3d96e7ed
2 changed files with 16 additions and 12 deletions

View File

@ -30,7 +30,7 @@ class EventCloner(QtCore.QObject):
for later async processing. for later async processing.
""" """
_event_types: set[QEvent] = set()
_send_chan: trio.abc.SendChannel = None _send_chan: trio.abc.SendChannel = None
def eventFilter( def eventFilter(
@ -39,14 +39,11 @@ class EventCloner(QtCore.QObject):
ev: QEvent, ev: QEvent,
) -> None: ) -> None:
if ev.type() in { if ev.type() in self._event_types:
QEvent.KeyPress,
# QEvent.KeyRelease, # TODO: what's the right way to allow this?
}: # if ev.isAutoRepeat():
# TODO: is there a global setting for this? # ev.ignore()
if ev.isAutoRepeat():
ev.ignore()
return False
# XXX: we unpack here because apparently doing it # XXX: we unpack here because apparently doing it
# after pop from the mem chan isn't showing the same # after pop from the mem chan isn't showing the same
@ -59,7 +56,7 @@ class EventCloner(QtCore.QObject):
txt = ev.text() txt = ev.text()
# run async processing # run async processing
self._send_chan.send_nowait((key, mods, txt)) self._send_chan.send_nowait((ev, key, mods, txt))
# never intercept the event # never intercept the event
return False return False
@ -69,7 +66,11 @@ class EventCloner(QtCore.QObject):
async def open_key_stream( async def open_key_stream(
source_widget: QtGui.QWidget, source_widget: QtGui.QWidget,
event_type: QEvent = QEvent.KeyPress, event_types: set[QEvent] = {QEvent.KeyPress},
# TODO: should we offer some kinda option for toggling releases?
# would it require a channel per event type?
# QEvent.KeyRelease,
) -> trio.abc.ReceiveChannel: ) -> trio.abc.ReceiveChannel:
@ -78,10 +79,13 @@ async def open_key_stream(
kc = EventCloner() kc = EventCloner()
kc._send_chan = send kc._send_chan = send
kc._event_types = event_types
source_widget.installEventFilter(kc) source_widget.installEventFilter(kc)
try: try:
yield recv yield recv
finally: finally:
await send.aclose() await send.aclose()
source_widget.removeEventFilter(kc) source_widget.removeEventFilter(kc)

View File

@ -825,7 +825,7 @@ async def handle_keyboard_input(
) )
) )
async for key, mods, txt in recv_chan: async for event, key, mods, txt in recv_chan:
log.debug(f'key: {key}, mods: {mods}, txt: {txt}') log.debug(f'key: {key}, mods: {mods}, txt: {txt}')