fix(microblog): publish sheet opens unfocused

Since the categories field landed, the Micro.blog publish sheet
opened with that field focused and its completion menu popping. A
sheet window takes its initial first responder like any window — its
first text input — and textInputSuggestions shows its list when the
editing interface is activated, so the menu popped the moment the
sheet opened.

The sheet now rides the launch-focus-reset rider from r8, once per
window: it resigns the auto-focus at the sheet's first become-key,
and a later re-key keeps whatever the writer focused. The rider is
InitialFocusReset(scope:) in the shared chrome; the library column
mounts the once-per-process scope exactly as before. The SwiftUI door
is closed: defaultFocus with false or nil changes nothing for this
assignment on macOS 27.2.1.

Verified with a bundle-launched OS-behaviour probe kept under
prototypes/ (re-run it after every macOS point release): the three
non-fix variants reproduce the auto-focus and the suggestions
window, the rider variant never shows either, and a run whose sheet
never became key exits 2 instead of printing the green verdict.
Owner live check passed.

Reviewed (three-reviewer finish-audit): the probe's false-green
verdict, a snippet missing its scope label, and duplicated mechanism
comments were found and fixed before this commit. Rail 814/814;
SwiftFormat and SwiftLint clean.

Docs: Museion/AGENTS.md bullets, an AGENTS.md learning, CHANGELOG
[Unreleased], the devlog, and the swift wiki (a sheet hands its
initial focus to its first text input).

Unticketed follow-up to the Micro.blog categories work; owner
report 2026-12-05.
This commit is contained in:
AlexKucera committed 2026-12-05 14:59:16 +01:00
1 parent 36393a9e8e
commit eca90a981c
9 files changed
+438 -64

No files matched your search

+71 -1
View File
@@ -5,7 +5,9 @@
// #193: the shared workspace chrome, moved from WorkspaceView.swift
// (file-length ceiling) — SelectedRowStrip, BorderedSecondaryButtonStyle,
// PaneHeader; every pane reuses them, code unchanged. #205 adds
// FinderHandOffButtons, #206 MicroblogHandOffButton (both born here).
// FinderHandOffButtons, #206 MicroblogHandOffButton (both born here), and
// InitialFocusReset (the r8 launch-focus rider — #207 follow-up: once per
// window for the publish sheet too).
//
import AppKit
@@ -136,3 +138,71 @@ struct MicroblogHandOffButton: View {
}
}
}
/// r8 (argon#6, mercury#12 — the launch auto-focus ROOT CAUSE, killed at the source): AppKit assigns a
/// window's INITIAL first responder to its first text input when the window becomes key — AFTER onAppear,
/// so a FocusState write can't undo it (FocusState merely mirrors the assignment back as true: terracotta
/// ring + caret, the strongest element on screen). This invisible rider, at the window's FIRST become-key,
/// resigns focus back to the window itself (`makeFirstResponder(nil)` — the neutral state; nothing gains
/// focus instead). The second, next-runloop resign covers an assignment that lands after the notification
/// (the PostEditorView layout-race precedent). #207 follow-up: a SHEET window takes the focus the same way
/// (the publish sheet's Categories field — its completion menu popped on open; `.defaultFocus(…, false)`
/// changes nothing on macOS 27.2.1, `prototypes/MicropubSheetFocusProbe.swift`), so the rider carries a
/// scope — the library column's launch reset and the sheet's, two callers.
struct InitialFocusReset: NSViewRepresentable {
/// `.oncePerProcess` — the launch: a pane-switch remount must never steal a focus the user set on
/// return. `.oncePerWindow` — a sheet: every presentation is a new window, and its later re-keys keep
/// the user's focus.
enum Scope {
case oncePerProcess
case oncePerWindow
}
private static var processHasRun = false
let scope: Scope
final class RiderView: NSView {
var scope: Scope = .oncePerProcess
private var windowHasRun = false
private var keyObserver: NSObjectProtocol?
override func viewDidMoveToWindow() {
if let keyObserver {
NotificationCenter.default.removeObserver(keyObserver)
}
keyObserver = nil
guard let window else { return }
keyObserver = NotificationCenter.default.addObserver(
forName: NSWindow.didBecomeKeyNotification,
object: window,
queue: .main,
) { _ in
MainActor.assumeIsolated {
// The observer is scoped to `window` (object:), so the
// notification carries nothing we don't already hold —
// reading `note` here would cross a Sendable boundary.
switch self.scope {
case .oncePerProcess:
guard !InitialFocusReset.processHasRun else { return }
InitialFocusReset.processHasRun = true
case .oncePerWindow:
guard !self.windowHasRun else { return }
self.windowHasRun = true
}
window.makeFirstResponder(nil)
DispatchQueue.main.async { window.makeFirstResponder(nil) }
}
}
}
}
func makeNSView(context _: Context) -> RiderView {
// The scope is set once at creation: it is a `let` on the representable and never changes
// for the view's lifetime.
let view = RiderView()
view.scope = scope
return view
}
func updateNSView(_: RiderView, context _: Context) {}
}