Files
cutdex/lib/integrations/editor_command_host.dart
T
lingjhf 38ca180cf7
Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
refactor(core): own editor UI and pin verified native releases
2026-10-12 03:20:29 +08:00

181 lines
5.6 KiB
Dart

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<Map<SequenceId, EditorToolDispatcher>>();
final _toolReferences = Expando<Map<EditorToolDispatcher, int>>();
/// Remove a dispatcher from its session cache before closing admission.
Future<void> 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 = <SequenceId, EditorToolDispatcher>{};
_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<EditorOperation> get supportedOperations => {
...session.supportedCommandOperations.where(
(operation) => operation != EditorOperation.startVocalSeparation,
),
if (session is EditorTimelineSession) ...{
EditorOperation.querySelection,
EditorOperation.setSelection,
},
};
@override
Future<CommandResult> execute(
EditorCommand command,
CommandContext context,
) async {
if (command is StartVocalSeparation) {
throw const CommandRejected(
'unsupported',
'Vocal separation is temporarily unavailable',
);
}
return _execute(command, context);
}
Future<CommandResult> _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 = <ElementHandle>[];
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 = <String>{};
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<String, Object?> _time(int ticks) {
final value = TimelineClock.standard.fromTicks(ticks);
return {'numerator': '${value.numerator}', 'denominator': value.denominator};
}