Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a web widget for the activity indicator (shoelace spinner) #2050

Merged
merged 6 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2050.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Activity indicator now has a web widget.
7 changes: 4 additions & 3 deletions core/src/toga/style/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,10 @@ def __css__(self):
# direction
css.append(f"flex-direction: {self.direction.lower()};")
# flex
if (self.width == NONE and self.direction == ROW) or (
self.height == NONE and self.direction == COLUMN
):
if (
(self.width == NONE and self.direction == ROW)
or (self.height == NONE and self.direction == COLUMN)
) and getattr(self, "_use_default_width", True):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity: this approach was developed pairing with @ahter.

The underlying problem is that if the ActivityIndicator has no Toga style, it falls back to rendering as flex: 0 0 0px, which evaluates as 0 width in CSS. If you apply a fixed width of 20px, the animation for the spin and the gray background become misaligned. We need to work out how to:

  1. Apply a style override to the ActivityIndicator so that it doesn't collapse to 0 size, or
  2. Work out how to inject a widget-specific modification to Pack's __css__ implementation to disable the default

It feels to me like both are needed. (1) is needed because if a user applies a "width=100" pack style, we don't want the widget to render weird; (2) is needed to make sure that the default style results in a visible widget.

css.append(f"flex: {self.flex} 0 0;")

# width/flex
Expand Down
2 changes: 2 additions & 0 deletions web/src/toga_web/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# from .images import Image
from .paths import Paths
from .widgets.activityindicator import ActivityIndicator
from .widgets.box import Box
from .widgets.button import Button

Expand Down Expand Up @@ -64,6 +65,7 @@ def not_implemented(feature):
# 'OptionContainer',
# 'PasswordInput',
"ProgressBar",
"ActivityIndicator",
# 'ScrollContainer',
# 'Selection',
# 'Slider',
Expand Down
28 changes: 28 additions & 0 deletions web/src/toga_web/widgets/activityindicator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
A web widget to display an activity indicator.

This is a web implementation of the `toga.ActivityIndicator` widget.

Details can be found in
https://shoelace.style/components/spinner
"""
from .base import Widget


class ActivityIndicator(Widget):
def create(self):
self.interface.style._use_default_width = False
self.native = self._create_native_widget("sl-spinner")
self.stop()

# Actions
def start(self):
self.native.style.visibility = "visible"
self._is_running = True

def stop(self):
self.native.style.visibility = "hidden"
self._is_running = False

def is_running(self):
return self._is_running
Loading