Files
pipelines/app/lib/features/artifacts/ui/artifact_viewer_body.dart
T
alan a322e367da Artifact viewer: keep the preview between the top bar and the controls
Every preview (image, text, markdown, HTML, video) now fills the space between
the viewer's top bar and its bottom controls instead of passing behind them;
the insets are applied once here and removed for the content inside.
2026-11-01 21:22:36 +00:00

393 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 '../../../ui/adaptive/floating_bar_layout.dart';
import '../../../ui/adaptive/glass_pill.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_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,
this.onRetry,
});
/// Loads the current page's preview again after a failure.
final VoidCallback? onRetry;
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 preview = Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: ArtifactPreviewBody(
plan: plan,
loading: loading,
markdownBuilder: markdownBuilder,
text: text,
bytes: bytes,
error: error,
onRetry: onRetry,
),
),
],
);
// The page never scrolls: the preview fills the space between the top
// bar and the bottom controls (it zooms or scrolls itself), never
// passing behind either; the insets are spent here, not again inside.
final content = Builder(
builder: (context) {
final insets = MediaQuery.paddingOf(context);
return Padding(
padding: EdgeInsets.only(top: insets.top, bottom: insets.bottom),
child: MediaQuery.removePadding(
context: context,
removeTop: true,
removeBottom: true,
child: preview,
),
);
},
);
final hasPrevious = pagedIndex(index, -1, count) != index;
final hasNext = pagedIndex(index, 1, count) != index;
final swipe = 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,
);
// An unresolved single artifact has no actions and nothing to page:
// no empty glass bar, and no bottom clearance reserved for one.
if (resolved == null && count <= 1) return swipe;
return FloatingBarOverlay(
// The SafeArea already lifts the bar by the inherited bottom inset (a
// shell bar below the pane); adding that clearance again floated the
// bar far above it.
bar: SafeArea(
top: false,
minimum: const EdgeInsets.all(8),
child: SizedBox(
height: 56,
child: GlassPill(
cupertino: usesCupertinoHost(context),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
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: _Pager(
index: index,
count: count,
onPrevious: onPrevious,
onNext: onNext,
),
),
],
),
),
),
),
),
child: swipe,
);
}
}
/// 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 ArtifactViewerInfoAction extends StatelessWidget {
const ArtifactViewerInfoAction({super.key, required this.facts});
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) {
return ArtifactIconAction(
key: const Key('artifact-info'),
label: 'Details',
icon: Icons.info_outline,
cupertinoIcon: CupertinoIcons.info,
onPressed: () => _show(context),
);
}
}