Skip to content

Open source CapCut alternative.

TypeScript

feat(app): slide the notch off-screen during Mission Control

David Le85b26370dfbc
diff --git a/apps/Donkey/Sources/Donkey/MissionControlMonitor.swift b/apps/Donkey/Sources/Donkey/MissionControlMonitor.swift
new file mode 100644
index 00000000..d209db52
--- /dev/null
+++ b/apps/Donkey/Sources/Donkey/MissionControlMonitor.swift
@@ -0,0 +1,161 @@
+import AppKit
+import CoreGraphics
+
+/// Watches for macOS Mission Control and reports when it becomes active or inactive.
+///
+/// macOS exposes no public notification for Mission Control, so we poll the window
+/// server. When Mission Control opens, the Dock process puts up one or more windows that
+/// span a full display; at rest the Dock owns no full-screen window (its strip is tiny).
+/// That full-screen Dock-owned window is the signal. Desktop elements are excluded so the
+/// always-present wallpaper window (also Dock-owned) doesn't read as a false positive.
+///
+/// Two things make naive polling feel bad, and this type handles both:
+/// - The poll runs on a background queue, not a main-thread timer. Mission Control's
+/// open/close animation starves the main run loop, which would otherwise delay a
+/// main-thread poll until the animation finishes — the source of the "laggy" feel.
+/// - The full-screen Dock window briefly disappears mid-transition. We turn the state
+/// ON immediately but OFF only after the signal has been gone for a few polls, so the
+/// notch doesn't twitch on those gaps.
+///
+/// Threading contract (why `@unchecked Sendable` is sound): `onChange`, `isActive`, the
+/// timer, and the observer are touched only on the main thread; `screenSizes`,
+/// `publishedActive`, and `offPollStreak` are touched only on `queue`. The two sides
+/// communicate by dispatching across, never by sharing.
+final class MissionControlMonitor: @unchecked Sendable {
+ /// Always invoked on the main thread when the active/inactive state flips.
+ var onChange: ((Bool) -> Void)?
+ private(set) var isActive = false
+
+ private let queue = DispatchQueue(label: "com.donkey.mission-control-monitor", qos: .userInitiated)
+ private var timer: DispatchSourceTimer?
+ private var screenParameterObserver: Any?
+
+ // Queue-confined state.
+ private var screenSizes: [CGSize] = []
+ private var publishedActive = false
+ private var offPollStreak = 0
+
+ /// A Dock-owned window must cover at least this fraction of a screen in both axes to
+ /// count as Mission Control. The Dock strip is large in only one axis, so requiring
+ /// both keeps the strip (and ordinary windows) from matching.
+ private let coverageThreshold: CGFloat = 0.9
+
+ /// 100 ms is responsive without being costly; the geometry-only query is cheap.
+ private let pollInterval: TimeInterval = 0.1
+
+ /// Mission Control drops its full-screen Dock window for a poll or two during the
+ /// open/close transition. Turn ON the instant we see the signal, but turn OFF only
+ /// after it has been absent this many consecutive polls, so the notch rides the gaps.
+ private let deactivationDebouncePolls = 3
+
+ func start() {
+ stop()
+ refreshScreenSizes()
+ screenParameterObserver = NotificationCenter.default.addObserver(
+ forName: NSApplication.didChangeScreenParametersNotification,
+ object: nil,
+ queue: .main
+ ) { [weak self] _ in
+ self?.refreshScreenSizes()
+ }
+
+ let timer = DispatchSource.makeTimerSource(queue: queue)
+ // Fire immediately so the first activation doesn't pay one-time setup latency.
+ timer.schedule(deadline: .now(), repeating: pollInterval)
+ timer.setEventHandler { [weak self] in
+ self?.poll()
+ }
+ self.timer = timer
+ timer.resume()
+ }
+
+ func stop() {
+ timer?.cancel()
+ timer = nil
+ if let screenParameterObserver {
+ NotificationCenter.default.removeObserver(screenParameterObserver)
+ self.screenParameterObserver = nil
+ }
+ queue.async { [weak self] in
+ self?.publishedActive = false
+ self?.offPollStreak = 0
+ }
+ if isActive {
+ isActive = false
+ onChange?(false)
+ }
+ }
+
+ /// Main thread: snapshot screen sizes and hand them to the polling queue.
+ private func refreshScreenSizes() {
+ let sizes = NSScreen.screens.map(\.frame.size)
+ queue.async { [weak self] in
+ self?.screenSizes = sizes
+ }
+ }
+
+ /// Polling queue: detect, debounce, and publish state changes to the main thread.
+ private func poll() {
+ let detected = Self.detectFullScreenDockWindow(
+ screenSizes: screenSizes,
+ coverageThreshold: coverageThreshold
+ )
+
+ let next: Bool
+ if detected {
+ offPollStreak = 0
+ next = true
+ } else {
+ offPollStreak += 1
+ next = offPollStreak >= deactivationDebouncePolls ? false : publishedActive
+ }
+
+ guard next != publishedActive else { return }
+ publishedActive = next
+ DispatchQueue.main.async { [weak self] in
+ self?.deliver(next)
+ }
+ }
+
+ /// Main thread: surface the flip to the owner.
+ private func deliver(_ active: Bool) {
+ guard active != isActive else { return }
+ isActive = active
+ onChange?(active)
+ }
+
+ /// Returns true when an on-screen, Dock-owned window covers most of any display.
+ private static func detectFullScreenDockWindow(
+ screenSizes: [CGSize],
+ coverageThreshold: CGFloat
+ ) -> Bool {
+ guard !screenSizes.isEmpty else { return false }
+ let options: CGWindowListOption = [.optionOnScreenOnly, .excludeDesktopElements]
+ guard let windows = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as? [[String: Any]] else {
+ return false
+ }
+
+ for window in windows {
+ guard let owner = window[kCGWindowOwnerName as String] as? String, owner == "Dock" else {
+ continue
+ }
+ // Skip the deep background layers (wallpaper, desktop icons); the Mission
+ // Control backdrop sits at or above the normal window layer.
+ if let layer = window[kCGWindowLayer as String] as? Int, layer < 0 {
+ continue
+ }
+ guard let boundsDict = window[kCGWindowBounds as String] as? NSDictionary,
+ let bounds = CGRect(dictionaryRepresentation: boundsDict) else {
+ continue
+ }
+
+ for size in screenSizes
+ where bounds.width >= size.width * coverageThreshold
+ && bounds.height >= size.height * coverageThreshold {
+ return true
+ }
+ }
+
+ return false
+ }
+}
diff --git a/apps/Donkey/Sources/Donkey/UserQueryOverlayController.swift b/apps/Donkey/Sources/Donkey/UserQueryOverlayController.swift
index d6d2e4cf..acfd2c61 100644
--- a/apps/Donkey/Sources/Donkey/UserQueryOverlayController.swift
+++ b/apps/Donkey/Sources/Donkey/UserQueryOverlayController.swift
@@ -18,6 +18,12 @@ final class UserQueryOverlayController {
private var statusHostingView: UserQueryHostingView<UserQueryNotchStatusView>?
private var inputPanel: NSPanel?
private var timer: Timer?
+ private let missionControlMonitor = MissionControlMonitor()
+ private var isMissionControlActive = false
+ /// 0 = the notch rests at the top of the screen, 1 = it has slid fully up and off-screen.
+ /// The 60 Hz tick eases this toward its target so the slide reads as a smooth animation
+ /// even though the panel is repositioned by direct per-frame `setFrame` calls.
+ private var missionControlHideProgress: CGFloat = 0
private var globalActivationMonitor: Any?
private var localActivationMonitor: Any?
private var appDeactivationObserver: Any?
@@ -86,6 +92,7 @@ final class UserQueryOverlayController {
prewarmInputPanel()
startActivationMonitoring()
startAppDeactivationMonitoring()
+ startMissionControlMonitoring()
positionStatusPanel()
centerInputPanel()
inputPanel.orderOut(nil)
@@ -220,6 +227,7 @@ final class UserQueryOverlayController {
statusHoverPhase = .collapsed
stopActivationMonitoring()
stopAppDeactivationMonitoring()
+ stopMissionControlMonitoring()
microphoneWaveformMeter.stop()
agentVisualizationCursorController.close()
spawnOverlayController.close()
@@ -289,6 +297,21 @@ final class UserQueryOverlayController {
}
}
+ private func startMissionControlMonitoring() {
+ missionControlMonitor.onChange = { [weak self] isActive in
+ Task { @MainActor in
+ self?.isMissionControlActive = isActive
+ }
+ }
+ missionControlMonitor.start()
+ }
+
+ private func stopMissionControlMonitoring() {
+ missionControlMonitor.onChange = nil
+ missionControlMonitor.stop()
+ isMissionControlActive = false
+ }
+
private func handleActivationEvent(_ event: NSEvent) {
switch event.type {
case .flagsChanged:
@@ -482,6 +505,7 @@ final class UserQueryOverlayController {
}
activateVoiceInputIfNeeded()
+ advanceMissionControlHideProgress()
// Build the notch metrics once per frame (the chin sizing does Core Text measurement) and feed
// both the panel position and the view update from it, instead of recomputing it for each.
let metrics = notchMetrics()
@@ -515,6 +539,23 @@ final class UserQueryOverlayController {
)
}
+ /// Extra travel beyond the surface height so the notch fully clears the screen edge
+ /// (the expanded chin can hang below the collapsed surface).
+ private static let missionControlHideClearance: CGFloat = 24
+
+ /// Eases `missionControlHideProgress` toward its target each frame. A per-frame
+ /// fraction gives an ease-out slide (~0.2s) that the 60 Hz tick renders by
+ /// repositioning the panel; snap to the endpoint to stop sub-pixel drift.
+ private func advanceMissionControlHideProgress() {
+ let target: CGFloat = isMissionControlActive ? 1 : 0
+ let delta = target - missionControlHideProgress
+ if abs(delta) < 0.001 {
+ missionControlHideProgress = target
+ return
+ }
+ missionControlHideProgress += delta * 0.22
+ }
+
private func positionStatusPanel(metrics: NotchMetrics? = nil, disableAnimations: Bool = true) {
guard let statusPanel,
let screen = activeScreen() else {
@@ -522,9 +563,13 @@ final class UserQueryOverlayController {
}
let metrics = metrics ?? notchMetrics(for: screen)
+ // While Mission Control is up, slide the notch past the top edge so it doesn't float
+ // over the Spaces/window thumbnails. The eased progress (driven by the tick) makes the
+ // extra Y offset animate the donkey up and off-screen, then back down when it closes.
+ let hideOffset = (metrics.surfaceSize.height + Self.missionControlHideClearance) * missionControlHideProgress
let frame = CGRect(
x: screen.frame.midX - metrics.surfaceSize.width / 2,
- y: screen.frame.maxY - metrics.surfaceSize.height,
+ y: screen.frame.maxY - metrics.surfaceSize.height + hideOffset,
width: metrics.surfaceSize.width,
height: metrics.surfaceSize.height
)