import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import '../domain/artifact_groups.dart'; import '../domain/artifact_kind.dart'; import '../domain/artifact_record.dart'; import '../domain/artifact_url.dart'; import 'artifact_row_labels.dart'; import 'artifact_thumbnail_image.dart'; import 'artifact_viewer_fetch.dart'; /// One artifact in the gallery grid: a thumbnail for an image, otherwise an /// icon for its kind of file, with its name and facts underneath. class ArtifactTile extends StatefulWidget { const ArtifactTile({ super.key, required this.group, required this.artifact, required this.selected, required this.configuredEndpoint, required this.fetch, this.onSelected, }); final ArtifactGroup group; final ArtifactRecord artifact; final bool selected; final String configuredEndpoint; final ArtifactFetch fetch; final ArtifactSelected? onSelected; @override State createState() => _ArtifactTileState(); } class _ArtifactTileState extends State { @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final artifact = widget.artifact; final secondary = cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant; final surface = cupertino ? CupertinoColors.tertiarySystemFill.resolveFrom(context) : Theme.of(context).colorScheme.surfaceContainerHighest; final border = widget.selected ? (cupertino ? CupertinoTheme.of(context).primaryColor : Theme.of(context).colorScheme.primary) : (cupertino ? CupertinoColors.separator.resolveFrom(context) : Theme.of(context).colorScheme.outlineVariant); final preview = ArtifactThumbnail( artifact: artifact, configuredEndpoint: widget.configuredEndpoint, fetch: widget.fetch, iconSize: 40, ); final name = artifactDisplayName(artifact); final facts = artifactFactsLabel(artifact); final body = Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ AspectRatio( aspectRatio: 4 / 3, child: ClipRRect( borderRadius: const BorderRadius.vertical(top: Radius.circular(11)), child: ColoredBox(color: surface, child: preview), ), ), Padding( padding: const EdgeInsets.fromLTRB(8, 6, 8, 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(name, maxLines: 1, overflow: TextOverflow.ellipsis), Text( facts, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(color: secondary, fontSize: 12), ), ], ), ), ], ); final onSelected = widget.onSelected; void tap() => onSelected?.call(widget.group, artifact); final decorated = DecoratedBox( decoration: BoxDecoration( border: Border.all(color: border, width: widget.selected ? 2 : 1), borderRadius: BorderRadius.circular(12), ), child: body, ); return Semantics( button: onSelected != null, selected: widget.selected, label: '$name, $facts', excludeSemantics: true, onTap: onSelected == null ? null : tap, child: cupertino ? GestureDetector( behavior: HitTestBehavior.opaque, onTap: onSelected == null ? null : tap, child: decorated, ) : Material( type: MaterialType.transparency, child: InkWell( borderRadius: BorderRadius.circular(12), onTap: onSelected == null ? null : tap, child: decorated, ), ), ); } } /// An artifact's picture: the image itself, loaded once through the app's own /// fetch, or an icon for its kind of file. class ArtifactThumbnail extends StatefulWidget { const ArtifactThumbnail({ super.key, required this.artifact, required this.configuredEndpoint, required this.fetch, this.iconSize = 24, }); final ArtifactRecord artifact; final String configuredEndpoint; final ArtifactFetch fetch; final double iconSize; @override State createState() => _ArtifactThumbnailState(); } class _ArtifactThumbnailState extends State with AutomaticKeepAliveClientMixin { Future? _image; /// A loaded picture stays alive when a lazy gallery scrolls it away, so /// scrolling back does not fetch it again. @override bool get wantKeepAlive => _image != null; @override void initState() { super.initState(); final artifact = widget.artifact; final format = artifactPreviewFormat(artifact.contentType, artifact.name); final resolved = artifact.hasUrl ? resolveArtifactUrl(widget.configuredEndpoint, artifact.url) : null; // Only same-origin images load a thumbnail, through the app's own fetch. if (format == ArtifactPreviewFormat.image && resolved != null && !resolved.external) { _image = widget.fetch(resolved.uri); } } @override Widget build(BuildContext context) { super.build(context); final cupertino = usesCupertinoHost(context); final icon = Icon( artifactKindIcon(widget.artifact, cupertino: cupertino), size: widget.iconSize, color: cupertino ? CupertinoColors.secondaryLabel.resolveFrom(context) : Theme.of(context).colorScheme.onSurfaceVariant, ); if (_image == null) return Center(child: icon); return FutureBuilder( future: _image, builder: (context, snapshot) { final result = snapshot.data; if (result == null || !result.ok || result.bytes.isEmpty) { return Center(child: icon); } return LayoutBuilder( builder: (context, constraints) { final ratio = MediaQuery.devicePixelRatioOf(context); // Decoded at the size shown, not the picture's full size. final sized = constraints.hasBoundedWidth && constraints.hasBoundedHeight ? CoverSizedMemoryImage( result.bytes, width: (constraints.maxWidth * ratio).ceil(), height: (constraints.maxHeight * ratio).ceil(), ) : null; return Image( image: sized ?? MemoryImage(result.bytes), fit: BoxFit.cover, gaplessPlayback: true, errorBuilder: (_, _, _) => Center(child: icon), ); }, ); }, ); } } /// The icon for an artifact's kind of file, when it has no thumbnail. IconData artifactKindIcon(ArtifactRecord artifact, {required bool cupertino}) { final name = artifact.name.toLowerCase(); final format = artifactPreviewFormat(artifact.contentType, artifact.name); if (name.endsWith('.zip') || name.endsWith('.tar') || name.endsWith('.gz')) { return cupertino ? CupertinoIcons.archivebox : Icons.folder_zip_outlined; } if (name.endsWith('.pdf')) { return cupertino ? CupertinoIcons.doc_richtext : Icons.picture_as_pdf; } return switch (format) { ArtifactPreviewFormat.image => cupertino ? CupertinoIcons.photo : Icons.image_outlined, ArtifactPreviewFormat.video => cupertino ? CupertinoIcons.film : Icons.movie_outlined, ArtifactPreviewFormat.audio => cupertino ? CupertinoIcons.music_note : Icons.audio_file_outlined, ArtifactPreviewFormat.markdown => cupertino ? CupertinoIcons.doc_text : Icons.article_outlined, ArtifactPreviewFormat.text => cupertino ? CupertinoIcons.doc_plaintext : Icons.description_outlined, ArtifactPreviewFormat.table => cupertino ? CupertinoIcons.table : Icons.table_chart_outlined, ArtifactPreviewFormat.html => cupertino ? CupertinoIcons.globe : Icons.html, ArtifactPreviewFormat.none => cupertino ? CupertinoIcons.doc : Icons.insert_drive_file_outlined, }; }