@@ -34,10 +34,22 @@ class DataGridBaseCellView: NSTableCellView {
3434 var isFocusedCell : Bool = false {
3535 didSet {
3636 guard oldValue != isFocusedCell else { return }
37- updateFocusRing ( )
37+ updateFocusPresentation ( )
3838 }
3939 }
4040
41+ private lazy var focusOverlay : CellFocusOverlay = {
42+ let overlay = CellFocusOverlay ( )
43+ addSubview ( overlay)
44+ NSLayoutConstraint . activate ( [
45+ overlay. leadingAnchor. constraint ( equalTo: leadingAnchor) ,
46+ overlay. trailingAnchor. constraint ( equalTo: trailingAnchor) ,
47+ overlay. topAnchor. constraint ( equalTo: topAnchor) ,
48+ overlay. bottomAnchor. constraint ( equalTo: bottomAnchor) ,
49+ ] )
50+ return overlay
51+ } ( )
52+
4153 private( set) lazy var backgroundView : NSView = {
4254 let view = NSView ( )
4355 view. wantsLayer = true
@@ -189,7 +201,7 @@ class DataGridBaseCellView: NSTableCellView {
189201 override var backgroundStyle : NSView . BackgroundStyle {
190202 didSet {
191203 backgroundView. isHidden = ( changeBackgroundColor == nil )
192- if isFocusedCell { needsDisplay = true }
204+ updateFocusPresentation ( )
193205 }
194206 }
195207
@@ -202,38 +214,10 @@ class DataGridBaseCellView: NSTableCellView {
202214 NSBezierPath ( rect: bounds) . fill ( )
203215 }
204216
205- override func draw( _ dirtyRect: NSRect ) {
206- super. draw ( dirtyRect)
207- guard isFocusedCell else { return }
208- if backgroundStyle == . emphasized {
209- drawEmphasizedFocusBorder ( )
210- } else {
211- NSGraphicsContext . saveGraphicsState ( )
212- NSFocusRingPlacement . only. set ( )
213- drawFocusRingMask ( )
214- NSGraphicsContext . restoreGraphicsState ( )
215- }
216- }
217-
218- private func drawEmphasizedFocusBorder( ) {
219- let inset : CGFloat = 1
220- let rect = bounds. insetBy ( dx: inset, dy: inset)
221- let path = NSBezierPath ( rect: rect)
222- path. lineWidth = 2
223- NSColor . alternateSelectedControlTextColor. setStroke ( )
224- path. stroke ( )
225- }
226-
227- override func viewDidChangeEffectiveAppearance( ) {
228- super. viewDidChangeEffectiveAppearance ( )
229- if isFocusedCell {
230- needsDisplay = true
231- }
232- }
233-
234- private func updateFocusRing( ) {
235- focusRingType = ( isFocusedCell && backgroundStyle != . emphasized) ? . exterior : . none
217+ private func updateFocusPresentation( ) {
218+ let onEmphasized = backgroundStyle == . emphasized
219+ focusOverlay. style = ( isFocusedCell && onEmphasized) ? . contrastingBorder : . hidden
220+ focusRingType = ( isFocusedCell && !onEmphasized) ? . exterior : . none
236221 noteFocusRingMaskChanged ( )
237- needsDisplay = true
238222 }
239223}
0 commit comments