@@ -37,7 +37,7 @@ public class MinimapView: FlippedNSView {
3737 public let separatorView : NSView
3838
3939 /// Responder for a drag gesture on the ``documentVisibleView``.
40- var documentVisibleViewDragGesture : NSPanGestureRecognizer ?
40+ var documentVisibleViewPanGesture : NSPanGestureRecognizer ?
4141
4242 /// The layout manager that uses the ``lineRenderer`` to render and layout lines.
4343 var layoutManager : TextLayoutManager ?
@@ -98,19 +98,36 @@ public class MinimapView: FlippedNSView {
9898
9999 super. init ( frame: . zero)
100100
101- let documentVisibleViewDragGesture = NSPanGestureRecognizer (
102- target: self ,
103- action: #selector( documentVisibleViewDragged ( _: ) )
104- )
105- documentVisibleView. addGestureRecognizer ( documentVisibleViewDragGesture)
106- self . documentVisibleViewDragGesture = documentVisibleViewDragGesture
101+ setUpPanGesture ( )
107102
108103 addSubview ( scrollView)
109104 addSubview ( documentVisibleView)
110105 addSubview ( separatorView)
111106 scrollView. documentView = contentView
112107
113- self . translatesAutoresizingMaskIntoConstraints = false
108+ translatesAutoresizingMaskIntoConstraints = false
109+ wantsLayer = true
110+ layer? . backgroundColor = theme. background. cgColor
111+
112+ setUpLayoutManager ( textView: textView)
113+ setUpSelectionManager ( textView: textView)
114+
115+ setUpConstraints ( )
116+ setUpListeners ( )
117+ }
118+
119+ /// Creates a pan gesture and attaches it to the ``documentVisibleView``.
120+ private func setUpPanGesture( ) {
121+ let documentVisibleViewPanGesture = NSPanGestureRecognizer (
122+ target: self ,
123+ action: #selector( documentVisibleViewDragged ( _: ) )
124+ )
125+ documentVisibleView. addGestureRecognizer ( documentVisibleViewPanGesture)
126+ self . documentVisibleViewPanGesture = documentVisibleViewPanGesture
127+ }
128+
129+ /// Create the layout manager, using text contents from the given textview.
130+ private func setUpLayoutManager( textView: TextView ) {
114131 let layoutManager = TextLayoutManager (
115132 textStorage: textView. textStorage,
116133 lineHeightMultiplier: 1.0 ,
@@ -121,7 +138,15 @@ public class MinimapView: FlippedNSView {
121138 )
122139 self . layoutManager = layoutManager
123140 ( textView. textStorage. delegate as? MultiStorageDelegate ) ? . addDelegate ( layoutManager)
141+ }
124142
143+ /// Set up a selection manager for drawing selections in the minimap.
144+ /// Requires ``layoutManager`` to not be `nil`.
145+ private func setUpSelectionManager( textView: TextView ) {
146+ guard let layoutManager = layoutManager else {
147+ assertionFailure ( " No layout manager setup for the minimap. " )
148+ return
149+ }
125150 self . selectionManager = TextSelectionManager (
126151 layoutManager: layoutManager,
127152 textStorage: textView. textStorage,
@@ -130,12 +155,6 @@ public class MinimapView: FlippedNSView {
130155 )
131156 contentView. textView = textView
132157 contentView. selectionManager = selectionManager
133-
134- wantsLayer = true
135- layer? . backgroundColor = theme. background. cgColor
136-
137- setUpConstraints ( )
138- setUpListeners ( )
139158 }
140159
141160 // MARK: - Constraints
@@ -166,6 +185,7 @@ public class MinimapView: FlippedNSView {
166185
167186 // MARK: - Scroll listeners
168187
188+ /// Set up listeners for relevant frame and selection updates.
169189 private func setUpListeners( ) {
170190 guard let editorScrollView = textView? . enclosingScrollView else { return }
171191 // Need to listen to:
@@ -213,7 +233,7 @@ public class MinimapView: FlippedNSView {
213233 }
214234
215235 override public func resetCursorRects( ) {
216- // Don't use an iBeam
236+ // Don't use an iBeam in this view
217237 addCursorRect ( bounds, cursor: . arrow)
218238 }
219239
@@ -235,7 +255,7 @@ public class MinimapView: FlippedNSView {
235255 }
236256 }
237257
238- // Eat mouse events so we don't pass them on to the text view. Leads to some odd behavior.
258+ // Eat mouse events so we don't pass them on to the text view. Leads to some odd behavior otherwise .
239259
240260 override public func mouseDown( with event: NSEvent ) { }
241261 override public func mouseDragged( with event: NSEvent ) { }
0 commit comments