forked from CodeEditApp/CodeEditSourceEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFoldingRibbonView+Draw.swift
More file actions
205 lines (175 loc) · 7.28 KB
/
Copy pathFoldingRibbonView+Draw.swift
File metadata and controls
205 lines (175 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// FoldingRibbonView.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 5/8/25.
//
import AppKit
import CodeEditTextView
extension FoldingRibbonView {
/// The context in which the fold is being drawn, including the depth and fold range.
struct FoldMarkerDrawingContext {
let range: ClosedRange<Int>
let depth: UInt
/// Increment the depth
func incrementDepth() -> FoldMarkerDrawingContext {
FoldMarkerDrawingContext(
range: range,
depth: depth + 1
)
}
}
override func draw(_ dirtyRect: NSRect) {
guard let context = NSGraphicsContext.current?.cgContext,
let layoutManager = model.textView?.layoutManager else {
return
}
context.saveGState()
context.clip(to: dirtyRect)
// Find the visible lines in the rect AppKit is asking us to draw.
guard let rangeStart = layoutManager.textLineForPosition(dirtyRect.minY),
let rangeEnd = layoutManager.textLineForPosition(dirtyRect.maxY) else {
return
}
let lineRange = rangeStart.index...rangeEnd.index
context.setFillColor(markerColor)
let folds = model.getFolds(in: lineRange)
for fold in folds {
drawFoldMarker(
fold,
markerContext: FoldMarkerDrawingContext(range: lineRange, depth: 0),
in: context,
using: layoutManager
)
}
context.restoreGState()
}
/// Draw a single fold marker for a fold.
///
/// Ensure the correct fill color is set on the drawing context before calling.
///
/// - Parameters:
/// - fold: The fold to draw.
/// - markerContext: The context in which the fold is being drawn, including the depth and if a line is
/// being hovered.
/// - context: The drawing context to use.
/// - layoutManager: A layout manager used to retrieve position information for lines.
private func drawFoldMarker(
_ fold: FoldRange,
markerContext: FoldMarkerDrawingContext,
in context: CGContext,
using layoutManager: TextLayoutManager
) {
guard let minYPosition = layoutManager.textLineForIndex(fold.lineRange.lowerBound)?.yPos,
let maxPosition = layoutManager.textLineForIndex(fold.lineRange.upperBound) else {
return
}
let maxYPosition = maxPosition.yPos + maxPosition.height
if let hoveringFold,
hoveringFold.depth == markerContext.depth,
fold.lineRange == hoveringFold.range {
drawHoveredFold(
markerContext: markerContext,
minYPosition: minYPosition,
maxYPosition: maxYPosition,
in: context
)
} else {
drawNestedFold(
markerContext: markerContext,
minYPosition: minYPosition,
maxYPosition: maxYPosition,
in: context
)
}
// Draw subfolds
for subFold in fold.subFolds.filter({ $0.lineRange.overlaps(markerContext.range) }) {
drawFoldMarker(subFold, markerContext: markerContext.incrementDepth(), in: context, using: layoutManager)
}
}
private func drawHoveredFold(
markerContext: FoldMarkerDrawingContext,
minYPosition: CGFloat,
maxYPosition: CGFloat,
in context: CGContext
) {
context.saveGState()
let plainRect = NSRect(x: -2, y: minYPosition, width: 11.0, height: maxYPosition - minYPosition)
let roundedRect = NSBezierPath(roundedRect: plainRect, xRadius: 11.0 / 2, yRadius: 11.0 / 2)
context.setFillColor(hoverFillColor.copy(alpha: hoverAnimationProgress) ?? hoverFillColor)
context.setStrokeColor(hoverBorderColor.copy(alpha: hoverAnimationProgress) ?? hoverBorderColor)
context.addPath(roundedRect.cgPathFallback)
context.drawPath(using: .fillStroke)
// Add the little arrows
drawChevron(in: context, yPosition: minYPosition + 8, pointingUp: false)
drawChevron(in: context, yPosition: maxYPosition - 8, pointingUp: true)
context.restoreGState()
}
private func drawChevron(in context: CGContext, yPosition: CGFloat, pointingUp: Bool) {
context.saveGState()
let path = CGMutablePath()
let chevronSize = CGSize(width: 4.0, height: 2.5)
let center = (Self.width / 2)
let minX = center - (chevronSize.width / 2)
let maxX = center + (chevronSize.width / 2)
let startY = pointingUp ? yPosition + chevronSize.height : yPosition - chevronSize.height
context.setStrokeColor(NSColor.secondaryLabelColor.withAlphaComponent(hoverAnimationProgress).cgColor)
context.setLineCap(.round)
context.setLineJoin(.round)
context.setLineWidth(1.3)
path.move(to: CGPoint(x: minX, y: startY))
path.addLine(to: CGPoint(x: center, y: yPosition))
path.addLine(to: CGPoint(x: maxX, y: startY))
context.addPath(path)
context.strokePath()
context.restoreGState()
}
private func drawNestedFold(
markerContext: FoldMarkerDrawingContext,
minYPosition: CGFloat,
maxYPosition: CGFloat,
in context: CGContext
) {
let plainRect = NSRect(x: 0, y: minYPosition + 1, width: 7, height: maxYPosition - minYPosition - 2)
// TODO: Draw a single horizontal line when folds are adjacent
let roundedRect = NSBezierPath(roundedRect: plainRect, xRadius: 3.5, yRadius: 3.5)
context.addPath(roundedRect.cgPathFallback)
context.drawPath(using: .fill)
// Add small white line if we're overlapping with other markers
if markerContext.depth != 0 {
drawOutline(
minYPosition: minYPosition,
maxYPosition: maxYPosition,
originalPath: roundedRect,
in: context
)
}
}
/// Draws a rounded outline for a rectangle, creating the small, light, outline around each fold indicator.
///
/// This function does not change fill colors for the given context.
///
/// - Parameters:
/// - minYPosition: The minimum y position of the rectangle to outline.
/// - maxYPosition: The maximum y position of the rectangle to outline.
/// - originalPath: The original bezier path for the rounded rectangle.
/// - context: The context to draw in.
private func drawOutline(
minYPosition: CGFloat,
maxYPosition: CGFloat,
originalPath: NSBezierPath,
in context: CGContext
) {
context.saveGState()
let plainRect = NSRect(x: -0.5, y: minYPosition, width: 8, height: maxYPosition - minYPosition)
let roundedRect = NSBezierPath(roundedRect: plainRect, xRadius: 4, yRadius: 4)
let combined = CGMutablePath()
combined.addPath(roundedRect.cgPathFallback)
combined.addPath(originalPath.cgPathFallback)
context.clip(to: CGRect(x: 0, y: minYPosition, width: 7, height: maxYPosition - minYPosition))
context.addPath(combined)
context.setFillColor(markerBorderColor)
context.drawPath(using: .eoFill)
context.restoreGState()
}
}