Files
cutdex/lib/features/properties/speed_properties.dart
T
lingjhf 1f73765745
Gitea CI / Windows x64 and Android (pull_request) Successful in 16m21s
Gitea CI / Gitea CI passed (pull_request) Successful in 3s
refactor(desktop): reorganize workspace panels and navigation
2026-10-06 12:11:59 +08:00

325 lines
11 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:cutdex/shared/widgets/error_notification_host.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;
ErrorNotificationHost.of(context).show(
ErrorNotification(
content: Text(context.l10n.errorMessage(error)),
details: error.toString(),
),
);
}
@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) {
ErrorNotificationHost.of(context).show(
ErrorNotification(
content: Text(context.l10n.errorMessage(error)),
details: error.toString(),
),
);
}
}
: 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,
),
],
),
);
},
);
}
}