Skip to content

Instantly share code, notes, and snippets.

@joelcox
Created September 23, 2015 18:41
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 joelcox/28de2f0cb21ea47bd789 to your computer and use it in GitHub Desktop.
Save joelcox/28de2f0cb21ea47bd789 to your computer and use it in GitHub Desktop.
Swift implementation to highlight Cocoa UI elements (http://stackoverflow.com/a/25984748/316803)
NSColor.selectedMenuItemColor().set()
NSRectFillUsingOperation(dirtyRect, .CompositeSourceOver);
if (dirtyRect.size.height > 1) {
let heightMinus1 = dirtyRect.size.height - 1
let currentControlTint = NSColor.currentControlTint()
var startingOpacity: CGFloat = 0.09
if currentControlTint == .BlueControlTint {
startingOpacity = 0.16
}
var 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)
}
}
@orta
Copy link

orta commented Apr 23, 2016

Thanks! - I tightened it up a bit to remove some vars - https://gist.github.com/orta/cfe35d71b8477ac20d02a4da11e7e0d2

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