Swift
Fix connecting, audio and display bugs from the audit.
Andy Gao37191cabc78f
diff --git a/Docs/DESIGN-SYSTEM.md b/Docs/DESIGN-SYSTEM.mdindex cf62bd0..16408d7 100644--- a/Docs/DESIGN-SYSTEM.md+++ b/Docs/DESIGN-SYSTEM.md@@ -35,11 +35,11 @@ Never show or describe tapping the back of the case, holding the open case uprig| Surface | Purpose | Size ||---|---|---|-| Setup assistant | First run: Allow → Connect → Calibrate → Tap. Permission comes first because the mouse is out of reach once the case is in hand: from then on each finished step leads to the next by itself, and the dots start on their own. Tap is the last step: it starts listening by itself, fills three dots as the person taps the front of the case, and can be skipped; once learned, its primary is Finish, with the fn ⌃ footer. Settings reaches the same step with Teach Tap…. One step per screen, a step indicator on top, a primary button bottom-right (Return). Connect walks through Connect → open the lid → Lid Closed (Return) → turn upright → "You’re connected", with the matching `CaseStepView`. Before the first press, Connect says what a session does to the AirPods and the case, and a problem shows above that note instead of replacing it. Setup always connects the closed case; the dots wait for the Calibrate step. | 560 × 620 window |+| Setup assistant | First run: Allow → Connect → Calibrate → Tap. Permission comes first because the mouse is out of reach once the case is in hand: from then on each finished step leads to the next by itself, and the dots start on their own. Tap is the last step: it starts listening by itself, fills three dots as the person taps the front of the case, and can be skipped; once learned, its primary is Finish, with the fn ⌃ footer. Settings reaches the same step with Teach Tap…. One step per screen, a step indicator on top, a primary button bottom-right (Return). Connect walks through Connect → open the lid → Lid Closed (Return) → turn upright → "You’re connected", with the matching `CaseStepView`. Before the first press, Connect says what a session does to the AirPods and the case, and a problem shows above that note instead of replacing it. Until that first press, fn ⌃ and every other way to connect open this step instead. Finish and Skip for Now wait for Accessibility: without it, they go back to Allow. Setup always connects the closed case; the dots wait for the Calibrate step. | 560 × 620 window || Calibration overlay | Full-screen dark overlay. Three dots: center, right edge, top edge. Each captures when the person holds still on it. Esc cancels. With saved edges it is one dot ("AirPointer · Recentering", no step row); if the case is held differently from last time, it carries on to the two edges. | Full screen of the controlled display || Main window ("Remote") | Two pages, **Pointer** and **Settings**, and no section tabs: the toolbar is only the gear. Settings opens with a "‹ Pointer" back button; esc and ⌘1 also go back. Pages scroll; a notice bar sits above them. | 440 × 700, min 400 × 620, max width 560 || Menu-bar popover | Live aim pad, Recenter, Pause, speed, then menu rows: Open AirPointer, Calibrate…, Disconnect (only while the closed case is connected), Settings…, Quit. | 320 wide |-| Control HUD | A small non-activating capsule: "Pointing", "Paused — reason", "Disconnected — connect again, then open the lid". | 40 tall, ≤ 480 wide |+| Control HUD | A small non-activating capsule: "Pointing", "Paused — reason", "Disconnected — connect again, then open the lid", "Pointer stopped — allow AirPointer in Accessibility". | 40 tall, ≤ 480 wide || Sheets | Attached to the main window, for a task that starts there. None are in use. | ≈ 420 wide || Developer tools | Keep audio on Mac and the tap test window. Under **File ▸ Developer** only; never linked from the main flows. The test window uses the same parts as the product: a DEVELOPER eyebrow and status chip, a 22 pt title, one primary bottom-right. | 480 × 440 |@@ -65,23 +65,23 @@ A flow should never open a second regular window to finish a task that started iUntil the closed case is upright and held, the stage shows the step to do with it (`CaseStepView`: closed, opening, open, close, turn). After that it shows the live aim pad.-**Pause never drops the connection.** Pause, fn ⌃, closing the window, opening Settings and display changes only stop the pointer (`ClosedCaseSession.pause`), and Resume continues at once. **Disconnect** is the one way to end it on purpose: a tertiary button on the Pointer page, a row in the popover and an item in the File menu, shown only while the closed case is connected. Quitting also disconnects. Cancel (esc) before the case is upright ends the attempt.+**Pause never drops the connection.** Pause, fn ⌃, closing the window, opening Settings and display changes only stop the pointer (`ClosedCaseSession.pause`), and Resume continues at once. **Disconnect** is the one way to end it on purpose: a tertiary button on the Pointer page, a row in the popover and an item in the File menu, shown only while the closed case is connected. Quitting also disconnects. Cancel (esc) before the case is upright ends the attempt. Allow only ever opens the Accessibility list: losing Accessibility mid-session stops the pointer but keeps the connection.When a connection drops on its own, the page goes back to "Connect your AirPods", the notice bar says why and what to do, and the HUD shows "Disconnected — connect again, then open the lid". Disconnecting on purpose shows no notice.**Motion drops keep the calibration.** Under two seconds without motion, the pointer just freezes and carries on. Longer, it pauses ("Lost the case’s motion. Pointing resumes when it’s back."), keeps the saved edges and asks for the center again (one dot); pointing then comes back without a click. A pause the person chose never resumes on its own.-**Every failure to connect names its cause and what to do:** no Bluetooth access (opens Privacy & Security ▸ Bluetooth), Bluetooth off, no AirPods Pro paired, more than one pair (opens Settings to choose), not paired any more, the AirPods didn't answer, or they're also connected to an iPhone and send no motion. No message tells the person to rebuild the app.+**Every failure to connect names its cause and what to do:** no Bluetooth access (opens Privacy & Security ▸ Bluetooth), Bluetooth off, no AirPods Pro paired, more than one pair (opens Settings to choose), not paired any more, the AirPods didn't answer, or they're also connected to an iPhone and send no motion. No message tells the person to rebuild the app. Setup says it on its Connect step and a session from the notch says it there; any other session opens the window to say it, even if it was closed.### Connecting from the notchfn ⌃ from any app drops the connect steps out of the notch (`NotchFlow.swift`), so nobody has to open the window. The panel hangs from the camera housing: a neck the notch's width, 7 pt concave fillets where it meets the menu bar's bottom edge, 20 pt outer top corners and 22 pt bottom corners, pure black fading to #1C1C1C. The case art sits in the middle, playing the same `CaseStepView` steps as the window (lid opening, closing, the quarter turn, upright); three progress bars (open, close, turn) sit under it, then the title and one line, all centered. It opens in about 0.3 s with a slight rebound and closes in about 0.26 s (the step first, then the shape); with Reduce Motion it appears and goes at once. It never takes focus or clicks. Each time it drops down it plays one shimmer (`Assets/NotchShimmer.wav`), on the Mac's output, never on a step change.-There's no Start Pointing at the end, from the notch or the window: with saved edges the pose the upright case settles in (within 2.5° for 0.4 s) is the center, and the cursor starts, even if it's held another way (Recalibrate fixes the aim then). Only the first time do the dots take over, and the notch tucks away. "Pointing" and "Paused" show for under two seconds, then the notch closes; a connection that drops on its own says why for four seconds. fn ⌃ cancels while connecting and pauses or resumes once pointing. The notch replaces the HUD on its own screen.+There's no Start Pointing at the end, from the notch or the window: with saved edges the pose the upright case settles in (within 2.5° for 0.4 s) is the center, and the cursor starts, even if it's held another way (Recalibrate fixes the aim then). Only the first time do the dots take over, and the notch tucks away. "Pointing" and "Paused" show for under two seconds, then the notch closes; a connection that drops on its own says why, in up to four lines, for at least four seconds and longer for a longer message. fn ⌃ cancels while connecting and pauses or resumes once pointing. The notch replaces the HUD on its own screen.### Sound stays on the Mac-While the pointer is in use, from Connect until the session ends, Mac sound and mic stay off AirPods (`AudioOutputGuard`). A mic opening on them would put them in a hands-free call and stop motion, and sound shouldn't move to AirPods that are in their case. When the session ends, whatever it moved goes back unless someone has chosen another device since. With no session, AirPointer leaves audio alone, even with the app open.+While the pointer is in use, from Connect until the session ends, Mac sound and mic stay off AirPods (`AudioOutputGuard`). A mic opening on them would put them in a hands-free call and stop motion, and sound shouldn't move to AirPods that are in their case. When the session ends, whatever it moved goes back unless someone has chosen another device since. The moves are saved, so after a force quit or crash they go back the next time the app opens, under the same rule, as long as the device is still connected. With no session, AirPointer leaves audio alone, even with the app open.### The pointer shortcutdiff --git a/README.md b/README.mdindex 0345de3..2a1372b 100644--- a/README.md+++ b/README.md@@ -20,7 +20,7 @@| **Case data** | Ending a session uses up the diagnostic data stored on the case. || **AirPods settings** | Some AirPods connection settings stay as the session set them. We do not know yet if this change, or the case data change, lasts after the Bluetooth link resets. || **Audio** | Each connection drops the AirPods' Bluetooth link for a moment, and their audio stops. Do not connect during a call. |-| **Microphone** | During a session, the app keeps Mac sound and the microphone off all AirPods. It restores them when the session ends. |+| **Microphone** | During a session, the app keeps Mac sound and the microphone off all AirPods. It restores them when the session ends. After a force quit, it restores them when you next open the app, if the AirPods are still connected. || **Silent audio** | During a session, the app plays silence to the AirPods to keep their audio link active. You do not hear it. || **Cursor size** | A cursor smaller than 2× grows to 2× while you point. It returns to its size when you pause or disconnect. After a force quit, it returns when you next open the app. This uses a private macOS call. |@@ -204,6 +204,9 @@ send any of this anywhere.| `PointerSessions/` | Logs from the newest ten sessions || `TapTests.jsonl` | Tap Test scores |+During a session, the app's preferences also save which audio devices it moved.+The saved names include the AirPods' Bluetooth address.+In a session's `native.log`:- `STEP` lines show the time of each connection step.diff --git a/Sources/AirPointer/App/AirPointer.swift b/Sources/AirPointer/App/AirPointer.swiftindex 699c3fd..3a5be2c 100644--- a/Sources/AirPointer/App/AirPointer.swift+++ b/Sources/AirPointer/App/AirPointer.swift@@ -45,6 +45,7 @@ final class App: NSObject, NSApplicationDelegate {if self?.closedCaseSession?.isActive == true { self?.closedCalibrationDeclined = true; self?.refreshHome() }}CursorSize.shared.restore()+ AudioOutputGuard.shared.restoreAfterCrash()setupMenu()installNotch()let stateTimer=Timer(timeInterval:0.08,repeats:true){[weak self] _ in self?.checkState()}@@ -61,9 +62,7 @@ final class App: NSObject, NSApplicationDelegate {pause();showHome();home?.setNotice("Connect your AirPods first, then recalibrate.")}func refreshScreenList() {- let count=NSScreen.screens.count- if count==0{pointerSettings.screen=0;return}- if pointerSettings.screen>=count{pointerSettings.screen=count-1}+ pointerSettings.screen=PointerSettingsState.screenIndex(chosen:pointerSettings.screenID,current:pointerSettings.screen,in:NSScreen.screens.map(\.displayID))}@objc func screensChanged() {pause("Your displays changed. Resume when you’re ready."); refreshScreenList(); pointerSettings.persist()diff --git a/Sources/AirPointer/App/AppHome.swift b/Sources/AirPointer/App/AppHome.swiftindex abc3e72..c03905f 100644--- a/Sources/AirPointer/App/AppHome.swift+++ b/Sources/AirPointer/App/AppHome.swift@@ -28,20 +28,26 @@ extension App {// The state timer stands down while the session owns the input, so the menu bar, HUD and setup follow the session here.self.refreshHome();self.refreshStatusBar();self.refreshSetup();self.refreshNotch()}- // A connection that drops on its own says why above the page; Disconnect stays quiet.- session.onEnded={ [weak self] reason in+ session.onEnded={ [weak self,weak session] reason in// The window is often out of sight while pointing, so the notch says what happened and pointing returns once the lid opens and closes.- if session.lostContact{+ if session?.lostContact == true{DispatchQueue.main.async{self?.startClosedPointerSession(ClosedCaseSessionLaunch(fromNotch:true,afterLoss:true))}return}+ // Disconnect stays quiet.guard let reason else{return}- self?.home?.setNotice(reason)- if reason==ClosedCaseAirPods.Choice.several.failure{self?.showHomeSettings()}+ self?.reportEnded(reason,fromNotch:session?.usesNotch == true)}home?.setNotice(nil)session.beginSession()}+ // A connection that drops on its own says why: setup on its Connect step, the notch for its own sessions, and the window for the rest, even if it was closed.+ func reportEnded(_ reason:String,fromNotch:Bool){+ let inSetup=setup?.window.isVisible == true,several=reason==ClosedCaseAirPods.Choice.several.failure+ if !inSetup,several || !fromNotch{showHome()}+ home?.setNotice(reason)+ if several,!inSetup{showHomeSettings()}+ }// Pause keeps the case connected; this is the one way to end the connection on purpose.@objc func disconnectClosedCase(){guard let session=closedCaseSession,session.isActive else{return}@@ -79,6 +85,12 @@ extension App {}var homeInputsReady:Bool{closedCaseSession?.inputReady == true}func homeControlAction(){+ // Without Accessibility the button says Allow, even during a session, so it only ever opens the list.+ guard AXIsProcessTrusted() else{+ home?.setNotice(nil)+ AccessibilityGuide.shared.open()+ refreshHome();return+ }if let session=closedCaseSession,session.isActive{switch session.stage {case .closeLid:session.confirmLidClosed()@@ -89,13 +101,8 @@ extension App {}refreshHome();return}- guard !closedCaseOwnsInput else{return}+ guard !closedCaseOwnsInput,!showConnectEffectsFirst() else{return}home?.setNotice(nil)- guard AXIsProcessTrusted() else{- homePhase = .needsAccessibility;home?.setNotice(nil)- AccessibilityGuide.shared.open()- refreshHome();return- }startClosedPointerSession()}func homeRecenter(){if let session=closedCaseSession,session.isActive{session.recenter()}else{pause();refreshHome()}}@@ -107,7 +114,8 @@ extension App {else if !trusted{homePhase = .needsAccessibility}else{homePhase = .needsInputs}let title:String- if let session=closedCaseSession,session.isActive {+ if !trusted{title="Allow AirPointer…"}+ else if let session=closedCaseSession,session.isActive {switch session.stage {case .pointing:title="Pause"case .ready:title="Start Pointing"@@ -155,7 +163,7 @@ extension App {let screenChanged=pointerSettings.screen != next.screenpointerSettings.speed=next.speedpointerSettings.clickSound=next.clickSound- if NSScreen.screens.indices.contains(next.screen){pointerSettings.screen=next.screen}+ if screenChanged,NSScreen.screens.indices.contains(next.screen){pointerSettings.screen=next.screen;pointerSettings.screenID=NSScreen.screens[next.screen].displayID}pointerSettings.persist()if screenChanged{pause()}if let session=closedCaseSession{applyPointerSettings(pointerSettings,to:session)}@@ -170,11 +178,19 @@ extension App {extension App {// First run: setup never finished.var needsSetup:Bool{!UserDefaults.standard.bool(forKey:"setupComplete")}+ // Setup's Connect step says what a session does, so every way to connect goes there until someone has pressed it.+ func showConnectEffectsFirst()->Bool{+ guard needsSetup,!UserDefaults.standard.bool(forKey:"connectEffectsSeen") else{return false}+ showSetup();return true+ }@objc func showSetup(){if setup==nil {let assistant=SetupAssistant()// Setup always connects the closed case.- assistant.onConnect={ [weak self] in self?.startClosedPointerSession() }+ assistant.onConnect={ [weak self] in+ UserDefaults.standard.set(true,forKey:"connectEffectsSeen")+ self?.startClosedPointerSession()+ }assistant.onChooseAirPods={ [weak self] in self?.chooseAirPods() }assistant.onLidClosed={ [weak self] in self?.closedCaseSession?.confirmLidClosed() }assistant.onRequestAccessibility={AccessibilityGuide.shared.open()}@@ -208,6 +224,7 @@ extension App {}@objc private func pickAirPods(_ item:NSMenuItem){guard let pair=item.representedObject as? ClosedCaseAirPods,(try? ClosedCaseAirPods.save(pair)) != nil else{return}+ UserDefaults.standard.set(true,forKey:"connectEffectsSeen")startClosedPointerSession()}@objc func runSetupAgain(){pause();showSetup();setup?.go(to:AXIsProcessTrusted() ? 1:0)}@@ -298,6 +315,12 @@ extension App {private func announcePhaseChange(){guard homePhase != hudPhase else{return}let previous=hudPhase;hudPhase=homePhase+ // The notch has no step for this, so the HUD says it for every session.+ switch (previous,homePhase) {+ case (.running,.needsAccessibility),(.paused,.needsAccessibility):+ hud.show("Pointer stopped — allow AirPointer in Accessibility",symbol:"hand.raised",on:controlledScreen);return+ default:break+ }// A session from the notch says it there; the HUD stays for a pointer on another screen.if closedCaseSession?.usesNotch == true,let notch,notch.screen == controlledScreen{return}switch (previous,homePhase) {diff --git a/Sources/AirPointer/Checks/HomeChecks.swift b/Sources/AirPointer/Checks/HomeChecks.swiftindex 2d20a7c..f19118c 100644--- a/Sources/AirPointer/Checks/HomeChecks.swift+++ b/Sources/AirPointer/Checks/HomeChecks.swift@@ -122,6 +122,12 @@ func runHomeChecks()throws {try require(primary().title=="Resume" || primary().title=="Start Pointing","Settings had no way back to the pointer")home.selectSettings();home.window.onEscape?()try require(primary().title=="Resume" || primary().title=="Start Pointing","Esc did not leave Settings")+ // Every step that offers Cancel (esc) answers to it.+ for phase in [HomeControlPhase.running,.connecting,.closedCase(.openLid),.closedCase(.turnUpright),.closedCase(.holdStill)] {+ home.updateControl(phase:phase,inputsReady:false)+ let before=controlPresses;home.window.onEscape?()+ try require(controlPresses==before+1,"Esc did nothing during \(phase)")+ }home.updateControl(phase:.connecting,inputsReady:false)try require(words().contains("Keep the lid open for a moment.") && !words().contains("sound"),"The pointer’s connection still waited for audio")for phase in [HomeControlPhase.needsInputs,.connecting,.needsAccessibility,.ready,.running,.paused("Paused"),.closedCase(.openLid),.closedCase(.closeLid),.closedCase(.turnUpright),.closedCase(.holdStill),.closedCase(.calibrate),.closedCase(.recenter)] {@@ -227,6 +233,41 @@ func runHomeChecks()throws {try retired(setupViews(),"Setup’s last step")finish?.performClick(nil)try require(finished==1,"Finish did not end setup")+ // Finish and Skip for Now both wait for Accessibility, since the pointer can't move without it.+ assistant.update(SetupState(link:.connected,calibrated:true,taps:SetupTaps(heard:3,learned:true)));assistant.go(to:3)+ setupViews().compactMap{$0 as? RemoteButton}.first{$0.style == .primary}?.performClick(nil)+ try require(finished==1 && assistant.step==0,"Setup finished without Accessibility")++ // Nobody connects before setup's Connect step has said what a session does, and each kind of ending says why somewhere.+ let defaults=UserDefaults.standard,firstRunKeys=["setupComplete","connectEffectsSeen"]+ let firstRunPrior=Dictionary(uniqueKeysWithValues:firstRunKeys.map{($0,defaults.object(forKey:$0))})+ defer{for key in firstRunKeys{if let value=firstRunPrior[key]{defaults.set(value,forKey:key)}else{defaults.removeObject(forKey:key)}}}+ for key in firstRunKeys{defaults.removeObject(forKey:key)}+ let app=App();defer{app.setup?.window.orderOut(nil);app.home?.window.orderOut(nil)}+ try require(app.showConnectEffectsFirst() && app.setup?.window.isVisible == true && app.setup?.step == (AXIsProcessTrusted() ? 1:0),"A first connection should open setup’s Connect step")+ func appWindow(_ text:String,on window:NSWindow?)->Bool{descendants(window!.contentView!).contains{($0 as? NSTextField)?.stringValue.contains(text)==true}}+ try require(appWindow("diagnostic data",on:app.setup?.window) || !AXIsProcessTrusted(),"The first connection should show what a session does")+ app.reportEnded("Reason during setup.",fromNotch:false)+ try require(app.home == nil,"Setup says why on its own step, so the window should stay closed")+ app.setup?.window.close()+ app.reportEnded("Reason in the notch.",fromNotch:true)+ try require(app.home == nil,"The notch says why for its own sessions, so the window should stay closed")+ app.reportEnded("Reason for the window.",fromNotch:false)+ try require(app.home?.window.isVisible == true && appWindow("Reason for the window.",on:app.home?.window),"A closed window should open to say why the connection ended")+ defaults.set(true,forKey:"connectEffectsSeen")+ try require(!app.showConnectEffectsFirst() && app.setup == nil,"Someone who pressed Connect shouldn’t be sent back to setup")+ defaults.removeObject(forKey:"connectEffectsSeen");defaults.set(true,forKey:"setupComplete")+ try require(!app.showConnectEffectsFirst() && app.setup == nil,"Finished setup shouldn’t be reopened to connect")++ // The notch shows every failure in full.+ let ending=NotchSurface();ending.frame=NSRect(x:0,y:0,width:360,height:NotchFlow.body+32)+ for reason in (0...60).flatMap({[ClosedCaseAirPods.helperFailure(Int32($0),stopping:false),ClosedCaseAirPods.restoreFailure(Int32($0))]}).compactMap({$0}) {+ ending.show(.ended(reason),fade:false);ending.layoutSubtreeIfNeeded()+ let shown=descendants(ending).compactMap{$0 as? NSTextField}.first{$0.stringValue==reason}+ let whole=rkWrap(reason,12);whole.maximumNumberOfLines=0+ let needed=whole.cell!.cellSize(forBounds:NSRect(x:0,y:0,width:shown?.frame.width ?? 0,height:400)).height+ try require(shown != nil && ceil(needed)<=shown!.frame.height,"The notch cuts off: \(reason)")+ }// The menu-bar popover: aim pad, Recenter, Pause and speed, then its rows. No mode and nothing to teach.let popover=MenuBarContentController()@@ -329,7 +370,7 @@ func runHomeChecks()throws {final class Wallpaper:NSView{override func draw(_ dirtyRect:NSRect){NSColor(srgbRed:0.36,green:0.42,blue:0.52,alpha:1).setFill();bounds.fill();NSColor(white:1,alpha:0.18).setFill();NSRect(x:0,y:bounds.height-32,width:bounds.width,height:32).fill()}}let notchSteps:[(String,NotchContent,Double)]=[("open-lid",NotchContent.connecting(.openLid,preparing:false)!,1.2),("looking",NotchContent.connecting(.connecting,preparing:false)!,0.8),("close-lid",NotchContent.connecting(.closeLid,preparing:false)!,1.2),("turn",NotchContent.connecting(.settling,preparing:false)!,1.25),- ("aim",NotchContent.connecting(.calibrating,preparing:false)!,0),("pointing",.pointing,0),("ended",.ended("AirPointer stopped waiting for the lid. Connect again when you’re ready."),0)]+ ("aim",NotchContent.connecting(.calibrating,preparing:false)!,0),("pointing",.pointing,0),("ended",.ended("AirPointer stopped waiting for the lid. Connect again when you’re ready."),0),("ended-long",.ended(ClosedCaseAirPods.helperFailure(30,stopping:false)!),0)]let notchRow=NotchFlow.body+32+20let notchSheet=Wallpaper(frame:NSRect(x:0,y:0,width:440,height:CGFloat(notchSteps.count)*notchRow))for (index,(_,content,time)) in notchSteps.enumerated() {diff --git a/Sources/AirPointer/Interface/AirPointerHome.swift b/Sources/AirPointer/Interface/AirPointerHome.swiftindex 5ca737b..0cef5da 100644--- a/Sources/AirPointer/Interface/AirPointerHome.swift+++ b/Sources/AirPointer/Interface/AirPointerHome.swift@@ -5,9 +5,20 @@ import AppKitenum ClosedCaseStep:Equatable {case openLid,closeLid,turnUpright,holdStill,calibrate,recenter}enum HomeControlPhase:Equatable {case needsInputs,connecting,needsAccessibility,ready,running,paused(String),closedCase(ClosedCaseStep)}+extension NSScreen {+ // Stays the same across unplugging and reordering, unlike the place in NSScreen.screens.+ var displayID:String? {+ guard let number=deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber,+ let uuid=CGDisplayCreateUUIDFromDisplayID(number.uint32Value)?.takeRetainedValue() else{return nil}+ return CFUUIDCreateString(nil,uuid) as String+ }+}+struct PointerSettingsState:Equatable {var screens:[String]=[]var screen=0+ // The display someone picked in Settings. While it's unplugged the pointer uses another, and this waits for it to return.+ var screenID:String?=nil// Paired AirPods Pro by name; -1 until one is chosen among several.var pairs:[String]=[]var pair=0@@ -21,6 +32,12 @@ struct PointerSettingsState:Equatable {private static let clickSoundKey="tapClickSound"private static let tapDirectionKey="tapDirection"private static let screenKey="pointerScreen"+ private static let screenIDKey="pointerScreenID"++ static func screenIndex(chosen:String?,current:Int,in displays:[String?])->Int {+ if let chosen,let found=displays.firstIndex(of:chosen){return found}+ return max(0,min(current,displays.count-1))+ }static func loadPersisted()->PointerSettingsState {var value=PointerSettingsState()@@ -30,6 +47,7 @@ struct PointerSettingsState:Equatable {value.clickSound=UserDefaults.standard.object(forKey:clickSoundKey) as? Bool ?? truevalue.tapDirection=(UserDefaults.standard.array(forKey:tapDirectionKey) as? [Double]).flatMap{$0.count==3 ? $0:nil}if let saved=UserDefaults.standard.object(forKey:screenKey) as? Int{value.screen=max(0,saved)}+ value.screenID=UserDefaults.standard.string(forKey:screenIDKey)return value}@@ -40,28 +58,38 @@ struct PointerSettingsState:Equatable {UserDefaults.standard.set(clickSound,forKey:Self.clickSoundKey)if let tapDirection{UserDefaults.standard.set(tapDirection,forKey:Self.tapDirectionKey)}else{UserDefaults.standard.removeObject(forKey:Self.tapDirectionKey)}UserDefaults.standard.set(screen,forKey:Self.screenKey)+ if let screenID{UserDefaults.standard.set(screenID,forKey:Self.screenIDKey)}else{UserDefaults.standard.removeObject(forKey:Self.screenIDKey)}}}func runPointerSettingsChecks()throws {let defaults=UserDefaults.standard- let keys=["pointerSensitivity","tapClickSound","pointerScreen","tapDirection"]+ let keys=["pointerSensitivity","tapClickSound","pointerScreen","pointerScreenID","tapDirection"]let prior=Dictionary(uniqueKeysWithValues:keys.map{($0,defaults.object(forKey:$0))})defer{for key in keys{if let value=prior[key]{defaults.set(value,forKey:key)}else{defaults.removeObject(forKey:key)}}}defaults.removeObject(forKey:"pointerSensitivity")defaults.removeObject(forKey:"tapClickSound")defaults.removeObject(forKey:"pointerScreen")+ defaults.removeObject(forKey:"pointerScreenID")defaults.removeObject(forKey:"tapDirection")let fresh=PointerSettingsState.loadPersisted()- guard fresh.speed==1,fresh.clickSound,fresh.screen==0,fresh.tapDirection==nil else{+ guard fresh.speed==1,fresh.clickSound,fresh.screen==0,fresh.screenID==nil,fresh.tapDirection==nil else{throw LessonError.message("Default pointer settings should be speed 1, click sound on, screen 0.")}- var sample=PointerSettingsState(screen:1,speed:1.4,clickSound:false,tapDirection:[0.6,0,-0.8])+ var sample=PointerSettingsState(screen:1,screenID:"side",speed:1.4,clickSound:false,tapDirection:[0.6,0,-0.8])sample.clampSpeed();sample.persist()let loaded=PointerSettingsState.loadPersisted()- guard abs(loaded.speed-1.4)<1e-9,!loaded.clickSound,loaded.screen==1,loaded.tapDirection==[0.6,0,-0.8] else{+ guard abs(loaded.speed-1.4)<1e-9,!loaded.clickSound,loaded.screen==1,loaded.screenID=="side",loaded.tapDirection==[0.6,0,-0.8] else{throw LessonError.message("Pointer settings did not round-trip through UserDefaults.")}+ // The chosen display wherever it now sits; with it unplugged, a display that exists; with no choice, the same place.+ guard PointerSettingsState.screenIndex(chosen:"side",current:0,in:["main","other","side"])==2,+ PointerSettingsState.screenIndex(chosen:"side",current:2,in:["main","other"])==1,+ PointerSettingsState.screenIndex(chosen:"side",current:1,in:[nil,"other"])==1,+ PointerSettingsState.screenIndex(chosen:nil,current:1,in:["main","other"])==1,+ PointerSettingsState.screenIndex(chosen:"side",current:3,in:[])==0 else{+ throw LessonError.message("The chosen display wasn’t found after the displays changed.")+ }}// MARK: - Copy@@ -335,7 +363,7 @@ final class AirPointerHome:NSObject,NSWindowDelegate {private func handleEscape() {switch phase {- case .running,.connecting,.closedCase(.turnUpright),.closedCase(.holdStill):onControl?();return+ case .running,.connecting,.closedCase(.openLid),.closedCase(.turnUpright),.closedCase(.holdStill):onControl?();returndefault:break}if page == .settings{present(.pointer)}diff --git a/Sources/AirPointer/Interface/NotchFlow.swift b/Sources/AirPointer/Interface/NotchFlow.swiftindex 925afdb..492dd2f 100644--- a/Sources/AirPointer/Interface/NotchFlow.swift+++ b/Sources/AirPointer/Interface/NotchFlow.swift@@ -186,7 +186,7 @@ final class NotchSurface:NSView {super.init(frame:.zero);wantsLayer=true;layer?.mask=outlineart.translatesAutoresizingMaskIntoConstraints=true;art.fill=0.7title.alignment = .center;detail.alignment = .center- detail.maximumNumberOfLines=2;detail.cell?.truncatesLastVisibleLine=true+ detail.maximumNumberOfLines=4;detail.cell?.truncatesLastVisibleLine=truefor view in [art,title,detail,progress,dot] as [NSView]{contents.addSubview(view)}addSubview(contents);contents.alphaValue=0setAccessibilityElement(true);setAccessibilityRole(.group);setAccessibilityLabel("AirPointer")@@ -218,7 +218,7 @@ final class NotchSurface:NSView {contents.frame=NSRect(x:0,y:visibleBody-body,width:w,height:body)// Art, progress, title and line as one centered column, however many lines the line takes.let lines=detail.cell?.cellSize(forBounds:NSRect(x:0,y:0,width:textWidth,height:200)).height ?? 16- let detailHeight=min(34,ceil(lines)),bars:CGFloat=progress.isHidden ? 0:4+12+ let bars:CGFloat=progress.isHidden ? 0:4+12,detailHeight=min(body-side-6-bars-20-3,ceil(lines))let column=side+6+bars+20+3+detailHeightvar y=(body+column)/2art.frame=NSRect(x:(w-side)/2,y:y-side,width:side,height:side);y-=side+6@@ -299,6 +299,7 @@ extension App {}refreshHome();return}+ if showConnectEffectsFirst(){return}// Missing Accessibility, or a session already running, goes through the window.guard AXIsProcessTrusted(),!closedCaseOwnsInput else{showHome();homeControlAction();return}home?.setNotice(nil)@@ -312,7 +313,7 @@ extension App {switch stage {case .off:if session.isActive{notch.show(.finishing)}- else if was != .off || notch.content == .finishing{if let reason=session.endReason{notch.show(.ended(reason),for:4)}else{notch.collapse()}}+ else if was != .off || notch.content == .finishing{if let reason=session.endReason{notch.show(.ended(reason),for:max(4,Double(reason.count)/15))}else{notch.collapse()}}case .pointing:if was != .pointing{notch.show(.pointing,for:1.8)}case .ready:if was == .pointing{notch.show(.paused,for:1.6)}else{notch.collapse()}// The dots take over: a first calibration, or the case held another way than the saved edges.diff --git a/Sources/AirPointer/Interface/SetupAssistant.swift b/Sources/AirPointer/Interface/SetupAssistant.swiftindex 14ace74..50d119b 100644--- a/Sources/AirPointer/Interface/SetupAssistant.swift+++ b/Sources/AirPointer/Interface/SetupAssistant.swift@@ -309,6 +309,8 @@ final class SetupAssistant:NSObject,NSWindowDelegate {case .learnTap:onLearnTap?()case .next:go(to:step+1)case .finish:+ // The pointer can't move without Accessibility, so setup isn't done until it's allowed.+ guard state.accessibilityTrusted else{go(to:0);return}onFinish?()if window.isVisible{window.close()}}diff --git a/Sources/AirPointer/Session/AudioOutputGuard.swift b/Sources/AirPointer/Session/AudioOutputGuard.swiftindex 1a8cec7..e9f2603 100644--- a/Sources/AirPointer/Session/AudioOutputGuard.swift+++ b/Sources/AirPointer/Session/AudioOutputGuard.swift@@ -31,6 +31,11 @@ struct PointerAudioOutputPolicy{private var preferred:[ProtectedAudioRole:String]=[:]private var moved:[ProtectedAudioRole:(from:String,to:String)]=[:]init(targetAddress:String?){self.targetAddress=targetAddress}+ // Each moved role as [from,to], in a form that can be saved.+ var moves:[String:[String]]{+ get{Dictionary(uniqueKeysWithValues:moved.map{($0.key.rawValue,[$0.value.from,$0.value.to])})}+ set{moved=[:];for (name,move) in newValue{if let role=ProtectedAudioRole(rawValue:name),move.count==2{moved[role]=(move[0],move[1])}}}+ }private func compact(_ value:String)->String{value.lowercased().filter{$0.isLetter || $0.isNumber}}func isAirPods(_ output:PointerAudioDevice)->Bool{guard output.transport==kAudioDeviceTransportTypeBluetooth || output.transport==kAudioDeviceTransportTypeBluetoothLE else{return false}@@ -145,8 +150,10 @@ final class PointerSilentOutput{// Keeps the default playback, alert and input devices off the AirPods. An app recording from the default input// would otherwise put them in a hands-free call, and motion stops about 2 s later. No audio stream or Bluetooth reconnect.// Only a session holds it, from Connect until it ends, and then whatever it moved goes back.+// The moves outlive the app, so they're saved, and a force quit or crash is put right at the next launch.final class AudioOutputGuard{static let shared=AudioOutputGuard()+ private static let movesKey="audioMovesToRestore"// Long enough that a session restarting after a drop doesn't move sound back and forth.private static let releaseAfter=2.0private var inSession=false,sessionChanges=0@@ -187,17 +194,25 @@ final class AudioOutputGuard{self.enforce()}}func stop(){queue.sync{release()}}+ func restoreAfterCrash(){queue.async{[self] in+ guard listener==nil,let saved=UserDefaults.standard.dictionary(forKey:Self.movesKey) as? [String:[String]] else{return}+ policy.moves=saved;restoreMoves()+ }}private func release(){guard listener != nil else{return}if let listener{for selector in installed{var key=PointerAudioHardware.address(selector);_ = AudioObjectRemovePropertyListenerBlock(PointerAudioHardware.system,&key,queue,listener)}}installed=[];listener=nil+ restoreMoves()+ }+ private func restoreMoves(){for role in ProtectedAudioRole.allCases{let devices=PointerAudioHardware.devices(for:role)let id=PointerAudioHardware.current(role),current=devices.first{$0.id==id}if let back=policy.restoration(for:role,current:current,devices:devices){_ = PointerAudioHardware.select(back,for:role)}}+ UserDefaults.standard.removeObject(forKey:Self.movesKey)}private func enforce(){guard listener != nil else{return}@@ -205,6 +220,7 @@ final class AudioOutputGuard{let devices=PointerAudioHardware.devices(for:role)let id=PointerAudioHardware.current(role),current=devices.first{$0.id==id}guard let replacement=policy.replacement(for:role,current:current,devices:devices) else{continue}+ UserDefaults.standard.set(policy.moves,forKey:Self.movesKey)_=PointerAudioHardware.select(replacement,for:role)}}@@ -249,5 +265,16 @@ func runAudioOutputPolicyChecks(){_ = session.replacement(for:.input,current:podsMic,devices:[podsMic,macMic])selfCheck(session.restoration(for:.playback,current:speakers,devices:[pods,speakers])==nil)selfCheck(session.restoration(for:.input,current:macMic,devices:[podsMic,macMic])?.id==podsMic.id)- print("PASS: AirPods UID matching, renamed devices, speaker fallback, independent playback/alerts, preferred-output changes, reconnect IDs, the default input moved off the AirPods, and only what moved going back unless changed since")+ // After a force quit, a later launch knows only what was saved, and a saved move follows the same rules.+ _ = session.replacement(for:.playback,current:pods,devices:[pods,speakers])+ var relaunched=PointerAudioOutputPolicy(targetAddress:nil);relaunched.moves=session.moves+ selfCheck(relaunched.moves.keys.sorted()==["playback"] && relaunched.restoration(for:.playback,current:speakers,devices:[pods,speakers])?.id==pods.id)+ selfCheck(relaunched.moves.isEmpty)+ relaunched.moves=session.moves+ selfCheck(relaunched.restoration(for:.playback,current:usb,devices:[pods,speakers,usb])==nil,"Someone chose another device since")+ relaunched.moves=session.moves+ selfCheck(relaunched.restoration(for:.playback,current:speakers,devices:[speakers])==nil && relaunched.moves.isEmpty,"The AirPods have gone, so the move is forgotten")+ relaunched.moves=["playback":["only one"],"nothing":["a","b"]]+ selfCheck(relaunched.moves.isEmpty)+ print("PASS: AirPods UID matching, renamed devices, speaker fallback, independent playback/alerts, preferred-output changes, reconnect IDs, the default input moved off the AirPods, only what moved going back unless changed since, and saved moves after a force quit")}diff --git a/Sources/AirPointer/Session/ClosedCaseSession.swift b/Sources/AirPointer/Session/ClosedCaseSession.swiftindex 45c54eb..05ff257 100644--- a/Sources/AirPointer/Session/ClosedCaseSession.swift+++ b/Sources/AirPointer/Session/ClosedCaseSession.swift@@ -952,7 +952,7 @@ final class ClosedCaseSession: NSObject {let done=afterEnd;afterEnd=[];for action in done{action()}}func shutdown(){- stop();let deadline=Date().addingTimeInterval(7)+ userEnded=true;stop();let deadline=Date().addingTimeInterval(7)while helper?.isRunning == true && deadline.timeIntervalSinceNow>0{RunLoop.current.run(until:Date().addingTimeInterval(0.05))}if helper?.isRunning == true{helper?.terminate()}}