Nininea
Nininea

Reputation: 2729

Change highlight color of NSTableView selected row

how to change NSTable selected row background color?

here is good answer, but it is for uitable view .

For now,what I see is that I can change selected hilight style :

MyTAble.SelectionHighlightStyle = NSTableViewSelectionHighlightStyle.Regular;

But here is only 3 options;

None = -1L,
        Regular,
        SourceList

I have tried following solution :

patientListDelegate.SelectionChanged += (o, e) => {
                        var r = PatientTableView.SelectedRow;
                        var v = PatientTableView.GetRowView (r, false);
                        v.Emphasized = false;
                    };

It works normally , but if I minimize and then open application again , still shows blue color

Upvotes: 1

Views: 1354

Answers (2)

Jano
Jano

Reputation: 63667

Here is a swift version of the same technique as the one in the accepted answer. Important detail: the variable isEmphasized becomes false when the control is not key or the application is on the background, so it can be used to trigger a refresh and alternate to the unemphasized color.

import AppKit

final class CustomTableRowView: NSTableRowView {
    override func layout() {
        super.layout()
        // the default .none doesn’t call drawSelection(in:)
        selectionHighlightStyle = .regular
    }

    override func drawSelection(in dirtyRect: NSRect) {
        guard selectionHighlightStyle != .none else { 
            return 
        }
        let selectionRect = NSInsetRect(bounds, 10, 0)
        let fillColor = isEmphasized ? NSColor.red : NSColor.unemphasizedSelectedContentBackgroundColor
        fillColor.setFill()
        let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 5, yRadius: 5)
        selectionPath.fill()
    }

    override var isEmphasized: Bool {
        didSet {
            needsDisplay = true
        }
    }
}

Telling NSViewController to use the custom cell

// MARK: - NSOutlineViewDelegate

func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
    CustomTableRowView()
}

Upvotes: 0

Nininea
Nininea

Reputation: 2729

I found answer in objective-c

Change selection color on view-based NSTableView

Here is c# implementation:

inside delegate :

public override NSTableRowView CoreGetRowView (NSTableView tableView, nint row)
        {
            var rowView = tableView.MakeView ("row", this);
            if (rowView == null) {
                rowView = new PatientTableRow ();
                rowView.Identifier = "row";
            }
            return rowView as NSTableRowView;
        }

and custom row :

public class PatientTableRow : NSTableRowView
    {
        public override void DrawSelection (CGRect dirtyRect)
        {
            if (SelectionHighlightStyle != NSTableViewSelectionHighlightStyle.None) {

                NSColor.FromCalibratedWhite (0.65f, 1.0f).SetStroke ();
                NSColor.FromCalibratedWhite (0.82f, 1.0f).SetFill ();

                var selectionPath = NSBezierPath.FromRoundedRect (dirtyRect, 0, 0);

                selectionPath.Fill ();
                selectionPath.Stroke ();
            }
        }
    }

Upvotes: 1

Related Questions