Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Annotate/LinkFocusHandoff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
81 changes: 81 additions & 0 deletions AnnotateTests/LinkFocusHandoffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -335,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)
}
Expand All @@ -357,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)
}
Expand Down
21 changes: 21 additions & 0 deletions AnnotateTests/URLCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -438,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)
Expand Down
Loading