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.
319 lines
11 KiB
Dart
319 lines
11 KiB
Dart
import 'dart:math' as math;
|
||
|
||
import 'package:cutdex_sdk/editor.dart';
|
||
import 'package:cutdex_sdk/timeline/engine.dart';
|
||
import 'package:cutdex_sdk/timeline/layout.dart';
|
||
import 'package:cutdex_sdk/timeline/model.dart';
|
||
import 'package:flutter/material.dart';
|
||
|
||
import '../../l10n/l10n.dart';
|
||
|
||
import 'package:cutdex_ui/theme.dart';
|
||
import 'package:cutdex_ui/widgets.dart';
|
||
|
||
import '../../workspace/timeline_session.dart';
|
||
|
||
class SpeedProperties extends StatefulWidget {
|
||
const SpeedProperties({
|
||
super.key,
|
||
required this.session,
|
||
required this.sequence,
|
||
required this.clip,
|
||
});
|
||
final EditorTimelineSession session;
|
||
final SequenceId sequence;
|
||
final ElementId clip;
|
||
|
||
@override
|
||
State<SpeedProperties> createState() => _SpeedPropertiesState();
|
||
}
|
||
|
||
class _SpeedPropertiesState extends State<SpeedProperties> {
|
||
double? _timeline;
|
||
TimelineVersion? _version;
|
||
LayoutConnectionEditPreview? _preview;
|
||
double? _previewSpeed;
|
||
(Object, StackTrace)? _previewFailure;
|
||
|
||
double _speed(double position) =>
|
||
(math.pow(10, position * 3 - 1).toDouble() * 100).round() / 100;
|
||
|
||
void _updatePreview(double speed) {
|
||
final view = widget.session.view(widget.sequence);
|
||
_preview?.cancel();
|
||
_preview = null;
|
||
_previewSpeed = null;
|
||
_previewFailure = null;
|
||
try {
|
||
final preview = widget.session.previewClipSpeed(
|
||
widget.sequence,
|
||
widget.clip,
|
||
speed,
|
||
);
|
||
_preview = preview;
|
||
_previewSpeed = preview == null ? null : speed;
|
||
if (preview == null) {
|
||
view.clearLayoutPreview();
|
||
} else {
|
||
view.showLayoutPreview(preview.result.version);
|
||
}
|
||
} catch (error, stack) {
|
||
_previewFailure = (error, stack);
|
||
view.clearLayoutPreview();
|
||
_showError(error);
|
||
}
|
||
}
|
||
|
||
void _commit(double speed) {
|
||
final view = widget.session.view(widget.sequence);
|
||
if (widget.session.activeSequenceId == widget.sequence &&
|
||
(_version == null || identical(_version, view.version.kernelVersion))) {
|
||
try {
|
||
widget.session.runEdit(
|
||
'setSpeed',
|
||
widget.sequence,
|
||
() {
|
||
if (_previewSpeed != speed) _updatePreview(speed);
|
||
final preview = _preview;
|
||
final failure = _previewFailure;
|
||
_preview = null;
|
||
_previewSpeed = null;
|
||
_previewFailure = null;
|
||
if (preview == null) {
|
||
view.clearLayoutPreview();
|
||
if (failure != null) {
|
||
widget.session.reportEditFailure(failure.$1, failure.$2);
|
||
}
|
||
} else {
|
||
widget.session.commitClipSpeedPreview(widget.sequence, preview);
|
||
}
|
||
},
|
||
context: {
|
||
'clipIds': [widget.clip.value],
|
||
'entryPoint': 'property',
|
||
'playheadTick': view.playheadTick,
|
||
},
|
||
);
|
||
} catch (error) {
|
||
_preview?.cancel();
|
||
_preview = null;
|
||
_previewSpeed = null;
|
||
view.clearLayoutPreview();
|
||
_showError(error);
|
||
}
|
||
} else {
|
||
_discardPreview();
|
||
}
|
||
setState(() {
|
||
_timeline = null;
|
||
_version = null;
|
||
});
|
||
}
|
||
|
||
void _discardPreview() {
|
||
_preview?.cancel();
|
||
_preview = null;
|
||
_previewSpeed = null;
|
||
_previewFailure = null;
|
||
widget.session.view(widget.sequence).clearLayoutPreview();
|
||
}
|
||
|
||
void _showError(Object error) {
|
||
if (!mounted) return;
|
||
ScaffoldMessenger.of(
|
||
context,
|
||
).showSnackBar(SnackBar(content: Text(context.l10n.errorMessage(error))));
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_discardPreview();
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final view = widget.session.view(widget.sequence);
|
||
return ListenableBuilder(
|
||
listenable: Listenable.merge([view, widget.session]),
|
||
builder: (context, _) {
|
||
final query = view.version.kernelVersion.query();
|
||
final handle = query.elementById(widget.clip);
|
||
final record = handle == null ? null : query.element(handle);
|
||
final segments = record == null
|
||
? null
|
||
: widget.session.clipSpeedSegments(record);
|
||
final saved = segments?.length == 1 ? segments!.single.speed : null;
|
||
final enabled =
|
||
saved != null && widget.session.acceptsEdits && !view.isDragging;
|
||
final canReset =
|
||
segments != null && widget.session.acceptsEdits && !view.isDragging;
|
||
final localPlayhead = handle == null
|
||
? 0
|
||
: view.playheadTick - query.absoluteStart(handle);
|
||
final custom = widget.session.isCustomSpeedEnabled(
|
||
widget.sequence,
|
||
widget.clip,
|
||
);
|
||
final canSplit =
|
||
custom &&
|
||
segments != null &&
|
||
localPlayhead > 0 &&
|
||
localPlayhead < (record?.duration ?? 0) &&
|
||
!segments.any((segment) => segment.projectStart == localPlayhead) &&
|
||
widget.session.acceptsEdits &&
|
||
!view.isDragging;
|
||
final value =
|
||
_version == null || identical(_version, view.version.kernelVersion)
|
||
? _timeline ?? saved ?? 1
|
||
: saved ?? 1;
|
||
return Padding(
|
||
padding: AppSpacing.panelSection,
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
Row(
|
||
children: [
|
||
Expanded(
|
||
child: Text(
|
||
context.l10n.speed,
|
||
style: AppTheme.propertySectionTitleStyle(context),
|
||
),
|
||
),
|
||
Text(
|
||
saved == null && segments != null
|
||
? context.l10n.segmentedSpeed
|
||
: '${value.toStringAsFixed(2)}×',
|
||
key: const Key('clip-speed-value'),
|
||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||
),
|
||
),
|
||
const SizedBox(width: AppSpacing.small),
|
||
TextButton(
|
||
onPressed: canReset
|
||
? () {
|
||
_version = null;
|
||
_commit(1);
|
||
}
|
||
: null,
|
||
child: const Text('1×'),
|
||
),
|
||
],
|
||
),
|
||
if (saved != null) ...[
|
||
const SizedBox(height: AppSpacing.small),
|
||
AppSlider(
|
||
key: const Key('clip-speed-slider'),
|
||
value: ((math.log(value.clamp(0.1, 100)) / math.ln10 + 1) / 3)
|
||
.clamp(0, 1),
|
||
semanticFormatterCallback: (position) =>
|
||
'${_speed(position).toStringAsFixed(2)}×',
|
||
onChangeStart: enabled
|
||
? (_) {
|
||
_discardPreview();
|
||
_version = view.version.kernelVersion;
|
||
}
|
||
: null,
|
||
onChanged: enabled
|
||
? (position) {
|
||
if (_version != null &&
|
||
!identical(
|
||
_version,
|
||
view.version.kernelVersion,
|
||
)) {
|
||
_discardPreview();
|
||
setState(() => _timeline = null);
|
||
return;
|
||
}
|
||
final speed = _speed(position);
|
||
setState(() => _timeline = speed);
|
||
_updatePreview(speed);
|
||
}
|
||
: null,
|
||
onChangeEnd: enabled
|
||
? (position) => _commit(_speed(position))
|
||
: null,
|
||
),
|
||
Row(
|
||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||
children: [
|
||
Text('0.1×', style: Theme.of(context).textTheme.bodySmall),
|
||
Text('100×', style: Theme.of(context).textTheme.bodySmall),
|
||
],
|
||
),
|
||
] else if (segments != null)
|
||
Text(
|
||
context.l10n.speedSegmentCount(segments.length),
|
||
style: Theme.of(context).textTheme.bodySmall,
|
||
),
|
||
const SizedBox(height: AppSpacing.small),
|
||
Row(
|
||
children: [
|
||
Expanded(
|
||
child: Text(
|
||
context.l10n.custom,
|
||
style: AppTheme.propertySectionTitleStyle(context),
|
||
),
|
||
),
|
||
AppSwitch(
|
||
key: const Key('clip-custom-speed-toggle'),
|
||
value: custom,
|
||
semanticLabel: context.l10n.custom,
|
||
onChanged: canReset
|
||
? (value) {
|
||
_discardPreview();
|
||
setState(() {
|
||
_timeline = null;
|
||
_version = null;
|
||
});
|
||
widget.session.setCustomSpeedEnabled(
|
||
widget.sequence,
|
||
widget.clip,
|
||
value,
|
||
);
|
||
}
|
||
: null,
|
||
),
|
||
],
|
||
),
|
||
const SizedBox(height: AppSpacing.small),
|
||
SizedBox(
|
||
width: double.infinity,
|
||
height: AppControlSize.propertyControl,
|
||
child: OutlinedButton.icon(
|
||
key: const Key('split-speed-segment'),
|
||
onPressed: canSplit
|
||
? () {
|
||
try {
|
||
widget.session.splitClipSpeedAt(
|
||
widget.sequence,
|
||
widget.clip,
|
||
localPlayhead,
|
||
);
|
||
} catch (error) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
SnackBar(
|
||
content: Text(context.l10n.errorMessage(error)),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
: null,
|
||
icon: const Icon(Icons.content_cut),
|
||
label: Text(context.l10n.splitSpeedAtPlayhead),
|
||
),
|
||
),
|
||
if (segments == null)
|
||
Text(
|
||
context.l10n.speedNotAvailable,
|
||
style: Theme.of(context).textTheme.bodySmall,
|
||
),
|
||
],
|
||
),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
}
|