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

+6 -59
View File
@@ -4,11 +4,9 @@
//
// #193: the Posts library column, moved from PostsPaneView.swift
// (file-length ceiling) — PostsLibraryColumn with its rename/move
// sheets, the file-scope librarySearch mount (#144), and
// LaunchFocusReset (r8), code unchanged.
// sheets and the file-scope librarySearch mount (#144). The r8 focus
// rider is InitialFocusReset (WorkspaceChrome.swift).
//
import AppKit
import SwiftUI
/// #21: the library column, shared by the unfocused layout and the focus-mode
@@ -36,8 +34,8 @@ struct PostsLibraryColumn: View {
/// onAppear-unfocus could never work — the launch focus is AppKit
/// assigning the window's INITIAL first responder to its first text
/// input (this field), at window-become-key, AFTER onAppear. FocusState
/// merely mirrors that back as true. LaunchFocusReset (below) resigns
/// it at the first become-key; from then on only the user drives this.
/// merely mirrors that back as true. InitialFocusReset (WorkspaceChrome)
/// resigns it at the first become-key; from then on only the user drives this.
@FocusState private var searchFocused: Bool
private var trimmedQuery: String {
@@ -156,8 +154,8 @@ struct PostsLibraryColumn: View {
}
.frame(width: DesignTokens.libraryWidth)
// r8 (argon#6, mercury#12): the focus-rider replaces the r6/r7
// onAppear-unfocus — see LaunchFocusReset below for the mechanism.
.background(LaunchFocusReset())
// onAppear-unfocus — see InitialFocusReset (WorkspaceChrome) for the mechanism.
.background(InitialFocusReset(scope: .oncePerProcess))
.sheet(item: $renamingPost) { post in
RenamePostSheet(post: post) { title in
model.rename(post, to: title)
@@ -384,57 +382,6 @@ private nonisolated func librarySearch(
}
}
/// r8 (argon#6, mercury#12 — the launch auto-focus ROOT CAUSE, killed at
/// the source): the search field is the window's first — and at launch,
/// only — text input, and AppKit assigns the window's initial first
/// responder to it when the window becomes key. That happens AFTER
/// onAppear, so the r6/r7 `searchFocused = false` writes were always
/// overridden (FocusState mirrors the assignment back as true: terracotta
/// ring + caret, the strongest element on screen). This invisible rider
/// rides the library column and, at the window's FIRST become-key,
/// resigns focus back to the window itself (`makeFirstResponder(nil)` —
/// the neutral state; nothing gains focus instead). ONCE per process: a
/// pane-switch remount must never steal a focus the user set on return.
/// The second, next-runloop resign covers an assignment that lands after
/// the notification (the PostEditorView layout-race precedent).
private struct LaunchFocusReset: NSViewRepresentable {
/// Process-wide once-flag — launch is a once-per-process event.
private static var hasRun = false
final class RiderView: NSView {
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.
guard !LaunchFocusReset.hasRun else { return }
LaunchFocusReset.hasRun = true
window.makeFirstResponder(nil)
DispatchQueue.main.async { window.makeFirstResponder(nil) }
}
}
}
}
func makeNSView(context _: Context) -> RiderView {
RiderView()
}
func updateNSView(_: RiderView, context _: Context) {}
}
/// #108: Move to Project ask — radio list of the non-archived projects,
/// default = the piece's current project. Matches RenamePostSheet's shape.
struct MoveToProjectSheet: View {