diff --git a/app/lib/features/artifacts/ui/artifact_viewer_body.dart b/app/lib/features/artifacts/ui/artifact_viewer_body.dart index c304ee4e..4c711c98 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_body.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_body.dart @@ -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? 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: diff --git a/app/lib/features/artifacts/ui/artifact_viewer_content.dart b/app/lib/features/artifacts/ui/artifact_viewer_content.dart index 22f5e1e8..9dc3e8bd 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_content.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_content.dart @@ -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? 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(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? 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. diff --git a/app/lib/features/artifacts/ui/artifact_viewer_page.dart b/app/lib/features/artifacts/ui/artifact_viewer_page.dart index f5afe1e0..7d3dc30c 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_page.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_page.dart @@ -97,6 +97,7 @@ class _ArtifactViewerPageState extends State 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 setState(() { _index = next; _status = null; + _imageZoomed = false; }); _beginLoad(); } @@ -286,6 +288,11 @@ class _ArtifactViewerPageState extends State 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() diff --git a/app/notes/file-image-gestures.md b/app/notes/file-image-gestures.md new file mode 100644 index 00000000..b3130cc7 --- /dev/null +++ b/app/notes/file-image-gestures.md @@ -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. diff --git a/app/test/features/artifacts/ui/artifact_viewer_page_test.dart b/app/test/features/artifacts/ui/artifact_viewer_page_test.dart index 4b1d595e..c9352d1b 100644 --- a/app/test/features/artifacts/ui/artifact_viewer_page_test.dart +++ b/app/test/features/artifacts/ui/artifact_viewer_page_test.dart @@ -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; diff --git a/app/test/features/chat/files/files_panel_test.dart b/app/test/features/chat/files/files_panel_test.dart index 8453601f..87a6c4df 100644 --- a/app/test/features/chat/files/files_panel_test.dart +++ b/app/test/features/chat/files/files_panel_test.dart @@ -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 {