@@ -38,6 +38,13 @@ public class TextSelectionManager: NSObject {
38
38
}
39
39
}
40
40
41
+ /// Determines how far inset to draw selection content.
42
+ public var edgeInsets : HorizontalEdgeInsets = . zero {
43
+ didSet {
44
+ delegate? . setNeedsDisplay ( )
45
+ }
46
+ }
47
+
41
48
internal( set) public var textSelections : [ TextSelection ] = [ ]
42
49
weak var layoutManager : TextLayoutManager ?
43
50
weak var textStorage : NSTextStorage ?
@@ -224,87 +231,4 @@ public class TextSelectionManager: NSObject {
224
231
textSelection. view? . removeFromSuperview ( )
225
232
}
226
233
}
227
-
228
- // MARK: - Draw
229
-
230
- /// Draws line backgrounds and selection rects for each selection in the given rect.
231
- /// - Parameter rect: The rect to draw in.
232
- func drawSelections( in rect: NSRect ) {
233
- guard let context = NSGraphicsContext . current? . cgContext else { return }
234
- context. saveGState ( )
235
- var highlightedLines : Set < UUID > = [ ]
236
- // For each selection in the rect
237
- for textSelection in textSelections {
238
- if textSelection. range. isEmpty {
239
- drawHighlightedLine (
240
- in: rect,
241
- for: textSelection,
242
- context: context,
243
- highlightedLines: & highlightedLines
244
- )
245
- } else {
246
- drawSelectedRange ( in: rect, for: textSelection, context: context)
247
- }
248
- }
249
- context. restoreGState ( )
250
- }
251
-
252
- /// Draws a highlighted line in the given rect.
253
- /// - Parameters:
254
- /// - rect: The rect to draw in.
255
- /// - textSelection: The selection to draw.
256
- /// - context: The context to draw in.
257
- /// - highlightedLines: The set of all lines that have already been highlighted, used to avoid highlighting lines
258
- /// twice and updated if this function comes across a new line id.
259
- private func drawHighlightedLine(
260
- in rect: NSRect ,
261
- for textSelection: TextSelection ,
262
- context: CGContext ,
263
- highlightedLines: inout Set < UUID >
264
- ) {
265
- guard let linePosition = layoutManager? . textLineForOffset ( textSelection. range. location) ,
266
- !highlightedLines. contains ( linePosition. data. id) else {
267
- return
268
- }
269
- highlightedLines. insert ( linePosition. data. id)
270
- context. saveGState ( )
271
- let selectionRect = CGRect (
272
- x: rect. minX,
273
- y: linePosition. yPos,
274
- width: rect. width,
275
- height: linePosition. height
276
- )
277
- if selectionRect. intersects ( rect) {
278
- context. setFillColor ( selectedLineBackgroundColor. cgColor)
279
- context. fill ( selectionRect)
280
- }
281
- context. restoreGState ( )
282
- }
283
-
284
- /// Draws a selected range in the given context.
285
- /// - Parameters:
286
- /// - rect: The rect to draw in.
287
- /// - range: The range to highlight.
288
- /// - context: The context to draw in.
289
- private func drawSelectedRange( in rect: NSRect , for textSelection: TextSelection , context: CGContext ) {
290
- context. saveGState ( )
291
-
292
- let fillColor = ( textView? . isFirstResponder ?? false )
293
- ? selectionBackgroundColor. cgColor
294
- : selectionBackgroundColor. grayscale. cgColor
295
-
296
- context. setFillColor ( fillColor)
297
-
298
- let fillRects = getFillRects ( in: rect, for: textSelection)
299
-
300
- let minX = fillRects. min ( by: { $0. origin. x < $1. origin. x } ) ? . origin. x ?? 0
301
- let minY = fillRects. min ( by: { $0. origin. y < $1. origin. y } ) ? . origin. y ?? 0
302
- let max = fillRects. max ( by: { $0. maxY < $1. maxY } ) ?? . zero
303
- let origin = CGPoint ( x: minX, y: minY)
304
- let size = CGSize ( width: max. maxX - minX, height: max. maxY - minY)
305
- textSelection. boundingRect = CGRect ( origin: origin, size: size)
306
-
307
- context. fill ( fillRects)
308
- context. restoreGState ( )
309
- }
310
234
}
0 commit comments