@@ -87,19 +87,27 @@ extension TextView {
8787 return
8888 }
8989
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+
9098 if mouseDragAnchor == nil {
91- mouseDragAnchor = convert ( event . locationInWindow , from : nil )
99+ mouseDragAnchor = locationInView
92100 super. mouseDragged ( with: event)
93101 } else {
94102 guard let mouseDragAnchor,
95103 let startPosition = layoutManager. textOffsetAtPoint ( mouseDragAnchor) ,
96- let endPosition = layoutManager. textOffsetAtPoint ( convert ( event . locationInWindow , from : nil ) ) else {
104+ let endPosition = layoutManager. textOffsetAtPoint ( locationInView ) else {
97105 return
98106 }
99107
100108 let modifierFlags = event. modifierFlags. intersection ( . deviceIndependentFlagsMask)
101109 if modifierFlags. contains ( . option) {
102- dragColumnSelection ( mouseDragAnchor: mouseDragAnchor, event : event )
110+ dragColumnSelection ( mouseDragAnchor: mouseDragAnchor, locationInView : locationInView )
103111 } else {
104112 dragSelection ( startPosition: startPosition, endPosition: endPosition, mouseDragAnchor: mouseDragAnchor)
105113 }
@@ -197,9 +205,7 @@ extension TextView {
197205 }
198206 }
199207
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)
204210 }
205211}
0 commit comments