From 573bf986c56dc6d0c597325e9c5887b703342a39 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Sat, 19 Apr 2025 16:50:01 +0100 Subject: [PATCH] ui(raylib): calculcate spinner progress in update The spinner logic was refactored to calculate the progress or text ahead of time in the `set_text` method, instead of checking `text.isdigit()` in every render iteration. This change improves performance by avoiding unnecessary checks in the render loop. The lock is used when reading `_progress` and `_text` to avoid race conditions. --- system/ui/spinner.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/system/ui/spinner.py b/system/ui/spinner.py index 951c6f697e..edf1fe5ca9 100755 --- a/system/ui/spinner.py +++ b/system/ui/spinner.py @@ -26,11 +26,17 @@ class Spinner: self._spinner_texture = gui_app.load_texture_from_image(os.path.join(BASEDIR, "selfdrive/assets/img_spinner_track.png"), TEXTURE_SIZE, TEXTURE_SIZE) self._rotation = 0.0 self._text: str = "" + self._progress: int | None = None self._lock = threading.Lock() def set_text(self, text: str) -> None: with self._lock: - self._text = text + if text.isdigit(): + self._progress = clamp(int(text), 0, 100) + self._text = "" + else: + self._progress = None + self._text = text def render(self): center = rl.Vector2(gui_app.width / 2.0, gui_app.height / 2.0) @@ -49,23 +55,21 @@ class Spinner: rl.draw_texture_v(self._comma_texture, comma_position, rl.WHITE) # Display progress bar or text based on user input - text = None + y_pos = rl.get_screen_height() - MARGIN - PROGRESS_BAR_HEIGHT with self._lock: + progress = self._progress text = self._text - if text: - y_pos = rl.get_screen_height() - MARGIN - PROGRESS_BAR_HEIGHT - if text.isdigit(): - progress = clamp(int(text), 0, 100) - bar = rl.Rectangle(center.x - PROGRESS_BAR_WIDTH / 2.0, y_pos, PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGHT) - rl.draw_rectangle_rounded(bar, 1, 10, DARKGRAY) + if progress is not None: + bar = rl.Rectangle(center.x - PROGRESS_BAR_WIDTH / 2.0, y_pos, PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGHT) + rl.draw_rectangle_rounded(bar, 1, 10, DARKGRAY) - bar.width *= progress / 100.0 - rl.draw_rectangle_rounded(bar, 1, 10, rl.WHITE) - else: - text_size = rl.measure_text_ex(gui_app.font(), text, FONT_SIZE, 1.0) - rl.draw_text_ex(gui_app.font(), text, - rl.Vector2(center.x - text_size.x / 2, y_pos), FONT_SIZE, 1.0, rl.WHITE) + bar.width *= progress / 100.0 + rl.draw_rectangle_rounded(bar, 1, 10, rl.WHITE) + elif text: + text_size = rl.measure_text_ex(gui_app.font(), text, FONT_SIZE, 1.0) + rl.draw_text_ex(gui_app.font(), text, + rl.Vector2(center.x - text_size.x / 2, y_pos), FONT_SIZE, 1.0, rl.WHITE) if __name__ == "__main__":