Files
cutdex/lib/features/properties/speed_properties.dart
T
lingjhf 2e0033ce2d
Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s
feat(workspace): refresh desktop editing experience and prepare 0.1.2
2026-10-09 01:56:01 +08:00

446 lines
16 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 '../../shared/widgets/listenable_selector.dart';
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/icons.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 ListenableSelector<Object>(
listenable: Listenable.merge([view, widget.session]),
select: () {
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 custom = widget.session.isCustomSpeedEnabled(
widget.sequence,
widget.clip,
);
final tick = handle == null
? 0
: view.playheadTick - query.absoluteStart(handle);
final split =
custom &&
segments != null &&
tick > 0 &&
tick < record!.duration &&
!segments.any((segment) => segment.projectStart == tick);
return (
record,
custom,
split,
widget.session.acceptsEdits,
view.isDragging,
_version == null || identical(_version, view.version.kernelVersion),
);
},
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: AppTheme.propertySectionPadding(context),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ConstrainedBox(
constraints: BoxConstraints(
minHeight: AppTheme.isDesktop(context)
? AppInspectorMetrics.action
: AppControlSize.propertyControl,
),
child: 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,
),
),
SizedBox(width: AppTheme.propertyRowGap(context)),
TextButton(
style: AppTheme.isDesktop(context)
? TextButton.styleFrom(
minimumSize: const Size(24, 24),
padding: EdgeInsets.zero,
)
: null,
onPressed: canReset
? () {
_version = null;
_commit(1);
}
: null,
child: const Text('1×'),
),
],
),
),
if (saved != null) ...[
SizedBox(height: AppTheme.propertyRowGap(context)),
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,
),
SizedBox(
height: AppTheme.isDesktop(context)
? AppInspectorMetrics.groupGap
: AppSpacing.small,
),
if (AppTheme.isDesktop(context))
AppPropertyGroup(
label: context.l10n.custom,
actions: [
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,
),
],
child: SizedBox(
width: double.infinity,
height: AppTheme.isDesktop(context)
? AppInspectorMetrics.input
: AppControlSize.propertyControl,
child: OutlinedButton.icon(
key: const Key('split-speed-segment'),
onPressed: canSplit
? () {
try {
final current = view.version.kernelVersion
.query();
final target = current.elementById(widget.clip);
if (target == null) return;
widget.session.splitClipSpeedAt(
widget.sequence,
widget.clip,
view.playheadTick -
current.absoluteStart(target),
);
} catch (error) {
ErrorNotificationHost.of(context).show(
ErrorNotification(
content: Text(
context.l10n.errorMessage(error),
),
details: error.toString(),
),
);
}
}
: null,
icon: const Icon(AppIcons.cut),
label: Text(context.l10n.splitSpeedAtPlayhead),
),
),
)
else ...[
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,
),
],
),
SizedBox(height: AppTheme.propertyRowGap(context)),
SizedBox(
width: double.infinity,
height: AppTheme.isDesktop(context)
? AppInspectorMetrics.input
: AppControlSize.propertyControl,
child: OutlinedButton.icon(
key: const Key('split-speed-segment'),
onPressed: canSplit
? () {
try {
final current = view.version.kernelVersion
.query();
final target = current.elementById(widget.clip);
if (target == null) return;
widget.session.splitClipSpeedAt(
widget.sequence,
widget.clip,
view.playheadTick -
current.absoluteStart(target),
);
} catch (error) {
ErrorNotificationHost.of(context).show(
ErrorNotification(
content: Text(
context.l10n.errorMessage(error),
),
details: error.toString(),
),
);
}
}
: null,
icon: const Icon(AppIcons.cut),
label: Text(context.l10n.splitSpeedAtPlayhead),
),
),
],
if (segments == null)
Text(
context.l10n.speedNotAvailable,
style: Theme.of(context).textTheme.bodySmall,
),
],
),
);
},
);
}
}