Skip to content

Open source CapCut alternative.

TypeScript

Constrain pointer overlays to cursor surfaces

David Le28861f01c675
diff --git a/apps/Donkey/Sources/Donkey/PointerCoachCursorOverlayController.swift b/apps/Donkey/Sources/Donkey/PointerCoachCursorOverlayController.swift
index d85898f5..523f7ec6 100644
--- a/apps/Donkey/Sources/Donkey/PointerCoachCursorOverlayController.swift
+++ b/apps/Donkey/Sources/Donkey/PointerCoachCursorOverlayController.swift
@@ -6,21 +6,34 @@ import SwiftUI
@MainActor
final class PointerCoachCursorOverlayController {
private var panel: NSPanel?
+ private var hostingView: NSHostingView<PointerCoachCursorOverlayView>?
+ private var viewModel: PointerCoachCursorOverlayViewModel?
+ private var timer: Timer?
private var dismissalWorkItem: DispatchWorkItem?
+ private var screenFrame: CGRect = .zero
func show(request: PointerCoachCursorGuideRequest) {
close()
guard let screen = NSScreen.main ?? NSScreen.screens.first else { return }
- let hostingView = NSHostingView(rootView: PointerCoachCursorOverlayView(request: request))
- hostingView.frame = CGRect(origin: .zero, size: screen.frame.size)
+ screenFrame = screen.frame
+ let viewModel = PointerCoachCursorOverlayViewModel(
+ request: request,
+ screenSize: screenFrame.size
+ )
+ viewModel.start()
+ let localFrame = viewModel.visualFrame
+ viewModel.updateViewport(origin: localFrame.origin, size: localFrame.size)
+
+ let hostingView = NSHostingView(rootView: PointerCoachCursorOverlayView(viewModel: viewModel))
+ hostingView.frame = CGRect(origin: .zero, size: localFrame.size)
hostingView.autoresizingMask = [.width, .height]
hostingView.wantsLayer = true
hostingView.layer?.backgroundColor = NSColor.clear.cgColor
let panel = NSPanel(
- contentRect: screen.frame,
+ contentRect: panelFrame(for: localFrame, in: screenFrame),
styleMask: [.borderless, .nonactivatingPanel],
backing: .buffered,
defer: false
@@ -40,9 +53,11 @@ final class PointerCoachCursorOverlayController {
.ignoresCycle,
.stationary
]
- panel.setFrame(screen.frame, display: true)
panel.orderFrontRegardless()
self.panel = panel
+ self.hostingView = hostingView
+ self.viewModel = viewModel
+ startTimer()
let workItem = DispatchWorkItem { [weak self] in
Task { @MainActor in
@@ -54,10 +69,15 @@ final class PointerCoachCursorOverlayController {
}
func close() {
+ timer?.invalidate()
+ timer = nil
dismissalWorkItem?.cancel()
dismissalWorkItem = nil
panel?.close()
panel = nil
+ hostingView = nil
+ viewModel = nil
+ screenFrame = .zero
}
private func duration(for request: PointerCoachCursorGuideRequest) -> TimeInterval {
@@ -65,4 +85,41 @@ final class PointerCoachCursorOverlayController {
total + step.travelDuration + step.holdDuration
}
}
+
+ private func startTimer() {
+ timer?.invalidate()
+ let timer = Timer(timeInterval: 1.0 / 60.0, repeats: true) { [weak self] _ in
+ Task { @MainActor in
+ self?.tick()
+ }
+ }
+ RunLoop.main.add(timer, forMode: .common)
+ self.timer = timer
+ }
+
+ private func tick() {
+ guard let panel,
+ let hostingView,
+ let viewModel else {
+ return
+ }
+
+ viewModel.update(now: Date(), screenSize: screenFrame.size)
+ let localFrame = viewModel.visualFrame
+ viewModel.updateViewport(origin: localFrame.origin, size: localFrame.size)
+ hostingView.frame = CGRect(origin: .zero, size: localFrame.size)
+ panel.setFrame(panelFrame(for: localFrame, in: screenFrame), display: true)
+ }
+
+ private func panelFrame(
+ for localFrame: CGRect,
+ in screenFrame: CGRect
+ ) -> CGRect {
+ CGRect(
+ x: screenFrame.minX + localFrame.minX,
+ y: screenFrame.maxY - localFrame.maxY,
+ width: localFrame.width,
+ height: localFrame.height
+ )
+ }
}
diff --git a/apps/Donkey/Sources/Donkey/PointerPromptSpawnOverlayController.swift b/apps/Donkey/Sources/Donkey/PointerPromptSpawnOverlayController.swift
index c25c58cd..8e69b149 100644
--- a/apps/Donkey/Sources/Donkey/PointerPromptSpawnOverlayController.swift
+++ b/apps/Donkey/Sources/Donkey/PointerPromptSpawnOverlayController.swift
@@ -2,15 +2,13 @@ import AppKit
import DonkeyContracts
import DonkeyRuntime
import DonkeyUI
+import QuartzCore
import SwiftUI
@MainActor
final class PointerPromptSpawnOverlayController {
- private let store = PointerPromptSpawnOverlayStore()
- private var panel: PointerPromptSpawnPanel?
- private var hostingView: PointerPromptSpawnHostingView<PointerPromptSpawnOverlayContainerView>?
+ private var surfacesByID: [String: PointerPromptSpawnSurface] = [:]
private var viewModelsByID: [String: PointerPromptSpawnOverlayViewModel] = [:]
- private var closeWorkItem: DispatchWorkItem?
private var windowResolver = MacWindowResolver()
var followUpSubmitted: ((String, String, String) -> Void)? {
@@ -35,22 +33,17 @@ final class PointerPromptSpawnOverlayController {
screen: NSScreen?,
notchMetrics: PointerPromptNotchMetrics
) {
- guard !spawnStates.isEmpty else {
+ guard let screen else { return }
+
+ let visibleSpawnStates = spawnStates.filter { $0.phase != .notchCue }
+ guard !visibleSpawnStates.isEmpty else {
fadeAndCloseAll()
return
}
- closeWorkItem?.cancel()
- closeWorkItem = nil
-
- guard let screen else { return }
-
- ensurePanel(on: screen)
- let visibleSpawnStates = spawnStates.filter { $0.phase != .notchCue }
let visibleIDs = Set(visibleSpawnStates.map(\.id))
-
for spawnState in visibleSpawnStates {
- updateViewModel(
+ updateSurface(
for: spawnState,
selectedSpawnID: selectedSpawnID,
screen: screen,
@@ -58,48 +51,167 @@ final class PointerPromptSpawnOverlayController {
)
}
- for staleID in viewModelsByID.keys where !visibleIDs.contains(staleID) {
+ for staleID in surfacesByID.keys where !visibleIDs.contains(staleID) {
fadeAndRemove(id: staleID)
}
}
func close() {
- closeWorkItem?.cancel()
- closeWorkItem = nil
- for viewModel in viewModelsByID.values {
- viewModel.fadeOut()
+ for surface in surfacesByID.values {
+ surface.travelWorkItem?.cancel()
+ surface.removalWorkItem?.cancel()
+ surface.viewModel.fadeOut()
+ surface.panel.close()
}
- panel?.close()
- panel = nil
- hostingView = nil
- store.viewModels = []
+ surfacesByID = [:]
viewModelsByID = [:]
}
- private func ensurePanel(on screen: NSScreen) {
- if let panel {
- guard panel.frame.size != screen.frame.size || panel.frame.origin != screen.frame.origin else { return }
+ private func updateSurface(
+ for spawnState: PointerPromptSpawnState,
+ selectedSpawnID: String?,
+ screen: NSScreen,
+ notchMetrics: PointerPromptNotchMetrics
+ ) {
+ let destination = destinationPoint(
+ for: spawnState.targetHint,
+ viewModel: viewModelsByID[spawnState.id],
+ screen: screen,
+ notchMetrics: notchMetrics
+ )
- panel.setFrame(screen.frame, display: true)
- hostingView?.frame = CGRect(origin: .zero, size: screen.frame.size)
+ if let surface = surfacesByID[spawnState.id] {
+ updateExistingSurface(
+ surface,
+ spawnState: spawnState,
+ selectedSpawnID: selectedSpawnID,
+ destination: destination,
+ screen: screen
+ )
return
}
- let hostingView = PointerPromptSpawnHostingView(rootView: PointerPromptSpawnOverlayContainerView(store: store))
- hostingView.frame = CGRect(origin: .zero, size: screen.frame.size)
+ createSurface(
+ for: spawnState,
+ selectedSpawnID: selectedSpawnID,
+ destination: destination,
+ screen: screen
+ )
+ }
+
+ private func createSurface(
+ for spawnState: PointerPromptSpawnState,
+ selectedSpawnID: String?,
+ destination: CGPoint,
+ screen: NSScreen
+ ) {
+ let viewModel = PointerPromptSpawnOverlayViewModel()
+ viewModel.followUpSubmitted = followUpSubmitted
+ viewModel.selected = selected
+ viewModel.setSelected(spawnState.id == selectedSpawnID)
+
+ let origin = spawnOrigin(
+ in: screen,
+ index: surfacesByID.count
+ )
+ viewModel.show(
+ state: spawnState,
+ origin: origin,
+ destination: destination,
+ screenSize: screen.frame.size
+ )
+
+ let initialLocalFrame = viewModel.cursorOnlyVisualFrame(at: origin)
+ viewModel.updateViewport(
+ origin: initialLocalFrame.origin,
+ size: initialLocalFrame.size
+ )
+ let surface = makeSurface(
+ id: spawnState.id,
+ viewModel: viewModel,
+ localFrame: initialLocalFrame,
+ screen: screen
+ )
+ configureCallbacks(for: surface)
+ surfacesByID[spawnState.id] = surface
+ viewModelsByID[spawnState.id] = viewModel
+ animateSurfaceTravel(
+ surface,
+ to: destination,
+ on: screen
+ )
+ }
+
+ private func updateExistingSurface(
+ _ surface: PointerPromptSpawnSurface,
+ spawnState: PointerPromptSpawnState,
+ selectedSpawnID: String?,
+ destination: CGPoint,
+ screen: NSScreen
+ ) {
+ surface.screen = screen
+ surface.viewModel.setSelected(spawnState.id == selectedSpawnID)
+ let shouldRetarget = !surface.viewModel.freezesMovement &&
+ distance(from: surface.destination, to: destination) > 1
+
+ surface.viewModel.update(
+ state: spawnState,
+ destination: destination,
+ screenSize: screen.frame.size
+ )
+
+ if spawnState.phase == .fading {
+ fadeAndRemove(id: spawnState.id)
+ return
+ }
+
+ if shouldRetarget {
+ animateSurfaceTravel(
+ surface,
+ to: destination,
+ on: screen
+ )
+ return
+ }
+
+ if surface.isTraveling {
+ updateMouseEventPassthrough(for: surface)
+ return
+ }
+
+ layoutSurface(
+ surface,
+ on: screen,
+ animated: false
+ )
+ }
+
+ private func makeSurface(
+ id: String,
+ viewModel: PointerPromptSpawnOverlayViewModel,
+ localFrame: CGRect,
+ screen: NSScreen
+ ) -> PointerPromptSpawnSurface {
+ let globalFrame = panelFrame(
+ for: localFrame,
+ on: screen
+ )
+ let hostingView = PointerPromptSpawnHostingView(
+ rootView: PointerPromptSpawnOverlayView(viewModel: viewModel)
+ )
+ hostingView.frame = CGRect(origin: .zero, size: globalFrame.size)
hostingView.autoresizingMask = [.width, .height]
hostingView.wantsLayer = true
hostingView.layer?.backgroundColor = NSColor.clear.cgColor
- hostingView.hitTestRegionProvider = { [weak self] in
- guard let self else { return [] }
+ hostingView.hitTestRegionProvider = { [weak viewModel] in
+ guard let viewModel else { return [] }
- return self.store.viewModels
- .map(\.hitTestFrame)
- .filter { !$0.isNull }
+ let frame = viewModel.localHitTestFrame
+ return frame.isNull || frame.isEmpty ? [] : [frame]
}
let panel = PointerPromptSpawnPanel(
- contentRect: screen.frame,
+ contentRect: globalFrame,
styleMask: [.borderless],
backing: .buffered,
defer: false
@@ -112,7 +224,7 @@ final class PointerPromptSpawnOverlayController {
panel.hidesOnDeactivate = false
panel.isReleasedWhenClosed = false
panel.becomesKeyOnlyIfNeeded = false
- panel.ignoresMouseEvents = false
+ panel.ignoresMouseEvents = true
panel.level = .statusBar
panel.collectionBehavior = [
.canJoinAllSpaces,
@@ -120,74 +232,176 @@ final class PointerPromptSpawnOverlayController {
.ignoresCycle,
.stationary
]
- panel.setFrame(screen.frame, display: true)
+ panel.setFrame(globalFrame, display: true)
panel.orderFrontRegardless()
- self.panel = panel
- self.hostingView = hostingView
- }
-
- private func updateViewModel(
- for spawnState: PointerPromptSpawnState,
- selectedSpawnID: String?,
- screen: NSScreen,
- notchMetrics: PointerPromptNotchMetrics
- ) {
- let viewModel = viewModel(for: spawnState)
- viewModel.setSelected(spawnState.id == selectedSpawnID)
- let destination = destinationPoint(
- for: spawnState.targetHint,
+ return PointerPromptSpawnSurface(
+ id: id,
viewModel: viewModel,
+ panel: panel,
+ hostingView: hostingView,
screen: screen,
- notchMetrics: notchMetrics
+ destination: viewModel.destination
)
+ }
- if viewModel.state == nil {
- viewModel.show(
- state: spawnState,
- origin: spawnOrigin(in: screen, index: store.viewModels.firstIndex { $0.objectID == viewModel.objectID } ?? 0),
- destination: destination,
- screenSize: screen.frame.size
+ private func configureCallbacks(for surface: PointerPromptSpawnSurface) {
+ surface.viewModel.inputActivityChanged = { [weak self, weak surface] isActive in
+ guard let self,
+ let surface else {
+ return
+ }
+
+ self.updateInputInteractivity(
+ isActive: isActive,
+ for: surface
)
- return
}
+ surface.viewModel.travelCompleted = { [weak self, weak surface] in
+ guard let self,
+ let surface else {
+ return
+ }
- viewModel.update(
- state: spawnState,
- destination: destination,
- screenSize: screen.frame.size
+ surface.isTraveling = false
+ self.layoutSurface(
+ surface,
+ on: surface.screen,
+ animated: false
+ )
+ }
+ }
+
+ private func animateSurfaceTravel(
+ _ surface: PointerPromptSpawnSurface,
+ to destination: CGPoint,
+ on screen: NSScreen
+ ) {
+ surface.travelWorkItem?.cancel()
+ surface.screen = screen
+ surface.destination = destination
+ surface.isTraveling = true
+
+ let currentLocalFrame = surface.viewModel.cursorOnlyVisualFrame(
+ at: surface.viewModel.position
)
- if spawnState.phase == .fading {
- scheduleRemove(id: spawnState.id)
+ applyViewport(
+ currentLocalFrame,
+ to: surface,
+ on: screen
+ )
+ surface.panel.setFrame(
+ panelFrame(for: currentLocalFrame, on: screen),
+ display: true
+ )
+
+ let destinationLocalFrame = surface.viewModel.cursorOnlyVisualFrame(
+ at: destination
+ )
+ let destinationGlobalFrame = panelFrame(
+ for: destinationLocalFrame,
+ on: screen
+ )
+
+ NSAnimationContext.runAnimationGroup { context in
+ context.duration = PointerPromptSpawnOverlayViewModel.travelDuration
+ context.timingFunction = CAMediaTimingFunction(controlPoints: 0.45, 0.05, 0.3, 1)
+ surface.panel.animator().setFrame(destinationGlobalFrame, display: true)
+ }
+
+ let workItem = DispatchWorkItem { [weak self, weak surface] in
+ guard let self,
+ let surface,
+ surface.isTraveling else {
+ return
+ }
+
+ surface.isTraveling = false
+ self.layoutSurface(
+ surface,
+ on: surface.screen,
+ animated: false
+ )
}
+ surface.travelWorkItem = workItem
+ DispatchQueue.main.asyncAfter(
+ deadline: .now() + PointerPromptSpawnOverlayViewModel.travelDuration + 0.03,
+ execute: workItem
+ )
+ updateMouseEventPassthrough(for: surface)
}
- private func viewModel(for spawnState: PointerPromptSpawnState) -> PointerPromptSpawnOverlayViewModel {
- if let viewModel = viewModelsByID[spawnState.id] {
- return viewModel
+ private func layoutSurface(
+ _ surface: PointerPromptSpawnSurface,
+ on screen: NSScreen,
+ animated: Bool
+ ) {
+ guard !surface.viewModel.visualFrame.isNull,
+ !surface.viewModel.visualFrame.isEmpty else {
+ updateMouseEventPassthrough(for: surface)
+ return
}
- let viewModel = PointerPromptSpawnOverlayViewModel()
- viewModel.followUpSubmitted = followUpSubmitted
- viewModel.selected = selected
- viewModel.inputActivityChanged = { [weak self] isActive in
- self?.updateInputInteractivity(isActive: isActive)
+ surface.screen = screen
+ let localFrame = surface.viewModel.visualFrame
+ applyViewport(
+ localFrame,
+ to: surface,
+ on: screen
+ )
+ let globalFrame = panelFrame(for: localFrame, on: screen)
+ if animated {
+ surface.panel.animator().setFrame(globalFrame, display: true)
+ } else {
+ surface.panel.setFrame(globalFrame, display: true)
}
- viewModelsByID[spawnState.id] = viewModel
- store.viewModels.append(viewModel)
- return viewModel
+ updateMouseEventPassthrough(for: surface)
}
- private func updateInputInteractivity(isActive: Bool) {
- guard let panel else { return }
+ private func applyViewport(
+ _ localFrame: CGRect,
+ to surface: PointerPromptSpawnSurface,
+ on screen: NSScreen
+ ) {
+ surface.viewModel.updateViewport(
+ origin: localFrame.origin,
+ size: localFrame.size
+ )
+ surface.hostingView.frame = CGRect(origin: .zero, size: localFrame.size)
+ surface.screen = screen
+ }
+
+ private func updateInputInteractivity(
+ isActive: Bool,
+ for surface: PointerPromptSpawnSurface
+ ) {
+ surface.travelWorkItem?.cancel()
+ surface.isTraveling = false
+ layoutSurface(
+ surface,
+ on: surface.screen,
+ animated: false
+ )
if isActive {
NSApp.activate(ignoringOtherApps: true)
- panel.orderFrontRegardless()
- panel.makeKeyAndOrderFront(nil)
+ surface.panel.orderFrontRegardless()
+ surface.panel.makeKeyAndOrderFront(nil)
} else {
- panel.orderFrontRegardless()
+ surface.panel.orderFrontRegardless()
+ }
+ updateMouseEventPassthrough(for: surface)
+ }
+
+ private func updateMouseEventPassthrough(for surface: PointerPromptSpawnSurface) {
+ let hitTestFrame = surface.viewModel.localHitTestFrame
+ guard !hitTestFrame.isNull, !hitTestFrame.isEmpty else {
+ surface.panel.ignoresMouseEvents = true
+ return
}
+
+ let mouseLocation = surface.panel.convertPoint(fromScreen: NSEvent.mouseLocation)
+ surface.panel.ignoresMouseEvents = !hitTestFrame.contains(mouseLocation)
}
func beginVoiceInput(spawnID: String) -> Bool {
@@ -238,46 +452,28 @@ final class PointerPromptSpawnOverlayController {
}
private func fadeAndCloseAll() {
- guard panel != nil else { return }
-
- for viewModel in viewModelsByID.values {
- viewModel.fadeOut()
+ for spawnID in Array(surfacesByID.keys) {
+ fadeAndRemove(id: spawnID)
}
- scheduleCloseAfterFade()
}
private func fadeAndRemove(id spawnID: String) {
- viewModelsByID[spawnID]?.fadeOut()
- scheduleRemove(id: spawnID)
- }
-
- private func scheduleRemove(id spawnID: String) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.24) { [weak self] in
- guard let self else { return }
-
- self.viewModelsByID[spawnID] = nil
- self.store.viewModels.removeAll { viewModel in
- viewModel.state?.id == spawnID || viewModel.state == nil
- }
- if self.store.viewModels.isEmpty {
- self.scheduleCloseAfterFade()
+ guard let surface = surfacesByID[spawnID] else { return }
+
+ surface.travelWorkItem?.cancel()
+ surface.removalWorkItem?.cancel()
+ surface.viewModel.fadeOut()
+ let workItem = DispatchWorkItem { [weak self, weak surface] in
+ guard let self,
+ let surface else {
+ return
}
- }
- }
- private func scheduleCloseAfterFade() {
- closeWorkItem?.cancel()
- let workItem = DispatchWorkItem { [weak self] in
- Task { @MainActor in
- self?.panel?.close()
- self?.panel = nil
- self?.hostingView = nil
- self?.store.viewModels = []
- self?.viewModelsByID = [:]
- self?.closeWorkItem = nil
- }
+ surface.panel.close()
+ self.surfacesByID[spawnID] = nil
+ self.viewModelsByID[spawnID] = nil
}
- closeWorkItem = workItem
+ surface.removalWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + 0.24, execute: workItem)
}
@@ -288,12 +484,16 @@ final class PointerPromptSpawnOverlayController {
private func destinationPoint(
for hint: PointerPromptSpawnTargetHint?,
- viewModel: PointerPromptSpawnOverlayViewModel,
+ viewModel: PointerPromptSpawnOverlayViewModel?,
screen: NSScreen,
notchMetrics: PointerPromptNotchMetrics
) -> CGPoint {
- guard !viewModel.freezesMovement else {
- return viewModel.destination
+ guard viewModel?.freezesMovement != true else {
+ return viewModel?.destination ?? destinationPoint(
+ for: hint,
+ screen: screen,
+ notchMetrics: notchMetrics
+ )
}
return destinationPoint(
@@ -391,6 +591,50 @@ final class PointerPromptSpawnOverlayController {
in: screen.frame.size
)
}
+
+ private func panelFrame(
+ for localFrame: CGRect,
+ on screen: NSScreen
+ ) -> CGRect {
+ CGRect(
+ x: screen.frame.minX + localFrame.minX,
+ y: screen.frame.maxY - localFrame.maxY,
+ width: localFrame.width,
+ height: localFrame.height
+ )
+ }
+
+ private func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
+ hypot(lhs.x - rhs.x, lhs.y - rhs.y)
+ }
+}
+
+private final class PointerPromptSpawnSurface {
+ let id: String
+ let viewModel: PointerPromptSpawnOverlayViewModel
+ let panel: PointerPromptSpawnPanel
+ let hostingView: PointerPromptSpawnHostingView<PointerPromptSpawnOverlayView>
+ var screen: NSScreen
+ var destination: CGPoint
+ var isTraveling = false
+ var travelWorkItem: DispatchWorkItem?
+ var removalWorkItem: DispatchWorkItem?
+
+ init(
+ id: String,
+ viewModel: PointerPromptSpawnOverlayViewModel,
+ panel: PointerPromptSpawnPanel,
+ hostingView: PointerPromptSpawnHostingView<PointerPromptSpawnOverlayView>,
+ screen: NSScreen,
+ destination: CGPoint
+ ) {
+ self.id = id
+ self.viewModel = viewModel
+ self.panel = panel
+ self.hostingView = hostingView
+ self.screen = screen
+ self.destination = destination
+ }
}
private final class PointerPromptSpawnPanel: NSPanel {
diff --git a/apps/Donkey/Sources/DonkeyContracts/PointerPromptLayout.swift b/apps/Donkey/Sources/DonkeyContracts/PointerPromptLayout.swift
index 72d5dcbd..ecbc4d39 100644
--- a/apps/Donkey/Sources/DonkeyContracts/PointerPromptLayout.swift
+++ b/apps/Donkey/Sources/DonkeyContracts/PointerPromptLayout.swift
@@ -20,7 +20,7 @@ public enum PointerPromptLayout {
public static let composerBottomPadding: CGFloat = 0
public static let composerInputHorizontalPadding: CGFloat = 16
public static let composerInputLeadingContentPadding: CGFloat = 20
- public static let composerInputTrailingContentPadding: CGFloat = 8
+ public static let composerInputTrailingContentPadding: CGFloat = 14.6
public static let composerTextWaveformSpacing: CGFloat = 12
public static let composerWaveformSize = CGSize(width: 54, height: 28)
public static let composerInputMinimumHeight: CGFloat = 66
@@ -29,8 +29,8 @@ public enum PointerPromptLayout {
public static let composerInputTextVerticalPadding: CGFloat = 23.4
public static let composerInputVoiceButtonSize: CGFloat = 33.6
public static let composerMicrophoneIconSize: CGFloat = 28
- public static let composerSendButtonSize: CGFloat = 56
- public static let composerTrailingControlsSpacing: CGFloat = 8
+ public static let composerSendButtonSize: CGFloat = 36.8
+ public static let composerTrailingControlsSpacing: CGFloat = 16
public static let composerTrailingControlsWidth: CGFloat =
composerMicrophoneIconSize + composerTrailingControlsSpacing + composerSendButtonSize
public static let composerExpandedTextTopPadding: CGFloat = 18
diff --git a/apps/Donkey/Sources/DonkeyUI/PointerCoachCursorOverlayView.swift b/apps/Donkey/Sources/DonkeyUI/PointerCoachCursorOverlayView.swift
index 3599e186..f434d64e 100644
--- a/apps/Donkey/Sources/DonkeyUI/PointerCoachCursorOverlayView.swift
+++ b/apps/Donkey/Sources/DonkeyUI/PointerCoachCursorOverlayView.swift
@@ -1,83 +1,81 @@
import DonkeyContracts
import SwiftUI
-public struct PointerCoachCursorOverlayView: View {
- private let request: PointerCoachCursorGuideRequest
- @State private var startedAt = Date()
- @State private var now = Date()
-
- private let timer = Timer.publish(every: 1.0 / 60.0, on: .main, in: .common).autoconnect()
+@MainActor
+public final class PointerCoachCursorOverlayViewModel: ObservableObject {
+ public let request: PointerCoachCursorGuideRequest
+ public private(set) var screenSize: CGSize
+ @Published public private(set) var viewportOrigin: CGPoint = .zero
+ @Published public private(set) var viewportSize: CGSize = .zero
+ @Published public private(set) var startedAt = Date()
+ @Published public private(set) var now = Date()
- public init(request: PointerCoachCursorGuideRequest) {
+ public init(
+ request: PointerCoachCursorGuideRequest,
+ screenSize: CGSize
+ ) {
self.request = request
+ self.screenSize = screenSize
}
- public var body: some View {
- GeometryReader { proxy in
- let frame = animationFrame(size: proxy.size)
-
- ZStack(alignment: .topLeading) {
- Color.black.opacity(0.001)
-
- if frame.isHolding {
- Circle()
- .stroke(Color.white.opacity(0.55), lineWidth: 1.5)
- .frame(width: 36, height: 36)
- .scaleEffect(frame.haloScale)
- .opacity(frame.haloOpacity)
- .position(frame.position)
- }
-
- cursor(angle: frame.angle)
- .position(frame.position)
-
- if !frame.visibleLabel.isEmpty {
- label(text: frame.visibleLabel, accent: frame.accent)
- .position(labelPosition(for: frame.position, in: proxy.size))
- .opacity(frame.labelOpacity)
- }
- }
- .ignoresSafeArea()
- }
- .onAppear {
- startedAt = Date()
- now = startedAt
- }
- .onReceive(timer) { value in
- now = value
+ public var animationFrame: CoachCursorAnimationFrame {
+ animationFrame(size: screenSize)
+ }
+
+ public var visualFrame: CGRect {
+ visualFrame(for: animationFrame)
+ }
+
+ public func start(at date: Date = Date()) {
+ startedAt = date
+ now = date
+ }
+
+ public func update(now: Date, screenSize: CGSize? = nil) {
+ self.now = now
+ if let screenSize {
+ self.screenSize = screenSize
}
}
- private func cursor(angle: Double) -> some View {
- PointerCoachCursorShape()
- .fill(Color.white)
- .overlay {
- PointerCoachCursorShape()
- .stroke(Color(red: 0.34, green: 0.95, blue: 1.0), lineWidth: 1.4)
- }
- .shadow(color: Color.black.opacity(0.28), radius: 3, x: 0, y: 2)
- .frame(width: 26, height: 26)
- .rotationEffect(.degrees(angle + 50))
+ public func updateViewport(origin: CGPoint, size: CGSize) {
+ viewportOrigin = origin
+ viewportSize = size
}
- private func label(text: String, accent: Color) -> some View {
- Text(text)
- .font(.system(size: 12, weight: .medium))
- .foregroundStyle(.white)
- .lineLimit(2)
- .fixedSize(horizontal: false, vertical: true)
- .padding(.horizontal, 10)
- .padding(.vertical, 6)
- .frame(maxWidth: 280, alignment: .leading)
- .background {
- RoundedRectangle(cornerRadius: 7, style: .continuous)
- .fill(accent)
- }
- .overlay {
- RoundedRectangle(cornerRadius: 7, style: .continuous)
- .stroke(Color.white.opacity(0.22), lineWidth: 1)
- }
- .shadow(color: Color.black.opacity(0.32), radius: 8, x: 0, y: 4)
+ public func renderPoint(_ point: CGPoint) -> CGPoint {
+ CGPoint(
+ x: point.x - viewportOrigin.x,
+ y: point.y - viewportOrigin.y
+ )
+ }
+
+ public func labelPosition(for cursorPosition: CGPoint) -> CGPoint {
+ let prefersLeft = cursorPosition.x > screenSize.width - 340
+ let prefersAbove = cursorPosition.y > screenSize.height - 120
+ return CGPoint(
+ x: cursorPosition.x + (prefersLeft ? -156 : 156),
+ y: cursorPosition.y + (prefersAbove ? -54 : 54)
+ )
+ }
+
+ private func visualFrame(for frame: CoachCursorAnimationFrame) -> CGRect {
+ var bounds = CGRect(
+ x: frame.position.x - 30,
+ y: frame.position.y - 30,
+ width: 60,
+ height: 60
+ )
+ if !frame.visibleLabel.isEmpty {
+ let labelCenter = labelPosition(for: frame.position)
+ bounds = bounds.union(CGRect(
+ x: labelCenter.x - 160,
+ y: labelCenter.y - 42,
+ width: 320,
+ height: 84
+ ))
+ }
+ return bounds.insetBy(dx: -8, dy: -8)
}
private func animationFrame(size: CGSize) -> CoachCursorAnimationFrame {
@@ -162,15 +160,6 @@ public struct PointerCoachCursorOverlayView: View {
)
}
- private func labelPosition(for cursorPosition: CGPoint, in size: CGSize) -> CGPoint {
- let prefersLeft = cursorPosition.x > size.width - 340
- let prefersAbove = cursorPosition.y > size.height - 120
- return CGPoint(
- x: cursorPosition.x + (prefersLeft ? -156 : 156),
- y: cursorPosition.y + (prefersAbove ? -54 : 54)
- )
- }
-
private func typedText(_ text: String, progress: Double) -> String {
guard progress < 1 else { return text }
@@ -189,7 +178,72 @@ public struct PointerCoachCursorOverlayView: View {
}
}
-private struct CoachCursorAnimationFrame {
+public struct PointerCoachCursorOverlayView: View {
+ @ObservedObject private var viewModel: PointerCoachCursorOverlayViewModel
+
+ public init(viewModel: PointerCoachCursorOverlayViewModel) {
+ self.viewModel = viewModel
+ }
+
+ public var body: some View {
+ let frame = viewModel.animationFrame
+
+ ZStack(alignment: .topLeading) {
+ if frame.isHolding {
+ Circle()
+ .stroke(Color.white.opacity(0.55), lineWidth: 1.5)
+ .frame(width: 36, height: 36)
+ .scaleEffect(frame.haloScale)
+ .opacity(frame.haloOpacity)
+ .position(viewModel.renderPoint(frame.position))
+ }
+
+ cursor(angle: frame.angle)
+ .position(viewModel.renderPoint(frame.position))
+
+ if !frame.visibleLabel.isEmpty {
+ label(text: frame.visibleLabel, accent: frame.accent)
+ .position(viewModel.renderPoint(viewModel.labelPosition(for: frame.position)))
+ .opacity(frame.labelOpacity)
+ }
+ }
+ }
+
+ private func cursor(angle: Double) -> some View {
+ PointerCoachCursorShape()
+ .fill(Color.white)
+ .overlay {
+ PointerCoachCursorShape()
+ .stroke(Color(red: 0.34, green: 0.95, blue: 1.0), lineWidth: 1.4)
+ }
+ .shadow(color: Color.black.opacity(0.28), radius: 3, x: 0, y: 2)
+ .frame(width: 26, height: 26)
+ .rotationEffect(.degrees(angle + 50))
+ }
+
+ private func label(text: String, accent: Color) -> some View {
+ Text(text)
+ .font(.system(size: 12, weight: .medium))
+ .foregroundStyle(.white)
+ .lineLimit(2)
+ .fixedSize(horizontal: false, vertical: true)
+ .padding(.horizontal, 10)
+ .padding(.vertical, 6)
+ .frame(maxWidth: 280, alignment: .leading)
+ .background {
+ RoundedRectangle(cornerRadius: 7, style: .continuous)
+ .fill(accent)
+ }
+ .overlay {
+ RoundedRectangle(cornerRadius: 7, style: .continuous)
+ .stroke(Color.white.opacity(0.22), lineWidth: 1)
+ }
+ .shadow(color: Color.black.opacity(0.32), radius: 8, x: 0, y: 4)
+ }
+
+}
+
+public struct CoachCursorAnimationFrame {
var position: CGPoint
var angle: Double
var visibleLabel: String
diff --git a/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swift b/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swift
index 735bda5d..bfcb1fce 100644
--- a/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swift
+++ b/apps/Donkey/Sources/DonkeyUI/PointerPromptSpawnOverlayView.swift
@@ -9,6 +9,8 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
@Published public private(set) var position: CGPoint = .zero
@Published public private(set) var destination: CGPoint = .zero
@Published public private(set) var screenSize: CGSize = .zero
+ @Published public private(set) var viewportOrigin: CGPoint = .zero
+ @Published public private(set) var viewportSize: CGSize = .zero
@Published public private(set) var opacity: Double = 0
@Published public private(set) var isHolding = false
@Published public private(set) var isSelected = false
@@ -19,6 +21,7 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
public var inputActivityChanged: ((Bool) -> Void)?
public var followUpSubmitted: ((String, String, String) -> Void)?
public var selected: ((String) -> Void)?
+ public var travelCompleted: (() -> Void)?
private var animationGeneration = 0
@@ -47,16 +50,66 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
public var hitTestFrame: CGRect {
guard state != nil, isHolding else { return .null }
+ return visualFrame(at: position, includesLabel: true)
+ }
+
+ public var localHitTestFrame: CGRect {
+ guard !hitTestFrame.isNull else { return .null }
+
+ return hitTestFrame.offsetBy(dx: -viewportOrigin.x, dy: -viewportOrigin.y)
+ }
+
+ public var visualFrame: CGRect {
+ guard state != nil else { return .null }
+
+ return visualFrame(at: position, includesLabel: isHolding)
+ }
+
+ public var renderPosition: CGPoint {
+ CGPoint(
+ x: position.x - viewportOrigin.x,
+ y: position.y - viewportOrigin.y
+ )
+ }
+
+ public var isTraveling: Bool {
+ state != nil && !isHolding
+ }
+
+ public var hasScreenSize: Bool {
+ screenSize.width > 0 && screenSize.height > 0
+ }
+
+ public func cursorOnlyVisualFrame(at point: CGPoint) -> CGRect {
+ CGRect(
+ x: point.x - 30,
+ y: point.y - 30,
+ width: 60,
+ height: 60
+ )
+ }
+
+ public func updateViewport(origin: CGPoint, size: CGSize) {
+ viewportOrigin = origin
+ viewportSize = size
+ }
+
+ private func visualFrame(
+ at point: CGPoint,
+ includesLabel: Bool
+ ) -> CGRect {
+ var frame = cursorOnlyVisualFrame(at: point)
+ guard includesLabel else { return frame }
+
let width: CGFloat = isInputActive ? 312 : 244
let height: CGFloat = isInputActive ? 142 : 54
- let offset = labelOffset(in: screenSize)
+ let offset = labelOffset(for: point, in: screenSize)
let origin = CGPoint(
- x: position.x + offset.width - width / 2,
- y: position.y + offset.height - height / 2
+ x: point.x + offset.width - width / 2,
+ y: point.y + offset.height - height / 2
)
- return cursorHitTestFrame
- .union(CGRect(origin: origin, size: CGSize(width: width, height: height)))
- .insetBy(dx: -14, dy: -14)
+ frame = frame.union(CGRect(origin: origin, size: CGSize(width: width, height: height)))
+ return frame.insetBy(dx: -10, dy: -10)
}
public var cursorHitTestFrame: CGRect {
@@ -71,6 +124,13 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
}
public func labelOffset(in screenSize: CGSize) -> CGSize {
+ labelOffset(for: position, in: screenSize)
+ }
+
+ private func labelOffset(
+ for cursorPosition: CGPoint,
+ in screenSize: CGSize
+ ) -> CGSize {
let labelSize = CGSize(
width: isInputActive ? 292 : 220,
height: isInputActive ? 126 : 46
@@ -81,13 +141,13 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
let upOffset = CGSize(width: rightOffset.width, height: -labelSize.height - 28)
let upLeftOffset = CGSize(width: leftOffset.width, height: upOffset.height)
- if point(position, offsetBy: rightOffset, labelSize: labelSize, fitsIn: screenSize, margin: margin) {
+ if point(cursorPosition, offsetBy: rightOffset, labelSize: labelSize, fitsIn: screenSize, margin: margin) {
return rightOffset
}
- if point(position, offsetBy: leftOffset, labelSize: labelSize, fitsIn: screenSize, margin: margin) {
+ if point(cursorPosition, offsetBy: leftOffset, labelSize: labelSize, fitsIn: screenSize, margin: margin) {
return leftOffset
}
- if point(position, offsetBy: upOffset, labelSize: labelSize, fitsIn: screenSize, margin: margin) {
+ if point(cursorPosition, offsetBy: upOffset, labelSize: labelSize, fitsIn: screenSize, margin: margin) {
return upOffset
}
return upLeftOffset
@@ -106,6 +166,8 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
self.screenSize = screenSize
self.opacity = 0
self.isHolding = false
+ self.viewportOrigin = .zero
+ self.viewportSize = .zero
self.inputText = ""
self.inputTextHeight = Self.inputMinimumTextHeight
self.isInputExpanded = false
@@ -115,11 +177,10 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
DispatchQueue.main.async { [weak self] in
guard let self, self.animationGeneration == generation else { return }
- withAnimation(Self.travelAnimation) {
- self.position = destination
+ withAnimation(.easeOut(duration: 0.12)) {
self.opacity = 1
}
- self.finishTravelAfterDelay(generation: generation)
+ self.finishTravelAfterDelay(generation: generation, finalPosition: destination)
}
}
@@ -147,11 +208,10 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
let generation = animationGeneration
self.destination = destination
self.isHolding = false
- withAnimation(Self.travelAnimation) {
- self.position = destination
+ withAnimation(.easeOut(duration: 0.12)) {
self.opacity = 1
}
- finishTravelAfterDelay(generation: generation)
+ finishTravelAfterDelay(generation: generation, finalPosition: destination)
}
public func fadeOut() {
@@ -170,6 +230,8 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
self.inputText = ""
self.inputTextHeight = Self.inputMinimumTextHeight
self.isInputExpanded = false
+ self.viewportOrigin = .zero
+ self.viewportSize = .zero
}
}
@@ -262,16 +324,18 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
isInputExpanded = clamped > Self.inputMinimumTextHeight + 1 || inputText.contains("\n")
}
- private func finishTravelAfterDelay(generation: Int) {
+ private func finishTravelAfterDelay(generation: Int, finalPosition: CGPoint) {
DispatchQueue.main.asyncAfter(deadline: .now() + Self.travelDuration) { [weak self] in
guard let self, self.animationGeneration == generation else { return }
+ self.position = finalPosition
self.isHolding = true
if var state = self.state, state.phase == .traveling || state.phase == .notchCue {
state.phase = .holding
state.updatedAt = Date()
self.state = state
}
+ self.travelCompleted?()
}
}
@@ -320,32 +384,6 @@ public final class PointerPromptSpawnOverlayViewModel: ObservableObject {
private static let travelAnimation = Animation.timingCurve(0.45, 0.05, 0.3, 1, duration: travelDuration)
}
-@MainActor
-public final class PointerPromptSpawnOverlayStore: ObservableObject {
- @Published public var viewModels: [PointerPromptSpawnOverlayViewModel] = []
-
- public init() {}
-}
-
-public struct PointerPromptSpawnOverlayContainerView: View {
- @ObservedObject private var store: PointerPromptSpawnOverlayStore
-
- public init(store: PointerPromptSpawnOverlayStore) {
- self.store = store
- }
-
- public var body: some View {
- ZStack(alignment: .topLeading) {
- Color.black.opacity(0.001)
-
- ForEach(store.viewModels, id: \.objectID) { viewModel in
- PointerPromptSpawnOverlayView(viewModel: viewModel)
- }
- }
- .ignoresSafeArea()
- }
-}
-
public struct PointerPromptSpawnOverlayView: View {
@ObservedObject private var viewModel: PointerPromptSpawnOverlayViewModel
@FocusState private var labelIsFocused: Bool
@@ -357,11 +395,9 @@ public struct PointerPromptSpawnOverlayView: View {
public var body: some View {
GeometryReader { proxy in
ZStack(alignment: .topLeading) {
- Color.black.opacity(0.001)
-
if let state = viewModel.state {
- spawnSurface(state: state, screenSize: proxy.size)
- .position(viewModel.position)
+ spawnSurface(state: state, screenSize: viewModel.hasScreenSize ? viewModel.screenSize : proxy.size)
+ .position(viewModel.renderPosition)
.opacity(viewModel.opacity)
}
}
diff --git a/apps/Donkey/Sources/DonkeyUI/PointerPromptStageView.swift b/apps/Donkey/Sources/DonkeyUI/PointerPromptStageView.swift
index 76f86434..e944af0d 100644
--- a/apps/Donkey/Sources/DonkeyUI/PointerPromptStageView.swift
+++ b/apps/Donkey/Sources/DonkeyUI/PointerPromptStageView.swift
@@ -251,23 +251,15 @@ struct PointerPromptComposer: View {
private var composerTrailingControls: some View {
HStack(spacing: PointerPromptLayout.composerTrailingControlsSpacing) {
- microphoneIcon
-
- if hasMessageText {
- sendButton(size: PointerPromptLayout.composerSendButtonSize)
- } else if state.isVoiceInputActive {
+ if state.isVoiceInputActive {
VoiceWaveformView(levels: state.voiceWaveformLevels)
.frame(
width: PointerPromptLayout.composerWaveformSize.width,
height: PointerPromptLayout.composerWaveformSize.height
)
} else {
- Color.clear
- .frame(
- width: PointerPromptLayout.composerSendButtonSize,
- height: PointerPromptLayout.composerSendButtonSize
- )
- .accessibilityHidden(true)
+ microphoneIcon
+ sendButton(size: PointerPromptLayout.composerSendButtonSize)
}
}
.frame(
@@ -279,7 +271,7 @@ struct PointerPromptComposer: View {
private var microphoneIcon: some View {
Image(systemName: "mic")
- .font(.system(size: 24, weight: .regular))
+ .font(.system(size: 24, weight: .ultraLight))
.symbolRenderingMode(.monochrome)
.foregroundStyle(Color.white.opacity(microphoneOpacity))
.frame(
@@ -310,9 +302,9 @@ struct PointerPromptComposer: View {
Button(action: submit) {
Image(systemName: "arrow.up")
.font(.system(size: sendIconSize(for: size), weight: .semibold))
- .foregroundStyle(Color.black.opacity(hasMessageText ? 0.78 : 0.32))
+ .foregroundStyle(Color.black.opacity(hasMessageText ? 0.78 : 0.42))
.frame(width: size, height: size)
- .background(Color.white.opacity(hasMessageText ? 0.94 : 0.34))
+ .background(Color.white.opacity(hasMessageText ? 0.94 : 0.68))
.clipShape(Circle())
}
.buttonStyle(.plain)
@@ -388,7 +380,7 @@ struct PointerPromptComposer: View {
}
private func sendIconSize(for buttonSize: CGFloat) -> CGFloat {
- buttonSize >= 44 ? 26 : 13
+ buttonSize <= 32 ? 13 : buttonSize * 0.46
}
private var usesExpandedSurface: Bool {