Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/asynckivy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'anim_with_dt_et_ratio',
'anim_with_et',
'anim_with_ratio',
'block_touch_events',
'event',
'event_freq',
'fade_transition',
Expand Down Expand Up @@ -33,7 +34,8 @@

from asyncgui import *
from ._sleep import sleep, sleep_free, repeat_sleeping, move_on_after, n_frames, sleep_freq
from ._event import event, event_freq, suppress_event, rest_of_touch_events, rest_of_touch_events_cm
from ._event import event, event_freq, suppress_event, rest_of_touch_events, rest_of_touch_events_cm, \
block_touch_events
from ._anim_with_xxx import anim_with_dt, anim_with_et, anim_with_ratio, anim_with_dt_et, anim_with_dt_et_ratio
from ._anim_attrs import anim_attrs, anim_attrs_abbr
from ._interpolate import interpolate, interpolate_seq, fade_transition
Expand Down
45 changes: 44 additions & 1 deletion src/asynckivy/_event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__all__ = ("event", "event_freq", "suppress_event", "rest_of_touch_events", "rest_of_touch_events_cm", )
__all__ = (
"event", "event_freq", "suppress_event", "rest_of_touch_events", "rest_of_touch_events_cm", "block_touch_events",
)

from collections.abc import AsyncIterator
import types
Expand Down Expand Up @@ -168,6 +170,47 @@ def __exit__(self, *args):
self._dispatcher.unbind_uid(self._name, self._bind_uid)


def _is_colliding(w, t):
return w.collide_point(*t.pos)


class block_touch_events:
'''
.. code-block::

with block_touch_events(widget):
...

is equivalent to:

.. code-block::

def f(w, t):
return w.collide_point(*t.pos)
with (
suppress_event(widget, 'on_touch_down', filter=f),
suppress_event(widget, 'on_touch_move', filter=f),
suppress_event(widget, 'on_touch_up', filter=f),
):
...

.. versionadded:: 0.9.1
'''
__slots__ = ('_dispatcher', '_filter', )

def __init__(self, event_dispatcher, *, filter=_is_colliding):
self._dispatcher = event_dispatcher
self._filter = filter

def __enter__(self):
f = self._filter
self._dispatcher.bind(on_touch_down=f, on_touch_move=f, on_touch_up=f)

def __exit__(self, *__):
f = self._filter
self._dispatcher.unbind(on_touch_down=f, on_touch_move=f, on_touch_up=f)


async def rest_of_touch_events(widget, touch, *, stop_dispatching=False, grab=True) -> AsyncIterator[None]:
'''
Returns an async iterator that yields None on each ``on_touch_move`` event
Expand Down