Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Sources/CodeEditTextView/STTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -342,3 +342,30 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
highlighter = nil
}
}

class CEScrollView: NSScrollView {
Comment thread
thecoolwinter marked this conversation as resolved.
Outdated

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..<string.endIndex, in: string)
Comment thread
thecoolwinter marked this conversation as resolved.
Outdated
if let newRange = NSTextRange(range, provider: provider) {
_ = textView.becomeFirstResponder()
textView.setSelectedRange(newRange)
return
}
}

super.mouseDown(with: event)
}
}