@@ -87,19 +87,27 @@ extension TextView {
87
87
return
88
88
}
89
89
90
+ // We receive global events because our view received the drag event, but we need to clamp the potentially
91
+ // out-of-bounds positions to a position our layout manager can deal with.
92
+ let locationInWindow = convert ( event. locationInWindow, from: nil )
93
+ let locationInView = CGPoint (
94
+ x: max ( 0.0 , min ( locationInWindow. x, frame. width) ) ,
95
+ y: max ( 0.0 , min ( locationInWindow. y, frame. height) )
96
+ )
97
+
90
98
if mouseDragAnchor == nil {
91
- mouseDragAnchor = convert ( event . locationInWindow , from : nil )
99
+ mouseDragAnchor = locationInView
92
100
super. mouseDragged ( with: event)
93
101
} else {
94
102
guard let mouseDragAnchor,
95
103
let startPosition = layoutManager. textOffsetAtPoint ( mouseDragAnchor) ,
96
- let endPosition = layoutManager. textOffsetAtPoint ( convert ( event . locationInWindow , from : nil ) ) else {
104
+ let endPosition = layoutManager. textOffsetAtPoint ( locationInView ) else {
97
105
return
98
106
}
99
107
100
108
let modifierFlags = event. modifierFlags. intersection ( . deviceIndependentFlagsMask)
101
109
if modifierFlags. contains ( . option) {
102
- dragColumnSelection ( mouseDragAnchor: mouseDragAnchor, event : event )
110
+ dragColumnSelection ( mouseDragAnchor: mouseDragAnchor, locationInView : locationInView )
103
111
} else {
104
112
dragSelection ( startPosition: startPosition, endPosition: endPosition, mouseDragAnchor: mouseDragAnchor)
105
113
}
@@ -197,9 +205,7 @@ extension TextView {
197
205
}
198
206
}
199
207
200
- private func dragColumnSelection( mouseDragAnchor: CGPoint , event: NSEvent ) {
201
- // Drag the selection and select in columns
202
- let eventLocation = convert ( event. locationInWindow, from: nil )
203
- selectColumns ( betweenPointA: eventLocation, pointB: mouseDragAnchor)
208
+ private func dragColumnSelection( mouseDragAnchor: CGPoint , locationInView: CGPoint ) {
209
+ selectColumns ( betweenPointA: mouseDragAnchor, pointB: locationInView)
204
210
}
205
211
}
0 commit comments