Files
pipelines/app/lib/features/artifacts/ui/artifact_viewer_body.dart
T
2026-11-07 22:01:10 +00:00

210 lines
6.3 KiB
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 '../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_html_view.dart';
import 'artifact_viewer_bar.dart';
import 'artifact_viewer_content.dart';
import 'artifact_viewer_fetch.dart';
export 'artifact_controls_overlay.dart';
export 'artifact_viewer_bar.dart'
show artifactFullscreenKey, artifactNextKey, artifactPreviousKey;
/// 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.downloadResolved,
this.text,
this.bytes,
this.error,
this.status,
this.onRetry,
this.htmlBuilder = defaultArtifactHtml,
this.fullscreen = false,
this.onFullscreen,
this.onImageZoomChanged,
this.imageZoomed = false,
});
/// The inert HTML renderer; tests inject a plain stand-in.
final ArtifactHtmlBuilder htmlBuilder;
/// Whether this body is the full-screen view's ([ArtifactViewerBar]).
final bool fullscreen;
/// Enters or leaves the full-screen view.
final VoidCallback? onFullscreen;
final ValueChanged<bool>? onImageZoomChanged;
final bool imageZoomed;
/// 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 ResolvedArtifactUrl? downloadResolved;
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,
htmlBuilder: htmlBuilder,
onImageZoomChanged: onImageZoomChanged,
),
),
],
);
// 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: !imageZoomed
? (details) {
final velocity = details.primaryVelocity ?? 0;
if (velocity < -250 && hasNext) onNext();
if (velocity > 250 && hasPrevious) onPrevious();
}
: null,
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(
bar: ArtifactViewerBar(
page: page,
resolved: resolved,
index: index,
count: count,
fetch: fetch,
fileBridge: fileBridge,
downloadResolved: downloadResolved,
onOpen: onOpen,
onCopy: onCopy,
onPrevious: onPrevious,
onNext: onNext,
fullscreen: fullscreen,
onFullscreen: onFullscreen,
),
child: swipe,
);
}
}
/// 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),
);
}
}