229 lines
7.9 KiB
Dart
229 lines
7.9 KiB
Dart
import 'package:cutdex_sdk/project.dart';
|
||
import 'package:cutdex_ui/icons.dart';
|
||
import 'package:cutdex_ui/theme.dart';
|
||
import 'package:flutter/material.dart';
|
||
|
||
import '../../l10n/l10n.dart';
|
||
import '../../workspace/media_availability.dart';
|
||
import 'audio_asset_tile.dart';
|
||
import 'media_asset_strip.dart';
|
||
import 'media_browser_layout.dart';
|
||
import 'unavailable_media_placeholder.dart';
|
||
import 'visual_asset_tile.dart';
|
||
|
||
/// Desktop cards and rows use the same cover, name and availability labels.
|
||
class DesktopMediaAssetTile extends StatelessWidget {
|
||
const DesktopMediaAssetTile({
|
||
super.key,
|
||
required this.asset,
|
||
required this.grid,
|
||
required this.availability,
|
||
});
|
||
final AssetDocument asset;
|
||
final bool grid;
|
||
final MediaAvailability? availability;
|
||
|
||
static TextStyle? _nameStyle(BuildContext context) =>
|
||
Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||
fontFamily: 'packages/cutdex_ui/Inter',
|
||
fontSize: AppInspectorMetrics.fontSize,
|
||
height: AppInspectorMetrics.lineHeight / AppInspectorMetrics.fontSize,
|
||
fontWeight: FontWeight.w500,
|
||
fontVariations: const [FontVariation('wght', 500)],
|
||
letterSpacing: AppInspectorMetrics.fontSize * .005,
|
||
);
|
||
|
||
static TextStyle? _metadataStyle(BuildContext context) =>
|
||
Theme.of(context).textTheme.bodySmall?.copyWith(
|
||
fontFamily: 'packages/cutdex_ui/Inter',
|
||
fontSize: AppInspectorMetrics.fontSize,
|
||
height: AppInspectorMetrics.lineHeight / AppInspectorMetrics.fontSize,
|
||
fontVariations: const [FontVariation('wght', 450)],
|
||
letterSpacing: AppInspectorMetrics.fontSize * .005,
|
||
);
|
||
|
||
/// Font fallback and fractional scaling can round a line above size × height.
|
||
static double gridLabelHeight(BuildContext context) {
|
||
double lineHeight(TextStyle? style) {
|
||
final painter = TextPainter(
|
||
text: TextSpan(text: '国Ag', style: style),
|
||
maxLines: 1,
|
||
textDirection: Directionality.of(context),
|
||
textScaler: MediaQuery.textScalerOf(context),
|
||
locale: Localizations.maybeLocaleOf(context),
|
||
textHeightBehavior: DefaultTextStyle.of(context).textHeightBehavior,
|
||
)..layout();
|
||
final height = painter.height.ceilToDouble();
|
||
painter.dispose();
|
||
return height;
|
||
}
|
||
|
||
return lineHeight(_nameStyle(context)) +
|
||
lineHeight(_metadataStyle(context));
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final theme = Theme.of(context);
|
||
final unavailable =
|
||
availability != null && availability != MediaAvailability.available;
|
||
final kind = switch (asset.metadata['kind']) {
|
||
'video' => context.l10n.video,
|
||
'image' => context.l10n.picture,
|
||
'audio' => context.l10n.audio,
|
||
_ => context.l10n.materials,
|
||
};
|
||
final width = asset.metadata['width'];
|
||
final height = asset.metadata['height'];
|
||
final dimensions = width is num && height is num && width > 0 && height > 0
|
||
? '${width.toInt()} × ${height.toInt()}'
|
||
: null;
|
||
final info = unavailable
|
||
? availability == MediaAvailability.missing
|
||
? context.l10n.mediaMissing
|
||
: context.l10n.mediaUnavailable
|
||
: dimensions == null
|
||
? kind
|
||
: '$kind · $dimensions';
|
||
final name = Tooltip(
|
||
message: asset.name,
|
||
child: Text(
|
||
asset.name,
|
||
key: ValueKey('media-name-${asset.id}'),
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: _nameStyle(context),
|
||
),
|
||
);
|
||
Widget metadata({bool showDuration = false}) => Row(
|
||
children: [
|
||
Flexible(
|
||
child: Text(
|
||
info,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: _metadataStyle(context)?.copyWith(
|
||
color: unavailable
|
||
? theme.colorScheme.error
|
||
: theme.colorScheme.onSurfaceVariant,
|
||
),
|
||
),
|
||
),
|
||
if (showDuration && !unavailable) ...[
|
||
const SizedBox(width: 8),
|
||
Flexible(child: MediaAssetDuration(asset: asset, badge: false)),
|
||
],
|
||
],
|
||
);
|
||
final cover = ClipRRect(
|
||
key: ValueKey('desktop-media-cover-${asset.id}'),
|
||
borderRadius: BorderRadius.circular(AppInspectorMetrics.radius),
|
||
child: Stack(
|
||
children: [
|
||
if (unavailable)
|
||
AspectRatio(
|
||
aspectRatio: 16 / 9,
|
||
child: UnavailableMediaPlaceholder(
|
||
availability: availability!,
|
||
showLabel: false,
|
||
),
|
||
)
|
||
else
|
||
switch (asset.metadata['kind']) {
|
||
'video' ||
|
||
'image' => VisualAssetTile(asset: asset, showName: false),
|
||
'audio' => AudioAssetTile(asset: asset, showName: false),
|
||
_ => AspectRatio(
|
||
aspectRatio: 16 / 9,
|
||
child: ColoredBox(
|
||
color: AppSurfaces.of(context).previewBackground,
|
||
child: const Center(child: Icon(AppIcons.file, size: 20)),
|
||
),
|
||
),
|
||
},
|
||
if (grid && !unavailable)
|
||
Positioned(
|
||
bottom: 4,
|
||
left: 4,
|
||
right: 4,
|
||
child: Align(
|
||
alignment: AlignmentDirectional.centerEnd,
|
||
child: MediaAssetDuration(asset: asset),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
if (!grid) {
|
||
return LayoutBuilder(
|
||
builder: (context, constraints) {
|
||
final columns = DesktopMediaListColumns(constraints.maxWidth);
|
||
return Row(
|
||
children: [
|
||
SizedBox(
|
||
width: DesktopMediaMetrics.listCover,
|
||
height: DesktopMediaMetrics.listCover * 9 / 16,
|
||
child: cover,
|
||
),
|
||
const SizedBox(width: DesktopMediaMetrics.gap),
|
||
Expanded(
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
name,
|
||
if (!columns.specification)
|
||
metadata(showDuration: !columns.duration),
|
||
],
|
||
),
|
||
),
|
||
if (columns.duration)
|
||
SizedBox(
|
||
width: DesktopMediaListColumns.durationWidth,
|
||
child: Align(
|
||
alignment: AlignmentDirectional.centerEnd,
|
||
child: unavailable
|
||
? const SizedBox.shrink()
|
||
: MediaAssetDuration(asset: asset, badge: false),
|
||
),
|
||
),
|
||
if (columns.specification) ...[
|
||
const SizedBox(width: DesktopMediaMetrics.gap),
|
||
SizedBox(
|
||
width: DesktopMediaListColumns.specificationWidth,
|
||
child: Text(
|
||
unavailable ? info : dimensions ?? '—',
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: _metadataStyle(context)?.copyWith(
|
||
color: unavailable
|
||
? theme.colorScheme.error
|
||
: theme.colorScheme.onSurfaceVariant,
|
||
),
|
||
),
|
||
),
|
||
],
|
||
if (columns.kind) ...[
|
||
const SizedBox(width: DesktopMediaMetrics.gap),
|
||
SizedBox(
|
||
width: DesktopMediaListColumns.kindWidth,
|
||
child: Text(
|
||
kind,
|
||
maxLines: 1,
|
||
overflow: TextOverflow.ellipsis,
|
||
style: _metadataStyle(context),
|
||
),
|
||
),
|
||
],
|
||
],
|
||
);
|
||
},
|
||
);
|
||
}
|
||
return Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [cover, const SizedBox(height: 4), name, metadata()],
|
||
);
|
||
}
|
||
}
|