From 1a5f04c9f43bbe9e8811934be2f3973a9446a8f8 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Thu, 8 Oct 2026 10:44:26 +0200 Subject: [PATCH] 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] --- Museion/PostEditorView.swift | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Museion/PostEditorView.swift b/Museion/PostEditorView.swift index b7d4909..1bfb041 100644 --- a/Museion/PostEditorView.swift +++ b/Museion/PostEditorView.swift @@ -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)) }