Skip to content

fix(url): Hand focus back when a second link beats the launch activation - #139

Merged
epilande merged 2 commits into
mainfrom
fix/cold-launch-link-handback
Nov 2, 2026
Merged

epilande merged 2 commits into
mainfrom
fix/cold-launch-link-handback

Conversation

@epilande

@epilande epilande commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Follow-ups from the #137 review, both in the link focus handoff.

Cold launch with a second passive link

Say a color link launches Annotate. The launch link settles before the launch activation lands, so the handoff defers the decision until it does. If a second passive link (color, toolbar, fade) arrived in that gap, linksDidRun dropped the deferred decision and started a plain, non-launch burst. That burst never took the launch activation and had no launchPreviousApp to fall back to, so Annotate stayed frontmost.

Now a new burst that replaces a live deferred launch decision takes it over:

  • it counts as a launch burst, so it takes the launch activation whether that lands while it settles or after (then it re-defers and settles again, as before)
  • it keeps the original deadline (launchActivationWindow from when the launch links settled), so a chain of later links can't stretch the window, and an activation past it is left alone even mid-settle

Unchanged:

  • Burst settling. Links within 0.25 s still decide once, after the last one.
  • Newer overlay link wins. A tool link that replaces the deferred decision still keeps focus, because keepsFocus is asked at settle.
  • Reactivation during a settle. A resign still clears the burst's activation and marks it resigned.

Tests that asserted before the settle

testALateLaunchActivationIsLeftAlone, testLaunchLinksThatOpenTheOverlayKeepFocus, and URLCommandTests.testAToolLinkThatLaunchesTheAppKeepsFocus asserted right after the launch activation. A settle started by that activation wouldn't have run yet, so the "nothing handed back" checks passed no matter what. Each one now asserts isSettling is false, advances the ManualScheduler through settleDelay, and then asserts. No sleeps or run-loop waits. I fixed all three since they share the pattern.

Testing

New tests:

  • testAPassiveLinkBeforeTheLaunchActivationStillHandsBack (activation lands after the second link settled)
  • testAPassiveLinkSettlingWhenTheLaunchActivationLandsHandsBack (activation lands while it settles)
  • URLCommandTests.testAColorLinkBeforeTheLaunchActivationStillHandsFocusBack (same through application(_:open:))
  • testPassiveLinksAfterLaunchDoNotExtendTheLaunchActivationWindow
  • testALaunchActivationPastTheWindowIsLeftAloneWhileALaterLinkSettles

The first three fail on main (496f58c):

LinkFocusHandoffTests.swift:349: testAPassiveLinkBeforeTheLaunchActivationStillHandsBack : XCTAssertEqual failed: ("[]") is not equal to ("[<NSRunningApplication ...>]")
LinkFocusHandoffTests.swift:366: testAPassiveLinkSettlingWhenTheLaunchActivationLandsHandsBack : XCTAssertEqual failed: ("[]") is not equal to ("[<NSRunningApplication ...>]")
URLCommandTests.swift:440: testAColorLinkBeforeTheLaunchActivationStillHandsFocusBack : XCTAssertEqual failed: ("[]") is not equal to ("[<NSRunningApplication ...>]")

The two deadline tests pass on main, since main never carries the launch over. They guard the deadline handling: with the deadline checks removed from the fix, both fail.

  • swift test: 941 tests, 0 failures
  • SwiftLint: 0 violations

I didn't do a live cold-launch check. All of this is covered with the injected clock and scheduler.

Summary by CodeRabbit

  • Bug Fixes
    • Improved launch-link handling so follow-up links respect the original activation window.
    • Prevented late activations from triggering an outdated focus handoff.
    • Kept focus on the active overlay when links arrive during launch settling.

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.
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.
@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e44120c9-eb96-4213-9484-84fd7d445bb4
Commits

Reviewing files that changed from the base of the PR and between 7172c76 and 2b9b0a2.

Files selected for processing: 3
  • Annotate/LinkFocusHandoff.swift
  • AnnotateTests/LinkFocusHandoffTests.swift
  • AnnotateTests/URLCommandTests.swift

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

The handoff now carries an unexpired launch-activation deadline when a later link takes over a deferred launch decision. Activation after that deadline does not restore focus. Tests cover links and activations that arrive before, during, and after the deadline.

Changes: Launch Handoff

Layer / File(s) Summary
Carry the deferred launch deadline
Annotate/LinkFocusHandoff.swift
A later link takes over a deferred launch decision only if its deadline has not passed. The burst carries that deadline forward when it settles.
Enforce the deadline during activation
Annotate/LinkFocusHandoff.swift, AnnotateTests/LinkFocusHandoffTests.swift, AnnotateTests/URLCommandTests.swift
Activation is captured only by the carried deadline, and a burst recreated from deferred activation retains the deadline. Tests cover handoff timing, color-link behavior, and settling state.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

Merge Risk: Minimal · up to 2b9b0

No merge-blocking issue is established for the launch-focus deadline change. Merge after normal checks pass.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a second passive link that arrives while a launch activation is pending now takes over the launch decision and hands focus back. The raw summary and objectives describ…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: a second passive link that arrives while a launch activation is pending now takes over the launch decision and hands focus back. The raw summary and objectives describe this same handoff change in LinkFocusHandoff.swift, with matching tests. The title uses the conventional fix(url) prefix and is specific, so it passes.

Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

epilande commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

Live verification

At 2b9b0a2 on macOS 27.2 (Mac mini), with a Debug build under its own bundle ID (com.epilande.Annotate.pr139) so no real settings were touched.

Build and tests

  • Clean Debug build passes.
  • 941 tests, 938 passed, 3 skipped (the two-display tests), 0 failures. The five new focus tests pass.

Focus handoff (a small test app frontmost before each run, frontmost app polled every ~50 ms)

  • Cold launch, two passive links back to back: 5/5 pass, focus returns about 0.8 s after the first link. Also 6/6 with the second link sent 0.3, 0.6, and 1.0 s later.
  • Cold launch, one color link: 5/5 pass.
  • Burst of 4 to 5 passive links while running: 5/5 pass, focus returns once with no bounce.
  • Overlay links (tool/arrow, show): 5/5 warm and 3/3 cold keep Annotate in front.
  • Switching away and back during the settle: 5/5 pass, Annotate stays in front.

Notes

  • The race did not come up live. The same cold double-link runs against the base (7172c76) also handed focus back, 9/9. On this machine the launch activation lands before the first link settles, so the new path is covered by the unit tests rather than a live repro.
  • A color link followed right away by open -b for Annotate, without switching away first, hands focus back (3/3). That matches the existing design.

Not tested

  • Two displays, open -g, and URL commands off.

@epilande
epilande merged commit 4bdc389 into main Nov 2, 2026
2 checks passed
@epilande
epilande deleted the fix/cold-launch-link-handback branch November 2, 2026 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant