import 'dart:async'; import 'package:cutdex_core/editor.dart'; import 'package:cutdex_core/timeline/engine.dart'; import 'package:cutdex_core/timeline/layout.dart'; import 'package:cutdex_core/timeline/model.dart'; import 'package:cutdex_core/tools.dart'; import '../workspace/timeline_session.dart'; final _sessionTools = Expando>(); final _toolReferences = Expando>(); /// Remove a dispatcher from its session cache before closing admission. Future releaseEditorTools( EditorSession session, EditorToolDispatcher dispatcher, ) async { final references = _toolReferences[session]; final remaining = (references?[dispatcher] ?? 1) - 1; if (remaining > 0) { references![dispatcher] = remaining; return; } references?.remove(dispatcher); _sessionTools[session]?.removeWhere( (_, value) => identical(value, dispatcher), ); await dispatcher.dispose(); } EditorToolDispatcher editorToolsFor( EditorSession session, SequenceId sequence, ) { session.commands.ensureOpen(sequence); var tools = _sessionTools[session]; if (tools == null) { final owned = {}; _sessionTools[session] = tools = owned; session.onDispose(() { for (final dispatcher in owned.values) { unawaited(dispatcher.dispose()); } }); } final dispatcher = tools.putIfAbsent( sequence, () => EditorToolDispatcher(host: _EditorCommandHost(session, sequence)), ); final references = _toolReferences[session] ??= {}; references[dispatcher] = (references[dispatcher] ?? 0) + 1; return dispatcher; } /// Maps SDK command outcomes to tools and adapts the app-owned UI selection. final class _EditorCommandHost extends EditorSessionCommandHost { const _EditorCommandHost(super.session, super.sequence); @override Set get supportedOperations => { ...session.supportedCommandOperations.where( (operation) => operation != EditorOperation.startVocalSeparation, ), if (session is EditorTimelineSession) ...{ EditorOperation.querySelection, EditorOperation.setSelection, }, }; @override Future execute( EditorCommand command, CommandContext context, ) async { if (command is StartVocalSeparation) { throw const CommandRejected( 'unsupported', 'Vocal separation is temporarily unavailable', ); } return _execute(command, context); } Future _execute( EditorCommand command, CommandContext context, ) async { if (command is QuerySelection || command is SetSelection) { session.commands.ensureOpen(sequence); if (context.isCancelled()) { throw const CommandRejected('cancelled', 'Cancelled before execution'); } if (!supportedOperations.contains(command.operation)) { throw const CommandRejected( 'unsupported', 'Operation is not available', ); } if (command.expectedVersion != null && command.expectedVersion != session.version) { throw const CommandRejected( 'staleVersion', 'Read the current state before editing', ); } return _selection(command); } return super.execute(command, context); } CommandResult _selection(EditorCommand command) { final ui = session as EditorTimelineSession; final view = ui.view(sequence); final query = view.version.kernelVersion.query(); if (command is SetSelection) { if (view.isDragging) { throw const CommandRejected('busy', 'Finish the current gesture first'); } if (command.clipIds.length > 1024 || command.clipIds.toSet().length != command.clipIds.length) { throw const CommandRejected( 'invalidArguments', 'Invalid clip selection', ); } final handles = []; for (final id in command.clipIds) { final handle = query.elementById(ElementId(id)); if (handle == null || query.ownerOf(handle) == null) { throw const CommandRejected( 'missingClip', 'Selected clip does not exist', ); } handles.add(handle); } view.setSelection([ for (final handle in handles) session.captionComponentFor(sequence, handle) ?? (query.connectionForTarget(handle) == null ? handle : LayoutBodyId.element(handle)), ]); } final selected = {}; for (final identity in view.selectedIdentities) { final handle = switch (identity) { ElementHandle value => value, LayoutBodyId value => view.elementForIdentity(value), _ => null, }; if (handle != null && query.ownerOf(handle) != null) { selected.add(query.element(handle).id.value); } } final keyframe = view.selectedKeyframe; return CommandResult( status: CommandStatus.succeeded, message: 'UI selection', version: session.version, data: { 'sequenceId': sequence.value, 'active': ui.activeSequenceId == sequence, 'clipIds': selected.toList(), 'keyframe': keyframe == null ? null : { 'clipId': query.element(keyframe.element).id.value, 'offset': _time(keyframe.offset), }, }, ); } } Map _time(int ticks) { final value = TimelineClock.standard.fromTicks(ticks); return {'numerator': '${value.numerator}', 'denominator': value.denominator}; }