Files
museion/docs/devlog/20261205-publish-sheet-opens-unfocused_log.md
T
AlexKucera eca90a981c 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.
2026-12-05 14:59:16 +01:00

77 lines
8.1 KiB
Markdown

# Publish sheet opens unfocused — the Categories field stops taking focus
> **Date:** 2026-12-05
> **Type:** generic
> **Reference:** follow-up to #207 (unticketed; owner report 2026-12-05)
## Goal
Since #207 the Micro.blog publish sheet opens with its **Categories** field focused, and the native completion menu pops with the focus. The owner asked for the sheet to open without focusing that field.
## What Was Done
- Root-caused it as the launch auto-focus (r8) in a second window: a `.sheet` is its own `NSWindow`, and at its first become-key the focus system makes the sheet's FIRST text input first responder — which the Categories field now is (it sits ahead of the Summary editor and the token field). `textInputSuggestions` presents its list when the editing interface is activated, so the menu popped on open.
- Probed the fix candidates on macOS 27.2.1 before touching the app (`prototypes/MicropubSheetFocusProbe.swift`, new): a `WindowGroup` → `.sheet(isPresented:)` mock of the sheet's control order, a 4 ms sampler over the sheet's `firstResponder` and `NSApp.windows`, then a user-focus + second `didBecomeKey` phase.
- `plain`, `.defaultFocus(Bool, false)`, `.defaultFocus(Enum?, nil)`: Categories field editor first responder, `SwiftUISuggestionsWindow` visible from the first sample (seeded and unseeded field alike).
- the rider: never a field editor, never the menu; the re-key keeps the focus the "user" set.
- Reused the r8 rider instead of writing a new mechanism: `LaunchFocusReset` became `InitialFocusReset(scope:)` (`.oncePerProcess` / `.oncePerWindow`), moved from `PostsLibraryColumn.swift` to `WorkspaceChrome.swift` (two callers now, so it is shared chrome). The library column mounts `.oncePerProcess` exactly as before; the publish sheet mounts `.oncePerWindow` in its content's `.background`.
- Verified: lint clean on the four touched Swift files; rail run `20261205T114641Z-Museion` compiled all three changed app files (`SwiftCompile` lines at 12:46:45) and passed 814/814; owner live check in the built app 2026-12-05: approved.
## Decisions & Rationale
| Decision | Rationale |
|----------|-----------|
| Rider at the sheet's first `didBecomeKey`, not a SwiftUI focus API | The SwiftUI door is closed: `.defaultFocus(…, false)` and `(…, nil)` changed nothing in the probe, and an `onAppear` focus write lands BEFORE AppKit's assignment (the r8 finding). The rider is the mechanism this app has shipped since r8. |
| Generalize the existing rider (scope) rather than add a sibling | One mechanism, two call sites; a copy would have left two places to fix when the OS moves. Per-window flag is what a sheet needs (every presentation is a new window; a later re-key must not steal a focus the user set). |
| Neutral state (window is first responder), not "focus the Summary editor" | Nothing in the sheet is the thing the writer came to type; the launch posture (`makeFirstResponder(nil)`) already models "review, then click". |
| Keep the probe in `prototypes/` | Focus routing is an OS-version claim (the drag-routing precedent): re-run the bundle after every macOS point release. |
| Other ask sheets untouched | Rename / New Project / Move want typing — their auto-focus is the feature. |
## Gotchas & Fixes
| Problem | Root Cause | Fix |
|---------|------------|-----|
| `.defaultFocus($focused, false)` and `.defaultFocus($field, nil)` had no effect | On macOS 27.2.1 SwiftUI falls back to the first text input when no view matches the default-focus value | The rider; both non-fixes are recorded in the probe header so nobody retries them |
| Probe segfaulted at the first timer tick (`objc_opt_respondsToSelector` at 0x1) | A Swift `Bool` passed to `String(format:)` under `%@` — bridged as a non-object | Pass a string (`? "yes" : "no"`) |
| swiftformat + swiftlint disagreed on the sampler's `if` | The known wrapped-condition trap (#142/#179) | Hoisted the predicate (`summaryEditor(in:)`) |
| The rail's second run showed zero `SwiftCompile` lines | My first `start` was piped through `tail` and its run dir went unseen; the re-start found everything already built | Compile evidence read from the FIRST run's log (`20261205T114641Z-Museion`); second run 814/814 as well |
| Driving the real app: the terminal re-grabbed focus between calls | Hermes TUI is the frontmost app on this machine | The owner offered to drive — handed a four-step checklist instead of walking the AX tree |
## Files Changed
| File | Change Summary |
|------|---------------|
| `Museion/WorkspaceChrome.swift` | `InitialFocusReset(scope:)` — the r8 rider, moved here, with `.oncePerProcess` / `.oncePerWindow` |
| `Museion/PostsLibraryColumn.swift` | Mounts `InitialFocusReset(scope: .oncePerProcess)`; the private `LaunchFocusReset` and its stranded `import AppKit` removed |
| `Museion/MicropubPublishSheet.swift` | Mounts `InitialFocusReset(scope: .oncePerWindow)`; header note |
| `Museion/AGENTS.md` | DOX pass: the rider's new home and scope, the sheet's unfocused open |
| `prototypes/MicropubSheetFocusProbe.swift` | New OS-behaviour probe (four variants, 4 ms sampler, re-key phase) |
| `CHANGELOG.md` | `[Unreleased]` → Fixed entry |
| `AGENTS.md`, `docs/devlog/INDEX.md` | Learning bullet; this log's row + window |
## Verification
- Finish-audit gates (final tree): whole-repo `swiftformat --lint .` 0/162 and `swiftlint --strict .` 0 violations before dispatch; the four touched Swift files re-linted after the audit fixes (0/4, 0 violations); `full-test.sh` **814/814** (run `20261205T134019Z-Museion`, pid-matched — the three app files edited in the audit compiled in it, SwiftCompile lines at 14:40:25). Pre-audit baseline on the same shape: run `20261205T114641Z-Museion`, 814/814.
- Finish-up gates (the committed tree): `swiftformat --lint .` exit 0, 0/162 · `swiftlint --strict .` exit 0, 0 violations in 151 files · `full-test.sh` **814/814**, 0 failed (run `20261205T135748Z-Museion`, pid-matched; 814 → 814 — no new pins, the probe is the check). That run compiled nothing by design (the tree was already built): the sources' last edits (14:39) predate run `20261205T134019Z-Museion`, which holds the SwiftCompile lines.
- Probe (the change's only check — an OS-behaviour claim): all four variants, seeded and unseeded, re-run after the audit fix. plain / defaultFalse / defaultNil auto-focus the Categories field with the suggestions window visible; rider never focuses a field and shows no menu; the re-key keeps the user-set focus in every variant. The INVALID path exercised with a bare (unbundled) launch: the sheet never becomes key there, and the probe now exits 2 with `PROBE INVALID` instead of the green string.
- Owner live check in the built app, 2026-12-05: approved.
## Finish Audit
Three fresh-context reviewers (ponytail-review, security-review, review) over the working tree.
- Security: no findings — the diff's one runtime act is `makeFirstResponder(nil)`; the observer is registered once per view↔window attachment and removed on every window change.
- General, MEDIUM — accepted: the probe's green verdict could not tell "rider worked" from "sheet never became key". `finish()` now requires a key sample and a landed user-focus step, else `PROBE INVALID` + exit 2.
- General, LOW — accepted: the root learning's snippet lacked the `scope:` label (fixed, 192 chars); the probe header now says its two `@FocusState` bindings exist for the defaultFocus variants only.
- General, INFO — no action: the AGENTS.md window holds exactly 10 data rows (the reviewer's 12 counted the header and separator).
- Ponytail — accepted in part: the dead `updateNSView` scope write is gone (the scope is set once at creation); the sheet's header and mount comments were cut to pointers, so `InitialFocusReset`'s doc is the one home of the mechanism; the move-history clauses left the code comments (the DOX keeps them).
- Ponytail — declined: `let scope` + `RiderView(scope:)` — an init pair for a nested class only this representable constructs, +3 lines to remove a default no other caller can reach.
## Open Items & Next Steps
- [ ] Re-run `prototypes/MicropubSheetFocusProbe.swift` after every macOS point release.
---
*Log written by write-log skill*