Files
cutdex/lib/features/timeline/timeline_speed_layout.dart
lingjhf 38ca180cf7
Gitea CI / Check code and tests (pull_request) Failing after 8s
Gitea CI / Build Android APK (pull_request) Skipped
Gitea CI / Build Windows x64 app (pull_request) Skipped
Gitea CI / Audit and package Windows x64 (pull_request) Skipped
Gitea CI / Assemble and validate CI artifacts (pull_request) Skipped
Gitea CI / Gitea CI passed (pull_request) Failing after 1s
refactor(core): own editor UI and pin verified native releases
2026-10-12 03:20:29 +08:00

96 lines
2.9 KiB
Dart

import 'dart:math' as math;
import 'dart:ui';
import 'package:cutdex_core/editor.dart';
import 'package:cutdex_timeline_ui/cutdex_timeline_ui.dart';
/// Speed segments in the clip's local coordinates. Paint and hit targets use
/// the same cells, including the minimum pixel width and clipping at low zoom.
final class TimelineSpeedLayout {
TimelineSpeedLayout({
required TimelineClipLayout clip,
required Iterable<ClipSpeedSegment> segments,
required double pixelsPerTick,
}) {
bounds = Rect.fromLTWH(
clip.header.left,
clip.header.top,
clip.header.width,
math.min(
clip.header.height,
EditorLayoutConfiguration.speedSectionHeight.toDouble(),
),
);
var left = bounds.left;
final cells = <TimelineSpeedSegmentLayout>[];
for (final segment in segments) {
final width = math.max(1.0, segment.projectDuration * pixelsPerTick);
final right = math.min(bounds.right, left + width);
final cell = Rect.fromLTWH(
math.min(left, bounds.right),
bounds.top,
math.max(0, right - left),
bounds.height,
);
cells.add(TimelineSpeedSegmentLayout(segment, cell));
left += width;
}
this.segments = List.unmodifiable(cells);
}
late final Rect bounds;
late final List<TimelineSpeedSegmentLayout> segments;
}
final class TimelineSpeedSegmentLayout {
TimelineSpeedSegmentLayout(this.segment, this.cell) {
bar = Rect.fromLTWH(
cell.left,
cell.top,
math.max(0, cell.width - segmentGap),
cell.height,
);
label = Rect.fromLTWH(
bar.left + math.min(labelPadding, bar.width / 2),
bar.top + 1,
math.max(0, bar.width - labelPadding * 2),
math.max(0, bar.height - 2),
);
final horizontalInset = math.min(handleInset, bar.width / 2);
final verticalInset = math.min(handleInset, bar.height / 2);
final handleRight = bar.right - horizontalInset;
final availableWidth = math.max(0.0, bar.width - horizontalInset * 2);
final handleTop = bar.top + verticalInset;
final handleHeight = math.max(0.0, bar.height - verticalInset * 2);
handle = Rect.fromLTWH(
handleRight - math.min(handleWidth, availableWidth),
handleTop,
math.min(handleWidth, availableWidth),
handleHeight,
);
final hitWidth = math.min(handleHitWidth, bar.width);
handleHit = Rect.fromLTWH(
(handle.center.dx - hitWidth / 2).clamp(bar.left, bar.right - hitWidth),
bar.top,
hitWidth,
bar.height,
);
}
static const segmentGap = 2.0;
static const labelPadding = 8.0;
static const handleInset = 3.0;
static const handleWidth = 2.0;
static const handleHitWidth = 10.0;
static const handleRadius = 1.0;
final ClipSpeedSegment segment;
final Rect cell;
late final Rect bar;
late final Rect label;
late final Rect handle;
late final Rect handleHit;
double get speed => segment.speed;
}