156 lines
4.6 KiB
Dart
156 lines
4.6 KiB
Dart
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.');
|
|
return Center(
|
|
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 _PreviewPadding(
|
|
child: SelectableText(
|
|
text ?? '',
|
|
key: const Key('artifact-text-preview'),
|
|
style: const TextStyle(fontFamily: appMonospaceFamily),
|
|
),
|
|
);
|
|
case ArtifactPreviewKind.markdown:
|
|
return _PreviewPadding(child: markdownBuilder(context, text ?? ''));
|
|
case ArtifactPreviewKind.html:
|
|
return _PreviewPadding(child: htmlBuilder(context, text ?? ''));
|
|
case ArtifactPreviewKind.table:
|
|
return ArtifactTableView(text: text ?? '', delimiter: plan.delimiter);
|
|
case ArtifactPreviewKind.video:
|
|
return 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,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Preview text at its natural height; the viewer body owns the page scroll so
|
|
/// a long document and its actions scroll together instead of nested scrolls.
|
|
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),
|
|
),
|
|
);
|
|
}
|
|
}
|