Skip to content

Commit e7a082a

Browse files
authored
Make cursor appear when clicking after the last line of a file (#56) (#145)
1 parent f4f2fcd commit e7a082a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// CEScrollView.swift
3+
//
4+
//
5+
// Created by Renan Greca on 18/02/23.
6+
//
7+
8+
import AppKit
9+
import STTextView
10+
11+
class CEScrollView: NSScrollView {
12+
13+
override func mouseDown(with event: NSEvent) {
14+
15+
if let textView = self.documentView as? STTextView,
16+
!textView.visibleRect.contains(event.locationInWindow) {
17+
// If the `scrollView` was clicked, but the click did not happen within the `textView`,
18+
// set cursor to the last index of the `textView`.
19+
20+
let endLocation = textView.textLayoutManager.documentRange.endLocation
21+
let range = NSTextRange(location: endLocation)
22+
_ = textView.becomeFirstResponder()
23+
textView.setSelectedRange(range)
24+
}
25+
26+
super.mouseDown(with: event)
27+
}
28+
}

Sources/CodeEditTextView/STTextViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
107107
public override func loadView() {
108108
textView = STTextView()
109109

110-
let scrollView = NSScrollView()
110+
let scrollView = CEScrollView()
111111
scrollView.translatesAutoresizingMaskIntoConstraints = false
112112
scrollView.hasVerticalScroller = true
113113
scrollView.documentView = textView

0 commit comments

Comments
 (0)