525 lines
19 KiB
Dart
525 lines
19 KiB
Dart
import 'package:cutdex/shared/widgets/error_notification_host.dart';
|
|
import 'package:cutdex/errors/app_failure.dart';
|
|
|
|
import 'dart:math' as math;
|
|
|
|
import 'package:cutdex/l10n/l10n.dart';
|
|
import 'package:cutdex_sdk/project.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../integrations/platform/platform_capabilities.dart';
|
|
|
|
import 'package:cutdex_ui/theme.dart';
|
|
import 'package:cutdex_ui/widgets.dart';
|
|
|
|
import '../../workspace/external_media_import.dart';
|
|
import '../../workspace/media_availability.dart';
|
|
import '../../workspace/projects.dart';
|
|
import '../../workspace/timeline_provider.dart';
|
|
import 'audio_asset_tile.dart';
|
|
import 'media_asset_drag.dart';
|
|
import 'media_asset_strip.dart';
|
|
import 'media_browser_controller.dart';
|
|
import 'unavailable_media_placeholder.dart';
|
|
import 'visual_asset_tile.dart';
|
|
|
|
extension MediaFilterLabel on MediaFilter {
|
|
String label(BuildContext context) => switch (this) {
|
|
MediaFilter.all => context.l10n.all,
|
|
MediaFilter.image => context.l10n.picture,
|
|
MediaFilter.video => context.l10n.video,
|
|
MediaFilter.audio => context.l10n.audio,
|
|
};
|
|
}
|
|
|
|
const maximumMediaGridTileWidth = 160.0;
|
|
const maximumCompactMediaGridTileWidth = 120.0;
|
|
|
|
class MediaBrowserBody extends ConsumerStatefulWidget {
|
|
const MediaBrowserBody({
|
|
super.key,
|
|
required this.projectId,
|
|
required this.grid,
|
|
this.draggable = false,
|
|
this.compact = false,
|
|
});
|
|
final String projectId;
|
|
final bool grid;
|
|
final bool draggable;
|
|
final bool compact;
|
|
@override
|
|
ConsumerState<MediaBrowserBody> createState() => _MediaBrowserBodyState();
|
|
}
|
|
|
|
class _MediaBrowserBodyState extends ConsumerState<MediaBrowserBody>
|
|
with WidgetsBindingObserver {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
ref.invalidate(mediaAvailabilityProvider);
|
|
}
|
|
}
|
|
|
|
MediaBrowserController get _browser =>
|
|
ref.read(mediaBrowserProvider(widget.projectId));
|
|
bool get _checkingDrop => _browser.busy;
|
|
String? get _selectedAssetId => _browser.selectedAssetId;
|
|
set _selectedAssetId(String? value) => _browser.selectedAssetId = value;
|
|
MediaFilter get _filter => _browser.filter;
|
|
bool get _isGrid => widget.grid;
|
|
bool get _desktop => ref.read(platformCapabilitiesProvider).nativeFileDrop;
|
|
|
|
Future<void> _action(Future<bool> Function() action) async {
|
|
final ok = await action();
|
|
if (!mounted || ok) return;
|
|
final failure =
|
|
ref.read(projectsProvider).asData?.value.error ??
|
|
const AppFailure(AppErrorCode.operationFailed);
|
|
ErrorNotificationHost.of(context)
|
|
.showFailure(failure, context.l10n.failureMessage(failure));
|
|
}
|
|
|
|
Future<void> _rename(AssetDocument asset) async {
|
|
final controller = TextEditingController(text: asset.name);
|
|
final name = await showAppDialog<String>(
|
|
context: context,
|
|
builder: (context) => AppDialog(
|
|
title: context.l10n.renameMedia,
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: Text(context.l10n.cancel),
|
|
),
|
|
ListenableBuilder(
|
|
listenable: controller,
|
|
builder: (context, _) => FilledButton(
|
|
onPressed: controller.text.trim().isEmpty
|
|
? null
|
|
: () => Navigator.pop(context, controller.text.trim()),
|
|
child: Text(context.l10n.save),
|
|
),
|
|
),
|
|
],
|
|
child: AppField(
|
|
label: context.l10n.name,
|
|
child: TextField(
|
|
controller: controller,
|
|
autofocus: true,
|
|
onSubmitted: (value) {
|
|
if (value.trim().isNotEmpty) Navigator.pop(context, value.trim());
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
// The route's closing animation can still read its text controller.
|
|
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
controller.dispose();
|
|
if (!mounted || name == null) return;
|
|
await _action(() => _browser.rename(asset, name));
|
|
}
|
|
|
|
Future<void> _remove(AssetDocument asset) async {
|
|
final confirmed = await showAppDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AppDialog(
|
|
title: context.l10n.removeMedia,
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: Text(context.l10n.cancel),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: Theme.of(context).colorScheme.error,
|
|
),
|
|
child: Text(context.l10n.remove),
|
|
),
|
|
],
|
|
child: Text(context.l10n.removeMediaConfirmation(asset.name)),
|
|
),
|
|
);
|
|
if (!mounted || confirmed != true) return;
|
|
await _action(() => _browser.remove(asset));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ListenableBuilder(
|
|
listenable: ref.watch(mediaBrowserProvider(widget.projectId)),
|
|
builder: (context, _) => _buildBrowser(context),
|
|
);
|
|
Widget _buildBrowser(BuildContext context) {
|
|
ref.watch(externalMediaImportProvider(widget.projectId));
|
|
ref.watch(
|
|
projectsProvider.select(
|
|
(value) => (
|
|
value.value?.activeProject?.id,
|
|
value.value?.assets,
|
|
value.value?.timeline?.id,
|
|
value.value?.projectBusy,
|
|
),
|
|
),
|
|
);
|
|
final workspace = ref.read(projectsProvider).value;
|
|
final active = workspace?.activeProject?.id == widget.projectId;
|
|
final allAssets = active ? workspace!.assets : <AssetDocument>[];
|
|
final assets = _filter == MediaFilter.all
|
|
? allAssets
|
|
: allAssets
|
|
.where((asset) => asset.metadata['kind'] == _filter.kind)
|
|
.toList();
|
|
final timelineId = active ? workspace?.timeline?.id : null;
|
|
final timeline = timelineId == null
|
|
? null
|
|
: ref
|
|
.watch(editorTimelineProvider(widget.projectId, timelineId))
|
|
.asData
|
|
?.value;
|
|
return assets.isEmpty
|
|
? Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(AppSpacing.medium),
|
|
child: Text(
|
|
_filter != MediaFilter.all
|
|
? context.l10n.noMediaOfType(_filter.label(context))
|
|
: _desktop
|
|
? context.l10n.dragMediaHint
|
|
: context.l10n.noMedia,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
)
|
|
: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final spacing = widget.compact
|
|
? AppSpacing.gap
|
|
: AppSpacing.small;
|
|
final padding = widget.compact
|
|
? AppSidebarMetrics.outerPadding
|
|
: AppSpacing.compact;
|
|
final width = math.max(1.0, constraints.maxWidth - padding * 2);
|
|
final columns = math.max(
|
|
1,
|
|
widget.compact
|
|
? ((width + spacing) /
|
|
(maximumCompactMediaGridTileWidth + spacing))
|
|
.ceil()
|
|
: ((width + spacing) /
|
|
(maximumMediaGridTileWidth + spacing))
|
|
.ceil(),
|
|
);
|
|
final tileWidth = (width - spacing * (columns - 1)) / columns;
|
|
final textStyle = Theme.of(context).textTheme.bodySmall!;
|
|
final labelHeight =
|
|
MediaQuery.textScalerOf(context).scale(textStyle.fontSize!) *
|
|
(textStyle.height ?? 1.2);
|
|
Widget item(BuildContext context, int index) {
|
|
final asset = assets[index];
|
|
return Consumer(
|
|
builder: (context, ref, _) {
|
|
final availability = ref
|
|
.watch(
|
|
mediaAvailabilityProvider(widget.projectId, asset),
|
|
)
|
|
.asData
|
|
?.value;
|
|
final unavailable =
|
|
availability != null &&
|
|
availability != MediaAvailability.available;
|
|
return AppContextMenu(
|
|
key: ValueKey(('media-menu', asset.id)),
|
|
itemsBuilder: (_) {
|
|
final available = ref
|
|
.read(projectsProvider)
|
|
.asData
|
|
?.value;
|
|
final canEdit =
|
|
available?.activeProject?.id == widget.projectId &&
|
|
available?.projectBusy == false &&
|
|
!_checkingDrop;
|
|
final sequence = timeline?.activeSequenceId;
|
|
return [
|
|
AppMenuItem(
|
|
label: context.l10n.addToTimeline,
|
|
icon: Icons.add,
|
|
onPressed:
|
|
canEdit &&
|
|
!unavailable &&
|
|
sequence != null &&
|
|
available?.timeline?.id == timelineId
|
|
? () =>
|
|
_action(() => _browser.insertAsset(asset))
|
|
: null,
|
|
),
|
|
AppMenuItem(
|
|
label: context.l10n.rename,
|
|
icon: Icons.drive_file_rename_outline,
|
|
onPressed: canEdit ? () => _rename(asset) : null,
|
|
),
|
|
AppMenuItem(
|
|
label: defaultTargetPlatform == TargetPlatform.macOS
|
|
? context.l10n.revealFinder
|
|
: context.l10n.revealFileManager,
|
|
icon: Icons.folder_open,
|
|
onPressed: canEdit && _desktop
|
|
? () => _action(() => _browser.reveal(asset))
|
|
: null,
|
|
),
|
|
const AppMenuSeparator(),
|
|
AppMenuItem(
|
|
label: context.l10n.removeMedia,
|
|
icon: Icons.delete_outline,
|
|
destructive: true,
|
|
onPressed: canEdit ? () => _remove(asset) : null,
|
|
),
|
|
];
|
|
},
|
|
child: Listener(
|
|
onPointerDown: (event) {
|
|
if (event.buttons == 2) {
|
|
setState(() => _selectedAssetId = asset.id);
|
|
}
|
|
},
|
|
child: _SelectableAsset(
|
|
key: ValueKey(asset.id),
|
|
grid: _isGrid,
|
|
drag: MediaAssetDrag(
|
|
projectId: widget.projectId,
|
|
assetId: asset.id,
|
|
),
|
|
name: asset.name,
|
|
draggable:
|
|
widget.draggable &&
|
|
!workspace!.projectBusy &&
|
|
!_checkingDrop &&
|
|
!unavailable,
|
|
selected: _selectedAssetId == asset.id,
|
|
onTap: () => _browser.selectAsset(asset),
|
|
child: _assetTile(asset, availability),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
return ScrollEdgeFadeBuilder(
|
|
key: ValueKey(('media-scroll-fade', widget.projectId, _isGrid)),
|
|
builder: (controller) => !_isGrid
|
|
? ListView.separated(
|
|
key: PageStorageKey(('media-list', widget.projectId)),
|
|
controller: controller,
|
|
padding: EdgeInsets.all(padding),
|
|
itemCount: assets.length,
|
|
itemBuilder: item,
|
|
separatorBuilder: (_, _) => SizedBox(height: spacing),
|
|
)
|
|
: GridView.builder(
|
|
key: PageStorageKey(('media-grid', widget.projectId)),
|
|
controller: controller,
|
|
padding: EdgeInsets.all(padding),
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: columns,
|
|
crossAxisSpacing: spacing,
|
|
mainAxisSpacing: widget.compact
|
|
? spacing
|
|
: AppSpacing.compact,
|
|
mainAxisExtent:
|
|
tileWidth * 9 / 16 +
|
|
(widget.compact
|
|
? AppSpacing.gap
|
|
: AppSpacing.small) +
|
|
labelHeight +
|
|
2,
|
|
),
|
|
itemCount: assets.length,
|
|
itemBuilder: item,
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _assetTile(AssetDocument asset, MediaAvailability? availability) {
|
|
final unavailable =
|
|
availability != null && availability != MediaAvailability.available;
|
|
final cover = unavailable
|
|
? AspectRatio(
|
|
aspectRatio: 16 / 9,
|
|
child: _isGrid
|
|
? UnavailableMediaPlaceholder(availability: availability)
|
|
: ColoredBox(
|
|
color: AppSurfaces.of(context).previewBackground,
|
|
child: Center(
|
|
child: Text(
|
|
availability == MediaAvailability.missing
|
|
? context.l10n.mediaMissing
|
|
: context.l10n.mediaUnavailable,
|
|
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
|
color: Theme.of(context).colorScheme.error,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
: !_isGrid &&
|
|
const {'audio', 'video', 'image'}.contains(asset.metadata['kind'])
|
|
? MediaAssetStrip(asset: asset)
|
|
: 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 Icon(Icons.insert_drive_file_outlined),
|
|
),
|
|
),
|
|
};
|
|
final name = Tooltip(
|
|
// Hover still shows the full name; touch long press belongs to actions.
|
|
triggerMode: TooltipTriggerMode.manual,
|
|
message: asset.name,
|
|
child: Text(
|
|
asset.name,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
);
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(6),
|
|
child: Stack(
|
|
children: [
|
|
if (!_isGrid)
|
|
SizedBox(height: MediaAssetStrip.height, child: cover)
|
|
else
|
|
cover,
|
|
Positioned(
|
|
top: 4,
|
|
right: 4,
|
|
child: MediaAssetDuration(asset: asset),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(height: widget.compact ? AppSpacing.gap : AppSpacing.small),
|
|
name,
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _SelectableAsset extends StatelessWidget {
|
|
const _SelectableAsset({
|
|
super.key,
|
|
required this.grid,
|
|
required this.selected,
|
|
required this.drag,
|
|
required this.name,
|
|
required this.draggable,
|
|
required this.onTap,
|
|
required this.child,
|
|
});
|
|
|
|
final bool selected;
|
|
final bool grid;
|
|
final MediaAssetDrag drag;
|
|
final String name;
|
|
final bool draggable;
|
|
final VoidCallback onTap;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final selectionDecoration = BoxDecoration(
|
|
borderRadius: BorderRadius.circular(6),
|
|
border: Border.all(color: Theme.of(context).colorScheme.primary),
|
|
);
|
|
final tile = Semantics(
|
|
selected: selected,
|
|
child: InkWell(
|
|
onTap: onTap,
|
|
hoverColor: Colors.transparent,
|
|
highlightColor: Colors.transparent,
|
|
splashFactory: NoSplash.splashFactory,
|
|
child: Stack(
|
|
children: [
|
|
child,
|
|
if (selected && !grid)
|
|
Positioned(
|
|
top: 0,
|
|
left: 0,
|
|
right: 0,
|
|
height: MediaAssetStrip.height,
|
|
child: IgnorePointer(
|
|
child: DecoratedBox(decoration: selectionDecoration),
|
|
),
|
|
),
|
|
if (selected && grid)
|
|
Positioned(
|
|
top: 0,
|
|
left: 0,
|
|
right: 0,
|
|
child: IgnorePointer(
|
|
child: AspectRatio(
|
|
aspectRatio: 16 / 9,
|
|
child: DecoratedBox(decoration: selectionDecoration),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
return Draggable<MediaAssetDrag>(
|
|
data: drag,
|
|
maxSimultaneousDrags: draggable ? 1 : 0,
|
|
dragAnchorStrategy: pointerDragAnchorStrategy,
|
|
onDragStarted: onTap,
|
|
onDragEnd: (_) => drag.feedbackVisible.value = true,
|
|
feedback: ValueListenableBuilder<bool>(
|
|
valueListenable: drag.feedbackVisible,
|
|
builder: (context, visible, child) =>
|
|
visible ? child! : const SizedBox.shrink(),
|
|
child: Material(
|
|
elevation: 4,
|
|
color: Theme.of(context).colorScheme.surfaceContainerHigh,
|
|
child: SizedBox(
|
|
width: 144,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(AppSpacing.small),
|
|
child: Text(
|
|
name,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
child: tile,
|
|
);
|
|
}
|
|
}
|