Skip to content

Commit 5c0e0fe

Browse files
committed
Improve gutter line number cell layout and marker positioning
- Store text size in `STGutterLineNumberCell` for more efficient layout - Add `firstBaselineOffsetFromTop` to `STGutterLineNumberCell` for proper vertical alignment - Position gutter markers based on the text size and baseline of the corresponding line number cell - Ensure marker views don't exceed the height of the line number cell
1 parent fc3a733 commit 5c0e0fe

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

Sources/STTextViewAppKit/Gutter/STGutterLineNumberCell.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class STGutterLineNumberCell: NSView {
99
let lineNumber: Int
1010
private let firstBaseline: CGFloat
1111
private let ctLine: CTLine
12-
private let textWidth: CGFloat
12+
let textSize: CGSize
1313
var insets: STRulerInsets = STRulerInsets()
1414

1515
override func animation(forKey key: NSAnimatablePropertyKey) -> Any? {
@@ -20,13 +20,17 @@ final class STGutterLineNumberCell: NSView {
2020
"\(super.debugDescription) (number: \(lineNumber))"
2121
}
2222

23+
override var firstBaselineOffsetFromTop: CGFloat {
24+
firstBaseline
25+
}
26+
2327
init(firstBaseline: CGFloat, attributes: [NSAttributedString.Key: Any], number: Int) {
2428
self.lineNumber = number
2529
self.firstBaseline = firstBaseline
2630

2731
let attributedString = NSAttributedString(string: "\(number)", attributes: attributes)
2832
self.ctLine = CTLineCreateWithAttributedString(attributedString)
29-
self.textWidth = ceil(CTLineGetTypographicBounds(ctLine, nil, nil, nil))
33+
self.textSize = CGSize(width: ceil(CTLineGetTypographicBounds(ctLine, nil, nil, nil)), height: ctLine.height())
3034

3135
super.init(frame: .zero)
3236
wantsLayer = true
@@ -49,7 +53,7 @@ final class STGutterLineNumberCell: NSView {
4953
}
5054

5155
override var intrinsicContentSize: NSSize {
52-
NSSize(width: textWidth + insets.trailing + insets.leading, height: 14)
56+
NSSize(width: textSize.width + insets.trailing + insets.leading, height: textSize.height)
5357
}
5458

5559
override func draw(_ rect: CGRect) {
@@ -63,7 +67,7 @@ final class STGutterLineNumberCell: NSView {
6367
ctx.textMatrix = CGAffineTransform(scaleX: 1, y: isFlipped ? -1 : 1)
6468

6569
// align to right
66-
ctx.textPosition = CGPoint(x: frame.width - (textWidth + insets.trailing), y: firstBaseline)
70+
ctx.textPosition = CGPoint(x: frame.width - (textSize.width + insets.trailing), y: firstBaseline)
6771
CTLineDraw(ctLine, ctx)
6872
ctx.restoreGState()
6973
}

Sources/STTextViewAppKit/Gutter/STGutterView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ open class STGutterView: NSView, NSDraggingSource {
164164
}
165165

166166
if let cellView {
167-
markerContainerView.addSubview(marker.view)
168-
marker.view.frame.size = cellView.frame.size
169167
marker.view.frame.origin = cellView.frame.origin
168+
marker.view.frame.size = cellView.frame.size
169+
marker.view.frame.size.height = min(cellView.textSize.height + cellView.firstBaselineOffsetFromTop, cellView.frame.size.height)
170+
markerContainerView.addSubview(marker.view)
170171
}
171172
}
172173
}

0 commit comments

Comments
 (0)