Refactor animated Text into component

This commit is contained in:
David Rodenkirchen
2024-08-27 15:27:46 +02:00
parent 81ec29cda1
commit 259786a1d3
4 changed files with 69 additions and 130 deletions
@@ -0,0 +1,38 @@
from asyncio import sleep
from rio import Text, Component, TextStyle
class AnimatedText(Component):
def __post_init__(self) -> None:
self._display_printing: list[bool] = [False]
self.text_comp = Text("")
async def display_text(self, success: bool, text: str, speed: float = 0.08) -> None:
if self._display_printing[0]:
return
else:
self._display_printing[0] = True
self.text_comp.text = ""
if success:
self.text_comp.style = TextStyle(
fill=self.session.theme.success_color,
font_size=0.9
)
for c in text:
self.text_comp.text = self.text_comp.text + c
await self.text_comp.force_refresh()
await sleep(speed)
else:
self.text_comp.style = TextStyle(
fill=self.session.theme.danger_color,
font_size=0.9
)
for c in text:
self.text_comp.text = self.text_comp.text + c
await self.text_comp.force_refresh()
await sleep(speed)
self._display_printing[0] = False
def build(self) -> Component:
return self.text_comp