Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NSTableCellView.backgroundStyle is never set to NSBackgroundStyle.Dark for selected row?

In a view-based NSTableView, I have a subclass of NSTableCellView.

I want to change text color for the selected row's cellView.

class CellView: NSTableCellView {

    override var backgroundStyle: NSBackgroundStyle {
        set {
            super.backgroundStyle = newValue

            self.udpateSelectionHighlight()
        }
        get {
            return super.backgroundStyle;
        }
    }

    func udpateSelectionHighlight() {
        if ( self.backgroundStyle == NSBackgroundStyle.Dark ) {
            self.textField?.textColor = NSColor.whiteColor()
        } else if( self.backgroundStyle == NSBackgroundStyle.Light ) {
            self.textField?.textColor = NSColor.blackColor()
        }
    }

}

The problem is that all cellViews are set with NSBackgroundStyle.Light.

My selection is custom-drawn in a subclass of NSTableRowView.

class RowView: NSTableRowView {

    override func drawSelectionInRect(dirtyRect: NSRect) {
        if ( self.selectionHighlightStyle != NSTableViewSelectionHighlightStyle.None ) {

            var selectionRect = NSInsetRect(self.bounds, 0, 2.5)
            NSColor( fromHexString: "d1d1d1" ).setFill()
            var selectionPath = NSBezierPath(
                roundedRect: selectionRect,
                xRadius: 10,
                yRadius: 60
            )
            // ...
            selectionPath.fill()
        }
    }

    // ...

}

Why isn't the selected row cellView's backgroundStyle property set to Dark?

Thanks.

like image 445
Nikolay Tsenkov Avatar asked Jan 28 '15 08:01

Nikolay Tsenkov


1 Answers

While I still don't know why doesn't the TableView/RowView set the Dark background on the selected row's cellView, I found this to be an acceptable workaround:

class CellView: NSTableCellView {

    override var backgroundStyle: NSBackgroundStyle {
        set {
            if let rowView = self.superview as? NSTableRowView {
                super.backgroundStyle = rowView.selected ? NSBackgroundStyle.Dark : NSBackgroundStyle.Light
            } else {
                super.backgroundStyle = newValue
            }
            self.udpateSelectionHighlight()
        }
        get {
            return super.backgroundStyle;
        }
    }

    func udpateSelectionHighlight() {
        if ( self.backgroundStyle == NSBackgroundStyle.Dark ) {
            self.textField?.textColor = NSColor.whiteColor()
        } else {
            self.textField?.textColor = NSColor.blackColor()
        }
    }

}
like image 106
Nikolay Tsenkov Avatar answered Sep 28 '22 04:09

Nikolay Tsenkov