fix(editor): typewriter centering works at document end, editor-wide (#88, #21)

The bottom clamp parked the scroll and no-oped every keystroke when
typing at the document end — the natural writing position. Padding the
text container by half the viewport (typewriter breathing room) lets the
clamp center the last lines. Calibration ruling: typewriter centering
and breathing room are editor-wide, not focus-gated.
This commit is contained in:
AlexKucera committed 2026-10-09 20:24:51 +02:00
1 parent 695ad5e5f9
commit 34aa88b020
2 files changed
+35 -13

No files matched your search

+20
View File
@@ -164,11 +164,31 @@ nonisolated enum CaretCentering {
return clamped return clamped
} }
/// #21 calibration defect: typing at the document END (the natural
/// writing position) parked at the bottom clamp and no-oped — the caret
/// rode the lower edge forever. Breathing room: pad the document by
/// half the viewport so the clamp can center the last lines (and the
/// first — the inset is symmetric). ponytail: window resizes between
/// updateNSView passes re-sync on the next keystroke via the guard.
/// Calibration amendment (2026-10-09): typewriter is EDITOR-WIDE, not
/// focus-gated — Alexander's call at the #88 gate; revert = restore the
/// focused guard at the call sites.
@MainActor
static func syncTypewriterInset(textView: NSTextView) {
guard let clip = textView.enclosingScrollView?.contentView else { return }
let target = clip.bounds.height / 2
guard textView.textContainerInset.height != target else { return }
textView.textContainerInset = NSSize(width: 0, height: target)
// Re-clamp the origin to the resized document (short-doc floor).
textView.scrollRangeToVisible(textView.selectedRange())
}
/// Applied: animate the scroll so the caret settles back toward the /// Applied: animate the scroll so the caret settles back toward the
/// center. Typing only — never route scroll or selection events here. /// center. Typing only — never route scroll or selection events here.
@MainActor @MainActor
static func centerCaretWhileTyping(textView: NSTextView) { static func centerCaretWhileTyping(textView: NSTextView) {
guard let window = textView.window else { return } guard let window = textView.window else { return }
syncTypewriterInset(textView: textView)
let sel = textView.selectedRange() let sel = textView.selectedRange()
guard sel.length == 0, sel.location >= 0 else { return } guard sel.length == 0, sel.location >= 0 else { return }
// firstRect(forCharacterRange:) answers in SCREEN coordinates. // firstRect(forCharacterRange:) answers in SCREEN coordinates.
+15 -13
View File
@@ -226,7 +226,9 @@ struct PostEditorView: View {
} }
} }
.padding(.horizontal, 16) .padding(.horizontal, 16)
.frame(minHeight: 40) // #90: align with the Posts header and mentor header at the
// export's 52px pane-head height.
.frame(minHeight: 52)
} }
private func saveNow() { private func saveNow() {
@@ -336,9 +338,11 @@ struct ProseTextView: NSViewRepresentable {
// #77: squiggles follow the posture toggle (default off). // #77: squiggles follow the posture toggle (default off).
tv.isContinuousSpellCheckingEnabled = spellChecking tv.isContinuousSpellCheckingEnabled = spellChecking
tv.usesFindBar = true tv.usesFindBar = true
// #21: focus ground joins the workspace background (NSColor(Color) // #21/#89: page ground — surface (cream page) unfocused, chrome in
// bridges; white in the unfocused card). // focus. One editing color + one focus color, nothing hardcoded.
tv.backgroundColor = focused ? NSColor(DesignTokens.background) : NSColor.white tv.backgroundColor = focused ? NSColor(DesignTokens.background) : NSColor(DesignTokens.surface)
// #89: accent caret — findable, matches the app's rust.
tv.insertionPointColor = NSColor(DesignTokens.accent)
tv.typingAttributes = ProseTypography.bodyAttributes tv.typingAttributes = ProseTypography.bodyAttributes
tv.delegate = context.coordinator tv.delegate = context.coordinator
tv.layoutManager?.allowsNonContiguousLayout = true tv.layoutManager?.allowsNonContiguousLayout = true
@@ -361,7 +365,7 @@ struct ProseTextView: NSViewRepresentable {
// out at rest (iOS-like), instead of always occupying the right edge. // out at rest (iOS-like), instead of always occupying the right edge.
scroll.scrollerStyle = .overlay scroll.scrollerStyle = .overlay
scroll.drawsBackground = true scroll.drawsBackground = true
scroll.backgroundColor = focused ? NSColor(DesignTokens.background) : NSColor.white scroll.backgroundColor = focused ? NSColor(DesignTokens.background) : NSColor(DesignTokens.surface)
context.coordinator.textView = tv context.coordinator.textView = tv
context.coordinator.sessionRef = session // strong: both die with the editor context.coordinator.sessionRef = session // strong: both die with the editor
return scroll return scroll
@@ -372,10 +376,12 @@ struct ProseTextView: NSViewRepresentable {
// #77: posture toggle while editing — cheap idempotent set. // #77: posture toggle while editing — cheap idempotent set.
tv.isContinuousSpellCheckingEnabled = spellChecking tv.isContinuousSpellCheckingEnabled = spellChecking
// #21: the ground follows focus in/out without a remount. // #21: the ground follows focus in/out without a remount.
let ground = focused ? NSColor(DesignTokens.background) : NSColor.white let ground = focused ? NSColor(DesignTokens.background) : NSColor(DesignTokens.surface)
tv.backgroundColor = ground tv.backgroundColor = ground
scroll.backgroundColor = ground scroll.backgroundColor = ground
context.coordinator.focused = focused // #21 calibration amendment: typewriter inset is editor-wide now —
// every editing surface gets the breathing room, focus or not.
CaretCentering.syncTypewriterInset(textView: tv)
if let session { if let session {
context.coordinator.sessionRef = session context.coordinator.sessionRef = session
// Load only when the generation moved (open, Yes-reload) — never // Load only when the generation moved (open, Yes-reload) — never
@@ -439,9 +445,6 @@ struct ProseTextView: NSViewRepresentable {
var sessionRef: EditorSession? var sessionRef: EditorSession?
var loadedGeneration = -1 var loadedGeneration = -1
var appliedCaretToken = -1 var appliedCaretToken = -1
/// #21: focus posture mirror — soft centering applies while typing
/// in focus only.
var focused = false
/// #77: fence delimiter count — the cross-paragraph fence state /// #77: fence delimiter count — the cross-paragraph fence state
/// fingerprint. A moved count means a ``` was typed/deleted and every /// fingerprint. A moved count means a ``` was typed/deleted and every
/// line until the matching delimiter changed kind. /// line until the matching delimiter changed kind.
@@ -469,10 +472,9 @@ struct ProseTextView: NSViewRepresentable {
} }
tv.typingAttributes = ProseTypography.bodyAttributes tv.typingAttributes = ProseTypography.bodyAttributes
// #21: soft caret centering — typing only, never scrollback or // #21: soft caret centering — typing only, never scrollback or
// drag-selection (the reader never gets fought). // drag-selection (the reader never gets fought). Calibration
if focused { // amendment: editor-wide, no longer focus-gated.
CaretCentering.centerCaretWhileTyping(textView: tv) CaretCentering.centerCaretWhileTyping(textView: tv)
}
if let sessionRef { if let sessionRef {
sessionRef.edit(storage.string) sessionRef.edit(storage.string)
} else { } else {