diff --git a/Sources/CodeEditTextView/CEScrollView.swift b/Sources/CodeEditTextView/CEScrollView.swift new file mode 100644 index 000000000..6e8dcc0c7 --- /dev/null +++ b/Sources/CodeEditTextView/CEScrollView.swift @@ -0,0 +1,28 @@ +// +// CEScrollView.swift +// +// +// Created by Renan Greca on 18/02/23. +// + +import AppKit +import STTextView + +class CEScrollView: NSScrollView { + + override func mouseDown(with event: NSEvent) { + + if let textView = self.documentView as? STTextView, + !textView.visibleRect.contains(event.locationInWindow) { + // If the `scrollView` was clicked, but the click did not happen within the `textView`, + // set cursor to the last index of the `textView`. + + let endLocation = textView.textLayoutManager.documentRange.endLocation + let range = NSTextRange(location: endLocation) + _ = textView.becomeFirstResponder() + textView.setSelectedRange(range) + } + + super.mouseDown(with: event) + } +} diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 92a149952..4f58eda8b 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -107,7 +107,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt public override func loadView() { textView = STTextView() - let scrollView = NSScrollView() + let scrollView = CEScrollView() scrollView.translatesAutoresizingMaskIntoConstraints = false scrollView.hasVerticalScroller = true scrollView.documentView = textView