From e3a3ac5468022ea94331f1c0798161b6dcbf9dbd Mon Sep 17 00:00:00 2001 From: Emmanuel Pilande Date: Sun, 1 Nov 2026 14:54:59 -0800 Subject: [PATCH 1/2] fix(url): Hand focus back when a second link beats the launch activation On cold launch, a passive link that arrived after the launch link settled but before the launch activation landed replaced the deferred decision without taking over its claim to the activation, so Annotate stayed frontmost. The newer link's burst now carries the launch on, keeping the original deadline so later links can't stretch the window. --- Annotate/LinkFocusHandoff.swift | 24 +++++-- AnnotateTests/LinkFocusHandoffTests.swift | 77 +++++++++++++++++++++++ AnnotateTests/URLCommandTests.swift | 18 ++++++ 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/Annotate/LinkFocusHandoff.swift b/Annotate/LinkFocusHandoff.swift index facb054b..4c54533f 100644 --- a/Annotate/LinkFocusHandoff.swift +++ b/Annotate/LinkFocusHandoff.swift @@ -50,8 +50,12 @@ final class LinkFocusHandoff: NSObject { /// Links waiting for focus to be decided. private struct Burst { let start: Date - /// Whether any of the links came with the launch. + /// Whether any of the links came with the launch, or arrived while a launch decision + /// waited for the launch activation and took it over. var launched: Bool + /// The deadline of the launch decision these links took over. A launch activation after + /// it is left alone. Nil when nothing was taken over. + var launchDeadline: Date? /// The latest `linksDidRun` call. Only its scheduled settle decides. var latest: Int /// Asks whether an interactive overlay is open. The latest call's. @@ -73,7 +77,8 @@ final class LinkFocusHandoff: NSObject { private var burst: Burst? private var callCount = 0 /// A launch burst settled before the launch activation landed. When it lands, the burst - /// settles again, unless a newer link has replaced it. + /// settles again. A newer link replaces it and takes over its claim to the activation, so + /// focus is still decided after the last link. private var deferredLaunch: (keepsFocus: @MainActor () -> Bool, deadline: Date)? init( @@ -115,6 +120,8 @@ final class LinkFocusHandoff: NSObject { /// open. If not, and opening the links activated Annotate, the previous app gets focus back. /// A newer link always supersedes any decision still waiting. func linksDidRun(launched: Bool = false, keepsFocus: @escaping @MainActor () -> Bool) { + // A live burst means nothing is deferred, so only a new burst takes over the launch. + let takenOver = deferredLaunch.flatMap { now() <= $0.deadline ? $0.deadline : nil } deferredLaunch = nil if var burst { burst.launched = burst.launched || launched @@ -123,9 +130,11 @@ final class LinkFocusHandoff: NSObject { self.burst = burst } else { let start = now() + let launched = launched || takenOver != nil var burst = Burst( start: start, launched: launched, + launchDeadline: takenOver, latest: 0, keepsFocus: keepsFocus, activation: takeActivation(forBurstAt: start, launched: launched)) @@ -168,7 +177,8 @@ final class LinkFocusHandoff: NSObject { guard isActive() else { // The launch activation hasn't landed yet. Decide again once it does. if burst.launched { - deferredLaunch = (burst.keepsFocus, now().addingTimeInterval(Self.launchActivationWindow)) + let deadline = burst.launchDeadline ?? now().addingTimeInterval(Self.launchActivationWindow) + deferredLaunch = (burst.keepsFocus, deadline) } return } @@ -193,8 +203,11 @@ final class LinkFocusHandoff: NSObject { /// The workspace posts the other app's deactivation before AppKit posts this. @objc private func didBecomeActive() { let activation = Activation(previousApp: lastDeactivatedApp, date: now()) - // A launch burst still settling takes the launch activation, whenever it lands. - if var burst, burst.launched, !burst.resigned, burst.activation == nil { + // A launch burst still settling takes the launch activation, whenever it lands, unless + // it took over a launch decision whose window has passed. + if var burst, burst.launched, !burst.resigned, burst.activation == nil, + burst.launchDeadline.map({ activation.date <= $0 }) ?? true + { burst.activation = activation self.burst = burst return @@ -207,6 +220,7 @@ final class LinkFocusHandoff: NSObject { var burst = Burst( start: activation.date, launched: true, + launchDeadline: deferred.deadline, latest: 0, keepsFocus: deferred.keepsFocus, activation: activation) diff --git a/AnnotateTests/LinkFocusHandoffTests.swift b/AnnotateTests/LinkFocusHandoffTests.swift index d0130bd3..969c8cdc 100644 --- a/AnnotateTests/LinkFocusHandoffTests.swift +++ b/AnnotateTests/LinkFocusHandoffTests.swift @@ -327,6 +327,83 @@ final class LinkFocusHandoffTests: XCTestCase { XCTAssertTrue(activated.isEmpty) } + func testAPassiveLinkBeforeTheLaunchActivationStillHandsBack() { + // Regression: a second color link arrives after the launch link settled but before the + // launch activation lands. It replaced the deferred decision and never took the + // activation, so Annotate stayed frontmost. + handoff.noteLaunch(previousApp: previousApp) + isActive = false + deliver(leavingOverlayOpen: false, launched: true) + settle() + + scheduler.advance(by: 0.05) + deliver(leavingOverlayOpen: false) + settle() + XCTAssertTrue(activated.isEmpty, "Not active yet, so nothing to hand back") + + scheduler.advance(by: 0.3) + becomeActive() + XCTAssertTrue(activated.isEmpty, "Settles again, in case a link arrives with the activation") + settle() + + XCTAssertEqual(activated, [previousApp]) + XCTAssertFalse(handoff.isSettling) + } + + func testAPassiveLinkSettlingWhenTheLaunchActivationLandsHandsBack() { + // Same as above, but the activation lands while the second link is still settling. + handoff.noteLaunch(previousApp: previousApp) + isActive = false + deliver(leavingOverlayOpen: false, launched: true) + settle() + + scheduler.advance(by: 0.05) + deliver(leavingOverlayOpen: false) + scheduler.advance(by: 0.1) + becomeActive() + settle() + + XCTAssertEqual(activated, [previousApp]) + XCTAssertFalse(handoff.isSettling) + } + + func testPassiveLinksAfterLaunchDoNotExtendTheLaunchActivationWindow() { + // The window runs from when the launch links settled. A later link carries it over + // as is, so a launch activation after it is still left alone. + handoff.noteLaunch(previousApp: previousApp) + isActive = false + deliver(leavingOverlayOpen: false, launched: true) + settle() + + scheduler.advance(by: LinkFocusHandoff.launchActivationWindow - 0.25) + deliver(leavingOverlayOpen: false) + settle() + + scheduler.advance(by: 0.125) + becomeActive() + XCTAssertFalse(handoff.isSettling, "Past the window, so nothing settles again") + settle() + + XCTAssertTrue(activated.isEmpty) + } + + func testALaunchActivationPastTheWindowIsLeftAloneWhileALaterLinkSettles() { + // The later link is still settling when the activation lands, just past the window. + handoff.noteLaunch(previousApp: previousApp) + isActive = false + deliver(leavingOverlayOpen: false, launched: true) + settle() + + scheduler.advance(by: LinkFocusHandoff.launchActivationWindow - 0.125) + deliver(leavingOverlayOpen: false) + scheduler.advance(by: 0.1875) + becomeActive() + settle() + + XCTAssertTrue(activated.isEmpty) + XCTAssertFalse(handoff.isSettling) + } + func testALateLaunchActivationIsLeftAlone() { handoff.noteLaunch(previousApp: previousApp) isActive = false diff --git a/AnnotateTests/URLCommandTests.swift b/AnnotateTests/URLCommandTests.swift index 52e03340..e73469bf 100644 --- a/AnnotateTests/URLCommandTests.swift +++ b/AnnotateTests/URLCommandTests.swift @@ -422,6 +422,24 @@ final class URLCommandTests: XCTestCase { XCTAssertTrue(focus.activated().isEmpty) } + func testAColorLinkBeforeTheLaunchActivationStillHandsFocusBack() throws { + // Regression: a second color link arrives after the launch link settled but before the + // launch activation lands. + let focus = installLaunchFocusHarness() + try open("annotate-debug://color/green") + launchAndSettle(focus) + + try open("annotate-debug://color/red") + focus.settle() + XCTAssertTrue(focus.activated().isEmpty, "Not active yet, so nothing to hand back") + + focus.becomeActive() + focus.settle() + + XCTAssertEqual(appDelegate.currentColor, .systemRed) + XCTAssertEqual(focus.activated(), [NSRunningApplication.current]) + } + func testAColorLinkThatLaunchesTheAppHandsFocusBackWhenLaunchActivatedItFirst() throws { // What a live cold launch does: active before any delegate code, with no workspace // notification naming the previous app. From 2b9b0a29d59eee4e6f254fe41a1b5cc17d1cc0e4 Mon Sep 17 00:00:00 2001 From: Emmanuel Pilande Date: Sun, 1 Nov 2026 14:54:59 -0800 Subject: [PATCH 2/2] test(url): Let launch focus tests settle before asserting Three launch focus tests asserted right after the launch activation, before any settle it could start had run. They now check that nothing is settling, drive the manual scheduler through the settle, and then assert. --- AnnotateTests/LinkFocusHandoffTests.swift | 4 ++++ AnnotateTests/URLCommandTests.swift | 3 +++ 2 files changed, 7 insertions(+) diff --git a/AnnotateTests/LinkFocusHandoffTests.swift b/AnnotateTests/LinkFocusHandoffTests.swift index 969c8cdc..27be6b79 100644 --- a/AnnotateTests/LinkFocusHandoffTests.swift +++ b/AnnotateTests/LinkFocusHandoffTests.swift @@ -412,6 +412,8 @@ final class LinkFocusHandoffTests: XCTestCase { scheduler.advance(by: LinkFocusHandoff.launchActivationWindow + 0.1) becomeActive() + XCTAssertFalse(handoff.isSettling, "Past the window, so nothing settles again") + settle() XCTAssertTrue(activated.isEmpty) } @@ -434,6 +436,8 @@ final class LinkFocusHandoffTests: XCTestCase { deliver(leavingOverlayOpen: true, launched: true) settle() becomeActive() + XCTAssertFalse(handoff.isSettling, "The overlay kept focus, so nothing settles again") + settle() XCTAssertTrue(activated.isEmpty) } diff --git a/AnnotateTests/URLCommandTests.swift b/AnnotateTests/URLCommandTests.swift index e73469bf..6ead3b4f 100644 --- a/AnnotateTests/URLCommandTests.swift +++ b/AnnotateTests/URLCommandTests.swift @@ -456,6 +456,9 @@ final class URLCommandTests: XCTestCase { try open("annotate-debug://tool/arrow") launchAndSettle(focus) focus.becomeActive() + XCTAssertEqual( + appDelegate.linkFocusHandoff?.isSettling, false, "The overlay kept focus, so nothing settles again") + focus.settle() XCTAssertEqual(overlay?.isVisible, true) XCTAssertTrue(focus.activated().isEmpty)