Open source CapCut alternative.
TypeScript
feat(app): make spawn pointers dismissible and user-draggable
David Le6307bd2f9027
diff --git a/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swift b/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swiftindex bba63835..73b42271 100644--- a/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swift+++ b/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swift@@ -10,6 +10,7 @@ final class UserQuerySpawnOverlayController {private var surfacesByID: [String: UserQuerySpawnSurface] = [:]private var viewModelsByID: [String: UserQuerySpawnOverlayViewModel] = [:]private var pendingGuideRequestsBySpawnID: [String: PointerCoachCursorGuideRequest] = [:]+ private var dismissedSpawnIDs: Set<String> = []private var windowResolver = MacWindowResolver()private var localOutsideClickMonitor: Any?private var globalOutsideClickMonitor: Any?@@ -38,7 +39,11 @@ final class UserQuerySpawnOverlayController {) {guard let screen else { return }- let visibleSpawnStates = spawnStates.filter { $0.phase != .notchCue }+ let allSpawnIDs = Set(spawnStates.map(\.id))+ dismissedSpawnIDs.formIntersection(allSpawnIDs)+ let visibleSpawnStates = spawnStates.filter {+ $0.phase != .notchCue && !dismissedSpawnIDs.contains($0.id)+ }guard !visibleSpawnStates.isEmpty else {if spawnStates.isEmpty {pendingGuideRequestsBySpawnID = [:]@@ -47,7 +52,6 @@ final class UserQuerySpawnOverlayController {return}- let allSpawnIDs = Set(spawnStates.map(\.id))let visibleIDs = Set(visibleSpawnStates.map(\.id))pendingGuideRequestsBySpawnID = pendingGuideRequestsBySpawnID.filter { allSpawnIDs.contains($0.key) }for spawnState in visibleSpawnStates {@@ -76,6 +80,7 @@ final class UserQuerySpawnOverlayController {surfacesByID = [:]viewModelsByID = [:]pendingGuideRequestsBySpawnID = [:]+ dismissedSpawnIDs = []}@discardableResult@@ -90,6 +95,10 @@ final class UserQuerySpawnOverlayController {return false}+ // Swallow guides for a pointer the user dismissed so the fallback+ // overlay does not resurrect it.+ guard !dismissedSpawnIDs.contains(spawnID) else { return true }+guard let surface = surfacesByID[spawnID] else {pendingGuideRequestsBySpawnID[spawnID] = requestreturn true@@ -211,19 +220,22 @@ final class UserQuerySpawnOverlayController {return}+ // A user-dragged cursor keeps its spot through model updates; only an+ // explicit new guide step moves it again.+ let effectiveDestination = surface.isUserPositioned ? surface.destination : destinationlet shouldRetarget = !surface.viewModel.freezesMovement &&- distance(from: surface.destination, to: destination) > 1+ distance(from: surface.destination, to: effectiveDestination) > 1surface.viewModel.update(state: spawnState,- destination: destination,+ destination: effectiveDestination,screenSize: screen.frame.size)if shouldRetarget {animateSurfaceTravel(surface,- to: destination,+ to: effectiveDestination,on: screen)return@@ -347,6 +359,47 @@ final class UserQuerySpawnOverlayController {animated: false)}+ surface.viewModel.dismissed = { [weak self] spawnID in+ guard let self else { return }++ self.dismissedSpawnIDs.insert(spawnID)+ self.fadeAndRemove(id: spawnID)+ }+ surface.viewModel.cursorDragged = { [weak self, weak surface] globalPoint in+ guard let self,+ let surface else {+ return+ }++ self.moveSurfaceCursor(surface, toGlobalPoint: globalPoint)+ }+ }++ /// Repositions a holding cursor under the user's drag. The global AppKit+ /// mouse point (bottom-left origin) is converted to the overlay's+ /// top-left-origin screen-local space before being applied.+ private func moveSurfaceCursor(+ _ surface: UserQuerySpawnSurface,+ toGlobalPoint globalPoint: CGPoint+ ) {+ let screenFrame = surface.screen.frame+ let localPoint = UserQuerySpawnGeometry.clampedPoint(+ CGPoint(+ x: globalPoint.x - screenFrame.minX,+ y: screenFrame.maxY - globalPoint.y+ ),+ in: screenFrame.size+ )+ surface.travelWorkItem?.cancel()+ surface.isTraveling = false+ surface.isUserPositioned = true+ surface.destination = localPoint+ surface.viewModel.setPosition(localPoint)+ layoutSurface(+ surface,+ on: surface.screen,+ animated: false+ )}private func animateSurfaceTravel(@@ -494,6 +547,11 @@ final class UserQuerySpawnOverlayController {preRotateDuration: step.preRotateDuration,travelDuration: step.travelDuration)+ // The view model refuses to move while the user drags the cursor or+ // edits the label; moving the panel anyway would tear the two apart.+ guard !surface.viewModel.freezesMovement else { return }++ surface.isUserPositioned = falseanimateSurfaceTravel(surface,to: destination,@@ -628,9 +686,12 @@ final class UserQuerySpawnOverlayController {private func fadeAndRemove(id spawnID: String) {guard let surface = surfacesByID[spawnID] else { return }+ // Already fading out — restarting would keep postponing the close on+ // every model update, leaving an invisible panel that swallows clicks.+ guard surface.removalWorkItem == nil else { return }surface.travelWorkItem?.cancel()- surface.removalWorkItem?.cancel()+ surface.panel.ignoresMouseEvents = truependingGuideRequestsBySpawnID[spawnID] = nilcancelGuide(for: surface)surface.viewModel.fadeOut()@@ -839,6 +900,7 @@ private final class UserQuerySpawnSurface {var screen: NSScreenvar destination: CGPointvar isTraveling = false+ var isUserPositioned = falsevar travelWorkItem: DispatchWorkItem?var removalWorkItem: DispatchWorkItem?var isPlayingGuide = falsediff --git a/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swift b/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swiftindex b13f4724..52a71309 100644--- a/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swift+++ b/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swift@@ -18,7 +18,7 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {UserQuerySpawnGeometry.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 isCursorDragging = false@Published public private(set) var isLabelEditing = false@Published public var draftText = ""@Published public private(set) var draftTextHeight: CGFloat = UserQuerySpawnOverlayViewModel.inlineEditorMinimumTextHeight@@ -28,6 +28,8 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {public var followUpSubmitted: ((String, String, String) -> Void)?public var selected: ((String) -> Void)?public var travelCompleted: (() -> Void)?+ public var dismissed: ((String) -> Void)?+ public var cursorDragged: ((CGPoint) -> Void)?private var animationGeneration = 0@@ -82,7 +84,9 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {}public var freezesMovement: Bool {- isLabelEditing || !draftText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty+ isCursorDragging ||+ isLabelEditing ||+ !draftText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty}private var isAwaitingResponse: Bool {@@ -102,8 +106,10 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {}public var displayLabelContentWidth: CGFloat {- let maximumWidth = isLabelHovered ? Self.expandedCollapsedLabelWidth : Self.collapsedLabelContentWidth- return Self.displayLabelContentSize(for: state?.label ?? "", maximumWidth: maximumWidth).width+ Self.displayLabelContentSize(+ for: state?.label ?? "",+ maximumWidth: Self.collapsedLabelContentWidth+ ).width}public func cursorOnlyVisualFrame(at point: CGPoint) -> CGRect {@@ -210,18 +216,14 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {if isLabelEditing {return Self.inlineEditorLabelSize(for: state?.label ?? "")}- guard isLabelHovered else {- return Self.collapsedLabelSize(for: state?.label ?? "")- }- return Self.expandedCollapsedLabelSize(for: state?.label ?? "")+ return Self.collapsedLabelSize(for: state?.label ?? "")}private func reservedPanelLabelSizes() -> [CGSize] {let text = state?.label ?? ""return [Self.collapsedLabelSize(for: text),- Self.expandedCollapsedLabelSize(for: text),Self.inlineEditorLabelSize(for: text)]}@@ -250,7 +252,7 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {self.isWorking = falseself.viewportOrigin = .zeroself.viewportSize = .zero- self.isLabelHovered = false+ self.isCursorDragging = falseself.isLabelEditing = falseself.draftText = ""self.draftTextHeight = Self.inlineEditorMinimumTextHeight@@ -363,7 +365,7 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {self.isHolding = falseself.isWorking = falseself.terminalTailAngleDegrees = 0- self.isLabelHovered = false+ self.isCursorDragging = falseself.isLabelEditing = falseself.draftText = ""self.draftTextHeight = Self.inlineEditorMinimumTextHeight@@ -380,14 +382,33 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {self.isSelected = isSelected}- public func setLabelHovered(_ isHovered: Bool) {- guard !isLabelEditing else { return }+ public func dismiss() {+ guard let state else { return }- let shouldExpand = isHovered && Self.collapsedLabelNeedsExpansion(state?.label ?? "")- guard isLabelHovered != shouldExpand else { return }+ dismissed?(state.id)+ }- isLabelHovered = shouldExpand- labelLayoutChanged?()+ /// Reports a user drag of the cursor at a global AppKit screen point; the+ /// controller owning the panel converts it to overlay coordinates and calls+ /// `setPosition`.+ public func reportCursorDrag(at globalPoint: CGPoint) {+ guard isHolding else { return }++ isCursorDragging = true+ cursorDragged?(globalPoint)+ }++ public func endCursorDrag() {+ isCursorDragging = false+ }++ /// Moves the holding cursor directly to a point (top-left-origin screen-local+ /// coordinates), bypassing travel animation — used while the user drags it.+ public func setPosition(_ point: CGPoint) {+ animationGeneration += 1+ position = point+ destination = point+ isHolding = true}public func select() {@@ -405,7 +426,6 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {}selected?(state.id)- isLabelHovered = falsedraftText = ""draftTextHeight = Self.inlineEditorMinimumTextHeightisLabelEditing = true@@ -597,8 +617,7 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {fileprivate static let collapsedLabelContentWidth: CGFloat = 240fileprivate static let collapsedLabelMinimumHeight: CGFloat = 48fileprivate static let collapsedLabelBottomGap: CGFloat = 22- fileprivate static let expandedCollapsedLabelWidth: CGFloat = 480- fileprivate static let inlineEditorContentWidth: CGFloat = expandedCollapsedLabelWidth+ fileprivate static let inlineEditorContentWidth: CGFloat = 480fileprivate static let inlineEditorInputHeight: CGFloat = 64fileprivate static let inlineEditorHorizontalPadding: CGFloat = 16fileprivate static let inlineEditorVerticalPadding: CGFloat = 14@@ -614,10 +633,6 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {displayLabelSize(for: text, maximumWidth: collapsedLabelContentWidth)}- private static func expandedCollapsedLabelSize(for text: String) -> CGSize {- displayLabelSize(for: text, maximumWidth: expandedCollapsedLabelWidth)- }-private static func displayLabelSize(for text: String, maximumWidth: CGFloat) -> CGSize {let contentSize = displayLabelContentSize(for: text, maximumWidth: maximumWidth)return CGSize(@@ -660,15 +675,6 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {return max(ceil(rect.height), ceil(lineHeight))}- private static func collapsedLabelNeedsExpansion(_ text: String) -> Bool {- let trimmedText = text.trimmingCharacters(in: .whitespacesAndNewlines)- guard !trimmedText.isEmpty else { return false }-- let collapsed = displayLabelSize(for: text, maximumWidth: collapsedLabelContentWidth)- let expanded = displayLabelSize(for: text, maximumWidth: expandedCollapsedLabelWidth)- return expanded.height < collapsed.height - 0.5- }-fileprivate static func displayLabelContentSize(for text: String,maximumWidth: CGFloat@@ -756,6 +762,9 @@ public struct UserQuerySpawnOverlayView: View {}cursor(state: state)+ .frame(width: 44, height: 44)+ .contentShape(Rectangle())+ .gesture(cursorDragGesture).position(viewModel.localCursorCenter)if viewModel.isHolding {@@ -804,11 +813,45 @@ public struct UserQuerySpawnOverlayView: View {.onTapGesture {viewModel.beginInlineInput()}- .onHover { isHovered in- viewModel.setLabelHovered(isHovered)+ .overlay(alignment: .topTrailing) {+ dismissButton+ .offset(x: 6, y: -6)}}+ private var dismissButton: some View {+ Button {+ viewModel.dismiss()+ } label: {+ Image(systemName: "xmark")+ .font(.system(size: 7, weight: .bold))+ .foregroundStyle(.white)+ .frame(width: 16, height: 16)+ .background {+ Circle()+ .fill(Color.black.opacity(0.45))+ }+ .overlay {+ Circle()+ .stroke(Color.white.opacity(0.35), lineWidth: 1)+ }+ }+ .buttonStyle(.plain)+ }++ /// Lets the user grab the cursor and reposition it; gesture coordinates are+ /// unreliable because the panel follows the cursor, so the real mouse+ /// location is reported instead.+ private var cursorDragGesture: some Gesture {+ DragGesture(minimumDistance: 2)+ .onChanged { _ in+ viewModel.reportCursorDrag(at: NSEvent.mouseLocation)+ }+ .onEnded { _ in+ viewModel.endCursorDrag()+ }+ }+private func displayLabel(state: UserQuerySpawnState) -> some View {Text(state.label).font(.system(size: UserQuerySpawnOverlayViewModel.labelFontSize, weight: .medium))diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swiftindex 9495ae3e..9d050bc6 100644--- a/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swift+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swift@@ -189,39 +189,66 @@ struct UserQuerySpawnGeometryTests {}@Test @MainActor- func spawnLabelOnlyExpandsOnHoverWhenCollapsedTextOverflows() {+ func spawnCursorDragFreezesMovementAndRepositionsInPlace() async throws {let viewModel = UserQuerySpawnOverlayViewModel()- let shortState = UserQuerySpawnState(- id: "spawn-1",- commandText: "hi there",- label: "hi there",- accentIndex: 1,- phase: .holding- )-viewModel.show(- state: shortState,+ state: UserQuerySpawnState(+ id: "spawn-1",+ commandText: "plan",+ label: "checking",+ accentIndex: 1,+ phase: .holding+ ),origin: CGPoint(x: 600, y: 282),destination: CGPoint(x: 600, y: 282),screenSize: CGSize(width: 1200, height: 800))- viewModel.setLabelHovered(true)- #expect(!viewModel.isLabelHovered)-- let longState = UserQuerySpawnState(- id: "spawn-1",- commandText: "long",- label: Array(repeating: "details", count: 30).joined(separator: " "),- accentIndex: 1,- phase: .holding+ let landedDelay = UInt64(+ (UserQuerySpawnOverlayViewModel.travelDuration + 0.05) * 1_000_000_000)- viewModel.update(- state: longState,+ try await Task.sleep(nanoseconds: landedDelay)+ #expect(viewModel.isHolding)++ var reportedPoint: CGPoint?+ viewModel.cursorDragged = { reportedPoint = $0 }+ viewModel.reportCursorDrag(at: CGPoint(x: 400, y: 300))+ #expect(viewModel.isCursorDragging)+ #expect(viewModel.freezesMovement)+ #expect(reportedPoint == CGPoint(x: 400, y: 300))++ viewModel.setPosition(CGPoint(x: 400, y: 500))+ #expect(viewModel.position == CGPoint(x: 400, y: 500))+ #expect(viewModel.destination == CGPoint(x: 400, y: 500))+ #expect(viewModel.isHolding)++ viewModel.endCursorDrag()+ #expect(!viewModel.isCursorDragging)+ #expect(!viewModel.freezesMovement)+ }++ @Test @MainActor+ func spawnDismissReportsSpawnID() {+ let viewModel = UserQuerySpawnOverlayViewModel()+ var dismissedID: String?+ viewModel.dismissed = { dismissedID = $0 }++ viewModel.dismiss()+ #expect(dismissedID == nil)++ viewModel.show(+ state: UserQuerySpawnState(+ id: "spawn-1",+ commandText: "plan",+ label: "checking",+ accentIndex: 1,+ phase: .holding+ ),+ origin: CGPoint(x: 600, y: 282),destination: CGPoint(x: 600, y: 282),screenSize: CGSize(width: 1200, height: 800))- viewModel.setLabelHovered(true)- #expect(viewModel.isLabelHovered)+ viewModel.dismiss()+ #expect(dismissedID == "spawn-1")}@Test @MainActor