forked from CodeEditApp/CodeEditTextView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextView+Mouse.swift
More file actions
241 lines (211 loc) · 8.73 KB
/
TextView+Mouse.swift
File metadata and controls
241 lines (211 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// TextView+Mouse.swift
// CodeEditTextView
//
// Created by Khan Winter on 9/19/23.
//
import AppKit
extension TextView {
override public func mouseDown(with event: NSEvent) {
// Set cursor
guard isSelectable,
event.type == .leftMouseDown,
let offset = layoutManager.textOffsetAtPoint(self.convert(event.locationInWindow, from: nil)),
let content = layoutManager.contentRun(at: offset) else {
super.mouseDown(with: event)
return
}
if case let .attachment(attachment) = content.data, event.clickCount < 3 {
handleAttachmentClick(event: event, offset: offset, attachment: attachment)
return
}
switch event.clickCount {
case 1:
handleSingleClick(event: event, offset: offset)
case 2:
handleDoubleClick(event: event)
case 3:
handleTripleClick(event: event)
default:
break
}
setUpMouseAutoscrollTimer()
}
/// Single click, if control-shift we add a cursor
/// if shift, we extend the selection to the click location
/// else we set the cursor
fileprivate func handleSingleClick(event: NSEvent, offset: Int) {
cursorSelectionMode = .character
guard isEditable else {
super.mouseDown(with: event)
return
}
let eventFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
if eventFlags == [.control, .shift] {
unmarkText()
selectionManager.addSelectedRange(NSRange(location: offset, length: 0))
} else if eventFlags.contains(.shift) {
unmarkText()
shiftClickExtendSelection(to: offset)
} else {
selectionManager.setSelectedRange(NSRange(location: offset, length: 0))
unmarkTextIfNeeded()
}
}
fileprivate func handleDoubleClick(event: NSEvent) {
cursorSelectionMode = .word
guard !event.modifierFlags.contains(.shift) else {
super.mouseDown(with: event)
return
}
unmarkText()
selectWord(nil)
}
fileprivate func handleTripleClick(event: NSEvent) {
cursorSelectionMode = .line
guard !event.modifierFlags.contains(.shift) else {
super.mouseDown(with: event)
return
}
unmarkText()
selectLine(nil)
}
fileprivate func handleAttachmentClick(event: NSEvent, offset: Int, attachment: AnyTextAttachment) {
switch event.clickCount {
case 1:
selectionManager.setSelectedRange(attachment.range)
case 2:
performAttachmentAction(attachment: attachment)
default:
break
}
}
func performAttachmentAction(attachment: AnyTextAttachment) {
let action = attachment.attachment.attachmentAction()
switch action {
case .none:
return
case .discard:
layoutManager.attachments.remove(atOffset: attachment.range.location)
selectionManager.setSelectedRange(NSRange(location: attachment.range.location, length: 0))
case let .replace(text):
replaceCharacters(in: attachment.range, with: text)
}
}
override public func mouseUp(with event: NSEvent) {
mouseDragAnchor = nil
disableMouseAutoscrollTimer()
super.mouseUp(with: event)
}
override public func mouseDragged(with event: NSEvent) {
guard !(inputContext?.handleEvent(event) ?? false) && isSelectable && !isDragging else {
return
}
// We receive global events because our view received the drag event, but we need to clamp the potentially
// out-of-bounds positions to a position our layout manager can deal with.
let locationInWindow = convert(event.locationInWindow, from: nil)
let locationInView = CGPoint(
x: max(0.0, min(locationInWindow.x, frame.width)),
y: max(0.0, min(locationInWindow.y, frame.height))
)
if mouseDragAnchor == nil {
mouseDragAnchor = locationInView
super.mouseDragged(with: event)
} else {
guard let mouseDragAnchor,
let startPosition = layoutManager.textOffsetAtPoint(mouseDragAnchor),
let endPosition = layoutManager.textOffsetAtPoint(locationInView) else {
return
}
let modifierFlags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
if modifierFlags.contains(.option) {
dragColumnSelection(mouseDragAnchor: mouseDragAnchor, locationInView: locationInView)
} else {
dragSelection(startPosition: startPosition, endPosition: endPosition, mouseDragAnchor: mouseDragAnchor)
}
setNeedsDisplay()
self.autoscroll(with: event)
}
}
/// Extends the current selection to the offset. Only used when the user shift-clicks a location in the document.
///
/// If the offset is within the selection, trims the selection from the nearest edge (start or end) towards the
/// clicked offset.
/// Otherwise, extends the selection to the clicked offset.
///
/// - Parameter offset: The offset clicked on.
fileprivate func shiftClickExtendSelection(to offset: Int) {
// Use the last added selection, this is behavior copied from Xcode.
guard var selectedRange = selectionManager.textSelections.last?.range else { return }
if selectedRange.contains(offset) {
if offset - selectedRange.location <= selectedRange.max - offset {
selectedRange.length -= offset - selectedRange.location
selectedRange.location = offset
} else {
selectedRange.length -= selectedRange.max - offset
}
} else {
selectedRange.formUnion(NSRange(
start: min(offset, selectedRange.location),
end: max(offset, selectedRange.max)
))
}
selectionManager.setSelectedRange(selectedRange)
setNeedsDisplay()
}
// MARK: - Mouse Autoscroll
/// Sets up a timer that fires at a predetermined period to autoscroll the text view.
/// Ensure the timer is disabled using ``disableMouseAutoscrollTimer``.
func setUpMouseAutoscrollTimer() {
mouseDragTimer?.invalidate()
// https://cocoadev.github.io/AutoScrolling/ (fired at ~45Hz)
mouseDragTimer = Timer.scheduledTimer(withTimeInterval: 0.022, repeats: true) { [weak self] _ in
if let event = self?.window?.currentEvent, event.type == .leftMouseDragged {
self?.mouseDragged(with: event)
self?.autoscroll(with: event)
}
}
}
/// Disables the mouse drag timer started by ``setUpMouseAutoscrollTimer``
func disableMouseAutoscrollTimer() {
mouseDragTimer?.invalidate()
mouseDragTimer = nil
}
// MARK: - Drag Selection
private func dragSelection(startPosition: Int, endPosition: Int, mouseDragAnchor: CGPoint) {
switch cursorSelectionMode {
case .character:
selectionManager.setSelectedRange(
NSRange(
location: min(startPosition, endPosition),
length: max(startPosition, endPosition) - min(startPosition, endPosition)
)
)
case .word:
let startWordRange = findWordBoundary(at: startPosition)
let endWordRange = findWordBoundary(at: endPosition)
selectionManager.setSelectedRange(
NSRange(
location: min(startWordRange.location, endWordRange.location),
length: max(startWordRange.location + startWordRange.length,
endWordRange.location + endWordRange.length) -
min(startWordRange.location, endWordRange.location)
)
)
case .line:
let startLineRange = findLineBoundary(at: startPosition)
let endLineRange = findLineBoundary(at: endPosition)
selectionManager.setSelectedRange(
NSRange(
location: min(startLineRange.location, endLineRange.location),
length: max(startLineRange.location + startLineRange.length,
endLineRange.location + endLineRange.length) -
min(startLineRange.location, endLineRange.location)
)
)
}
}
private func dragColumnSelection(mouseDragAnchor: CGPoint, locationInView: CGPoint) {
selectColumns(betweenPointA: mouseDragAnchor, pointB: locationInView)
}
}