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)) }