Skip to content

Open source CapCut alternative.

TypeScript

feat(app): add sign-out menu and gate surfaces on login

David Lefd93431297eb
diff --git a/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swift b/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swift
index 1cfc62bf..7393ea35 100644
--- a/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swift
+++ b/apps/Donkey/Sources/Donkey/DonkeyAppDelegate.swift
@@ -33,6 +33,7 @@ final class DonkeyAppDelegate: NSObject, NSApplicationDelegate {
self?.startAuthenticatedAppSurfaces()
}
registerAuthCallbackHandler()
+ installMainMenu()
if authCoordinator.isAuthenticated {
startAuthenticatedAppSurfaces()
@@ -111,13 +112,15 @@ final class DonkeyAppDelegate: NSObject, NSApplicationDelegate {
private func startOverlaySurfaces() {
guard overlayController == nil else { return }
- NSApp.setActivationPolicy(.accessory)
-
let model = UserQueryOverlayModel()
let controller = UserQueryOverlayController(model: model)
overlayController = controller
controller.show()
+ // Run as a regular app so Donkey keeps a Dock icon and its menu bar (including Sign Out)
+ // while the overlay surfaces are live, instead of receding into an accessory agent.
+ NSApp.setActivationPolicy(.regular)
+
// The UI-understanding engine (AX + AI parse + per-window cache + background warming) runs in
// every build. Only the visual overlay is debug-only: debug builds inject the AppKit overlay
// renderer, while production parses headlessly through a no-op renderer.
@@ -147,6 +150,85 @@ final class DonkeyAppDelegate: NSObject, NSApplicationDelegate {
frontmostVisionWarmCache?.start()
}
+ // MARK: - Sign out
+
+ @objc private func signOutMenuAction(_ sender: Any?) {
+ signOut()
+ }
+
+ private func signOut() {
+ authCoordinator?.signOut()
+ teardownAuthenticatedSurfaces()
+ showLoginWindow()
+ }
+
+ /// Tears down the live overlay/runtime surfaces so a later sign-in can rebuild them cleanly.
+ /// `startAuthenticatedAppSurfaces()` guards on these being nil, so they must be reset here.
+ private func teardownAuthenticatedSurfaces() {
+ frontmostVisionWarmCache?.stop()
+ frontmostVisionWarmCache = nil
+ uiUnderstandingCoordinator?.stop()
+ uiUnderstandingCoordinator = nil
+ overlayController?.stop()
+ overlayController = nil
+ runtimeOnboardingController?.close()
+ runtimeOnboardingController = nil
+ permissionSetupController = nil
+ }
+
+ func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
+ if menuItem.action == #selector(signOutMenuAction) {
+ return authCoordinator?.isAuthenticated == true
+ }
+ return true
+ }
+
+ // MARK: - Menu
+
+ private func installMainMenu() {
+ let mainMenu = NSMenu()
+
+ let appMenuItem = NSMenuItem()
+ mainMenu.addItem(appMenuItem)
+ let appMenu = NSMenu()
+ appMenuItem.submenu = appMenu
+
+ let signOutItem = NSMenuItem(
+ title: "Sign Out",
+ action: #selector(signOutMenuAction(_:)),
+ keyEquivalent: ""
+ )
+ signOutItem.target = self
+ appMenu.addItem(signOutItem)
+ appMenu.addItem(.separator())
+ appMenu.addItem(
+ withTitle: "Quit \(Self.appDisplayName)",
+ action: #selector(NSApplication.terminate(_:)),
+ keyEquivalent: "q"
+ )
+
+ let editMenuItem = NSMenuItem()
+ mainMenu.addItem(editMenuItem)
+ let editMenu = NSMenu(title: "Edit")
+ editMenuItem.submenu = editMenu
+ editMenu.addItem(withTitle: "Cut", action: #selector(NSText.cut(_:)), keyEquivalent: "x")
+ editMenu.addItem(withTitle: "Copy", action: #selector(NSText.copy(_:)), keyEquivalent: "c")
+ editMenu.addItem(withTitle: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v")
+ editMenu.addItem(
+ withTitle: "Select All",
+ action: #selector(NSText.selectAll(_:)),
+ keyEquivalent: "a"
+ )
+
+ NSApp.mainMenu = mainMenu
+ }
+
+ private static var appDisplayName: String {
+ Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
+ ?? Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String
+ ?? ProcessInfo.processInfo.processName
+ }
+
private func registerAuthCallbackHandler() {
NSAppleEventManager.shared().setEventHandler(
self,
diff --git a/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swift b/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swift
index 520500c2..2d5c84f9 100644
--- a/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swift
+++ b/apps/Donkey/Sources/Donkey/DonkeyAuthCoordinator.swift
@@ -96,15 +96,18 @@ final class DonkeyAuthCoordinator: ObservableObject {
private let configuration: DonkeyAuthConfiguration
private let stateStore: DonkeyAuthStateStoring
private let nativeSessionExchanger: any DonkeyNativeSessionExchanging
+ private let cookieStorage: HTTPCookieStorage
init(
configuration: DonkeyAuthConfiguration = .current(),
stateStore: DonkeyAuthStateStoring = DonkeyAuthStateStore(),
- nativeSessionExchanger: any DonkeyNativeSessionExchanging = DonkeyNativeSessionCookieExchanger()
+ nativeSessionExchanger: any DonkeyNativeSessionExchanging = DonkeyNativeSessionCookieExchanger(),
+ cookieStorage: HTTPCookieStorage = .shared
) {
self.configuration = configuration
self.stateStore = stateStore
self.nativeSessionExchanger = nativeSessionExchanger
+ self.cookieStorage = cookieStorage
if let session = stateStore.loadSession() {
phase = .signedIn(session)
@@ -208,6 +211,24 @@ final class DonkeyAuthCoordinator: ObservableObject {
phase = stateStore.loadSession().map(DonkeyAuthPhase.signedIn) ?? .signedOut
}
+ /// Drops the local session and its native session cookie, returning to the
+ /// signed-out state. Callers should then surface the login flow again.
+ func signOut() {
+ stateStore.clearSession()
+ stateStore.clearPendingState()
+ clearSessionCookies()
+ phase = .signedOut
+ }
+
+ private func clearSessionCookies() {
+ guard let cookies = cookieStorage.cookies(for: configuration.webBaseURL) else {
+ return
+ }
+ for cookie in cookies {
+ cookieStorage.deleteCookie(cookie)
+ }
+ }
+
private func signInURL(state: String) -> URL? {
var components = URLComponents(
url: configuration.webBaseURL,
@@ -244,6 +265,7 @@ final class DonkeyAuthCoordinator: ObservableObject {
protocol DonkeyAuthStateStoring {
func loadSession() -> DonkeyAuthSession?
func saveSession(_ session: DonkeyAuthSession)
+ func clearSession()
func loadPendingState() -> String?
func savePendingState(_ state: String)
func clearPendingState()
@@ -272,6 +294,10 @@ struct DonkeyAuthStateStore: DonkeyAuthStateStoring {
defaults.set(data, forKey: Keys.session)
}
+ func clearSession() {
+ defaults.removeObject(forKey: Keys.session)
+ }
+
func loadPendingState() -> String? {
defaults.string(forKey: Keys.pendingState)
}