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

401 lines
12 KiB
Dart

import 'dart:async';
import '../../../ui/navigation/chevron_pager.dart';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../actions/artifact_file_actions.dart';
import '../actions/artifact_file_bridge.dart';
import '../domain/artifact_preview.dart';
import '../domain/artifact_url.dart';
import '../domain/gallery_navigation.dart';
import 'artifact_selection_facts.dart';
import 'artifact_viewer_content.dart';
import 'artifact_viewer_fetch.dart';
/// One viewer page: the recorded facts, position, live status, preview surface,
/// file transfer actions, external actions and paging controls.
///
/// It is stateless so the page owns loading, fetch generations and focus; this
/// surface only draws the current page and reports the operator's intent. The
/// page content scrolls as one column at large text sizes; paging stays fixed.
class ArtifactViewerBody extends StatelessWidget {
const ArtifactViewerBody({
super.key,
required this.page,
required this.plan,
required this.index,
required this.count,
required this.loading,
required this.markdownBuilder,
required this.onPrevious,
required this.onNext,
required this.onOpen,
required this.onCopy,
this.fetch = fetchArtifact,
this.fileBridge = const ArtifactFileBridge(),
this.text,
this.bytes,
this.error,
this.status,
});
final PagedArtifact page;
final ArtifactPreviewPlan plan;
final int index;
final int count;
final bool loading;
final ArtifactMarkdownBuilder markdownBuilder;
final VoidCallback onPrevious;
final VoidCallback onNext;
final VoidCallback onOpen;
final VoidCallback onCopy;
final ArtifactFetch fetch;
final ArtifactFileBridge fileBridge;
final String? text;
final Uint8List? bytes;
final String? error;
final String? status;
@override
Widget build(BuildContext context) {
final resolved = plan.resolved;
// The page never scrolls: a thin strip on top, the preview filling the
// middle (it zooms or scrolls itself), the controls along the bottom.
final content = Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_InfoStrip(
label: artifactViewerPositionLabel(
index,
count,
page.artifact.contentType,
),
facts: ArtifactSelectionFacts(
group: page.group,
artifact: page.artifact,
step: null,
resolved: plan.resolved,
),
),
Expanded(
child: ArtifactPreviewBody(
plan: plan,
loading: loading,
markdownBuilder: markdownBuilder,
text: text,
bytes: bytes,
error: error,
),
),
],
);
final hasPrevious = pagedIndex(index, -1, count) != index;
final hasNext = pagedIndex(index, 1, count) != index;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
// A horizontal swipe moves to the previous or next artifact.
child: 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();
},
child: content,
),
),
// Every action as an icon at the bottom, beside the pager.
SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Row(
key: const Key('artifact-viewer-actions'),
children: [
if (resolved != null && !resolved.external)
ArtifactFileActions(
key: ValueKey<String>(
'viewer-files-${page.group.stageKey}\u0000'
'${page.group.visit}\u0000${page.artifact.url}',
),
artifact: page.artifact,
resolved: resolved,
fetch: fetch,
bridge: fileBridge,
compact: true,
),
if (resolved != null)
_Actions(resolved: resolved, onOpen: onOpen, onCopy: onCopy),
Expanded(
child: Align(
alignment: AlignmentDirectional.centerEnd,
child: _Pager(
index: index,
count: count,
onPrevious: onPrevious,
onNext: onNext,
),
),
),
],
),
),
),
],
);
}
}
/// Open, Copy and the file actions float over the top of the content: shown
/// when the page opens and whenever the content is touched, hidden again after
/// a few seconds without use, so nothing needs scrolling to reach them.
class ArtifactControlsOverlay extends StatefulWidget {
const ArtifactControlsOverlay({
super.key,
required this.controls,
required this.child,
this.hideAfter = const Duration(seconds: 3),
});
final List<Widget> controls;
final Widget child;
final Duration hideAfter;
@override
State<ArtifactControlsOverlay> createState() =>
_ArtifactControlsOverlayState();
}
class _ArtifactControlsOverlayState extends State<ArtifactControlsOverlay> {
bool _shown = true;
Timer? _hide;
@override
void initState() {
super.initState();
_schedule();
}
@override
void dispose() {
_hide?.cancel();
super.dispose();
}
void _schedule() {
_hide?.cancel();
_hide = Timer(widget.hideAfter, () {
if (mounted) setState(() => _shown = false);
});
}
void _show() {
if (!_shown) setState(() => _shown = true);
_schedule();
}
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final surface = cupertino
? CupertinoColors.systemBackground
.resolveFrom(context)
.withValues(alpha: 0.92)
: Theme.of(
context,
).colorScheme.surfaceContainerHigh.withValues(alpha: 0.94);
return Stack(
children: [
// Any touch on the content brings the controls back.
Positioned.fill(
child: Listener(
behavior: HitTestBehavior.translucent,
onPointerDown: (_) => _show(),
child: widget.child,
),
),
Positioned(
left: 8,
right: 8,
top: 8,
child: IgnorePointer(
ignoring: !_shown,
child: AnimatedOpacity(
key: const Key('artifact-viewer-controls'),
opacity: _shown ? 1 : 0,
duration: const Duration(milliseconds: 200),
child: Listener(
// Using a control keeps the bar up.
onPointerDown: (_) => _schedule(),
child: DecoratedBox(
decoration: BoxDecoration(
color: surface,
borderRadius: BorderRadius.circular(12),
boxShadow: const [
BoxShadow(color: Color(0x22000000), blurRadius: 6),
],
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 2,
),
child: Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: widget.controls,
),
),
),
),
),
),
),
],
);
}
}
class _Actions extends StatelessWidget {
const _Actions({
required this.resolved,
required this.onOpen,
required this.onCopy,
});
final ResolvedArtifactUrl resolved;
final VoidCallback onOpen;
final VoidCallback onCopy;
@override
Widget build(BuildContext context) => Row(
mainAxisSize: MainAxisSize.min,
children: [
ArtifactIconAction(
key: const Key('artifact-open'),
label: 'Open',
icon: Icons.open_in_new,
cupertinoIcon: CupertinoIcons.arrow_up_right_square,
onPressed: onOpen,
),
ArtifactIconAction(
key: const Key('artifact-copy-link'),
label: 'Copy link',
icon: Icons.link,
cupertinoIcon: CupertinoIcons.link,
onPressed: onCopy,
),
],
);
}
const artifactPreviousKey = ValueKey<String>('artifact-previous');
const artifactNextKey = ValueKey<String>('artifact-next');
class _Pager extends StatelessWidget {
const _Pager({
required this.index,
required this.count,
required this.onPrevious,
required this.onNext,
});
final int index;
final int count;
final VoidCallback onPrevious;
final VoidCallback onNext;
@override
Widget build(BuildContext context) {
if (count <= 1) return const SizedBox.shrink();
final hasPrevious = pagedIndex(index, -1, count) != index;
final hasNext = pagedIndex(index, 1, count) != index;
return ChevronPager(
index: index,
count: count,
previousLabel: 'Previous artifact',
nextLabel: 'Next artifact',
previousKey: artifactPreviousKey,
nextKey: artifactNextKey,
onPrevious: hasPrevious ? onPrevious : null,
onNext: hasNext ? onNext : null,
);
}
}
/// The viewer's top edge: where this artifact sits in the list, and an
/// information button for its recorded facts.
class _InfoStrip extends StatelessWidget {
const _InfoStrip({required this.label, required this.facts});
final String label;
final Widget facts;
void _show(BuildContext context) {
if (usesCupertinoHost(context)) {
showCupertinoModalPopup<void>(
context: context,
builder: (context) => CupertinoPopupSurface(
child: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: facts,
),
),
),
);
return;
}
showModalBottomSheet<void>(
context: context,
showDragHandle: true,
builder: (context) => SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
child: facts,
),
),
);
}
@override
Widget build(BuildContext context) {
final muted = usesCupertinoHost(context)
? CupertinoTheme.of(context).textTheme.tabLabelTextStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
)
: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
);
return Padding(
padding: const EdgeInsetsDirectional.only(start: 16, end: 4),
child: Row(
children: [
Expanded(
child: Text(
label,
key: const Key('artifact-viewer-position'),
style: muted,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
ArtifactIconAction(
key: const Key('artifact-info'),
label: 'Details',
icon: Icons.info_outline,
cupertinoIcon: CupertinoIcons.info,
onPressed: () => _show(context),
),
],
),
);
}
}