Support opening a handler on a collection of widgets

ordermodepps_backup
Tyler Goodlet 2021-07-25 15:42:48 -04:00
parent 3302d21086
commit fd73d1eef1
3 changed files with 15 additions and 9 deletions

View File

@ -1689,8 +1689,8 @@ async def _async_main(
# start handling search bar kb inputs
async with (
_event.open_handler(
search.bar,
_event.open_handlers(
[search.bar],
event_types={QEvent.KeyPress},
async_handler=_search.handle_keyboard_input,
# let key repeats pass through for search

View File

@ -18,7 +18,7 @@
Qt event proxying and processing using ``trio`` mem chans.
"""
from contextlib import asynccontextmanager
from contextlib import asynccontextmanager, AsyncExitStack
from typing import Callable
from PyQt5 import QtCore
@ -124,9 +124,9 @@ async def open_event_stream(
@asynccontextmanager
async def open_handler(
async def open_handlers(
source_widget: QWidget,
source_widgets: list[QWidget],
event_types: set[QEvent],
async_handler: Callable[[QWidget, trio.abc.ReceiveChannel], None],
**kwargs,
@ -135,7 +135,13 @@ async def open_handler(
async with (
trio.open_nursery() as n,
open_event_stream(source_widget, event_types, **kwargs) as event_recv_stream,
AsyncExitStack() as stack,
):
n.start_soon(async_handler, source_widget, event_recv_stream)
for widget in source_widgets:
event_recv_stream = await stack.enter_async_context(
open_event_stream(widget, event_types, **kwargs)
)
n.start_soon(async_handler, widget, event_recv_stream)
yield

View File

@ -273,8 +273,8 @@ class ChartView(ViewBox):
) -> 'ChartView':
from . import _event
async with _event.open_handler(
self,
async with _event.open_handlers(
[self],
event_types={QEvent.KeyPress, QEvent.KeyRelease},
async_handler=handle_viewmode_inputs,
):