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

Added ActivityIndicator implementation for iOS #2829

Merged
merged 13 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/2829.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ActivityIndicator widget is now supported on iOS.
6 changes: 4 additions & 2 deletions docs/reference/api/widgets/activityindicator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ usually rendered as a "spinner" animation.

Not supported

.. group-tab:: iOS |no|
.. group-tab:: iOS

Not supported
.. figure:: /reference/images/activityindicator-iOS.png
:align: center
:width: 100px

.. group-tab:: Web |beta|

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/data/widgets_by_platform.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Application,Core Component,:class:`~toga.App`,The application itself,|y|,|y|,|y|
Window,Core Component,:class:`~toga.Window`,An operating system-managed container of widgets.,|y|,|y|,|y|,|y|,|y|,|b|,|b|
DocumentWindow,Core Component,:class:`~toga.DocumentWindow`,A window that can be used as the main interface to a document-based app.,|y|,|y|,|y|,,,,
MainWindow,Core Component,:class:`~toga.MainWindow`,A window that can use the full set of window-level user interface elements.,|y|,|y|,|y|,|y|,|y|,|b|,|b|
ActivityIndicator,General Widget,:class:`~toga.ActivityIndicator`,A spinning activity animation,|y|,|y|,,,,|b|,
ActivityIndicator,General Widget,:class:`~toga.ActivityIndicator`,A spinning activity animation,|y|,|y|,,|y|,,|b|,
Button,General Widget,:class:`~toga.Button`,Basic clickable Button,|y|,|y|,|y|,|y|,|y|,|b|,|b|
Canvas,General Widget,:class:`~toga.Canvas`,A drawing area for 2D vector graphics.,|y|,|y|,|y|,|y|,|y|,,
DateInput,General Widget,:class:`~toga.DateInput`,A widget to select a calendar date,,,|y|,,|y|,,
Expand Down
Binary file added docs/reference/images/activityindicator-iOS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion gtk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ root = ".."
dependencies = [
"gbulb >= 0.5.3",
"pycairo >= 1.17.0",
"pygobject >= 3.46.0",
# New asyncio handling introduced in 3.50.0; that code is incompatible
# with gbulb, See #2550 for the code that replaces GBulb with the new
# asyncio code.
"pygobject < 3.50.0",
"toga-core == {version}",
]

Expand Down
2 changes: 2 additions & 0 deletions iOS/src/toga_iOS/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .statusicons import MenuStatusIcon, SimpleStatusIcon, StatusIconSet

# Widgets
from .widgets.activityindicator import ActivityIndicator
from .widgets.box import Box
from .widgets.button import Button
from .widgets.canvas import Canvas
Expand Down Expand Up @@ -47,6 +48,7 @@ def not_implemented(feature):

__all__ = [
"not_implemented",
"ActivityIndicator",
"App",
"Command",
# Resources
Expand Down
4 changes: 4 additions & 0 deletions iOS/src/toga_iOS/libs/uikit.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def NSTextAlignment(alignment):

UIKeyInput = ObjCProtocol("UIKeyInput")

######################################################################
# UIActivityIndicatorView.h
UIActivityIndicatorView = ObjCClass("UIActivityIndicatorView")

######################################################################
# UIAlertController.h
UIAlertController = ObjCClass("UIAlertController")
Expand Down
31 changes: 31 additions & 0 deletions iOS/src/toga_iOS/widgets/activityindicator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from rubicon.objc import CGSize

from toga_iOS.libs import UIActivityIndicatorView
from toga_iOS.widgets.base import Widget


class ActivityIndicator(Widget):
def create(self):
self.native = UIActivityIndicatorView.new()
self.native.hidesWhenStopped = True
self.native.translatesAutoresizingMaskIntoConstraints = False
self.native.sizeToFit()
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason that both the UIView and UIActivatorView is needed? Is it not possible to use UIActivityIndicatorView directly as the native widget? If it isn't, there should be a docstring here describing the reason; if it is... then that's an easy cleanup :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this one I had to wrap it in a parent UIView, because for some reason the UIActivityIndicatorView shows up even when stopped, regardless of whether I set the hidesWhenStopped property to true - I'll add it in the docstrings in case someone figures out what is wrong, but for now this was the only way I could think of that was relatively clean

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@freakboy3742 I added some information about the issue in a comment here: 7d98567

Copy link
Member

Choose a reason for hiding this comment

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

Ok - you successfully nerd sniped me here :-)

I've worked out what is going on. hidesWhenStopped is implemented as a wrapper around the literal setHidden() implementation - so stopping the spinner isn't just making the image disappear, it's literally hiding the widget.

The catch: when Toga starts up, it applies styles to all widgets - and one of the styles it applies is visibility. So - when you don't have the wrapper, Toga creates the widget, sets hiddenWhenStopped, and then sets the widget to be visible... which means you can then see it.

This doesn't happen when the widget is wrapped by the UIView because the hidden attribute of the container view is separate from the hidden attribute of the spinner itself.

So - the fix here is to override set_hidden() on the iOS ActivityIndicator so that it takes into account whether the spinner is running before applying visibility on the underlying widget.

This might have an impact on the testbed test - the probe evaluating visibility might also need to be adjusted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the explanation! I was too stuck at thinking that it may be an issue with the bridge calls that I didn't think to look at the base widget 😅 I'll investigate, will have a look at the potential testbed changes as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've just pushed the changes @freakboy3742 - it seems the testbed didn't require any changes because the activityindicator test does not check for the visibility when stopped. This will need updating, but if it's fine on your end I'd like to do that in a separate change as I might need to implement probes for the other platforms too (gtk and cocoa)

Copy link
Member

Choose a reason for hiding this comment

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

I'm happy for the test improvement to be a separate PR.


self.add_constraints()

def set_hidden(self, hidden):
self.native.setHidden((not self.is_running()) or hidden)

def is_running(self):
return self.native.isAnimating()

def start(self):
self.native.startAnimating()

def stop(self):
self.native.stopAnimating()

def rehint(self):
fitting_size = self.native.systemLayoutSizeFittingSize(CGSize(0, 0))
self.interface.intrinsic.width = fitting_size.width
self.interface.intrinsic.height = fitting_size.height
7 changes: 7 additions & 0 deletions iOS/tests_backend/widgets/activityindicator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from toga_iOS.libs import UIActivityIndicatorView

from .base import SimpleProbe


class ActivityIndicatorProbe(SimpleProbe):
native_class = UIActivityIndicatorView
2 changes: 1 addition & 1 deletion testbed/tests/widgets/test_activityindicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@pytest.fixture
async def widget():
skip_on_platforms("android", "iOS", "windows")
skip_on_platforms("android", "windows")
return toga.ActivityIndicator()


Expand Down