Skip to content

Instantly share code, notes, and snippets.

@orta
Forked from joelcox/highlight.swift
Created April 23, 2016 11:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save orta/cfe35d71b8477ac20d02a4da11e7e0d2 to your computer and use it in GitHub Desktop.
Save orta/cfe35d71b8477ac20d02a4da11e7e0d2 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()
NSRectFillUsingOperation(dirtyRect, .CompositeSourceOver);
if (dirtyRect.size.height > 1) {
let heightMinus1 = dirtyRect.size.height - 1
let currentControlTint = NSColor.currentControlTint()
let startingOpacity: CGFloat = currentControlTint == .BlueControlTint ? 0.16 : 0.09
let gradient = NSGradient(startingColor: NSColor(white: CGFloat(1.0), alpha:startingOpacity), endingColor:NSColor(white: CGFloat(1.0), alpha: 0.0))!
let startPoint = NSMakePoint(dirtyRect.origin.x, dirtyRect.origin.y + heightMinus1)
let endPoint = NSMakePoint(dirtyRect.origin.x, dirtyRect.origin.y + 1)
gradient.drawFromPoint(startPoint, toPoint: endPoint, options:NSGradientDrawsBeforeStartingLocation)
if currentControlTint == .BlueControlTint {
NSColor(white: CGFloat(1.0), alpha: CGFloat(0.1)).set()
let smallerRect = NSMakeRect(dirtyRect.origin.x, dirtyRect.origin.y + heightMinus1, dirtyRect.size.width, CGFloat(1.0))
NSRectFillUsingOperation(smallerRect, .CompositeSourceOver)
}
}
@soffes
Copy link

soffes commented May 20, 2019

Updated for Swift 5 and modern types: https://gist.github.com/soffes/6a305de23f5663cf0e27f4b338606a3b

This doesn't match on Mojave though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment