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()