From c2b185fa47f43d866b902beeee30da38a2017503 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Fri, 1 Aug 2025 15:40:56 +0200 Subject: Fix incorrect scroll bounds when soft wrap is disabled --- .../MapTextEditor/MapTextEditor.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'Map/Presentation/Base Components/MapTextEditor') diff --git a/Map/Presentation/Base Components/MapTextEditor/MapTextEditor.swift b/Map/Presentation/Base Components/MapTextEditor/MapTextEditor.swift index c6ab483..a1b357d 100644 --- a/Map/Presentation/Base Components/MapTextEditor/MapTextEditor.swift +++ b/Map/Presentation/Base Components/MapTextEditor/MapTextEditor.swift @@ -96,8 +96,18 @@ class MapTextEditorController: NSViewController { textView.textContainer?.widthTracksTextView = false textView.textContainer?.containerSize = NSSize( width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) + // Allow text view to grow horizontally + textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) + // Enable horizontal scrollbar when soft wrap is disabled + scrollView.hasHorizontalScroller = true + scrollView.horizontalScrollElasticity = .none } else { textView.textContainer?.widthTracksTextView = true + // Reset max size for wrapping + textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) + // Hide horizontal scrollbar when soft wrap is enabled + scrollView.hasHorizontalScroller = false + scrollView.horizontalScrollElasticity = .automatic } textView.isHorizontallyResizable = true @@ -169,6 +179,13 @@ class MapTextEditorController: NSViewController { } } + private func scrollToCursor() { + if let textView = currentTextView { + let cursorRange = textView.selectedRange() + textView.scrollRangeToVisible(cursorRange) + } + } + private func setupTrackingArea(for textView: NSTextView) { let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .mouseMoved, .activeInKeyWindow] trackingArea = NSTrackingArea( @@ -349,6 +366,9 @@ extension MapTextEditorController: NSTextViewDelegate { if let textField = obj.object as? NSTextView { self.text = textField.string + // Auto-scroll to keep cursor visible when typing + scrollToCursor() + changeDebouncer.debounce { DispatchQueue.main.async { self.onChange() -- cgit