Skip to content

Commit 2daca7c

Browse files
committed
Improve appearance of the gutter view
Make the gutter view background translucent using NSVisualEffectView when no custom background color is set. This allows the gutter to blend better with the editor window. Extract the marker container view into a dedicated STGutterMarkerContainerView class to distinguish it from the main gutter container view.
1 parent 5f7af9f commit 2daca7c

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

Sources/STTextViewAppKit/Gutter/STGutterContainerView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class STGutterContainerView: NSView {
1717
}
1818

1919
override var isOpaque: Bool {
20-
true
20+
false
2121
}
2222

2323
@available(*, unavailable)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Created by Marcin Krzyzanowski
2+
// https://github.com/krzyzanowskim/STTextView/blob/main/LICENSE.md
3+
4+
import AppKit
5+
6+
// Gutter line number cells container
7+
final class STGutterMarkerContainerView: NSView {
8+
9+
override init(frame: CGRect) {
10+
super.init(frame: frame)
11+
wantsLayer = true
12+
clipsToBounds = true
13+
}
14+
15+
override var isFlipped: Bool {
16+
true
17+
}
18+
19+
override var isOpaque: Bool {
20+
false
21+
}
22+
23+
@available(*, unavailable)
24+
required init?(coder: NSCoder) {
25+
fatalError("init(coder:) has not been implemented")
26+
}
27+
}

Sources/STTextViewAppKit/Gutter/STGutterView.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public extension STGutterViewDelegate {
2828
/// A gutter to the side of a scroll view’s document view.
2929
open class STGutterView: NSView, NSDraggingSource {
3030
internal let containerView: STGutterContainerView
31-
internal let markerContainerView: STGutterContainerView
31+
internal let markerContainerView: STGutterMarkerContainerView
3232

3333
/// Delegate
3434
weak var delegate: (any STGutterViewDelegate)?
@@ -64,8 +64,20 @@ open class STGutterView: NSView, NSDraggingSource {
6464
internal var backgroundColor: NSColor? = nil {
6565
didSet {
6666
layer?.backgroundColor = backgroundColor?.cgColor
67+
if backgroundColor == nil, _backgroundEffectView == nil {
68+
let backgroundEffect = NSVisualEffectView(frame: bounds)
69+
backgroundEffect.autoresizingMask = [.width, .height]
70+
backgroundEffect.blendingMode = .withinWindow
71+
backgroundEffect.material = .contentBackground
72+
backgroundEffect.state = .followsWindowActiveState
73+
addSubview(backgroundEffect, positioned: .below, relativeTo: self)
74+
self._backgroundEffectView = backgroundEffect
75+
} else if backgroundColor != nil, _backgroundEffectView != nil {
76+
_backgroundEffectView?.removeFromSuperview()
77+
}
6778
}
6879
}
80+
private var _backgroundEffectView: NSVisualEffectView?
6981

7082
/// The background color of the highlighted line.
7183
@Invalidating(.display)
@@ -96,16 +108,21 @@ open class STGutterView: NSView, NSDraggingSource {
96108
true
97109
}
98110

111+
open override var allowsVibrancy: Bool {
112+
true
113+
}
114+
99115
override init(frame: CGRect) {
100116
containerView = STGutterContainerView(frame: frame)
101117
containerView.autoresizingMask = [.width, .height]
102118

103-
markerContainerView = STGutterContainerView(frame: frame)
119+
markerContainerView = STGutterMarkerContainerView(frame: frame)
104120
markerContainerView.autoresizingMask = [.width, .height]
105121

106122
super.init(frame: frame)
107123
wantsLayer = true
108124
clipsToBounds = true
125+
109126
addSubview(containerView)
110127
addSubview(markerContainerView)
111128

0 commit comments

Comments
 (0)