fix(editor): hop the runloop before caret scrolls land

Click-to-edit and open-with-cursor-restore both scrolled a freshly
mounted NSTextView synchronously. TextKit lays out asynchronously, so
scrollRangeToVisible clamped to the laid-out height - on long posts
that is near the top, which read as "click always lands at top".
Born with the #71 toggle, masked in devlogs by short verification
posts where top equals the clicked paragraph; reproduced offscreen
against a 3000-para doc (scroll y=69076 for a y=70000 target), one
runloop hop lands dead-on.

Both scroll sites (hint branch + cursor restore) now defer scroll and
first-responder to the next runloop tick. 93 tests green.

[Generated by AI]
This commit is contained in:
AlexKucera committed 2026-10-08 10:44:26 +02:00
1 parent c7a6a82283
commit 1a5f04c9f4
1 file changed
+16 -5
+16 -5
View File
@@ -303,7 +303,7 @@ struct ProseTextView: NSViewRepresentable {
// per keystroke: the text view owns the live text.
if context.coordinator.loadedGeneration != session.loadGeneration {
context.coordinator.loadedGeneration = session.loadGeneration
// Seed from session.text (LIVE), not session.loadText (the
// Seed from session.text (LIVE), not session.loadText (the
// open-time snapshot): the representable is recreated on the
// reading⇄editing swap, and seeding from the snapshot showed
// pre-edit text — then session.edit on the stale view would
@@ -315,8 +315,13 @@ struct ProseTextView: NSViewRepresentable {
cursor.selectionLocation = min(max(0, cursor.selectionLocation), length)
cursor.selectionLength = min(max(0, cursor.selectionLength), length - cursor.selectionLocation)
tv.setSelectedRange(NSRange(location: cursor.selectionLocation, length: cursor.selectionLength))
tv.scroll(NSPoint(x: 0, y: max(0, cursor.scrollOffset)))
tv.window?.makeFirstResponder(tv)
// ponytail: TextKit lays out async — a sync scroll on a fresh mount
// lands short of the target (clamps to what's laid out). One runloop
// hop lets layout finish; verified against a 3000-para doc offscreen.
DispatchQueue.main.async { [weak tv] in
tv?.scroll(NSPoint(x: 0, y: max(0, cursor.scrollOffset)))
tv?.window?.makeFirstResponder(tv)
}
session.cursorUpdate(cursor)
}
} else if let draft, tv.string != draft {
@@ -330,8 +335,14 @@ struct ProseTextView: NSViewRepresentable {
let length = (tv.string as NSString).length
let loc = min(max(0, hint.location), length)
tv.setSelectedRange(NSRange(location: loc, length: 0))
tv.scrollRangeToVisible(NSRange(location: loc, length: 0))
tv.window?.makeFirstResponder(tv)
// ponytail: same layout race as the cursor-restore scroll above —
// this was the click-to-edit "always lands at top" defect: the sync
// scrollRangeToVisible fired before the target region was laid out.
// One runloop hop fixes it (offscreen-verified, 3000-para doc).
DispatchQueue.main.async { [weak tv] in
tv?.scrollRangeToVisible(NSRange(location: loc, length: 0))
tv?.window?.makeFirstResponder(tv)
}
if let session {
session.cursorUpdate(ProseTextView.cursorState(of: tv))
}