feat(app): artifact videos play in the viewer, as the web
This commit is contained in:
@@ -8,7 +8,11 @@ enum ArtifactPreviewKind {
|
||||
text,
|
||||
markdown,
|
||||
|
||||
/// A recorded file the viewer cannot draw (video, CSV, archives, ...).
|
||||
/// A same-origin video, played in the viewer from its resolved url (never
|
||||
/// fetched as bytes, never autoplayed), as the web viewer does.
|
||||
video,
|
||||
|
||||
/// A recorded file the viewer cannot draw (CSV, archives, ...).
|
||||
unsupported,
|
||||
|
||||
/// No safe same-origin url to fetch: absent, unsafe or another origin.
|
||||
@@ -52,9 +56,9 @@ const String artifactPreviewExternalUrl =
|
||||
|
||||
/// Classifies [artifact] against the configured server origin.
|
||||
///
|
||||
/// Images are drawn from the resolved bytes; `text/plain` is selectable text; a
|
||||
/// `.md`/`.markdown` name or `text/markdown` is handed to the injected markdown
|
||||
/// builder. Everything else is explicitly unsupported rather than faked.
|
||||
/// Images are drawn from the resolved bytes; a video plays from the resolved
|
||||
/// url; `text/plain` is selectable text; a `.md`/`.markdown` name or
|
||||
/// `text/markdown` is handed to the injected markdown builder. Everything else is explicitly unsupported rather than faked.
|
||||
ArtifactPreviewPlan planArtifactPreview({
|
||||
required ArtifactRecord artifact,
|
||||
required String configuredEndpoint,
|
||||
@@ -89,6 +93,13 @@ ArtifactPreviewPlan planArtifactPreview({
|
||||
message: '',
|
||||
);
|
||||
}
|
||||
if (isVideoContentType(mime)) {
|
||||
return ArtifactPreviewPlan(
|
||||
kind: ArtifactPreviewKind.video,
|
||||
resolved: resolved,
|
||||
message: '',
|
||||
);
|
||||
}
|
||||
if (mime == 'text/markdown' || _isMarkdownName(artifact.name)) {
|
||||
return ArtifactPreviewPlan(
|
||||
kind: ArtifactPreviewKind.markdown,
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
|
||||
import '../../../ui/adaptive/host.dart';
|
||||
|
||||
/// Creates the player for one video url; tests inject a fake controller.
|
||||
typedef ArtifactVideoControllerFactory =
|
||||
VideoPlayerController Function(Uri uri);
|
||||
|
||||
VideoPlayerController defaultArtifactVideoController(Uri uri) =>
|
||||
VideoPlayerController.networkUrl(uri);
|
||||
|
||||
const String artifactVideoPlayLabel = 'Play video';
|
||||
const String artifactVideoPauseLabel = 'Pause video';
|
||||
const String artifactVideoError = 'The video could not be played.';
|
||||
|
||||
/// Plays one recorded same-origin video in the viewer, as the web viewer's
|
||||
/// `<video controls>`: it never autoplays, and the operator starts it.
|
||||
class ArtifactVideoPlayer extends StatefulWidget {
|
||||
const ArtifactVideoPlayer({
|
||||
super.key,
|
||||
required this.uri,
|
||||
this.controllerFactory = defaultArtifactVideoController,
|
||||
});
|
||||
|
||||
final Uri uri;
|
||||
final ArtifactVideoControllerFactory controllerFactory;
|
||||
|
||||
@override
|
||||
State<ArtifactVideoPlayer> createState() => _ArtifactVideoPlayerState();
|
||||
}
|
||||
|
||||
class _ArtifactVideoPlayerState extends State<ArtifactVideoPlayer> {
|
||||
late VideoPlayerController _controller;
|
||||
bool _failed = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_open();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant ArtifactVideoPlayer oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.uri != widget.uri) {
|
||||
_controller.dispose();
|
||||
_open();
|
||||
}
|
||||
}
|
||||
|
||||
void _open() {
|
||||
_failed = false;
|
||||
final controller = widget.controllerFactory(widget.uri);
|
||||
_controller = controller;
|
||||
controller.initialize().then(
|
||||
(_) {
|
||||
if (mounted && identical(controller, _controller)) setState(() {});
|
||||
},
|
||||
onError: (Object _) {
|
||||
if (mounted && identical(controller, _controller)) {
|
||||
setState(() => _failed = true);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _toggle() {
|
||||
final playing = _controller.value.isPlaying;
|
||||
playing ? _controller.pause() : _controller.play();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_failed) return const Center(child: Text(artifactVideoError));
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
return ValueListenableBuilder<VideoPlayerValue>(
|
||||
valueListenable: _controller,
|
||||
builder: (context, value, _) {
|
||||
if (value.hasError) {
|
||||
return const Center(child: Text(artifactVideoError));
|
||||
}
|
||||
if (!value.isInitialized) {
|
||||
return Center(
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator()
|
||||
: const CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
final label = value.isPlaying
|
||||
? artifactVideoPauseLabel
|
||||
: artifactVideoPlayLabel;
|
||||
final icon = cupertino
|
||||
? (value.isPlaying
|
||||
? CupertinoIcons.pause_fill
|
||||
: CupertinoIcons.play_fill)
|
||||
: (value.isPlaying ? Icons.pause : Icons.play_arrow);
|
||||
return Column(
|
||||
key: const Key('artifact-video'),
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(
|
||||
child: AspectRatio(
|
||||
aspectRatio: value.aspectRatio,
|
||||
child: VideoPlayer(_controller),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
child: Row(
|
||||
children: [
|
||||
cupertino
|
||||
? CupertinoButton(
|
||||
key: const Key('artifact-video-toggle'),
|
||||
onPressed: _toggle,
|
||||
child: Icon(icon, semanticLabel: label),
|
||||
)
|
||||
: IconButton(
|
||||
key: const Key('artifact-video-toggle'),
|
||||
tooltip: label,
|
||||
onPressed: _toggle,
|
||||
icon: Icon(icon),
|
||||
),
|
||||
Expanded(
|
||||
child: VideoProgressIndicator(
|
||||
_controller,
|
||||
allowScrubbing: true,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../domain/artifact_preview.dart';
|
||||
import 'artifact_video_player.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
|
||||
@@ -16,7 +17,8 @@ Widget defaultArtifactMarkdown(BuildContext context, String 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], and an explicit message
|
||||
/// 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 {
|
||||
@@ -28,6 +30,7 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
this.text,
|
||||
this.bytes,
|
||||
this.error,
|
||||
this.videoControllerFactory = defaultArtifactVideoController,
|
||||
});
|
||||
|
||||
final ArtifactPreviewPlan plan;
|
||||
@@ -37,6 +40,9 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
final Uint8List? bytes;
|
||||
final String? error;
|
||||
|
||||
/// The video player's controller source; tests inject a fake.
|
||||
final ArtifactVideoControllerFactory videoControllerFactory;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (plan.kind == ArtifactPreviewKind.unavailable ||
|
||||
@@ -65,6 +71,11 @@ class ArtifactPreviewBody extends StatelessWidget {
|
||||
return _PreviewPadding(child: SelectableText(text ?? ''));
|
||||
case ArtifactPreviewKind.markdown:
|
||||
return _PreviewPadding(child: markdownBuilder(context, text ?? ''));
|
||||
case ArtifactPreviewKind.video:
|
||||
return ArtifactVideoPlayer(
|
||||
uri: plan.resolved!.uri,
|
||||
controllerFactory: videoControllerFactory,
|
||||
);
|
||||
case ArtifactPreviewKind.unsupported:
|
||||
case ArtifactPreviewKind.unavailable:
|
||||
return const SizedBox.shrink();
|
||||
|
||||
Reference in New Issue
Block a user