import 'dart:math' as math; import 'package:flutter/material.dart'; import '../../../ui/adaptive/glass_bar.dart'; import '../../../ui/navigation/chevron_pager.dart'; import '../actions/artifact_file_actions.dart'; import '../actions/artifact_file_bridge.dart'; import '../domain/artifact_url.dart'; import '../domain/gallery_navigation.dart'; import 'artifact_viewer_fetch.dart'; const artifactPreviousKey = ValueKey('artifact-previous'); const artifactNextKey = ValueKey('artifact-next'); const artifactFullscreenKey = ValueKey('artifact-fullscreen'); /// The viewer's bottom controls: file transfer, Open, Copy link, Full screen /// and the pager, in one glass bar above the bottom safe area. /// /// The inline viewer and the full-screen view draw this same bar; in full /// screen the Full screen control reads Exit full screen and leaves it. class ArtifactViewerBar extends StatelessWidget { const ArtifactViewerBar({ super.key, required this.page, required this.resolved, required this.index, required this.count, required this.fetch, required this.fileBridge, this.downloadResolved, required this.onOpen, required this.onCopy, required this.onPrevious, required this.onNext, this.fullscreen = false, this.onFullscreen, this.onSendToChat, }); final PagedArtifact page; final ResolvedArtifactUrl? resolved; final int index; final int count; final ArtifactFetch fetch; final ArtifactFileBridge fileBridge; final ResolvedArtifactUrl? downloadResolved; final VoidCallback onOpen; final VoidCallback onCopy; final VoidCallback onPrevious; final VoidCallback onNext; /// Whether this bar is the full-screen view's. final bool fullscreen; /// Enters (inline) or leaves (in full screen) the full-screen view; absent, /// the control is not drawn. final VoidCallback? onFullscreen; final VoidCallback? onSendToChat; @override Widget build(BuildContext context) { final actions = _actions(resolved, onFullscreen); final pagerMin = count > 1 ? 2 * GlassBarMetrics.controlExtent() : 0.0; final pager = ChevronPager( index: index, count: count, previousLabel: 'Previous artifact', nextLabel: 'Next artifact', previousKey: artifactPreviousKey, nextKey: artifactNextKey, onPrevious: pagedIndex(index, -1, count) != index ? onPrevious : null, onNext: pagedIndex(index, 1, count) != index ? onNext : null, ); return SafeArea( // 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. top: false, minimum: const EdgeInsets.all(8), // Without a pager the bar is as wide as its actions, so its last // control keeps the same gap from the bar's end as the first. child: Align( alignment: AlignmentDirectional.centerStart, heightFactor: 1, child: GlassControlBar( child: LayoutBuilder( builder: (context, constraints) => Row( key: const Key('artifact-viewer-actions'), mainAxisSize: count > 1 ? MainAxisSize.max : MainAxisSize.min, children: [ // On a narrow window or at large text the actions scroll // sideways, so the chevrons beside them stay whole. ConstrainedBox( constraints: BoxConstraints( maxWidth: math.max(0, constraints.maxWidth - pagerMin), ), child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( mainAxisSize: MainAxisSize.min, children: actions, ), ), ), if (count > 1) Expanded(child: pager), ], ), ), ), ), ); } List _actions(ResolvedArtifactUrl? resolved, VoidCallback? toggle) => [ if (resolved != null && !resolved.external) ArtifactFileActions( key: ValueKey( 'viewer-files-${page.group.stageKey}\u0000' '${page.group.visit}\u0000${page.artifact.url}', ), artifact: page.artifact, resolved: downloadResolved ?? resolved, fetch: fetch, bridge: fileBridge, compact: true, ), if (resolved != null) ...[ ArtifactIconAction( key: const Key('artifact-open'), label: 'Open in browser', icon: Icons.open_in_new, onPressed: onOpen, ), ArtifactIconAction( key: const Key('artifact-copy-link'), label: 'Copy link', icon: Icons.link, onPressed: onCopy, ), ], if (toggle != null) ArtifactIconAction( key: artifactFullscreenKey, label: fullscreen ? 'Exit full screen' : 'Full screen', icon: fullscreen ? Icons.fullscreen_exit : Icons.fullscreen, onPressed: toggle, ), if (onSendToChat != null) ArtifactIconAction( key: const Key('artifact-send-to-chat'), label: 'Send to chat…', icon: Icons.chat_outlined, onPressed: onSendToChat!, ), ]; }