Add the Flutter video editing application with project management, media import, timeline editing, preview, AI assistance, diagnostics and settings. Include shared UI and native host packages, platform runners, application tests and release tooling. Provide usage and contribution guides, the MIT license and third-party notices. Exclude local credentials, build outputs and validation data.
407 lines
12 KiB
Dart
407 lines
12 KiB
Dart
import 'package:cutdex/errors/app_failure.dart';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:cutdex_sdk/editor.dart';
|
|
import 'package:cutdex_sdk/project.dart';
|
|
import 'package:cutdex_sdk/timeline/layout.dart';
|
|
import 'package:cutdex_sdk/timeline/model.dart';
|
|
import 'package:cutdex_sdk/timeline_ui.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../l10n/l10n.dart';
|
|
import '../../integrations/app_failure_adapter.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../workspace/projects.dart';
|
|
import '../../workspace/media_import.dart';
|
|
import '../../workspace/timeline_session.dart';
|
|
|
|
import 'package:cutdex_ui/widgets.dart';
|
|
import 'package:cutdex_native/cutdex_native.dart';
|
|
|
|
import '../../integrations/platform/native_file_drop_policy.dart';
|
|
import 'timeline_drop_preparation.dart';
|
|
|
|
typedef TimelineIncomingAsset = ({AssetDocument asset, Offset globalPosition});
|
|
|
|
/// OS and project drags share a provisional clip without publishing changes.
|
|
class TimelineExternalDropTarget extends ConsumerStatefulWidget {
|
|
const TimelineExternalDropTarget({
|
|
super.key,
|
|
required this.projectId,
|
|
required this.session,
|
|
required this.sequenceId,
|
|
required this.child,
|
|
this.incoming,
|
|
});
|
|
final String projectId;
|
|
final EditorTimelineSession session;
|
|
final SequenceId sequenceId;
|
|
final Widget child;
|
|
final ValueListenable<TimelineIncomingAsset?>? incoming;
|
|
@override
|
|
ConsumerState<TimelineExternalDropTarget> createState() =>
|
|
TimelineExternalDropTargetState();
|
|
}
|
|
|
|
class TimelineExternalDropTargetState
|
|
extends ConsumerState<TimelineExternalDropTarget> {
|
|
static const dropPolicy = NativeFileDropPolicy.timeline;
|
|
late final connection = NativeFileDropConnection(
|
|
regionId: 'timeline',
|
|
hidesSourceImage: true,
|
|
onEvent: event,
|
|
);
|
|
TimelineDropPreparationProvider get _preparationProvider =>
|
|
timelineDropPreparationProvider(
|
|
widget.projectId,
|
|
widget.session,
|
|
widget.sequenceId,
|
|
);
|
|
late TimelineDropPreparation _preparer;
|
|
final surface = GlobalKey();
|
|
int? sessionId;
|
|
int generation = 0;
|
|
ExternalMediaFile? file;
|
|
AssetDocument? projectAsset;
|
|
bool get hasPreview => file != null || projectAsset != null;
|
|
EditorIncomingClip? incomingClip;
|
|
({int tick, int top, ConnectionSide? side})? lastPlacement;
|
|
|
|
void updateClip() {
|
|
final prepared = preparedMedia;
|
|
if (!hasPreview || prepared == null || error != null) return;
|
|
final box = surface.currentContext?.findRenderObject() as RenderBox?;
|
|
if (box == null || !box.hasSize) return;
|
|
final viewport = TimelineViewportRect(
|
|
width: box.size.width,
|
|
height: box.size.height,
|
|
);
|
|
final y = view.camera.worldYForScreen(position.dy, viewport);
|
|
final geometry = view.geometry;
|
|
final side = y < geometry.primaryTop
|
|
? ConnectionSide.above
|
|
: y >= geometry.primaryTop + geometry.primaryHeight
|
|
? ConnectionSide.below
|
|
: null;
|
|
final raw = view.camera.tickAtScreenX(
|
|
position.dx,
|
|
viewport,
|
|
rounding: TimelineTickRounding.nearest,
|
|
);
|
|
final frame = view.format.frameGrid.quantumTicks;
|
|
final tick = raw <= 0 ? 0 : ((raw + frame ~/ 2) ~/ frame) * frame;
|
|
final top = (y - 12).round();
|
|
final placement = (tick: tick, top: top, side: side);
|
|
try {
|
|
incomingClip ??= widget.session.beginIncomingClip(
|
|
sequence: widget.sequenceId,
|
|
name: projectAsset?.name ?? file!.displayName,
|
|
duration: prepared.metadata.duration,
|
|
kind: prepared.metadata.kind.name,
|
|
hasAudio: prepared.metadata.hasAudio,
|
|
media: prepared.media,
|
|
);
|
|
if (lastPlacement != placement || incomingClip!.layout == null) {
|
|
incomingClip!.update(start: tick, side: side, top: top);
|
|
lastPlacement = placement;
|
|
}
|
|
placementError = null;
|
|
view.showIncomingClip(
|
|
incomingClip!.layout!,
|
|
element: incomingClip!.incoming.source,
|
|
start: tick,
|
|
top: top,
|
|
audioOnly: incomingClip!.kind == 'audio',
|
|
position: position,
|
|
viewport: viewport,
|
|
);
|
|
} catch (e) {
|
|
placementError = appFailureFromError(e);
|
|
view.clearIncomingClip();
|
|
}
|
|
}
|
|
|
|
PreparedTimelineMedia? get preparedMedia =>
|
|
ref.read(_preparationProvider).preparation.value;
|
|
Offset position = Offset.zero;
|
|
|
|
late TimelineViewController view;
|
|
bool get busy => ref.read(_preparationProvider).commit.isLoading;
|
|
AppFailure? get error => ref.read(_preparationProvider).preparation.hasError
|
|
? const AppFailure(AppErrorCode.mediaReadFailed)
|
|
: null;
|
|
AppFailure? placementError;
|
|
Rect? region;
|
|
bool get windows =>
|
|
!kIsWeb && defaultTargetPlatform == TargetPlatform.windows;
|
|
bool get native =>
|
|
!kIsWeb && (defaultTargetPlatform == TargetPlatform.macOS || windows);
|
|
bool get enabled {
|
|
final s = ref.read(projectsProvider).asData?.value;
|
|
return !busy &&
|
|
s?.projectBusy == false &&
|
|
s?.activeProject?.id == widget.projectId &&
|
|
s?.timeline?.id == widget.session.document.id;
|
|
}
|
|
|
|
bool get active => enabled && TickerMode.valuesOf(context).enabled;
|
|
|
|
TimelineCamera? lastCamera;
|
|
void viewChanged() {
|
|
if (!hasPreview || busy) return;
|
|
if (incomingClip?.layout != null && !view.isDragging) {
|
|
final g = generation;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted && g == generation && !busy) setState(clear);
|
|
});
|
|
return;
|
|
}
|
|
final camera = view.camera;
|
|
if (identical(lastCamera, camera)) return;
|
|
lastCamera = camera;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted || !hasPreview || busy) return;
|
|
setState(updateClip);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant TimelineExternalDropTarget oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
final changedView =
|
|
oldWidget.session != widget.session ||
|
|
oldWidget.sequenceId != widget.sequenceId;
|
|
if (oldWidget.incoming != widget.incoming) {
|
|
oldWidget.incoming?.removeListener(incomingChanged);
|
|
widget.incoming?.addListener(incomingChanged);
|
|
}
|
|
if (changedView) view.removeListener(viewChanged);
|
|
if (changedView ||
|
|
oldWidget.projectId != widget.projectId ||
|
|
oldWidget.incoming != widget.incoming && projectAsset != null) {
|
|
clear();
|
|
}
|
|
if (changedView || oldWidget.projectId != widget.projectId) {
|
|
_preparer = ref.read(_preparationProvider.notifier);
|
|
}
|
|
if (changedView) {
|
|
view = widget.session.view(widget.sequenceId)..addListener(viewChanged);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_preparer = ref.read(_preparationProvider.notifier);
|
|
if (native) {
|
|
connection.bind();
|
|
}
|
|
widget.incoming?.addListener(incomingChanged);
|
|
view = widget.session.view(widget.sequenceId)..addListener(viewChanged);
|
|
}
|
|
|
|
void incomingChanged() {
|
|
final incoming = widget.incoming?.value;
|
|
if (!mounted) return;
|
|
if (incoming == null) {
|
|
if (projectAsset != null && !busy) setState(clear);
|
|
return;
|
|
}
|
|
if (!active) return;
|
|
final box = surface.currentContext?.findRenderObject() as RenderBox?;
|
|
if (box == null || !box.hasSize) return;
|
|
position = box.globalToLocal(incoming.globalPosition);
|
|
if (projectAsset?.id == incoming.asset.id) {
|
|
setState(updateClip);
|
|
return;
|
|
}
|
|
setState(() {
|
|
clear();
|
|
projectAsset = incoming.asset;
|
|
});
|
|
prepareMedia();
|
|
}
|
|
|
|
void enterExternal(int id, String path, Offset localPosition) {
|
|
if (!active || !dropPolicy.accepts([ExternalMediaFile(path: path)])) {
|
|
return;
|
|
}
|
|
setState(() {
|
|
clear();
|
|
sessionId = id;
|
|
file = ExternalMediaFile(path: path);
|
|
position = localPosition;
|
|
});
|
|
prepareMedia();
|
|
}
|
|
|
|
void moveExternal(int id, Offset localPosition) {
|
|
if (!active || sessionId != id) return;
|
|
setState(() {
|
|
position = localPosition;
|
|
updateClip();
|
|
});
|
|
}
|
|
|
|
void exitExternal(int id) {
|
|
if (sessionId == id && !busy) setState(clear);
|
|
}
|
|
|
|
Future<void> dropExternal(int id, Offset localPosition) async {
|
|
if (!active || sessionId != id) return;
|
|
position = localPosition;
|
|
await drop();
|
|
}
|
|
|
|
Future<void> sendRegion(Rect? bounds) =>
|
|
connection.setRegion(bounds, dropPolicy.configuration);
|
|
|
|
void report(Rect r) {
|
|
if (!mounted || !connection.isOwner) return;
|
|
final next = active ? r : null;
|
|
if (region == next) return;
|
|
region = next;
|
|
unawaited(sendRegion(next));
|
|
if (next == null && hasPreview && !busy) setState(clear);
|
|
}
|
|
|
|
Future<void> release(int id, Future<void> pending) async {
|
|
await pending;
|
|
await connection.release(id);
|
|
}
|
|
|
|
void clear() {
|
|
if (incomingClip != null) {
|
|
view.clearIncomingClip();
|
|
}
|
|
incomingClip?.cancel();
|
|
incomingClip = null;
|
|
lastPlacement = null;
|
|
lastCamera = null;
|
|
if (native && sessionId != null) {
|
|
unawaited(release(sessionId!, _preparer.waitForCompletion()));
|
|
}
|
|
generation++;
|
|
sessionId = null;
|
|
file = null;
|
|
projectAsset = null;
|
|
_preparer.reset();
|
|
placementError = null;
|
|
}
|
|
|
|
Future<void> event(NativeFileDropEvent event) async {
|
|
if (!mounted || !connection.isOwner) return;
|
|
final id = event.session;
|
|
final localPosition = event.position;
|
|
switch (event.phase) {
|
|
case NativeFileDropPhase.enter:
|
|
final files = [
|
|
for (final path in event.paths) ExternalMediaFile(path: path),
|
|
];
|
|
if (!active || !dropPolicy.accepts(files)) {
|
|
await release(id, Future<void>.value());
|
|
return;
|
|
}
|
|
enterExternal(id, event.paths.single, localPosition);
|
|
case NativeFileDropPhase.move:
|
|
moveExternal(id, localPosition);
|
|
case NativeFileDropPhase.exit:
|
|
exitExternal(id);
|
|
case NativeFileDropPhase.drop:
|
|
await dropExternal(id, localPosition);
|
|
}
|
|
}
|
|
|
|
void prepareMedia() {
|
|
final source = projectAsset == null
|
|
? TimelineDropSource.file(file!)
|
|
: TimelineDropSource.asset(projectAsset!);
|
|
unawaited(_preparer.probe(source));
|
|
}
|
|
|
|
Future<void> drop() async {
|
|
if (!active || !hasPreview) return;
|
|
final box = surface.currentContext?.findRenderObject() as RenderBox?;
|
|
if (box == null || !(Offset.zero & box.size).contains(position)) {
|
|
setState(clear);
|
|
return;
|
|
}
|
|
final g = generation;
|
|
try {
|
|
await _preparer.commit(
|
|
(asset, media) async {
|
|
incomingClip!.commit(assetId: asset.id, uri: media.uri);
|
|
},
|
|
isCurrent: () => mounted && g == generation,
|
|
validatePlacement: () {
|
|
updateClip();
|
|
if (incomingClip?.layout == null ||
|
|
error != null ||
|
|
placementError != null) {
|
|
throw AppException.fromFailure(
|
|
error ??
|
|
placementError ??
|
|
const AppFailure(AppErrorCode.mediaReadFailed),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(context.l10n.errorMessage(e))));
|
|
}
|
|
} finally {
|
|
if (mounted && g == generation) {
|
|
setState(clear);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.incoming?.removeListener(incomingChanged);
|
|
view.removeListener(viewChanged);
|
|
clear();
|
|
// Flutter mounts the replacement before disposing the old target. Only
|
|
// the current owner may unregister the shared native channel and region.
|
|
if (native) connection.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
TickerMode.valuesOf(context);
|
|
ref.watch(projectsProvider);
|
|
ref.watch(_preparationProvider);
|
|
ref.listen(_preparationProvider.select((state) => state.preparation), (
|
|
_,
|
|
next,
|
|
) {
|
|
if (mounted && hasPreview && !next.isLoading) setState(updateClip);
|
|
});
|
|
final preview = Stack(
|
|
key: surface,
|
|
fit: StackFit.expand,
|
|
children: [
|
|
widget.child,
|
|
if (busy || hasPreview && incomingClip == null && error == null)
|
|
const DelayedLoadingIndicator(
|
|
child: Align(
|
|
alignment: Alignment.topCenter,
|
|
child: LinearProgressIndicator(),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
return native
|
|
? NativeBoundsReporter(onBounds: report, child: preview)
|
|
: preview;
|
|
}
|
|
}
|