Gitea CI / Build Windows x64 app (push) Successful in 2m38s
Gitea CI / Check code and tests (push) Successful in 5m19s
Gitea CI / Build Android APK (push) Successful in 3m13s
Gitea CI / Audit and package Windows x64 (push) Successful in 2m49s
Gitea CI / Assemble and validate CI artifacts (push) Successful in 59s
Gitea CI / Gitea CI passed (push) Successful in 2s
122 lines
3.7 KiB
Dart
122 lines
3.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cutdex_sdk/preview.dart';
|
|
import 'package:cutdex_sdk/runtime.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import 'preview_target.dart';
|
|
import 'timeline_session.dart';
|
|
|
|
part 'preview_controller_registry.g.dart';
|
|
|
|
@Riverpod(keepAlive: true)
|
|
PreviewControllerRegistry previewControllerRegistry(Ref ref) {
|
|
final registry = PreviewControllerRegistry(
|
|
ref.read(runtimeConnectionProvider),
|
|
onChanged: () => scheduleMicrotask(() {
|
|
if (ref.mounted) ref.notifyListeners();
|
|
}),
|
|
);
|
|
ref.onDispose(registry.dispose);
|
|
return registry;
|
|
}
|
|
|
|
/// Owns native preview controllers independently of the currently visible
|
|
/// timeline. Prepared controllers make timeline selection a synchronous swap.
|
|
final class PreviewControllerRegistry {
|
|
PreviewControllerRegistry(this._runtime, {this._onChanged});
|
|
|
|
final CutdexRuntime _runtime;
|
|
final void Function()? _onChanged;
|
|
final _controllers = <(String, String), PreviewController>{};
|
|
final _preparing = <(String, String), Future<PreviewController>>{};
|
|
bool _disposed = false;
|
|
|
|
PreviewController? cached((String, String) ids) => _controllers[ids];
|
|
|
|
FutureOr<PreviewController?> prepare(
|
|
(String, String) ids,
|
|
EditorTimelineSession timeline,
|
|
) {
|
|
if (_disposed) throw StateError('Preview registry is closed');
|
|
final sequence = timeline.activeSequenceId;
|
|
if (sequence == null || timeline.sequenceDurationTicks(sequence) == 0) {
|
|
return null;
|
|
}
|
|
final pending = _preparing[ids];
|
|
if (pending != null) return pending;
|
|
final cached = _controllers[ids];
|
|
if (cached != null) return cached;
|
|
final future = _initialize(ids, timeline);
|
|
_preparing[ids] = future;
|
|
return future;
|
|
}
|
|
|
|
Future<PreviewController> _initialize(
|
|
(String, String) ids,
|
|
EditorTimelineSession timeline,
|
|
) async {
|
|
final controller = PreviewController(
|
|
timeline: timeline,
|
|
activeSequence: () => timeline.activeSequenceId,
|
|
viewForSequence: timeline.view,
|
|
runtime: _runtime,
|
|
);
|
|
// Mount native output while initialization is pending. Android exact seeks
|
|
// cannot complete their first frame before the SurfaceView is attached.
|
|
_controllers[ids] = controller;
|
|
_onChanged?.call();
|
|
try {
|
|
await controller.initialize();
|
|
if (_disposed || !identical(_controllers[ids], controller)) {
|
|
await controller.close();
|
|
throw StateError('Preview registry is closed');
|
|
}
|
|
return controller;
|
|
} catch (_) {
|
|
if (identical(_controllers[ids], controller)) {
|
|
_controllers.remove(ids);
|
|
_onChanged?.call();
|
|
}
|
|
await controller.close().catchError((Object _) {});
|
|
rethrow;
|
|
} finally {
|
|
_preparing.remove(ids);
|
|
}
|
|
}
|
|
|
|
Future<void> invalidate((String, String) ids) async {
|
|
final controller = _controllers.remove(ids);
|
|
if (controller != null) _onChanged?.call();
|
|
if (controller != null) await controller.close();
|
|
}
|
|
|
|
Future<void> retainProject(
|
|
String projectId, {
|
|
Set<String> keep = const {},
|
|
}) async {
|
|
for (final entry in _controllers.entries.toList()) {
|
|
if (entry.key.$1 != projectId && keep.contains(entry.key.$1)) {
|
|
await entry.value.pauseForCanvasInteraction();
|
|
}
|
|
}
|
|
final stale = _controllers.keys
|
|
.where((ids) => ids.$1 != projectId && !keep.contains(ids.$1))
|
|
.toList();
|
|
for (final ids in stale) {
|
|
await invalidate(ids);
|
|
}
|
|
}
|
|
|
|
void dispose() {
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
final controllers = _controllers.values.toList();
|
|
_controllers.clear();
|
|
_preparing.clear();
|
|
for (final controller in controllers) {
|
|
unawaited(controller.close().catchError((Object _) {}));
|
|
}
|
|
}
|
|
}
|