spinner: add the ability to redraw after spinning

Every time a spinner "spins", it changes its visual representation. In
order for the user to see a smooth animation, we need to redraw the
spinner after each iteration. Hopefully this is not too much when
multiple spinners are visible at the same time.

For that, we give the option to pass the application to the spinner, so
that we can request a screen redraw after each iteration.

Alternatively, the user can decide not to pass the application to the
spinner, and to manage the animation themselves using calls to .spin()

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2024-03-18 14:25:20 +01:00
parent a81caf7f65
commit 0987dc5a7a
1 changed files with 4 additions and 1 deletions

View File

@ -52,11 +52,12 @@ class Spinner(Text):
event loop is closed (even if the spinner is no longer visible on the
screen)."""
def __init__(self, style="spin", align="center", *, debug=False):
def __init__(self, style="spin", align="center", *, debug=False, app=None):
self.debug = debug
self.spin_index = 0
self.spin_text = styles[style]["texts"]
self.rate = styles[style]["rate"]
self.app = app
super().__init__("", align=align)
self._spin_task = None
@ -65,6 +66,8 @@ class Spinner(Text):
log.debug("spinning spinner %s", id(self))
self.spin_index = (self.spin_index + 1) % len(self.spin_text)
self.set_text(self.spin_text[self.spin_index])
if self.app is not None:
self.app.request_screen_redraw()
async def _spin(self):
while True: