@@ -81,19 +81,21 @@ public class TextSelectionManager: NSObject {
8181 internal( set) public var textSelections : [ TextSelection ] = [ ]
8282 weak var layoutManager : TextLayoutManager ?
8383 weak var textStorage : NSTextStorage ?
84- weak var layoutView : NSView ?
84+ weak var textView : TextView ?
8585 weak var delegate : TextSelectionManagerDelegate ?
86+ var cursorTimer : CursorTimer
8687
8788 init (
8889 layoutManager: TextLayoutManager ,
8990 textStorage: NSTextStorage ,
90- layoutView : NSView ? ,
91+ textView : TextView ? ,
9192 delegate: TextSelectionManagerDelegate ?
9293 ) {
9394 self . layoutManager = layoutManager
9495 self . textStorage = textStorage
95- self . layoutView = layoutView
96+ self . textView = textView
9697 self . delegate = delegate
98+ self . cursorTimer = CursorTimer ( )
9799 super. init ( )
98100 textSelections = [ ]
99101 updateSelectionViews ( )
@@ -106,8 +108,10 @@ public class TextSelectionManager: NSObject {
106108 let selection = TextSelection ( range: range)
107109 selection. suggestedXPos = layoutManager? . rectForOffset ( range. location) ? . minX
108110 textSelections = [ selection]
109- updateSelectionViews ( )
110- NotificationCenter . default. post ( Notification ( name: Self . selectionChangedNotification, object: self ) )
111+ if textView? . isFirstResponder ?? false {
112+ updateSelectionViews ( )
113+ NotificationCenter . default. post ( Notification ( name: Self . selectionChangedNotification, object: self ) )
114+ }
111115 }
112116
113117 public func setSelectedRanges( _ ranges: [ NSRange ] ) {
@@ -123,8 +127,10 @@ public class TextSelectionManager: NSObject {
123127 selection. suggestedXPos = layoutManager? . rectForOffset ( $0. location) ? . minX
124128 return selection
125129 }
126- updateSelectionViews ( )
127- NotificationCenter . default. post ( Notification ( name: Self . selectionChangedNotification, object: self ) )
130+ if textView? . isFirstResponder ?? false {
131+ updateSelectionViews ( )
132+ NotificationCenter . default. post ( Notification ( name: Self . selectionChangedNotification, object: self ) )
133+ }
128134 }
129135
130136 public func addSelectedRange( _ range: NSRange ) {
@@ -146,12 +152,16 @@ public class TextSelectionManager: NSObject {
146152 textSelections. append ( newTextSelection)
147153 }
148154
149- updateSelectionViews ( )
150- NotificationCenter . default. post ( Notification ( name: Self . selectionChangedNotification, object: self ) )
155+ if textView? . isFirstResponder ?? false {
156+ updateSelectionViews ( )
157+ NotificationCenter . default. post ( Notification ( name: Self . selectionChangedNotification, object: self ) )
158+ }
151159 }
152160
153161 // MARK: - Selection Views
154162
163+ /// Update all selection cursors. Placing them in the correct position for each text selection and reseting the
164+ /// blink timer.
155165 func updateSelectionViews( ) {
156166 var didUpdate : Bool = false
157167
@@ -163,12 +173,16 @@ public class TextSelectionManager: NSObject {
163173 || textSelection. boundingRect. height != layoutManager? . estimateLineHeight ( ) ?? 0 {
164174 textSelection. view? . removeFromSuperview ( )
165175 textSelection. view = nil
176+
166177 let cursorView = CursorView ( color: insertionPointColor)
167178 cursorView. frame. origin = cursorOrigin
168179 cursorView. frame. size. height = layoutManager? . estimateLineHeight ( ) ?? 0
169- layoutView ? . addSubview ( cursorView)
180+ textView ? . addSubview ( cursorView)
170181 textSelection. view = cursorView
171182 textSelection. boundingRect = cursorView. frame
183+
184+ cursorTimer. register ( cursorView)
185+
172186 didUpdate = true
173187 }
174188 } else if !textSelection. range. isEmpty && textSelection. view != nil {
@@ -180,10 +194,13 @@ public class TextSelectionManager: NSObject {
180194
181195 if didUpdate {
182196 delegate? . setNeedsDisplay ( )
197+ cursorTimer. resetTimer ( )
183198 }
184199 }
185200
201+ /// Removes all cursor views and stops the cursor blink timer.
186202 func removeCursors( ) {
203+ cursorTimer. stopTimer ( )
187204 for textSelection in textSelections {
188205 textSelection. view? . removeFromSuperview ( )
189206 }
0 commit comments