Open source CapCut alternative.
TypeScript
Animate pointer prompt landing state
David Le1b0ca094d231
diff --git a/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swift b/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swiftindex 5b79d874..5255bcfd 100644--- a/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swift+++ b/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swift@@ -14,6 +14,10 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {@Published public private(set) var opacity: Double = 0@Published public private(set) var isHolding = false@Published public private(set) var isSelected = false+ @Published public private(set) var cursorAngleDegrees =+ PointerPromptSpawnGeometry.defaultExitAngleDegrees+ @Published public private(set) var terminalTailAngleDegrees = 0.0+ @Published public private(set) var isWorking = false@Published public private(set) var isLabelHovered = false@Published public private(set) var isLabelEditing = false@Published public var draftText = ""@@ -180,6 +184,12 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {self.screenSize = screenSizeself.opacity = 0self.isHolding = false+ self.cursorAngleDegrees = PointerPromptSpawnGeometry.angleDegrees(+ from: origin,+ to: destination+ )+ self.terminalTailAngleDegrees = 0+ self.isWorking = falseself.viewportOrigin = .zeroself.viewportSize = .zeroself.isLabelHovered = false@@ -222,8 +232,14 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {animationGeneration += 1let generation = animationGeneration+ cursorAngleDegrees = PointerPromptSpawnGeometry.angleDegrees(+ from: position,+ to: destination+ )+ terminalTailAngleDegrees = 0self.destination = destinationself.isHolding = false+ self.isWorking = falsewithAnimation(.easeOut(duration: 0.12)) {self.opacity = 1}@@ -242,6 +258,8 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {self.state = nilself.isHolding = false+ self.isWorking = false+ self.terminalTailAngleDegrees = 0self.isLabelHovered = falseself.isLabelEditing = falseself.draftText = ""@@ -343,6 +361,7 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {self.position = finalPositionself.isHolding = true+ self.playTerminalTailAnimation(generation: generation)if var state = self.state, state.phase == .traveling || state.phase == .notchCue {state.phase = .holdingstate.updatedAt = Date()@@ -352,6 +371,40 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {}}+ private func playTerminalTailAnimation(generation: Int) {+ isWorking = false+ terminalTailAngleDegrees = 0++ for frame in Self.terminalTailAnimationFrames {+ DispatchQueue.main.asyncAfter(deadline: .now() + frame.delay) { [weak self] in+ guard let self,+ self.animationGeneration == generation,+ self.isHolding else {+ return+ }++ withAnimation(.easeInOut(duration: Self.terminalTailFrameDuration)) {+ self.terminalTailAngleDegrees = frame.angleDegrees+ }+ }+ }++ DispatchQueue.main.asyncAfter(+ deadline: .now() + Self.terminalTailAnimationDuration+ ) { [weak self] in+ guard let self,+ self.animationGeneration == generation,+ self.isHolding else {+ return+ }++ withAnimation(.easeOut(duration: 0.12)) {+ self.terminalTailAngleDegrees = 0+ self.isWorking = true+ }+ }+ }+private func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {hypot(lhs.x - rhs.x, lhs.y - rhs.y)}@@ -376,6 +429,16 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {}public static let travelDuration: TimeInterval = 0.82+ public static let terminalTailAnimationDuration: TimeInterval = 0.70+ private static let terminalTailFrameDuration: TimeInterval = 0.08+ private static let terminalTailAnimationFrames: [(delay: TimeInterval, angleDegrees: Double)] = [+ (0.0875, -14),+ (0.2625, -7),+ (0.35, 0),+ (0.4375, 14),+ (0.6125, 7),+ (0.70, 0)+ ]fileprivate static let cursorVisualFrameSize = CGSize(width: 84, height: 112)fileprivate static let labelHorizontalPadding: CGFloat = 10fileprivate static let collapsedLabelContentWidth: CGFloat = 240@@ -498,7 +561,7 @@ public struct PointerPromptSpawnOverlayView: View {screenSize: CGSize) -> some View {ZStack(alignment: .topLeading) {- if viewModel.isHolding {+ if viewModel.isWorking {Circle().stroke(accentColor(for: state.accentIndex), lineWidth: 1.5).frame(@@ -545,7 +608,8 @@ public struct PointerPromptSpawnOverlayView: View {.shadow(color: Color.black.opacity(0.34), radius: 4, x: 0, y: 2).frame(width: 28, height: 28).rotationEffect(.degrees(cursorAngleDegrees + 50))- .scaleEffect(viewModel.isHolding ? holdingCursorScale : 1)+ .rotationEffect(.degrees(viewModel.terminalTailAngleDegrees))+ .scaleEffect(viewModel.isWorking ? holdingCursorScale : 1).animation(.easeInOut(duration: 1.4).repeatForever(autoreverses: true),value: haloPulseActive@@ -643,10 +707,7 @@ public struct PointerPromptSpawnOverlayView: View {}private var cursorAngleDegrees: Double {- PointerPromptSpawnGeometry.angleDegrees(- from: viewModel.position,- to: viewModel.destination- )+ viewModel.cursorAngleDegrees}private var haloPulseScale: CGFloat {diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/PointerPromptSpawnGeometryTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/PointerPromptSpawnGeometryTests.swiftindex b71598f5..1758c66d 100644--- a/apps/Donkey/Tests/DonkeyRuntimeTests/PointerPromptSpawnGeometryTests.swift+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/PointerPromptSpawnGeometryTests.swift@@ -103,6 +103,77 @@ struct PointerPromptSpawnGeometryTests {#expect(viewModel.localCursorCenter.y - viewModel.localLabelCenter(in: screenSize).y == 46)}+ @Test @MainActor+ func spawnOverlayKeepsTravelAngleAfterCursorArrives() async throws {+ let viewModel = PointerPromptSpawnOverlayViewModel()+ let origin = CGPoint(x: 600, y: -24)+ let destination = CGPoint(x: 420, y: 282)+ let screenSize = CGSize(width: 1200, height: 800)+ let state = PointerPromptSpawnState(+ id: "spawn-1",+ commandText: "hi there",+ label: "hi there",+ accentIndex: 1,+ phase: .traveling+ )+ let expectedAngle = PointerPromptSpawnGeometry.angleDegrees(+ from: origin,+ to: destination+ )++ viewModel.show(+ state: state,+ origin: origin,+ destination: destination,+ screenSize: screenSize+ )++ #expect(abs(viewModel.cursorAngleDegrees - expectedAngle) < 0.0001)++ try await Task.sleep(nanoseconds: 900_000_000)++ #expect(viewModel.position == destination)+ #expect(viewModel.isHolding)+ #expect(abs(viewModel.cursorAngleDegrees - expectedAngle) < 0.0001)+ }++ @Test @MainActor+ func spawnOverlayRunsTerminalTailAnimationBeforeWorkingPulse() async throws {+ let viewModel = PointerPromptSpawnOverlayViewModel()+ let origin = CGPoint(x: 600, y: -24)+ let destination = CGPoint(x: 420, y: 282)+ let state = PointerPromptSpawnState(+ id: "spawn-1",+ commandText: "hi there",+ label: "hi there",+ accentIndex: 1,+ phase: .traveling+ )++ viewModel.show(+ state: state,+ origin: origin,+ destination: destination,+ screenSize: CGSize(width: 1200, height: 800)+ )++ let landedBeforeWorkingDelay = UInt64(+ (PointerPromptSpawnOverlayViewModel.travelDuration + 0.15) * 1_000_000_000+ )+ try await Task.sleep(nanoseconds: landedBeforeWorkingDelay)++ #expect(viewModel.isHolding)+ #expect(!viewModel.isWorking)++ let workingDelay = UInt64(+ (PointerPromptSpawnOverlayViewModel.terminalTailAnimationDuration + 0.2) * 1_000_000_000+ )+ try await Task.sleep(nanoseconds: workingDelay)++ #expect(viewModel.isWorking)+ #expect(abs(viewModel.terminalTailAngleDegrees) < 0.0001)+ }+@Test @MainActorfunc spawnLabelOnlyExpandsOnHoverWhenCollapsedTextOverflows() {let viewModel = PointerPromptSpawnOverlayViewModel()