@@ -9,10 +9,8 @@ import Foundation
99
1010extension TextSelectionManager {
1111 /// Calculate a set of rects for a text selection suitable for filling with the selection color to indicate a
12- /// multi-line selection.
13- ///
14- /// The returned rects are inset by edge insets passed to the text view, the given `rect` parameter can be the 'raw'
15- /// rect to draw in, no need to inset it before this method call.
12+ /// multi-line selection. The returned rects surround all selected line fragments for the given selection,
13+ /// following the available text layout space, rather than the available selection layout space.
1614 ///
1715 /// - Parameters:
1816 /// - rect: The bounding rect of available draw space.
@@ -26,16 +24,27 @@ extension TextSelectionManager {
2624
2725 var fillRects : [ CGRect ] = [ ]
2826
29- let insetXPos = max ( layoutManager. edgeInsets. left, rect. minX)
30- let insetWidth = max ( 0 , rect. maxX - insetXPos - layoutManager. edgeInsets. right)
31- let insetRect = NSRect ( x: insetXPos, y: rect. origin. y, width: insetWidth, height: rect. height)
27+ let textWidth = if layoutManager. maxLineLayoutWidth == . greatestFiniteMagnitude {
28+ layoutManager. maxLineWidth
29+ } else {
30+ layoutManager. maxLineLayoutWidth
31+ }
32+ let maxWidth = max ( textWidth, layoutManager. wrapLinesWidth)
33+ let validTextDrawingRect = CGRect (
34+ x: layoutManager. edgeInsets. left,
35+ y: rect. minY,
36+ width: maxWidth,
37+ height: rect. height
38+ ) . intersection ( rect)
3239
3340 for linePosition in layoutManager. lineStorage. linesInRange ( range) {
34- fillRects. append ( contentsOf: getFillRects ( in: insetRect, selectionRange: range, forPosition: linePosition) )
41+ fillRects. append (
42+ contentsOf: getFillRects ( in: validTextDrawingRect, selectionRange: range, forPosition: linePosition)
43+ )
3544 }
3645
3746 // Pixel align these to avoid aliasing on the edges of each rect that should be a solid box.
38- return fillRects. map { $0. pixelAligned }
47+ return fillRects. map { $0. intersection ( validTextDrawingRect ) . pixelAligned }
3948 }
4049
4150 /// Find fill rects for a specific line position.
0 commit comments