Skip to content

Commit

Permalink
feat: add progress to ItemWidget, reflect it in its look and optimi…
Browse files Browse the repository at this point in the history
…ze its rendering for rapid re-renders
  • Loading branch information
sassanh committed Jul 16, 2024
1 parent 9fcf4fe commit 287ce87
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 39 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 @@
## Version 0.11.18

- refactor: just change the items of the menu when items are changed, instead of creating a whole new menu widget
- feat: add progress to `ItemWidget`, reflect it in its look and optimize its rendering for rapid re-renders

## Version 0.11.17

Expand Down
40 changes: 20 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ optional = true
[tool.poetry.group.dev.dependencies]
poethepoet = "^0.24.4"
pyright = "^1.1.371"
ruff = "^0.5.1"
ruff = "^0.5.2"

[build-system]
requires = ["poetry-core"]
Expand Down
1 change: 1 addition & 0 deletions ubo_gui/menu/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class Item(Immutable):
icon: str | None | Callable[[], str | None] = None
is_short: bool | Callable[[], bool] = False
opacity: float | None = None
progress: float | None = None


class ActionItem(Item):
Expand Down
14 changes: 12 additions & 2 deletions ubo_gui/menu/widgets/item_widget.kv
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

<ItemWidget>:
orientation: 'horizontal'
_width: dp(UBO_GUI_SHORT_WIDTH) if self.is_short else self.width - dp(6)
_progress: 0 if self.progress is None else int(self._width * self.progress)
height: dp(UBO_GUI_MENU_ITEM_HEIGHT)
opacity: root.opacity if root.is_set else 0

canvas.before:
Color:
rgba: root.background_color if root.is_set else (0, 0, 0, 0)
rgb: root.background_color[0] + (-0.2 if root.background_color[0] > 0.5 else 0.2), root.background_color[1] + (-0.2 if root.background_color[1] > 0.5 else 0.2), root.background_color[2] + (-0.2 if root.background_color[2] > 0.5 else 0.2)

RoundedRectangle:
pos: self.pos
size: (dp(UBO_GUI_SHORT_WIDTH) if self.is_short else self.width - dp(6), self.height) if root.is_set else (0, 0)
size: (self._width, self.height) if root.is_set else (0, 0)
radius: (0, 0), (dp(26), dp(26)), (dp(26), dp(26)), (0, 0)

Color:
rgba: root.background_color if root.is_set else (0, 0, 0, 0)

RoundedRectangle:
pos: self.pos[0], self.pos[1]
size: self._progress, self.height
radius: (0, 0), (dp(26), dp(26)), (dp(26), dp(26)), (0, 0)

orientation: 'horizontal'
Expand Down
37 changes: 21 additions & 16 deletions ubo_gui/menu/widgets/item_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class ItemWidget(BoxLayout):
is_short: bool = BooleanProperty(defaultvalue=False)
item: Item | None = ObjectProperty(allownone=True)
opacity: float = NumericProperty(default=1, min=0, max=1)
progress: float = NumericProperty(default=1, min=0, max=1)

_width: float = NumericProperty()
_progress: int = NumericProperty()

def __init__(self: ItemWidget, item: Item | None = None, **kwargs: Any) -> None: # noqa: ANN401
"""Initialize an `ItemWidget`."""
Expand All @@ -63,66 +67,67 @@ def __del__(self: ItemWidget) -> None:
"""Unsubscribe from the item."""
self.clear_subscriptions()

def on_item(self: ItemWidget, instance: ItemWidget, value: Item | None) -> None:
def on_item(self: ItemWidget, _: ItemWidget, value: Item | None) -> None:
"""Update the widget properties when the item changes."""
self.clear_subscriptions()
if value is not None:
instance.is_set = True
instance.label = ''
if value is None:
self.is_set = False
else:
self.is_set = True
self.label = ''
self._subscriptions.append(
process_subscribable_value(
value.label,
lambda value: setattr(instance, 'label', value or ''),
lambda value: setattr(self, 'label', value or ''),
),
)

instance.is_short = False
self.is_short = False
self._subscriptions.append(
process_subscribable_value(
value.is_short,
lambda value: setattr(
instance,
self,
'is_short',
False if value is None else value,
),
),
)

instance.color = ItemWidget.color.defaultvalue
self.color = ItemWidget.color.defaultvalue
self._subscriptions.append(
process_subscribable_value(
value.color,
lambda value: setattr(
instance,
self,
'color',
value or ItemWidget.color.defaultvalue,
),
),
)

instance.background_color = ItemWidget.background_color.defaultvalue
self.background_color = ItemWidget.background_color.defaultvalue
self._subscriptions.append(
process_subscribable_value(
value.background_color,
lambda value: setattr(
instance,
self,
'background_color',
value or ItemWidget.background_color.defaultvalue,
),
),
)

instance.icon = ''
self.icon = ''
self._subscriptions.append(
process_subscribable_value(
value.icon,
lambda value: setattr(instance, 'icon', value or ''),
lambda value: setattr(self, 'icon', value or ''),
),
)

instance.opacity = value.opacity or 1
else:
instance.is_set = False
self.opacity = value.opacity or 1
self.progress = min(max(value.progress or 1, 0), 1)

def clear_subscriptions(self: ItemWidget) -> None:
"""Clear the subscriptions."""
Expand Down

0 comments on commit 287ce87

Please sign in to comment.