Created
April 29, 2021 12:51
-
-
Save Lessica/0e81519f0bedbc8de7856dde90ba83b4 to your computer and use it in GitHub Desktop.
An attempt to re-draw 'focus ring' of NSTableView and NSOutlineView.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TemplateOutlineView.swift | |
// JSTColorPicker | |
// | |
// Created by Rachel on 4/16/21. | |
// Copyright © 2021 JST. All rights reserved. | |
// | |
import Cocoa | |
class TemplateOutlineView: NSOutlineView { | |
private static let focusLineWidth: CGFloat = 2.0 | |
private static let focusLineColor = NSColor.controlAccentColor | |
@objc | |
func drawContextMenuHighlightForRow(_ row: Int) { | |
guard !inLiveResize else { return } | |
guard let ctx = NSGraphicsContext.current?.cgContext else { return } | |
let rects = [CGRect](arrayLiteral: rect(ofRow: row)) | |
ctx.setLineWidth(TemplateOutlineView.focusLineWidth) | |
ctx.setStrokeColor(TemplateOutlineView.focusLineColor.cgColor) | |
let outerRect = bounds | |
.insetBy(dx: 0.0, dy: 0.5) | |
.offsetBy(dx: 0.0, dy: 0.5) | |
for rect in rects.filter({ outerRect.intersects($0) }) { | |
let innerRect = rect.intersection(outerRect) | |
if innerRect.height > rect.height + 2.0 { | |
ctx.addRect(innerRect | |
.insetBy(dx: TemplateOutlineView.focusLineWidth, dy: TemplateOutlineView.focusLineWidth + 0.5) | |
.offsetBy(dx: 0.0, dy: -0.5) | |
) | |
} else { | |
ctx.addRect(rect | |
.insetBy(dx: TemplateOutlineView.focusLineWidth, dy: TemplateOutlineView.focusLineWidth + 0.5) | |
.offsetBy(dx: 0.0, dy: -0.5) | |
) | |
} | |
} | |
ctx.strokePath() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment