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

[a11y] Adding reduce motion logic to LottieAnimation for compose usage #2542

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.ScaleFactor
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.IntSize
import com.airbnb.lottie.AsyncUpdates
import com.airbnb.lottie.LottieComposition
Expand Down Expand Up @@ -101,6 +102,7 @@ fun LottieAnimation(
if (composition == null || composition.duration == 0f) return Box(modifier)

val bounds = composition.bounds
val context = LocalContext.current
Canvas(
modifier = modifier
.lottieSize(bounds.width(), bounds.height())
Expand Down Expand Up @@ -131,7 +133,11 @@ fun LottieAnimation(
drawable.maintainOriginalImageBounds = maintainOriginalImageBounds
drawable.clipToCompositionBounds = clipToCompositionBounds
drawable.clipTextToBoundingBox = clipTextToBoundingBox
drawable.progress = progress()
if (!drawable.animationsEnabled(context) && drawable.markerForAnimationsDisabled != null) {
drawable.progress = drawable.markerForAnimationsDisabled!!.startFrame
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (!drawable.animationsEnabled(context) && drawable.markerForAnimationsDisabled != null) {
drawable.progress = drawable.markerForAnimationsDisabled!!.startFrame
val markerForAnimationsDisabled = drawable.markerForAnimationsDisabled
if (!drawable.animationsEnabled(context) && markerForAnimationsDisabled != null) {
drawable.progress = markerForAnimationsDisabled.startFrame

Let's avoid !! here with a smart cast

} else {
drawable.progress = progress()
}
drawable.setBounds(0, 0, bounds.width(), bounds.height())
drawable.draw(canvas.nativeCanvas, matrix)
}
Expand Down
14 changes: 7 additions & 7 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,15 +823,15 @@ public void playAnimation() {
}

computeRenderMode();
if (animationsEnabled() || getRepeatCount() == 0) {
if (animationsEnabled(getContext()) || getRepeatCount() == 0) {
if (isVisible()) {
animator.playAnimation();
onVisibleAction = OnVisibleAction.NONE;
} else {
onVisibleAction = OnVisibleAction.PLAY;
}
}
if (!animationsEnabled()) {
if (!animationsEnabled(getContext())) {
Marker markerForAnimationsDisabled = getMarkerForAnimationsDisabled();
if (markerForAnimationsDisabled != null) {
setFrame((int) markerForAnimationsDisabled.startFrame);
Expand All @@ -853,7 +853,7 @@ public void playAnimation() {
*
* @return The first non-null marker from the list of allowed reduced motion markers, or null if no such marker is found.
*/
private Marker getMarkerForAnimationsDisabled() {
public Marker getMarkerForAnimationsDisabled() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add a @RestrictTo(Library) on this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Marker marker = null;
for (String markerName : ALLOWED_REDUCED_MOTION_MARKERS) {
marker = composition.getMarker(markerName);
Expand Down Expand Up @@ -885,15 +885,15 @@ public void resumeAnimation() {
}

computeRenderMode();
if (animationsEnabled() || getRepeatCount() == 0) {
if (animationsEnabled(getContext()) || getRepeatCount() == 0) {
if (isVisible()) {
animator.resumeAnimation();
onVisibleAction = OnVisibleAction.NONE;
} else {
onVisibleAction = OnVisibleAction.RESUME;
}
}
if (!animationsEnabled()) {
if (!animationsEnabled(getContext())) {
setFrame((int) (getSpeed() < 0 ? getMinFrame() : getMaxFrame()));
animator.endAnimation();
if (!isVisible()) {
Expand Down Expand Up @@ -1244,12 +1244,12 @@ boolean isAnimatingOrWillAnimateOnVisible() {
}
}

private boolean animationsEnabled() {
public boolean animationsEnabled(Context context) {
if (ignoreSystemAnimationsDisabled) {
return true;
}
return systemAnimationsEnabled &&
L.getReducedMotionOption().getCurrentReducedMotionMode(getContext()) == ReducedMotionMode.STANDARD_MOTION;
L.getReducedMotionOption().getCurrentReducedMotionMode(context) == ReducedMotionMode.STANDARD_MOTION;
}

/**
Expand Down
Loading