Skip to content

Instantly share code, notes, and snippets.

@soffes
Forked from orta/highlight.swift
Last active November 16, 2021 07:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soffes/6a305de23f5663cf0e27f4b338606a3b to your computer and use it in GitHub Desktop.
Save soffes/6a305de23f5663cf0e27f4b338606a3b to your computer and use it in GitHub Desktop.
Swift implementation to highlight Cocoa UI elements (http://stackoverflow.com/a/25984748/316803)
// Taken from:
// https://gist.github.com/joelcox/28de2f0cb21ea47bd789
NSColor.selectedMenuItemColor.set()
NSBezierPath(rect: rect).fill()
if rect.height > 1 {
let currentControlTint = NSColor.currentControlTint
let startingAlpha: CGFloat = currentControlTint == .blueControlTint ? 0.16 : 0.09
let gradient = NSGradient(starting: NSColor(white: 1, alpha: startingAlpha),
ending: NSColor(white: 1, alpha: 0))
let start = CGPoint(x: rect.minX, y: rect.maxY - 1)
let end = CGPoint(x: rect.minX, y: rect.minY + 1)
gradient?.draw(from: start, to: end, options: .drawsBeforeStartingLocation)
if currentControlTint == .blueControlTint {
NSColor(white: 1, alpha: 0.1).set()
let smallerRect = CGRect(x: rect.minX, y: rect.maxY - 1, width: rect.width, height: 1)
NSBezierPath(rect: smallerRect).fill()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment