Skip to content

Instantly share code, notes, and snippets.

@pjz
Last active April 1, 2023 15:23
Show Gist options
  • Save pjz/66ce2f50a332a7c1be60585f0c9e9900 to your computer and use it in GitHub Desktop.
Save pjz/66ce2f50a332a7c1be60585f0c9e9900 to your computer and use it in GitHub Desktop.
LongShortTouchMixin
from kivy.clock import Clock
from kivymd.uix.widget import MDWidget as Widget
LONG_TOUCH_TIME = 0.8
class LongShortTouchMixin(Widget):
def __init__(self, **kw):
self.last_touch = None
self._touch_clock = None
super().__init__(**kw)
def on_touch_down(self, touch):
if not self.collide_point(touch.x, touch.y):
return False
self._touch_callback = self.on_short_touch
self._touch_clock = Clock.schedule_once(self.set_long_touch, LONG_TOUCH_TIME)
return True
def set_long_touch(self, dt):
self._touch_callback = self.on_long_touch
def on_touch_up(self, touch):
if touch == self.last_touch:
return True
if not self.collide_point(touch.x, touch.y):
return False
if not self._touch_clock:
return self.on_short_touch(touch)
self.last_touch = touch
self._touch_clock.cancel()
return self._touch_callback(touch)
def on_short_touch(self, touch):
print('mixin short touch on %r' % self)
return True
def on_long_touch(self, touch):
print('mixin long touch on %r' % self)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment