async def _async_main(self): from asynckivy import or_, animate, event while True: if not self.disabled: await or_(event(self, 'disabled'), self._watch_touch_events()) await animate(self, opacity=.5, d=.2) if self.disabled: await event(self, 'disabled') await animate(self, opacity=1, d=.2)
async def show_yes_no_dialog(*, text_main='', font_name=DEFAULT_FONT, text_yes='Yes', text_no='No', _cache=[]): import asynckivy as ak try: dialog = _cache.pop() except IndexError: dialog = F.KPYesNoDialog() main_label = dialog.ids.main.__self__ yes_button = dialog.ids.yes.__self__ no_button = dialog.ids.no.__self__ main_label.text = text_main main_label.font_name = font_name yes_button.text = text_yes yes_button.font_name = font_name no_button.text = text_no no_button.font_name = font_name try: dialog.open() tasks = await ak.or_( ak.event(yes_button, 'on_release'), ak.event(no_button, 'on_release'), ak.event(dialog, 'on_dismiss'), ) if tasks[0].done: return 'yes' elif tasks[1].done: return 'no' else: return 'cancelled' finally: dialog.dismiss() _cache.append(dialog)
async def animate(root): label = root.ids.label.__self__ while True: await ak.or_( ak.event(root, 'on_touch_down'), ak.animate(label, right=root.width), ) label.right = root.width await ak.or_( ak.event(root, 'on_touch_down'), ak.animate(label, top=root.height), ) label.top = root.height await ak.or_( ak.event(root, 'on_touch_down'), ak.animate(label, x=0), ) label.x = 0 await ak.or_( ak.event(root, 'on_touch_down'), ak.animate(label, y=0), ) label.y = 0
async def some_task(): label = self.root.ids.label button = self.root.ids.button label.text = '--' button.text = 'start spinning' await ak.event(button, 'on_press') button.text = 'stop' tasks = await ak.or_( ak.event(button, 'on_press'), spinning(label), ) tasks[1].coro.close() # 'tasks[1].cancel()' is preferable self.root.remove_widget(button) label.text = 'fin.'
async def test_await_multiple_coroutines(): # ----------------------------------------- # wait for the completion of one coroutine # ----------------------------------------- label.text = "wait until button'A' is pressed or 5s passes" tasks = await ak.or_( ak.event(buttons[0], 'on_press'), ak.sleep(5), ) label.text = 'Done! ({})'.format( "button'A' was pressed" if tasks[0].done else "5s passed") await ak.sleep(1) label.text = 'next' await ak.sleep(1) # ------------------------------------------ # wait for the completion of all coroutines # ------------------------------------------ label.text = "wait until all buttons are pressed and 5s passes" tasks = await ak.and_( ak.sleep(5), *(ak.event(button, 'on_press') for button in buttons)) label.text = 'Done!' await ak.sleep(2)
def on_touch_move(self, touch): ud_key = self.__ud_key touch_ud = touch.ud if ud_key not in touch_ud and self.collide_point(*touch.pos): drag_cls = touch_ud.get('kivyx_drag_cls', None) if drag_cls is not None: touch_ud[ud_key] = None if drag_cls in self.drag_classes: # Watches 'is_being_dragged' as well, so that the # '_watch_touch()' will be automatically cancelled when # the drag is cancelled. ak.start(ak.or_( self._watch_touch(touch), ak.event( touch.ud['kivyx_draggable'], 'is_being_dragged'), )) return super().on_touch_move(touch)
async def rest_of_touch_moves(widget, touch, *, stop_dispatching=False, timeout=1.): ''' Returns an async-generator that iterates the number of times `on_touch_move` is fired, and ends when `on_touch_up` is fired. Grabs and ungrabs the touch automatically. If `stop_dispatching` is True, the touch will never be dispatched further i.e. the next widget will never get this touch until the generator ends. If `on_touch_up` was already fired, `MotionEventAlreadyEndedError` will be raised. ''' if touch.time_end != -1: # `on_touch_up` might have been already fired so we need to find out # it actually was or not. tasks = await or_( sleep(timeout), event(widget, 'on_touch_up', filter=lambda w, t: t is touch), ) if tasks[0].done: raise MotionEventAlreadyEndedError( f"MotionEvent(uid={touch.uid}) has already ended") else: return step_coro = await get_step_coro() on_touch_up, on_touch_move = _callbacks[not stop_dispatching] on_touch_up = partial(on_touch_up, step_coro, touch) on_touch_move = partial(on_touch_move, step_coro, touch) touch.grab(widget) uid_up = widget.fbind('on_touch_up', on_touch_up) uid_move = widget.fbind('on_touch_move', on_touch_move) assert uid_up assert uid_move # LOAD_FAST true_if_touch_move_false_if_touch_up = _true_if_touch_move_false_if_touch_up try: while await true_if_touch_move_false_if_touch_up(): yield touch finally: touch.ungrab(widget) widget.unbind_uid('on_touch_up', uid_up) widget.unbind_uid('on_touch_move', uid_move)
async def _watch_touch(self, touch): await ak.or_( self._watch_touch_movement(touch), ak.event(touch.ud['kivyx_draggable'], 'is_being_dragged'), )