From 910848e75f1a96d3044b8bef11f1a17838da3ca3 Mon Sep 17 00:00:00 2001 From: Renan Greca Date: Fri, 17 Feb 2023 14:17:47 +0100 Subject: [PATCH 1/3] Make cursor appear when clicking after the last line of a file (#56) --- .../STTextViewController.swift | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 92a149952..193684e86 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 @@ -342,3 +342,30 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt highlighter = nil } } + +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`. + + guard let provider = textView.textLayoutManager.textContentManager else { + return + } + + let string = textView.string + + let range = NSRange(string.endIndex.. Date: Sat, 18 Feb 2023 09:46:27 +0100 Subject: [PATCH 2/3] Moved CEScrollView to separate file and used documentRange.endLocation to simplify textView range calculation (#56) --- Sources/CodeEditTextView/CEScrollView.swift | 28 +++++++++++++++++++ .../STTextViewController.swift | 27 ------------------ 2 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 Sources/CodeEditTextView/CEScrollView.swift diff --git a/Sources/CodeEditTextView/CEScrollView.swift b/Sources/CodeEditTextView/CEScrollView.swift new file mode 100644 index 000000000..daa2a1a2e --- /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 193684e86..4f58eda8b 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -342,30 +342,3 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt highlighter = nil } } - -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`. - - guard let provider = textView.textLayoutManager.textContentManager else { - return - } - - let string = textView.string - - let range = NSRange(string.endIndex.. Date: Sat, 18 Feb 2023 09:50:36 +0100 Subject: [PATCH 3/3] Removed trailing whitespace from one line --- Sources/CodeEditTextView/CEScrollView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CodeEditTextView/CEScrollView.swift b/Sources/CodeEditTextView/CEScrollView.swift index daa2a1a2e..6e8dcc0c7 100644 --- a/Sources/CodeEditTextView/CEScrollView.swift +++ b/Sources/CodeEditTextView/CEScrollView.swift @@ -16,7 +16,7 @@ class CEScrollView: NSScrollView { !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()