Skip to content

Commit

Permalink
feat: add spinner widget which doesn't trigger whole screen renders a…
Browse files Browse the repository at this point in the history
…nd uses new local renders of headless-kivy
  • Loading branch information
sassanh committed Oct 25, 2024
1 parent 65aeb61 commit d964ab6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Upcoming

- chore: migrate from poetry to uv for the sake of improving performance and dealing with conflicting sub-dependencies
- feat: add spinner widget which doesn't trigger whole screen renders and uses new local renders of headless-kivy

## Version 0.13.3

Expand Down
48 changes: 48 additions & 0 deletions ubo_gui/spinner/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Spinner widget for loading indication."""

from __future__ import annotations

import math
from typing import Any, cast

from headless_kivy import HeadlessWidget
from kivy.clock import Clock
from kivy.graphics.context_instructions import Rotate
from kivy.properties import NumericProperty
from kivy.uix.label import Label


class SpinnerWidget(HeadlessWidget):
"""Spinner widget for loading indication."""

angle = NumericProperty(0)

def __init__(self: SpinnerWidget, **kwargs: object) -> None:
"""Initialize the SpinnerWidget class."""
super().__init__(**cast(Any, kwargs))
self.interval = Clock.schedule_interval(self.rotate_spinner, 1 / 40)
self.label = Label(
text='',
color=(1, 1, 1),
)
self.label.bind(texture_size=self.label.setter('size'))
self.bind(
size=lambda *_: self.label.setter('center')(
self.label,
(self.width / 2, self.height / 2),
),
)
self.add_widget(self.label)

def __del__(self: SpinnerWidget) -> None:
"""Remove the spinner widget."""
self.interval.cancel()

def rotate_spinner(self: SpinnerWidget, dt: float) -> None:
"""Rotate the spinner."""
self.angle = (self.angle + dt) % math.tau
with self.label.canvas.before:
Rotate(
angle=-dt * 400,
origin=self.label.center,
)

0 comments on commit d964ab6

Please sign in to comment.