Files
pipelines/app/lib/features/artifacts/ui/artifact_viewer_content.dart
T

304 lines
9.1 KiB
Dart

import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.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,
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;
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!, onRetry: onRetry);
}
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(
key: ValueKey<String>(plan.resolved?.url ?? 'artifact-image'),
onZoomChanged: onImageZoomChanged,
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.audio:
return Center(
child: ArtifactVideoPlayer(
uri: plan.resolved!.uri,
controllerFactory: videoControllerFactory,
audio: true,
),
);
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({super.key, required this.child, this.onZoomChanged});
final Widget child;
final ValueChanged<bool>? onZoomChanged;
@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);
widget.onZoomChanged?.call(zoomed);
}
});
}
@override
void dispose() {
_transform.dispose();
super.dispose();
}
@override
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: usesCupertinoHost(context)
? Tooltip(
message: 'Reset image zoom',
child: CupertinoButton(
key: const Key('artifact-image-reset-zoom'),
sizeStyle: CupertinoButtonSize.small,
onPressed: _reset,
child: const Icon(
CupertinoIcons.arrow_down_right_arrow_up_left,
),
),
)
: 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.
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,
);
}
}
const artifactPreviewRetryKey = Key('artifact-preview-retry');
class _Message extends StatelessWidget {
const _Message(this.message, {this.onRetry});
final String message;
final VoidCallback? onRetry;
@override
Widget build(BuildContext context) {
final retry = onRetry;
final cupertino = usesCupertinoHost(context);
return Center(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(message, textAlign: TextAlign.center),
if (retry != null) ...[
const SizedBox(height: 12),
cupertino
? CupertinoButton.tinted(
key: artifactPreviewRetryKey,
sizeStyle: CupertinoButtonSize.small,
onPressed: retry,
child: const Text('Retry'),
)
: FilledButton.tonal(
key: artifactPreviewRetryKey,
onPressed: retry,
child: const Text('Retry'),
),
],
],
),
),
);
}
}