From 22aa76756b0118305135f461fc1a148d39f31640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natt=C5=8Dsai=20Mit=C5=8D?= Date: Sat, 20 Sep 2025 19:44:24 +0900 Subject: [PATCH] Add `block_touch_events` --- src/asynckivy/__init__.py | 4 +++- src/asynckivy/_event.py | 45 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/asynckivy/__init__.py b/src/asynckivy/__init__.py index 78f4b94..8b382e0 100644 --- a/src/asynckivy/__init__.py +++ b/src/asynckivy/__init__.py @@ -6,6 +6,7 @@ 'anim_with_dt_et_ratio', 'anim_with_et', 'anim_with_ratio', + 'block_touch_events', 'event', 'event_freq', 'fade_transition', @@ -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 diff --git a/src/asynckivy/_event.py b/src/asynckivy/_event.py index 3207258..f90f0d5 100644 --- a/src/asynckivy/_event.py +++ b/src/asynckivy/_event.py @@ -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 @@ -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