Skip to content

Commit f0f3f7b

Browse files
committed
fix: use background subview instead of draw(_:) for change-state coloring
draw(_:) with wantsLayer=true caused text to be invisible because the fill painted on the cell's layer backing store while the text field rendered on its own sublayer. Using a dedicated background NSView positioned below the text field avoids layer compositing issues. backgroundStyle didSet hides the background view when .emphasized (row selected) so the native selection highlight shows through.
1 parent e4944ad commit f0f3f7b

1 file changed

Lines changed: 30 additions & 12 deletions

File tree

TablePro/Views/Results/DataGridCellView.swift

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,40 @@
55

66
import AppKit
77

8-
/// Custom cell view that draws change-state backgrounds via `draw(_:)` instead
9-
/// of `layer.backgroundColor`. AppKit's `NSTableRowView` sets `backgroundStyle`
10-
/// to `.emphasized` when the row is selected — we skip the custom background in
11-
/// that case so the native selection highlight shows through.
8+
/// Custom cell view that uses a background subview for change-state coloring.
9+
/// AppKit's `NSTableRowView` sets `backgroundStyle` to `.emphasized` when the
10+
/// row is selected — we hide the background view so the native selection highlight
11+
/// shows through.
1212
final class DataGridCellView: NSTableCellView {
13-
var changeBackgroundColor: NSColor?
13+
private lazy var backgroundView: NSView = {
14+
let view = NSView()
15+
view.wantsLayer = true
16+
view.translatesAutoresizingMaskIntoConstraints = false
17+
addSubview(view, positioned: .below, relativeTo: subviews.first)
18+
NSLayoutConstraint.activate([
19+
view.leadingAnchor.constraint(equalTo: leadingAnchor),
20+
view.trailingAnchor.constraint(equalTo: trailingAnchor),
21+
view.topAnchor.constraint(equalTo: topAnchor),
22+
view.bottomAnchor.constraint(equalTo: bottomAnchor),
23+
])
24+
return view
25+
}()
1426

15-
override var backgroundStyle: NSView.BackgroundStyle {
16-
didSet { needsDisplay = true }
27+
var changeBackgroundColor: NSColor? {
28+
didSet {
29+
if let color = changeBackgroundColor {
30+
backgroundView.layer?.backgroundColor = color.cgColor
31+
backgroundView.isHidden = (backgroundStyle == .emphasized)
32+
} else {
33+
backgroundView.layer?.backgroundColor = nil
34+
backgroundView.isHidden = true
35+
}
36+
}
1737
}
1838

19-
override func draw(_ dirtyRect: NSRect) {
20-
if backgroundStyle != .emphasized, let color = changeBackgroundColor {
21-
color.setFill()
22-
dirtyRect.fill()
39+
override var backgroundStyle: NSView.BackgroundStyle {
40+
didSet {
41+
backgroundView.isHidden = (backgroundStyle == .emphasized) || (changeBackgroundColor == nil)
2342
}
24-
super.draw(dirtyRect)
2543
}
2644
}

0 commit comments

Comments
 (0)