Skip to content

Open source CapCut alternative.

TypeScript

feat(app): show elapsed time on the spawn overlay

David Le8c755a8815f9
diff --git a/apps/Donkey/Sources/Donkey/UserQueryOverlayModel.swift b/apps/Donkey/Sources/Donkey/UserQueryOverlayModel.swift
index d730bb4b..0ed1b1af 100644
--- a/apps/Donkey/Sources/Donkey/UserQueryOverlayModel.swift
+++ b/apps/Donkey/Sources/Donkey/UserQueryOverlayModel.swift
@@ -297,6 +297,7 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
spawnState.label = Self.collapsedDisplayText(for: detail ?? spawnState.label)
}
spawnState.updatedAt = Date()
+ spawnState.finishedAt = Date()
spawnStates[index] = spawnState
if !UserQuerySpawnLifecycle.keepsVisibleResult(for: status) {
scheduleSpawnFade(id: spawnID, after: 2.0)
@@ -352,7 +353,8 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
id: promptFollowUpTarget.spawnID,
commandText: text,
label: Self.collapsedDisplayText(for: text),
- phase: .holding
+ phase: .holding,
+ resumesWork: true
)
spawnID = promptFollowUpTarget.spawnID
} else {
@@ -458,7 +460,8 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
id: spawnID,
commandText: trimmedText,
label: Self.collapsedDisplayText(for: trimmedText),
- phase: .holding
+ phase: .holding,
+ resumesWork: true
)
startCommandRun(
text: trimmedText,
@@ -570,6 +573,7 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
activeTaskIDs.insert(taskID)
lastActiveTaskID = taskID
updateTask(id: taskID, detail: "Running", status: .running)
+ updateSpawn(id: spawnStates.first { $0.taskID == taskID }?.id, resumesWork: true)
appendTaskEvent(taskID: taskID, role: .system, text: "Resumed")
promptState.leadingSignalLevel = .thinking
promptState.promptText = task.title
@@ -595,7 +599,7 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
if task.metadata["live.shellConsent"] == "true" {
updateTask(id: taskID, detail: "Approved — running", status: .running, metadata: [:])
if let turn = liveTurn, turn.taskID == taskID {
- updateSpawn(id: turn.spawnID, label: "Approved — running")
+ updateSpawn(id: turn.spawnID, label: "Approved — running", resumesWork: true)
restartLiveTurnWatchdog()
}
syncPrimaryTaskPausedFlag()
@@ -614,6 +618,7 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
promptState.promptText = task.title
syncPrimaryTaskPausedFlag()
let spawnID = spawnStates.first { $0.taskID == taskID }?.id
+ updateSpawn(id: spawnID, resumesWork: true)
let context = commandContext(taskID: taskID, isFollowUp: true, spawnID: spawnID)
Task { [weak self, commandHandler] in
let approved = await commandHandler.approvePermissionGate(taskID: taskID, alwaysAllow: alwaysAllow)
@@ -703,7 +708,8 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
label: String? = nil,
accentIndex: Int? = nil,
targetHint: UserQuerySpawnTargetHint? = nil,
- phase: UserQuerySpawnPhase? = nil
+ phase: UserQuerySpawnPhase? = nil,
+ resumesWork: Bool = false
) {
guard let spawnID,
let index = spawnIndex(id: spawnID) else {
@@ -711,6 +717,9 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
}
var spawnState = spawnStates[index]
+ if resumesWork {
+ spawnState.finishedAt = nil
+ }
if let taskID {
spawnState.taskID = taskID
}
@@ -752,6 +761,7 @@ final class UserQueryOverlayModel: ObservableObject, UserQueryIntentSink {
var spawnState = spawnStates[index]
spawnState.label = Self.spawnCompletionLabel(for: result)
spawnState.updatedAt = Date()
+ spawnState.finishedAt = Date()
// Keep the cursor visible whenever there is a response to read — a clarification, a wait, a
// failure, OR a completed task that produced a summary (e.g. "Now playing …"). Only a
diff --git a/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swift b/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swift
index 79703c76..50ee63e4 100644
--- a/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swift
+++ b/apps/Donkey/Sources/Donkey/UserQuerySpawnOverlayController.swift
@@ -280,10 +280,7 @@ final class UserQuerySpawnOverlayController {
hostingView.wantsLayer = true
hostingView.layer?.backgroundColor = NSColor.clear.cgColor
hostingView.hitTestRegionProvider = { [weak viewModel] in
- guard let viewModel else { return [] }
-
- let frame = viewModel.localHitTestFrame
- return frame.isNull || frame.isEmpty ? [] : [frame]
+ viewModel?.localHitTestFrames ?? []
}
let panel = UserQuerySpawnPanel(
@@ -658,8 +655,8 @@ final class UserQuerySpawnOverlayController {
return
}
- let hitTestFrame = surface.viewModel.localHitTestFrame
- guard !hitTestFrame.isNull, !hitTestFrame.isEmpty else {
+ let hitTestFrames = surface.viewModel.localHitTestFrames
+ guard !hitTestFrames.isEmpty else {
surface.panel.ignoresMouseEvents = true
return
}
@@ -668,13 +665,13 @@ final class UserQuerySpawnOverlayController {
for: NSEvent.mouseLocation,
in: surface
)
- surface.panel.ignoresMouseEvents = !hitTestFrame.contains(mouseLocation)
+ surface.panel.ignoresMouseEvents = !hitTestFrames.contains { $0.contains(mouseLocation) }
}
/// Converts a global screen point into the overlay's top-left-origin local
/// space. `convertPoint(fromScreen:)` alone is not enough: it returns
/// bottom-left-origin window coordinates, and comparing those against the
- /// flipped `localHitTestFrame` mirrors the interactive region vertically.
+ /// flipped `localHitTestFrames` mirrors the interactive regions vertically.
private func overlayLocalPoint(
for screenPoint: CGPoint,
in surface: UserQuerySpawnSurface
@@ -692,7 +689,7 @@ final class UserQuerySpawnOverlayController {
/// click-through, so hover and drags never start.
private func updateMouseMoveMonitoring() {
guard surfacesByID.values.contains(where: {
- !$0.viewModel.localHitTestFrame.isNull
+ !$0.viewModel.localHitTestFrames.isEmpty
}) else {
removeMouseMoveMonitoring()
return
@@ -866,7 +863,7 @@ final class UserQuerySpawnOverlayController {
guard surface.panel.frame.contains(screenPoint) else { return false }
let localPoint = overlayLocalPoint(for: screenPoint, in: surface)
- return surface.viewModel.localHitTestFrame.contains(localPoint)
+ return surface.viewModel.localHitTestFrames.contains { $0.contains(localPoint) }
}
private func spawnOrigin(in screen: NSScreen, index: Int) -> CGPoint {
diff --git a/apps/Donkey/Sources/DonkeyContracts/UserQuerySpawnContracts.swift b/apps/Donkey/Sources/DonkeyContracts/UserQuerySpawnContracts.swift
index 66a88f46..279eeef4 100644
--- a/apps/Donkey/Sources/DonkeyContracts/UserQuerySpawnContracts.swift
+++ b/apps/Donkey/Sources/DonkeyContracts/UserQuerySpawnContracts.swift
@@ -41,6 +41,9 @@ public struct UserQuerySpawnState: Codable, Equatable, Identifiable, Sendable {
public var targetHint: UserQuerySpawnTargetHint?
public var createdAt: Date
public var updatedAt: Date
+ /// When the spawn's work last came to rest (run finished or is waiting on
+ /// the user). Nil while a run is in flight; cleared when work resumes.
+ public var finishedAt: Date?
public init(
id: String = UUID().uuidString,
@@ -52,7 +55,8 @@ public struct UserQuerySpawnState: Codable, Equatable, Identifiable, Sendable {
notchCueAngleDegrees: Double = UserQuerySpawnGeometry.defaultExitAngleDegrees,
targetHint: UserQuerySpawnTargetHint? = nil,
createdAt: Date = Date(),
- updatedAt: Date = Date()
+ updatedAt: Date = Date(),
+ finishedAt: Date? = nil
) {
self.id = id
self.taskID = taskID
@@ -64,6 +68,7 @@ public struct UserQuerySpawnState: Codable, Equatable, Identifiable, Sendable {
self.targetHint = targetHint
self.createdAt = createdAt
self.updatedAt = updatedAt
+ self.finishedAt = finishedAt
}
}
diff --git a/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swift b/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swift
index 31567512..e684ebbf 100644
--- a/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swift
+++ b/apps/Donkey/Sources/DonkeyUI/UserQuerySpawnOverlayView.swift
@@ -35,16 +35,19 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {
public init() {}
- public var hitTestFrame: CGRect {
- guard state != nil, isHolding else { return .null }
+ /// The interactive regions: the grabbable cursor and the label bubble,
+ /// as two separate rects. The transparent panel area between and around
+ /// them must stay click-through for whatever is beneath it.
+ public var hitTestFrames: [CGRect] {
+ guard state != nil, isHolding else { return [] }
- return contentFrame(at: position, includesLabel: true)
+ let labelRect = labelFrame(for: position, labelSize: labelSize())
+ .insetBy(dx: -Self.contentOverdrawPadding, dy: -Self.contentOverdrawPadding)
+ return [cursorHitTestFrame, labelRect]
}
- public var localHitTestFrame: CGRect {
- guard !hitTestFrame.isNull else { return .null }
-
- return hitTestFrame.offsetBy(dx: -viewportOrigin.x, dy: -viewportOrigin.y)
+ public var localHitTestFrames: [CGRect] {
+ hitTestFrames.map { $0.offsetBy(dx: -viewportOrigin.x, dy: -viewportOrigin.y) }
}
public var visualFrame: CGRect {
@@ -614,6 +617,7 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {
private static let panelOverdrawPadding: CGFloat = 28
fileprivate static let labelHorizontalPadding: CGFloat = 10
fileprivate static let labelVerticalPadding: CGFloat = 3
+ fileprivate static let labelTimerLaneHeight: CGFloat = 11
fileprivate static let collapsedLabelContentWidth: CGFloat = 240
fileprivate static let collapsedLabelMinimumHeight: CGFloat = 48
fileprivate static let collapsedLabelBottomGap: CGFloat = 22
@@ -637,7 +641,10 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {
let contentSize = displayLabelContentSize(for: text, maximumWidth: maximumWidth)
return CGSize(
width: contentSize.width + Self.labelHorizontalPadding * 2,
- height: max(collapsedLabelMinimumHeight, contentSize.height + Self.labelVerticalPadding * 2)
+ height: max(
+ collapsedLabelMinimumHeight,
+ contentSize.height + Self.labelVerticalPadding * 2 + Self.labelTimerLaneHeight
+ )
)
}
@@ -709,6 +716,21 @@ public final class UserQuerySpawnOverlayViewModel: ObservableObject {
}
}
+private extension View {
+ /// SwiftUI gestures normally drop the click that activates the window, so
+ /// even with `acceptsFirstMouse` the user had to click an inactive pointer
+ /// once before dragging it. On macOS 15+ gestures can claim that
+ /// activating click, making the pointer draggable immediately.
+ @ViewBuilder
+ func handlesWindowActivationGestures() -> some View {
+ if #available(macOS 15.0, *) {
+ allowsWindowActivationEvents()
+ } else {
+ self
+ }
+ }
+}
+
public struct UserQuerySpawnOverlayView: View {
@ObservedObject private var viewModel: UserQuerySpawnOverlayViewModel
@State private var haloPulseActive = false
@@ -734,6 +756,7 @@ public struct UserQuerySpawnOverlayView: View {
}
}
.ignoresSafeArea()
+ .handlesWindowActivationGestures()
}
private func spawnSurface(
@@ -830,31 +853,38 @@ public struct UserQuerySpawnOverlayView: View {
}
.overlay(alignment: .bottomTrailing) {
if !viewModel.isLabelEditing {
- elapsedTimer(since: state.createdAt)
- .offset(
- x: -UserQuerySpawnOverlayViewModel.labelHorizontalPadding,
- y: 16
- )
+ elapsedTimer(since: state.createdAt, frozenAt: state.finishedAt)
+ .padding(.trailing, UserQuerySpawnOverlayViewModel.labelHorizontalPadding)
+ .padding(.bottom, UserQuerySpawnOverlayViewModel.labelVerticalPadding)
}
}
}
- /// A faint count-up timer hanging below the label bubble. It floats over
- /// arbitrary screen content, so legibility on both light and dark
- /// backgrounds comes from white text with a soft dark shadow rather than
- /// from color-scheme adaptation.
- private func elapsedTimer(since start: Date) -> some View {
- TimelineView(.periodic(from: start, by: 1)) { context in
- Text(UserQueryNotchStatusView.elapsedDescription(from: start, to: context.date))
- .font(.system(size: 9, weight: .medium).monospacedDigit())
- .foregroundStyle(Color.white.opacity(0.62))
- .shadow(color: Color.black.opacity(0.55), radius: 1.5, x: 0, y: 0.5)
- .lineLimit(1)
- .fixedSize()
+ /// A faint count-up timer tucked into the label bubble's bottom-right
+ /// corner. The display label reserves a lane below the text for it, so it
+ /// never overlaps the last line. Once the spawn's work comes to rest the
+ /// timer freezes at the total elapsed time instead of ticking on.
+ private func elapsedTimer(since start: Date, frozenAt: Date?) -> some View {
+ Group {
+ if let frozenAt {
+ elapsedTimerText(from: start, to: frozenAt)
+ } else {
+ TimelineView(.periodic(from: start, by: 1)) { context in
+ elapsedTimerText(from: start, to: context.date)
+ }
+ }
}
.allowsHitTesting(false)
}
+ private func elapsedTimerText(from start: Date, to end: Date) -> some View {
+ Text(UserQueryNotchStatusView.elapsedDescription(from: start, to: end))
+ .font(.system(size: 9, weight: .medium).monospacedDigit())
+ .foregroundStyle(Color.white.opacity(0.65))
+ .lineLimit(1)
+ .fixedSize()
+ }
+
private var showsDismissButton: Bool {
isLabelHovered || isDismissButtonHovered
}
@@ -904,7 +934,12 @@ public struct UserQuerySpawnOverlayView: View {
alignment: .leading
)
.padding(.horizontal, UserQuerySpawnOverlayViewModel.labelHorizontalPadding)
- .padding(.vertical, UserQuerySpawnOverlayViewModel.labelVerticalPadding)
+ .padding(.top, UserQuerySpawnOverlayViewModel.labelVerticalPadding)
+ .padding(
+ .bottom,
+ UserQuerySpawnOverlayViewModel.labelVerticalPadding +
+ UserQuerySpawnOverlayViewModel.labelTimerLaneHeight
+ )
}
private func inlineLabelEditor(state: UserQuerySpawnState) -> some View {
diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swift
index 9d050bc6..cb8b4b79 100644
--- a/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swift
+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/UserQuerySpawnGeometryTests.swift
@@ -275,17 +275,21 @@ struct UserQuerySpawnGeometryTests {
try await Task.sleep(nanoseconds: landedDelay)
let panelFrame = viewModel.visualFrame
- let hitTestFrame = viewModel.hitTestFrame
+ let hitTestFrames = viewModel.hitTestFrames
viewModel.updateViewport(origin: panelFrame.origin, size: panelFrame.size)
#expect(!panelFrame.isNull)
- #expect(!hitTestFrame.isNull)
- #expect(panelFrame.contains(hitTestFrame))
- #expect(panelFrame.width > hitTestFrame.width)
- #expect(viewModel.localHitTestFrame.minX >= 0)
- #expect(viewModel.localHitTestFrame.maxX <= viewModel.viewportSize.width)
- #expect(viewModel.localHitTestFrame.minY >= 0)
- #expect(viewModel.localHitTestFrame.maxY <= viewModel.viewportSize.height)
+ #expect(!hitTestFrames.isEmpty)
+ for hitTestFrame in hitTestFrames {
+ #expect(panelFrame.contains(hitTestFrame))
+ #expect(panelFrame.width > hitTestFrame.width)
+ }
+ for localFrame in viewModel.localHitTestFrames {
+ #expect(localFrame.minX >= 0)
+ #expect(localFrame.maxX <= viewModel.viewportSize.width)
+ #expect(localFrame.minY >= 0)
+ #expect(localFrame.maxY <= viewModel.viewportSize.height)
+ }
}
@Test @MainActor