fix(preview): keep source navigation and canvas interactions responsive

This commit is contained in:
lingjhf committed 2026-10-09 19:23:49 +08:00
1 parent 107e93b959
commit 77dbb1897f
4 files changed
+685 -22

No files matched your search

+227 -14
View File
@@ -12,6 +12,195 @@ import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('mask painting follows geometry and skips unchanged ticks', (
tester,
) async {
final preview = _MaskPreview();
addTearDown(preview.dispose);
Widget host({
double pixelRatio = 1,
Rect rect = const Rect.fromLTWH(0, 0, 500, 300),
}) => MaterialApp(
home: MediaQuery(
data: MediaQueryData(devicePixelRatio: pixelRatio),
child: MaskInteractionOverlay(controller: preview, canvasRect: rect),
),
);
await tester.pumpWidget(host());
final paint = find.descendant(
of: find.byType(MaskInteractionOverlay),
matching: find.byType(CustomPaint),
);
CustomPainter current() => tester.widget<CustomPaint>(paint).painter!;
var previous = current();
for (var i = 0; i < 100; i++) {
preview.notifyListeners();
await tester.pump();
expect(current().shouldRepaint(previous), isFalse);
previous = current();
}
for (final mask in [
preview.mask.copyWith(x: .6),
preview.mask.copyWith(width: .7),
preview.mask.copyWith(rotation: 30),
preview.mask.copyWith(shape: ClipMaskShape.ellipse),
]) {
preview.mask = mask;
preview.notifyListeners();
await tester.pump();
expect(current().shouldRepaint(previous), isTrue);
previous = current();
}
preview.value = const ClipTransform(x: .1);
preview.notifyListeners();
await tester.pump();
expect(current().shouldRepaint(previous), isTrue);
previous = current();
await tester.pumpWidget(host(pixelRatio: 2));
expect(current().shouldRepaint(previous), isTrue);
previous = current();
await tester.pumpWidget(
host(pixelRatio: 2, rect: const Rect.fromLTWH(20, 10, 250, 150)),
);
expect(current().shouldRepaint(previous), isTrue);
previous = current();
await tester.binding.handleSystemMessage({'type': 'fontsChange'});
await tester.pump();
expect(current().shouldRepaint(previous), isTrue);
await tester.pumpWidget(const SizedBox());
});
testWidgets('empty selection skips geometry on playback ticks', (
tester,
) async {
final preview = _Preview();
addTearDown(preview.dispose);
await tester.pumpWidget(
MaterialApp(
home: PreviewInteractionOverlay(
controller: preview,
canvasRect: const Rect.fromLTWH(0, 0, 500, 300),
),
),
);
for (var i = 0; i < 100; i++) {
preview.notifyListeners();
await tester.pump();
}
expect(preview.visualQueries, 0);
preview.selectVisual(ElementId('upper'));
await tester.pump();
expect(preview.visualQueries, greaterThan(0));
await tester.pumpWidget(const SizedBox());
});
testWidgets('selection and controller changes update the decoration', (
tester,
) async {
final first = _Preview(lowerOffset: const Offset(-100, 0))
..selectVisual(ElementId('upper'));
final second = _Preview(size: const Size(100, 100))
..selectVisual(ElementId('upper'));
addTearDown(first.dispose);
addTearDown(second.dispose);
Widget host(_Preview preview) => MaterialApp(
home: Scaffold(
body: SizedBox(
width: 600,
height: 400,
child: PreviewInteractionOverlay(
controller: preview,
canvasRect: const Rect.fromLTWH(40, 40, 500, 300),
),
),
),
);
final selection = find.descendant(
of: find.byType(PreviewInteractionOverlay),
matching: find.byType(CustomPaint),
);
await tester.pumpWidget(host(first));
final upper = tester.widget<CustomPaint>(selection).painter!;
first.selectVisual(ElementId('lower'));
await tester.pump();
final lower = tester.widget<CustomPaint>(selection).painter!;
expect(lower.shouldRepaint(upper), isTrue);
first.selectVisual(null);
await tester.pump();
expect(
tester.widget<CustomPaint>(selection).painter!.shouldRepaint(lower),
isTrue,
);
await tester.pumpWidget(host(second));
final replacement = tester.widget<CustomPaint>(selection);
first.selectVisual(ElementId('upper'));
first.transforms.presentation.value++;
await tester.pump();
expect(tester.widget<CustomPaint>(selection), same(replacement));
second.value = const ClipTransform(x: .1);
second.notifyListeners();
await tester.pump();
expect(
tester
.widget<CustomPaint>(selection)
.painter!
.shouldRepaint(replacement.painter!),
isTrue,
);
await tester.pumpWidget(const SizedBox());
first.notifyListeners();
second.notifyListeners();
expect(tester.takeException(), isNull);
});
testWidgets('selection follows scene edits without a parent rebuild', (
tester,
) async {
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetDevicePixelRatio);
final preview = _Preview()..selectVisual(ElementId('upper'));
addTearDown(preview.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SizedBox(
width: 600,
height: 400,
child: PreviewInteractionOverlay(
controller: preview,
canvasRect: const Rect.fromLTWH(40, 40, 500, 300),
),
),
),
),
);
final selection = find.descendant(
of: find.byType(PreviewInteractionOverlay),
matching: find.byType(CustomPaint),
);
final before = tester.widget<CustomPaint>(selection).painter!;
preview.value = const ClipTransform(x: -.35);
preview.notifyListeners();
await tester.pump();
final after = tester.widget<CustomPaint>(selection).painter!;
expect(after.shouldRepaint(before), isTrue);
expect(
tester.renderObject<RenderCustomPaint>(selection),
paints..rect(rect: const Rect.fromLTRB(36, 136, 44, 144)),
);
preview.value = const ClipTransform();
preview.notifyListeners();
await tester.pump();
expect(
tester.widget<CustomPaint>(selection).painter!.shouldRepaint(after),
isTrue,
);
expect(preview.commits, 0);
expect(preview.pauses, 0);
await tester.pumpWidget(const SizedBox());
});
Future<void> mount(
WidgetTester tester,
_Preview preview, {
@@ -905,11 +1094,25 @@ void main() {
});
}
class _MaskPreview extends _Preview {
@override
bool get canPlay => true;
ClipMask mask = const ClipMask();
@override
ElementId? get maskEditingElement => ElementId('upper');
@override
ClipMask? maskFor(ElementId id) => mask;
}
class _Preview extends ChangeNotifier implements PreviewController {
@override
Listenable get positionChanges => this;
@override
bool aspectRatioLocked = true;
_Preview({this.size = const Size(300, 200)});
_Preview({this.size = const Size(300, 200), this.lowerOffset = Offset.zero});
final Size size;
final Offset lowerOffset;
@override
final canvas = const ProjectCanvas(1000, 600);
ClipTransform value = const ClipTransform();
@@ -960,20 +1163,30 @@ class _Preview extends ChangeNotifier implements PreviewController {
bool transforming = false;
@override
Set<ElementId> selectedVisualIds = {};
int visualQueries = 0;
@override
List<PreviewVisual> get visuals => transforms.project([
for (final (id, lane) in [('lower', 0), ('upper', 1)])
PreviewVisual(
layerId: id,
elementId: ElementId(id),
lane: lane,
transform: value,
geometry: PreviewGeometry(
center: const Offset(500, 300),
size: size,
).changed(const ClipTransform(), value, const Size(1000, 600)),
),
], const Size(1000, 600));
List<PreviewVisual> visualsForSelection(Set<ElementId> elements) =>
visuals.where((v) => elements.contains(v.elementId)).toList();
@override
List<PreviewVisual> get visuals {
visualQueries++;
return transforms.project([
for (final (id, lane) in [('lower', 0), ('upper', 1)])
PreviewVisual(
layerId: id,
elementId: ElementId(id),
lane: lane,
transform: value,
geometry: PreviewGeometry(
center:
const Offset(500, 300) +
(id == 'lower' ? lowerOffset : Offset.zero),
size: size,
).changed(const ClipTransform(), value, const Size(1000, 600)),
),
], const Size(1000, 600));
}
@override
void selectVisual(ElementId? id) {
selectedVisualIds = {?id};