import 'package:cutdex_sdk/timeline/engine.dart'; import 'package:cutdex_sdk/timeline/layout.dart'; import 'package:cutdex_sdk/timeline/model.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../workspace/timeline_provider.dart'; import '../../workspace/timeline_session.dart'; import 'properties_presentation_state.dart'; final propertiesInspectorProvider = Provider.autoDispose .family((ref, identity) { final session = ref .watch(editorTimelineProvider(identity.$1, identity.$2)) .value; if (session == null) return null; final controller = PropertiesInspectorController(session); ref.onDispose(controller.dispose); return controller; }); /// Resolves the shared editing selection without prescribing a device layout. class PropertiesInspectorController extends ChangeNotifier { PropertiesInspectorController(this.session) { session.addListener(_sessionChanged); _bindView(); _captureSelection(); } final EditorTimelineSession session; Listenable? _editingChanges; SequenceId? _lastSequence; Object? _lastVersion; Set _lastSelection = {}; List _lastShell = const []; SequenceId? get sequence => session.activeSequenceId; void _bindView() { final id = sequence; final next = id == null ? null : session.view(id).editingChanges; if (identical(next, _editingChanges)) return; _editingChanges?.removeListener(_viewChanged); _editingChanges = next; _editingChanges?.addListener(_viewChanged); } bool _captureSelection({bool force = false}) { final id = sequence; final view = id == null ? null : session.view(id); final version = view?.version.kernelVersion; final selected = view?.selectedIdentities ?? const {}; if (!force && id == _lastSequence && identical(version, _lastVersion) && setEquals(selected, _lastSelection)) { return false; } final shell = [ for (final clip in selectedClips) ( clip.id, clip.structure, clip.enabled, clip.payloadRef == null ? null : session.payloadFor(clip.payloadRef!)?.type, clip.payloadRef == null ? null : session.mediaBinding(clip.payloadRef!)?.audioSelection, clip.payloadRef == null ? null : session.mediaBinding(clip.payloadRef!)?.videoTrackIndex, ), ]; final changed = id != _lastSequence || !listEquals(shell, _lastShell); _lastSequence = id; _lastVersion = version; _lastSelection = selected.toSet(); _lastShell = shell; return changed; } void _viewChanged() { // The inspector's selection shell does not depend on the playhead. Its // property editors subscribe separately to position and live edit changes. if (_captureSelection()) notifyListeners(); } void _sessionChanged() { _bindView(); if (_captureSelection(force: true)) notifyListeners(); } List get selectedClips { final id = sequence; if (id == null) return const []; final view = session.view(id); final query = view.version.kernelVersion.query(); final handles = {}; for (final identity in view.selectedIdentities) { final handle = switch (identity) { ElementHandle handle => handle, LayoutBodyId body => view.elementForIdentity(body), _ => null, }; if (handle != null && query.ownerOf(handle) != null) handles.add(handle); } final clips = [for (final handle in handles) query.element(handle)]; return clips.any((clip) => clip.structure != ElementStructure.gap) ? clips : const []; } @override void dispose() { session.removeListener(_sessionChanged); _editingChanges?.removeListener(_viewChanged); super.dispose(); } } List categoriesForClip(ElementRecord clip, String? type) { if (clip.structure != ElementStructure.content) { return [PropertyCategory.basic]; } return switch (type) { 'video' => [ PropertyCategory.picture, PropertyCategory.color, PropertyCategory.mask, PropertyCategory.audio, PropertyCategory.speed, PropertyCategory.animation, ], 'audio' => [PropertyCategory.audio, PropertyCategory.speed], 'image' => [ PropertyCategory.picture, PropertyCategory.mask, PropertyCategory.animation, ], _ => [PropertyCategory.basic], }; }