import 'dart:typed_data'; import 'package:flutter/material.dart'; import '../domain/artifact_preview.dart'; import 'artifact_html_view.dart'; import 'artifact_table_view.dart'; import 'artifact_video_player.dart'; import '../../../ui/text/monospace.dart'; /// Renders fetched markdown text for the viewer. It takes the build context so /// a themed markdown renderer can be supplied; the viewer's own default stays /// plain selectable text for a standalone preview. typedef ArtifactMarkdownBuilder = Widget Function(BuildContext context, String text); Widget defaultArtifactMarkdown(BuildContext context, String text) => SelectableText(text); /// The viewer's preview surface for one page. /// /// It draws only what was actually fetched: image bytes as an [Image], text as /// selectable text, markdown through [markdownBuilder], a same-origin video /// through [ArtifactVideoPlayer] (streamed, never autoplayed), and an explicit message /// for unsupported/unavailable pages. It never substitutes a placeholder that /// looks like a preview. class ArtifactPreviewBody extends StatelessWidget { const ArtifactPreviewBody({ super.key, required this.plan, required this.loading, required this.markdownBuilder, this.text, this.bytes, this.error, this.videoControllerFactory = defaultArtifactVideoController, this.htmlBuilder = defaultArtifactHtml, }); final ArtifactPreviewPlan plan; final bool loading; final ArtifactMarkdownBuilder markdownBuilder; final String? text; final Uint8List? bytes; final String? error; /// The video player's controller source; tests inject a fake. final ArtifactVideoControllerFactory videoControllerFactory; /// The inert HTML renderer; tests inject a plain stand-in. final ArtifactHtmlBuilder htmlBuilder; @override Widget build(BuildContext context) { if (plan.kind == ArtifactPreviewKind.unavailable || plan.kind == ArtifactPreviewKind.unsupported) { return _Message(plan.message); } if (loading) { return const _Loading(); } if (error != null) { return _Message(error!); } switch (plan.kind) { case ArtifactPreviewKind.image: final data = bytes; if (data == null) return const _Message('The image is empty.'); // Fitted to the frame; pinch to zoom and pan inside it. return _ZoomableImage( child: Image.memory( data, fit: BoxFit.contain, errorBuilder: (_, _, _) => const _Message('The image could not be decoded.'), ), ); case ArtifactPreviewKind.text: // Logs, JSON and source keep their columns: a monospace body. return _Scrolling( child: _PreviewPadding( child: SelectableText( text ?? '', key: const Key('artifact-text-preview'), style: const TextStyle(fontFamily: appMonospaceFamily), ), ), ); case ArtifactPreviewKind.markdown: return _Scrolling( child: _PreviewPadding(child: markdownBuilder(context, text ?? '')), ); case ArtifactPreviewKind.html: // The document fills the frame and scrolls itself. return htmlBuilder(context, text ?? ''); case ArtifactPreviewKind.table: return _Scrolling( child: ArtifactTableView(text: text ?? '', delimiter: plan.delimiter), ); case ArtifactPreviewKind.video: return Center( child: ArtifactVideoPlayer( uri: plan.resolved!.uri, controllerFactory: videoControllerFactory, ), ); case ArtifactPreviewKind.unsupported: case ArtifactPreviewKind.unavailable: return const SizedBox.shrink(); } } } class _Loading extends StatelessWidget { const _Loading(); @override Widget build(BuildContext context) { return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const CircularProgressIndicator(), const SizedBox(height: 12), Text( 'Loading preview…', style: Theme.of(context).textTheme.bodyMedium, ), ], ), ); } } /// A long document scrolls inside the preview frame; the page itself stays /// still, with its controls at the edges. class _Scrolling extends StatelessWidget { const _Scrolling({required this.child}); final Widget child; @override Widget build(BuildContext context) => SingleChildScrollView( key: const Key('artifact-preview-scroll'), child: child, ); } /// 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}); final Widget child; @override State<_ZoomableImage> createState() => _ZoomableImageState(); } class _ZoomableImageState extends State<_ZoomableImage> { final TransformationController _transform = TransformationController(); bool _zoomed = false; @override void initState() { super.initState(); _transform.addListener(() { final zoomed = _transform.value.getMaxScaleOnAxis() > 1.01; if (zoomed != _zoomed) setState(() => _zoomed = zoomed); }); } @override void dispose() { _transform.dispose(); super.dispose(); } @override Widget build(BuildContext context) => InteractiveViewer( key: const Key('artifact-image-zoom'), transformationController: _transform, panEnabled: _zoomed, maxScale: 8, child: SizedBox.expand(child: widget.child), ); } /// Preview text with room around it inside its frame. class _PreviewPadding extends StatelessWidget { const _PreviewPadding({required this.child}); final Widget child; @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 8, 16, 16), child: child, ); } } class _Message extends StatelessWidget { const _Message(this.message); final String message; @override Widget build(BuildContext context) { return Center( child: Padding( padding: const EdgeInsets.all(16), child: Text(message, textAlign: TextAlign.center), ), ); } }