Skip to content

Commit 22b4ab7

Browse files
committed
Moved CEScrollView to separate file and used documentRange.endLocation to simplify textView range calculation (#56)
1 parent 910848e commit 22b4ab7

2 files changed

Lines changed: 28 additions & 27 deletions

File tree

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: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -342,30 +342,3 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
342342
highlighter = nil
343343
}
344344
}
345-
346-
class CEScrollView: NSScrollView {
347-
348-
override func mouseDown(with event: NSEvent) {
349-
350-
if let textView = self.documentView as? STTextView,
351-
!textView.visibleRect.contains(event.locationInWindow) {
352-
// If the `scrollView` was clicked, but the click did not happen within the `textView`,
353-
// set cursor to the last index of the `textView`.
354-
355-
guard let provider = textView.textLayoutManager.textContentManager else {
356-
return
357-
}
358-
359-
let string = textView.string
360-
361-
let range = NSRange(string.endIndex..<string.endIndex, in: string)
362-
if let newRange = NSTextRange(range, provider: provider) {
363-
_ = textView.becomeFirstResponder()
364-
textView.setSelectedRange(newRange)
365-
return
366-
}
367-
}
368-
369-
super.mouseDown(with: event)
370-
}
371-
}

0 commit comments

Comments
 (0)