From 87eeb92ca01e703319ede7eeb9a9f9f0deb9ce72 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Fri, 26 Jul 2024 18:46:44 +0100 Subject: [PATCH] Button widget: improve lit_color code. --- gui/widgets/buttons.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/gui/widgets/buttons.py b/gui/widgets/buttons.py index 5c318be..fb7f284 100644 --- a/gui/widgets/buttons.py +++ b/gui/widgets/buttons.py @@ -5,7 +5,6 @@ import uasyncio as asyncio from gui.core.ugui import Screen, Widget, display -from gui.primitives.delay_ms import Delay_ms from gui.core.colors import * dolittle = lambda *_: None @@ -46,8 +45,6 @@ class Button(Widget): self.text = text self.callback = callback self.callback_args = args - if self.litcolor is not None: - self.delay = Delay_ms(self.shownormal) def show(self): if self.screen is not Screen.current_screen: @@ -86,21 +83,22 @@ class Button(Widget): self.writer, xc, yc, self.text, self.textcolor, self.bgcolor ) - async def shownormal(self): - # Handle case where screen changed while timer was active: delay repaint - # until screen is current. Pathological app behaviour where another - # control caused a screen change while timer running. - while self.screen is not Screen.current_screen: - await asyncio.sleep_ms(500) + async def shownormal(self): # Revert to normal color after a delay + try: + await asyncio.sleep_ms(Button.lit_time) + except asyncio.CancelledError: # Or prior to a screen change + pass self.bgcolor = self.def_bgcolor self.draw = True # Redisplay def do_sel(self): # Select was pushed self.callback(self, *self.callback_args) # CB takes self as 1st arg. if self.litcolor is not None and self.has_focus(): # CB may have changed focus - self.bgcolor = self.litcolor - self.draw = True # Redisplay - self.delay.trigger(Button.lit_time) + if self.bgcolor != self.litcolor: + self.bgcolor = self.litcolor + self.draw = True # Redisplay + revert = asyncio.create_task(self.shownormal()) + Screen.current_screen.reg_task(revert, True) # Cancel on screen change # Preferred way to close a screen or dialog. Produces an X button at the top RHS.