Skip to content

Commit

Permalink
Mirror before scaling in _AnimatedIconPainter (#93312)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P authored Jan 26, 2022
1 parent 3212248 commit 42a8122
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ Denis Grafov <grafov.denis@gmail.com>
TheOneWithTheBraid <the-one@with-the-braid.cf>
Alberto Miola <betoman96@gmail.com>
Twin Sun, LLC <google-contrib@twinsunsolutions.com>
Taskulu LDA <contributions@taskulu.com>
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ class _AnimatedIconPainter extends CustomPainter {
void paint(ui.Canvas canvas, Size size) {
// The RenderCustomPaint render object performs canvas.save before invoking
// this and canvas.restore after, so we don't need to do it here.
canvas.scale(scale, scale);
if (shouldMirror) {
canvas.rotate(math.pi);
canvas.translate(-size.width, -size.height);
}
canvas.scale(scale, scale);

final double clampedProgress = progress.value.clamp(0.0, 1.0);
for (final _PathFrames path in paths)
Expand Down
60 changes: 53 additions & 7 deletions packages/flutter/test/material/animated_icons_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

@Tags(<String>['reduced-test-set'])

import 'dart:math' as math show pi;

import 'package:flutter/material.dart';
Expand All @@ -26,6 +28,7 @@ class MockCanvas extends Fake implements Canvas {
void scale(double sx, [double? sy]) {
capturedSx = sx;
capturedSy = sy!;
invocations.add(RecordedScale(sx, sy));
}

final List<RecordedCanvasCall> invocations = <RecordedCanvasCall>[];
Expand Down Expand Up @@ -75,6 +78,21 @@ class RecordedTranslate extends RecordedCanvasCall {
int get hashCode => hashValues(dx, dy);
}

class RecordedScale extends RecordedCanvasCall {
const RecordedScale(this.sx, this.sy);

final double sx;
final double sy;

@override
bool operator ==(Object other) {
return other is RecordedScale && other.sx == sx && other.sy == sy;
}

@override
int get hashCode => hashValues(sx, sy);
}

void main() {
testWidgets('IconTheme color', (WidgetTester tester) async {
await tester.pumpWidget(
Expand Down Expand Up @@ -218,9 +236,11 @@ void main() {
data: IconThemeData(
color: Color(0xFF666666),
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
child: RepaintBoundary(
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
),
),
),
),
Expand All @@ -231,7 +251,10 @@ void main() {
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedRotate(math.pi),
RecordedTranslate(-48, -48),
RecordedScale(0.5, 0.5),
]);
await expectLater(find.byType(AnimatedIcon),
matchesGoldenFile('animated_icons_test.icon.rtl.png'));
});

testWidgets('Inherited text direction ltr', (WidgetTester tester) async {
Expand All @@ -242,17 +265,23 @@ void main() {
data: IconThemeData(
color: Color(0xFF666666),
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
child: RepaintBoundary(
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
),
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
expect(canvas.invocations, isEmpty);
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedScale(0.5, 0.5),
]);
await expectLater(find.byType(AnimatedIcon),
matchesGoldenFile('animated_icons_test.icon.ltr.png'));
});

testWidgets('Inherited text direction overridden', (WidgetTester tester) async {
Expand All @@ -277,8 +306,25 @@ void main() {
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedRotate(math.pi),
RecordedTranslate(-48, -48),
RecordedScale(0.5, 0.5),
]);
});

testWidgets('Direction has no effect on position of widget', (WidgetTester tester) async {
const AnimatedIcon icon = AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
);
await tester.pumpWidget(
const Directionality(textDirection: TextDirection.rtl, child: icon),
);
final Rect rtlRect = tester.getRect(find.byType(AnimatedIcon));
await tester.pumpWidget(
const Directionality(textDirection: TextDirection.ltr, child: icon),
);
final Rect ltrRect = tester.getRect(find.byType(AnimatedIcon));
expect(rtlRect, ltrRect);
});
}

PaintColorMatcher hasColor(int color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ void main() {
);
painter.paint(mockCanvas, size);
mockCanvas.verifyCallsInOrder(<MockCall>[
MockCall('scale', <dynamic>[1.0, 1.0]),
MockCall('rotate', <dynamic>[math.pi]),
MockCall('translate', <dynamic>[-48.0, -48.0]),
MockCall('scale', <dynamic>[1.0, 1.0]),
MockCall.any('drawPath'),
]);
});
Expand Down

0 comments on commit 42a8122

Please sign in to comment.