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

338 lines
9.5 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;
final controls = <Widget>[
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,
),
if (resolved != null)
_Actions(resolved: resolved, onOpen: onOpen, onCopy: onCopy),
];
final content = SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Room for the controls bar, so it never covers the first line.
if (controls.isNotEmpty) const SizedBox(height: 56),
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 12, 16, 4),
child: ArtifactSelectionFacts(
group: page.group,
artifact: page.artifact,
step: null,
resolved: plan.resolved,
),
),
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 16, 4),
child: Text(
artifactViewerPositionLabel(
index,
count,
page.artifact.contentType,
),
),
),
if (status != null)
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 4),
child: Semantics(liveRegion: true, child: Text(status!)),
),
ArtifactPreviewBody(
plan: plan,
loading: loading,
markdownBuilder: markdownBuilder,
text: text,
bytes: bytes,
error: error,
),
],
),
);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: controls.isEmpty
? content
: ArtifactControlsOverlay(
key: ValueKey<String>('viewer-overlay-${page.artifact.url}'),
controls: controls,
child: content,
),
),
_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) {
if (usesCupertinoHost(context)) {
return SafeArea(
top: false,
child: Wrap(
alignment: WrapAlignment.center,
spacing: 8,
children: [
CupertinoButton(onPressed: onOpen, child: const Text('Open')),
CupertinoButton(onPressed: onCopy, child: const Text('Copy link')),
],
),
);
}
return SafeArea(
top: false,
child: Wrap(
alignment: WrapAlignment.center,
spacing: 8,
children: [
TextButton.icon(
onPressed: onOpen,
icon: const Icon(Icons.open_in_new),
label: const Text('Open'),
),
TextButton.icon(
onPressed: onCopy,
icon: const Icon(Icons.content_copy),
label: const Text('Copy link'),
),
],
),
);
}
}
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 SafeArea(
top: false,
child: ChevronPager(
index: index,
count: count,
previousLabel: 'Previous artifact',
nextLabel: 'Next artifact',
previousKey: artifactPreviousKey,
nextKey: artifactNextKey,
onPrevious: hasPrevious ? onPrevious : null,
onNext: hasNext ? onNext : null,
),
);
}
}