Improve file image gestures

This commit is contained in:
2026-11-07 22:01:10 +00:00
parent 6e4e11aca2
commit 5917827bc5
6 changed files with 175 additions and 13 deletions
@@ -49,6 +49,8 @@ class ArtifactViewerBody extends StatelessWidget {
this.htmlBuilder = defaultArtifactHtml,
this.fullscreen = false,
this.onFullscreen,
this.onImageZoomChanged,
this.imageZoomed = false,
});
/// The inert HTML renderer; tests inject a plain stand-in.
@@ -60,6 +62,9 @@ class ArtifactViewerBody extends StatelessWidget {
/// Enters or leaves the full-screen view.
final VoidCallback? onFullscreen;
final ValueChanged<bool>? onImageZoomChanged;
final bool imageZoomed;
/// Loads the current page's preview again after a failure.
final VoidCallback? onRetry;
@@ -97,6 +102,7 @@ class ArtifactViewerBody extends StatelessWidget {
error: error,
onRetry: onRetry,
htmlBuilder: htmlBuilder,
onImageZoomChanged: onImageZoomChanged,
),
),
],
@@ -123,11 +129,13 @@ class ArtifactViewerBody extends StatelessWidget {
final swipe = GestureDetector(
key: const Key('artifact-viewer-swipe'),
behavior: HitTestBehavior.translucent,
onHorizontalDragEnd: (details) {
final velocity = details.primaryVelocity ?? 0;
if (velocity < -250 && hasNext) onNext();
if (velocity > 250 && hasPrevious) onPrevious();
},
onHorizontalDragEnd: !imageZoomed
? (details) {
final velocity = details.primaryVelocity ?? 0;
if (velocity < -250 && hasNext) onNext();
if (velocity > 250 && hasPrevious) onPrevious();
}
: null,
child: content,
);
// An unresolved single artifact has no actions and nothing to page:
@@ -39,11 +39,15 @@ class ArtifactPreviewBody extends StatelessWidget {
this.videoControllerFactory = defaultArtifactVideoController,
this.htmlBuilder = defaultArtifactHtml,
this.onRetry,
this.onImageZoomChanged,
});
/// Offered under a fetch failure; absent, the message stands alone.
final VoidCallback? onRetry;
/// Reports whether the image currently owns one-finger pan gestures.
final ValueChanged<bool>? onImageZoomChanged;
final ArtifactPreviewPlan plan;
final bool loading;
final ArtifactMarkdownBuilder markdownBuilder;
@@ -75,6 +79,8 @@ class ArtifactPreviewBody extends StatelessWidget {
if (data == null) return const _Message('The image is empty.');
// Fitted to the frame; pinch to zoom and pan inside it.
return _ZoomableImage(
key: ValueKey<String>(plan.resolved?.url ?? 'artifact-image'),
onZoomChanged: onImageZoomChanged,
child: Image.memory(
data,
fit: BoxFit.contain,
@@ -164,9 +170,10 @@ class _Scrolling extends StatelessWidget {
/// An image fitted to the frame that can be pinched to zoom; panning only
/// starts once zoomed, so a swipe at normal size still turns the page.
class _ZoomableImage extends StatefulWidget {
const _ZoomableImage({required this.child});
const _ZoomableImage({super.key, required this.child, this.onZoomChanged});
final Widget child;
final ValueChanged<bool>? onZoomChanged;
@override
State<_ZoomableImage> createState() => _ZoomableImageState();
@@ -181,7 +188,10 @@ class _ZoomableImageState extends State<_ZoomableImage> {
super.initState();
_transform.addListener(() {
final zoomed = _transform.value.getMaxScaleOnAxis() > 1.01;
if (zoomed != _zoomed) setState(() => _zoomed = zoomed);
if (zoomed != _zoomed) {
setState(() => _zoomed = zoomed);
widget.onZoomChanged?.call(zoomed);
}
});
}
@@ -192,13 +202,36 @@ class _ZoomableImageState extends State<_ZoomableImage> {
}
@override
Widget build(BuildContext context) => InteractiveViewer(
key: const Key('artifact-image-zoom'),
transformationController: _transform,
panEnabled: _zoomed,
maxScale: 8,
child: SizedBox.expand(child: widget.child),
Widget build(BuildContext context) => Stack(
fit: StackFit.expand,
children: [
InteractiveViewer(
key: const Key('artifact-image-zoom'),
transformationController: _transform,
panEnabled: _zoomed,
maxScale: 8,
// Keep Flutter's native trackpad scroll-to-scale behavior available
// alongside touch pinch and one-finger pan after zooming.
trackpadScrollCausesScale: true,
child: SizedBox.expand(child: widget.child),
),
if (_zoomed)
PositionedDirectional(
top: 8,
end: 8,
child: IconButton(
key: const Key('artifact-image-reset-zoom'),
tooltip: 'Reset image zoom',
onPressed: _reset,
icon: const Icon(Icons.zoom_out_map),
),
),
],
);
void _reset() {
_transform.value = Matrix4.identity();
}
}
/// Preview text with room around it inside its frame.
@@ -97,6 +97,7 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
late int _index = _initialIndex();
int _generation = 0;
bool _loading = false;
bool _imageZoomed = false;
String? _error;
_FetchedPreview? _fetched;
String? _status;
@@ -198,6 +199,7 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
setState(() {
_index = next;
_status = null;
_imageZoomed = false;
});
_beginLoad();
}
@@ -286,6 +288,11 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
onOpen: plan.resolved == null ? () {} : () => _open(plan.resolved!),
onCopy: plan.resolved == null ? () {} : () => _copy(plan.resolved!),
htmlBuilder: widget.htmlBuilder,
imageZoomed: _imageZoomed,
onImageZoomChanged: (zoomed) {
if (_imageZoomed == zoomed || !mounted) return;
setState(() => _imageZoomed = zoomed);
},
fullscreen: fullscreen,
onFullscreen: fullscreen
? () => Navigator.of(context).maybePop()
+8
View File
@@ -0,0 +1,8 @@
# File image gestures
The Files explorer now routes image previews through the shared viewer with
native `InteractiveViewer` pinch/trackpad zoom and one-finger pan after zoom.
While zoomed, the viewer's gallery drag is disabled; the reset control returns
to fit and changing images resets the transform. Focused widget coverage starts
the image through `FilesViewer` and exercises pinch/reset, while the artifact
viewer test also checks pan versus gallery navigation.
@@ -651,6 +651,70 @@ void main() {
expect(find.textContaining('1 of 2'), findsWidgets);
});
testWidgets('pinch owns the image gesture, pans while zoomed, and resets', (
tester,
) async {
final first = recordedArtifact(
'a.png',
url: '/artifacts/1/1/a.png',
contentType: 'image/png',
);
final second = recordedArtifact(
'b.png',
url: '/artifacts/1/2/b.png',
contentType: 'image/png',
);
final groups = [
_group([first], visit: 1),
_group([second], visit: 2),
];
await _pumpViewer(
tester,
groups: groups,
selected: artifactVisitIdentity(groups[0], first),
);
final viewport = tester.getRect(
find.byKey(const Key('artifact-image-zoom')),
);
final center = viewport.center;
final one = await tester.startGesture(
center + const Offset(-30, 0),
pointer: 1,
);
final two = await tester.startGesture(
center + const Offset(30, 0),
pointer: 2,
);
await one.moveTo(center + const Offset(-110, 0));
await two.moveTo(center + const Offset(110, 0));
await tester.pump();
await one.up();
await two.up();
await tester.pumpAndSettle();
expect(find.byKey(const Key('artifact-image-reset-zoom')), findsOneWidget);
// A one-finger drag now belongs to the zoomed image, not the gallery.
await tester.fling(
find.byKey(const Key('artifact-image-zoom')),
const Offset(-300, 0),
1000,
);
await tester.pumpAndSettle();
expect(find.textContaining('1 of 2'), findsWidgets);
await tester.tap(find.byKey(const Key('artifact-image-reset-zoom')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('artifact-image-reset-zoom')), findsNothing);
await tester.fling(
find.byKey(const Key('artifact-viewer-swipe')),
const Offset(-300, 0),
1000,
);
await tester.pumpAndSettle();
expect(find.textContaining('2 of 2'), findsWidgets);
});
testWidgets('Material paging stays inside 320px at 2x text', (tester) async {
tester.view.physicalSize = const Size(640, 1200);
tester.view.devicePixelRatio = 2;
@@ -115,6 +115,48 @@ void main() {
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(find.byType(Image), findsWidgets);
});
testWidgets('image opened from Files owns pinch and pan until reset', (
tester,
) async {
await pump(
tester,
viewer: FilesViewer(
fetch: (_) async => ArtifactFetchResult(
status: 200,
bytes: base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
),
),
),
);
await tester.tap(find.text('picture.png'));
await tester.pumpAndSettle();
final viewport = tester.getRect(
find.byKey(const Key('artifact-image-zoom')),
);
final center = viewport.center;
final one = await tester.startGesture(
center + const Offset(-24, 0),
pointer: 1,
);
final two = await tester.startGesture(
center + const Offset(24, 0),
pointer: 2,
);
await one.moveTo(center + const Offset(-96, 0));
await two.moveTo(center + const Offset(96, 0));
await tester.pump();
await one.up();
await two.up();
await tester.pumpAndSettle();
expect(find.byKey(const Key('artifact-image-reset-zoom')), findsOneWidget);
await tester.tap(find.byKey(const Key('artifact-image-reset-zoom')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('artifact-image-reset-zoom')), findsNothing);
});
testWidgets('unknown file has metadata and a working download', (
tester,
) async {