From 8de41e45fdf48c6a31c68af4036928ba26bcc8bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natt=C5=8Dsai=20Mit=C5=8D?= Date: Fri, 23 Aug 2024 21:06:43 +0900 Subject: [PATCH] Remove 'examples/painter.py' as it's not good practice to create an orphan task on every touch. --- examples/painter.py | 50 --------------------------------------------- 1 file changed, 50 deletions(-) delete mode 100644 examples/painter.py diff --git a/examples/painter.py b/examples/painter.py deleted file mode 100644 index 86664ec..0000000 --- a/examples/painter.py +++ /dev/null @@ -1,50 +0,0 @@ -''' -Painter -======= - -* can handle multiple touches simultaneously -''' - -from functools import cached_property -from kivy.graphics import Line, Color -from kivy.utils import get_random_color -from kivy.uix.relativelayout import RelativeLayout -from kivy.app import App -import asynckivy as ak - - -class Painter(RelativeLayout): - @cached_property - def _ud_key(self): - return 'Painter.' + str(self.uid) - - def accepts_touch(self, touch) -> bool: - return self.collide_point(*touch.opos) and (not touch.is_mouse_scrolling) and (self._ud_key not in touch.ud) - - def on_touch_down(self, touch): - if self.accepts_touch(touch): - touch.ud[self._ud_key] = True - ak.start(self.draw_rect(touch)) - return True - - async def draw_rect(self, touch): - with self.canvas: - Color(*get_random_color()) - line = Line(width=2) - self_to_local = self.to_local - ox, oy = self_to_local(*touch.opos) - async for __ in ak.rest_of_touch_events(self, touch, stop_dispatching=True): - # Don't await anything during the loop - x, y = self_to_local(*touch.pos) - min_x, max_x = (x, ox) if x < ox else (ox, x) - min_y, max_y = (y, oy) if y < oy else (oy, y) - line.rectangle = (min_x, min_y, max_x - min_x, max_y - min_y, ) - - -class SampleApp(App): - def build(self): - return Painter() - - -if __name__ == "__main__": - SampleApp(title='Painter').run()