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) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Expanded( child: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ 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, ), if (plan.resolved != null && !plan.resolved!.external) Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 16, 4), child: ArtifactFileActions( key: ValueKey( 'viewer-files-${page.group.stageKey}\u0000' '${page.group.visit}\u0000${page.artifact.url}', ), artifact: page.artifact, resolved: plan.resolved!, fetch: fetch, bridge: fileBridge, ), ), if (plan.resolved != null) _Actions( resolved: plan.resolved!, onOpen: onOpen, onCopy: onCopy, ), ], ), ), ), _Pager( index: index, count: count, onPrevious: onPrevious, onNext: onNext, ), ], ); } } 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'), ), ], ), ); } } 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; if (usesCupertinoHost(context)) { return SafeArea( top: false, child: Wrap( alignment: WrapAlignment.spaceBetween, children: [ CupertinoButton( onPressed: hasPrevious ? onPrevious : null, child: const Text('Previous artifact'), ), CupertinoButton( onPressed: hasNext ? onNext : null, child: const Text('Next artifact'), ), ], ), ); } return SafeArea( top: false, child: Padding( padding: const EdgeInsetsDirectional.fromSTEB(8, 0, 8, 4), // A `Row` clips the second labelled button once 2x text is wider than // the viewport; a `Wrap` keeps both controls whole and lets them take // separate lines, like the Cupertino pager. child: Wrap( alignment: WrapAlignment.spaceBetween, spacing: 8, runSpacing: 4, children: [ TextButton.icon( onPressed: hasPrevious ? onPrevious : null, icon: const Icon(Icons.arrow_back), label: const Text('Previous artifact'), ), TextButton.icon( onPressed: hasNext ? onNext : null, icon: const Icon(Icons.arrow_forward), label: const Text('Next artifact'), ), ], ), ), ); } }