Spike B: NSTextView/TextKit2 with custom attributes #53

Closed
opened 2026-09-26 11:38:56 +02:00 by AlexKucera · 3 comments
Owner

Part of #16
Blocked by: 51

Question

What does NSTextView/TextKit2 with custom attributes actually cost, and what does it buy? Throwaway SwiftUI spike on spike/editor-presentation in spike/textkit2/, editing the calibration piece.

  • Markdown presentation via custom attributes — dimmed syntax, heading scale, paragraph spacing, centered measure. (Native macOS: no third-party dependency.)
  • Reading toggle: cheap is fine; note the renderer choice for the ADR.
  • Editing feel: IME, undo, selection — TextKit2's own story, not SwiftUI's.
  • Performance: load and edit PERF-3k.md, note any hitching (layout invalidation on attribute runs is the known risk).
  • No anchor proof required here — it lives in the CodeEdit spike; note feasibility of quote-anchor range highlighting in a sentence.
  • Resolution comment: the verdict criteria as a checklist with findings; screenshots linked as assets.

[Generated by AI]

Part of #16 Blocked by: 51 ## Question What does NSTextView/TextKit2 with custom attributes actually cost, and what does it buy? Throwaway SwiftUI spike on `spike/editor-presentation` in `spike/textkit2/`, editing the calibration piece. - Markdown presentation via custom attributes — dimmed syntax, heading scale, paragraph spacing, centered measure. (Native macOS: no third-party dependency.) - Reading toggle: cheap is fine; note the renderer choice for the ADR. - Editing feel: IME, undo, selection — TextKit2's own story, not SwiftUI's. - Performance: load and edit `PERF-3k.md`, note any hitching (layout invalidation on attribute runs is the known risk). - No anchor proof required here — it lives in the CodeEdit spike; note feasibility of quote-anchor range highlighting in a sentence. - Resolution comment: the verdict criteria as a checklist with findings; screenshots linked as assets. [Generated by AI]
AlexKucera added the wayfinder:prototype label 2026-09-26 11:38:59 +02:00
AlexKucera added a new dependency 2026-09-26 11:39:02 +02:00
AlexKucera added a new dependency 2026-09-26 11:39:03 +02:00
Author
Owner

Corpus is pinned (#51): spike/prose/ on spike/editor-presentation @ 3b3544e. Note: the calibration piece has no blockquote/italics in the source — its epigraph is plain prose in curly quotes. Full inventory + word counts in the #51 resolution.

[Generated by AI]

Corpus is pinned (#51): `spike/prose/` on `spike/editor-presentation` @ 3b3544e. Note: the calibration piece has no blockquote/italics in the source — its epigraph is plain prose in curly quotes. Full inventory + word counts in the [#51 resolution](https://git.babylondreams.de/AlexKucera/Museion/issues/51#issuecomment-22188). [Generated by AI]
AlexKucera self-assigned this 2026-09-30 12:06:50 +02:00
Author
Owner

Spike B resolution — NSTextView/TextKit2 with custom attributes

Code: spike/textkit2/ on spike/editor-presentation (dd3542a). Run: spike/textkit2/run.sh (wraps the SPM binary in a .app — bare SwiftPM executables never materialize SwiftUI windows).

Verdict criteria checklist

Markdown presentation via custom attributes — PASS.

  • Serif body (Iowan Old Style 17pt), H1/H2/H3 scale 26/21/18pt bold, paragraph spacing, quote marks + inline syntax (##, **) dimmed gray monospace, fixed 640pt measure (~70 chars). Screenshot: spike/textkit2/assets/spikeB-calibration.png.
  • Styling is a flat [NSAttributedString.Key: Any] mapping applied per line kind — no layout manager surgery, no third-party dependency.

Reading toggle — PASS (with one renderer caveat).

  • Toggle swaps NSTextView for SwiftUI Text(AttributedString(markdown:)). Screenshot: spike/textkit2/assets/spikeB-reading-final.png.
  • Caveat for the ADR: used inlineOnlyPreservingWhitespace, so block markers (##) stay literal in reading mode. Switching the parse to .full made the app launch with four 30px broken windows — reproducible, reverted. Whatever renderer the real app picks needs a proper harness; the spike's cheap conclusion is only "a separate read-only view is trivial, keep rendering cheap."

Editing feel — PASS.

  • Live per-paragraph re-styling while typing: typed ## … became a bold heading instantly (screenshot: assets/spikeB-typed2.png). Undo after in-textDidChange re-attribution is clean — ⌘Z removed the typed line with no garbling (assets/spikeB-undo.png). IME/selection native because it is NSTextView.

Performance on PERF-3k (3,452 words) — PASS.

  • Full-doc attribute() load: 0.0–1.1 ms (readout in toolbar).
  • Anchor (quote search over whole doc): 2.4 ms (from --check).
  • Live typing at doc end: no visible hitches, no layout distress (assets/spikeB-perf-typed.png). Per-paragraph reattribute keeps invalidation local — the feared full-document attribute-run re-layout didn't materialize at this corpus size.

Anchor feasibility — one sentence as asked: quoting-match anchors are trivially feasible in TextKit2 — normalize whitespace, tokenize, and scrollRangeToVisible the found NSRange; the full proof lives in Spike A.

Stale anchor UX — PASS. Deleted-text quote → nil → alert with the exact web-proto wording ("Anchor not found / The Post changed after this Fix Card was issued; no text was selected or changed."), screenshot assets/spikeB-stale2.png.

Findings worth carrying into the verdict

  1. The strongest card in TextKit2's hand: editing is free. Undo, IME, selection, find-bar, accessibility — all native. Every presentation nicety is "just attributes."
  2. The attribute model is line-kind-based, not AST-based. Syntax stays visible but dimmed (a deliberate posture, matches the prose-first stance) — if the verdict wants rendered syntax hidden, that's the same mapping minus the dimmed runs, so no architecture risk.
  3. Known flaws (fine for a spike): measure is left-anchored, not centered; first line can sit tight under the toolbar divider.
  4. macOS state restoration can resurrect broken 30px window frames across relaunches — nuke ~/Library/Saved Application State/de.babylondreams.museion.spike-b* if the window is a stub.
## Spike B resolution — NSTextView/TextKit2 with custom attributes Code: `spike/textkit2/` on `spike/editor-presentation` (dd3542a). Run: `spike/textkit2/run.sh` (wraps the SPM binary in a `.app` — bare SwiftPM executables never materialize SwiftUI windows). ### Verdict criteria checklist **Markdown presentation via custom attributes — PASS.** - Serif body (Iowan Old Style 17pt), H1/H2/H3 scale 26/21/18pt bold, paragraph spacing, quote marks + inline syntax (`##`, `**`) dimmed gray monospace, fixed 640pt measure (~70 chars). Screenshot: `spike/textkit2/assets/spikeB-calibration.png`. - Styling is a flat `[NSAttributedString.Key: Any]` mapping applied per line kind — no layout manager surgery, no third-party dependency. **Reading toggle — PASS (with one renderer caveat).** - Toggle swaps NSTextView for SwiftUI `Text(AttributedString(markdown:))`. Screenshot: `spike/textkit2/assets/spikeB-reading-final.png`. - Caveat for the ADR: used `inlineOnlyPreservingWhitespace`, so block markers (`##`) stay literal in reading mode. Switching the parse to `.full` made the **app launch with four 30px broken windows** — reproducible, reverted. Whatever renderer the real app picks needs a proper harness; the spike's cheap conclusion is only "a separate read-only view is trivial, keep rendering cheap." **Editing feel — PASS.** - Live per-paragraph re-styling while typing: typed `## …` became a bold heading instantly (screenshot: `assets/spikeB-typed2.png`). Undo after in-`textDidChange` re-attribution is clean — ⌘Z removed the typed line with no garbling (`assets/spikeB-undo.png`). IME/selection native because it *is* NSTextView. **Performance on PERF-3k (3,452 words) — PASS.** - Full-doc `attribute()` load: **0.0–1.1 ms** (readout in toolbar). - Anchor (quote search over whole doc): **2.4 ms** (from `--check`). - Live typing at doc end: no visible hitches, no layout distress (`assets/spikeB-perf-typed.png`). Per-paragraph reattribute keeps invalidation local — the feared full-document attribute-run re-layout didn't materialize at this corpus size. **Anchor feasibility — one sentence as asked:** quoting-match anchors are trivially feasible in TextKit2 — normalize whitespace, tokenize, and `scrollRangeToVisible` the found NSRange; the full proof lives in Spike A. **Stale anchor UX — PASS.** Deleted-text quote → nil → alert with the exact web-proto wording ("Anchor not found / The Post changed after this Fix Card was issued; no text was selected or changed."), screenshot `assets/spikeB-stale2.png`. ### Findings worth carrying into the verdict 1. **The strongest card in TextKit2's hand: editing is free.** Undo, IME, selection, find-bar, accessibility — all native. Every presentation nicety is "just attributes." 2. **The attribute model is line-kind-based, not AST-based.** Syntax stays visible but dimmed (a deliberate posture, matches the prose-first stance) — if the verdict wants rendered syntax *hidden*, that's the same mapping minus the dimmed runs, so no architecture risk. 3. Known flaws (fine for a spike): measure is left-anchored, not centered; first line can sit tight under the toolbar divider. 4. macOS state restoration can resurrect broken 30px window frames across relaunches — nuke `~/Library/Saved Application State/de.babylondreams.museion.spike-b*` if the window is a stub.
Author
Owner

Correction to the Reading-toggle caveat above: the ".full breaks window creation" claim was an AX misread (System Events reported 0 windows while the window was visibly on screen — the user confirmed). Retracted.

What was actually wrong with the single-Text .full approach: it flattens block structure (headings render body-size, lines run together) and leaves presentation intents unstyled. The follow-up (d836083) renders reading mode per block: blank-line split, strip # prefixes for heading level, inline-markdown-parse each paragraph, font headings bold serif by level. Markup now fully hidden; --check asserts no # leaks into the reading view. Screenshot refreshed: spike/textkit2/assets/spikeB-reading-final.png.

This is the renderer stance the ADR should carry: block-aware rendering, not a monolithic markdown dump.

**Correction to the Reading-toggle caveat above:** the "`.full` breaks window creation" claim was an AX misread (System Events reported 0 windows while the window was visibly on screen — the user confirmed). Retracted. What was actually wrong with the single-`Text` `.full` approach: it flattens block structure (headings render body-size, lines run together) and leaves presentation intents unstyled. The follow-up (d836083) renders reading mode **per block**: blank-line split, strip `#` prefixes for heading level, inline-markdown-parse each paragraph, font headings bold serif by level. Markup now fully hidden; `--check` asserts no `#` leaks into the reading view. Screenshot refreshed: `spike/textkit2/assets/spikeB-reading-final.png`. This is the renderer stance the ADR should carry: **block-aware rendering, not a monolithic markdown dump.**
Sign in to join this conversation.