Instantly share code, notes, and snippets.
Last active
October 31, 2015 03:28
-
Save icodeforlove/a334884e59784b4c2567 to your computer and use it in GitHub Desktop.
Custom implementation of the OSX Yosemite close, miniaturize, zoom buttons.
This file contains 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
class WindowButton: NSButton { | |
private var _isMouseOver:Bool = false; | |
private var useTrackingArea:Bool = true; | |
var isMouseOver:Bool { | |
set { | |
self._isMouseOver = newValue; | |
self.setNeedsDisplay(); | |
} | |
get { | |
return self._isMouseOver; | |
} | |
}; | |
var type:NSWindowButton = NSWindowButton.MiniaturizeButton; | |
init(frame:NSRect, type:NSWindowButton, useTrackingArea:Bool = true) { | |
super.init(frame: frame); | |
self.type = type; | |
self.target = self; | |
self.action = "onClick"; | |
self.useTrackingArea = useTrackingArea; | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder); | |
self.target = self; | |
self.action = "onClick"; | |
} | |
func onClick () { | |
if (type == NSWindowButton.ZoomButton) { | |
self.window?.zoom(self); | |
} else if (type == NSWindowButton.MiniaturizeButton) { | |
self.window?.miniaturize(self); | |
} else if (type == NSWindowButton.CloseButton) { | |
self.window?.close(); | |
} | |
} | |
override func updateTrackingAreas() { | |
if (self.useTrackingArea) { | |
let options:NSTrackingAreaOptions = NSTrackingAreaOptions.ActiveInActiveApp | |
.union(NSTrackingAreaOptions.MouseEnteredAndExited) | |
.union(NSTrackingAreaOptions.AssumeInside) | |
.union(NSTrackingAreaOptions.InVisibleRect); | |
let trackingArea = NSTrackingArea(rect: self.bounds, options:options, owner: self, userInfo: nil); | |
self.addTrackingArea(trackingArea); | |
} | |
} | |
override func mouseEntered(theEvent: NSEvent) { | |
if (self.useTrackingArea) { | |
self.isMouseOver = true; | |
} | |
} | |
override func drawRect(dirtyRect: NSRect) { | |
NSGraphicsContext.currentContext()?.saveGraphicsState(); | |
var backgroundGradient:NSGradient?; | |
var strokeColor:NSColor?; | |
var lineColor:NSColor?; | |
if (type == NSWindowButton.ZoomButton) { | |
backgroundGradient = NSGradient( | |
startingColor: NSColor(red: 0.153, green: 0.788, blue: 0.247, alpha: 1), | |
endingColor: NSColor(red: 0.153, green: 0.816, blue: 0.255, alpha: 1))!; | |
lineColor = NSColor(red: 0.004, green: 0.392, blue: 0, alpha: 1); | |
strokeColor = NSColor(red: 0.180, green: 0.690, blue: 0.235, alpha: 1); | |
} else if (type == NSWindowButton.MiniaturizeButton) { | |
backgroundGradient = NSGradient( | |
startingColor: NSColor(red: 1, green: 0.741, blue: 0.180, alpha: 1), | |
endingColor: NSColor(red: 1, green: 0.773, blue: 0.184, alpha: 1))!; | |
lineColor = NSColor(red: 0.600, green: 0.345, blue: 0.004, alpha: 1); | |
strokeColor = NSColor(red: 0.875, green: 0.616, blue: 0.094, alpha: 1); | |
} else if (type == NSWindowButton.CloseButton) { | |
backgroundGradient = NSGradient( | |
startingColor: NSColor(red: 1, green: 0.373, blue: 0.337, alpha: 1), | |
endingColor: NSColor(red: 1, green: 0.388, blue: 0.357, alpha: 1))!; | |
lineColor = NSColor(red: 0.302, green: 0, blue: 0, alpha: 1); | |
strokeColor = NSColor(red: 0.886, green: 0.243, blue: 0.216, alpha: 1); | |
} | |
// draw background | |
var path = NSBezierPath(); | |
path.appendBezierPathWithOvalInRect(NSMakeRect(self.bounds.origin.x + 0.5, self.bounds.origin.y + 0.5, self.bounds.width - 1, self.bounds.height - 1)); | |
backgroundGradient?.drawInBezierPath(path, relativeCenterPosition: NSMakePoint(0, 0)); | |
strokeColor?.setStroke(); | |
path.lineWidth = 0.5; | |
path.stroke(); | |
if (self.highlighted) { | |
NSColor(calibratedRed: 0, green: 0, blue: 0, alpha: 0.2).setFill(); | |
path.fill(); | |
} | |
// draw contents | |
if (self.isMouseOver) { | |
path = NSBezierPath(); | |
if (type == NSWindowButton.ZoomButton) { | |
NSGraphicsContext.currentContext()?.shouldAntialias = false; | |
path.moveToPoint(NSMakePoint(self.bounds.width / 2, self.bounds.height * 0.20)); | |
path.lineToPoint(NSMakePoint(self.bounds.width / 2, self.bounds.height * 0.80)); | |
path.moveToPoint(NSMakePoint(self.bounds.width * 0.80, self.bounds.height / 2)); | |
path.lineToPoint(NSMakePoint(self.bounds.width * 0.20, self.bounds.height / 2)); | |
path.lineWidth = 0.75; | |
} else if (type == NSWindowButton.MiniaturizeButton) { | |
NSGraphicsContext.currentContext()?.shouldAntialias = false; | |
path.moveToPoint(NSMakePoint(self.bounds.width * 0.80, self.bounds.height / 2)); | |
path.lineToPoint(NSMakePoint(self.bounds.width * 0.20, self.bounds.height / 2)); | |
path.lineWidth = 0.75; | |
} else if (type == NSWindowButton.CloseButton) { | |
path.moveToPoint(NSMakePoint(self.bounds.width * 0.30, self.bounds.height * 0.30)); | |
path.lineToPoint(NSMakePoint(self.bounds.width * 0.70, self.bounds.height * 0.70)); | |
path.moveToPoint(NSMakePoint(self.bounds.width * 0.70, self.bounds.height * 0.30)); | |
path.lineToPoint(NSMakePoint(self.bounds.width * 0.30, self.bounds.height * 0.70)); | |
path.lineWidth = 1; | |
} | |
lineColor?.setStroke(); | |
path.stroke(); | |
} | |
NSGraphicsContext.currentContext()?.restoreGraphicsState(); | |
} | |
} | |
public class WindowButtonBar:NSView { | |
var closeButton:WindowButton?; | |
var miniaturizeButton:WindowButton?; | |
var zoomButton:WindowButton?; | |
required public init?(coder: NSCoder) { | |
super.init(coder: coder); | |
self.setupViews(); | |
} | |
override init (frame:NSRect) { | |
super.init(frame: frame); | |
self.setupViews(); | |
} | |
func setupViews () { | |
self.closeButton = WindowButton(frame: NSMakeRect(0, 0, 13, 13), type: NSWindowButton.CloseButton, useTrackingArea: false); | |
self.miniaturizeButton = WindowButton(frame: NSMakeRect(20, 0, 13, 13), type: NSWindowButton.MiniaturizeButton, useTrackingArea: false); | |
self.zoomButton = WindowButton(frame: NSMakeRect(40, 0, 13, 13), type: NSWindowButton.ZoomButton, useTrackingArea: false); | |
self.addSubview(self.closeButton!); | |
self.addSubview(self.miniaturizeButton!); | |
self.addSubview(self.zoomButton!); | |
let trackingArea = NSTrackingArea(rect: self.bounds, options: NSTrackingAreaOptions.MouseEnteredAndExited.union(NSTrackingAreaOptions.MouseMoved).union(NSTrackingAreaOptions.ActiveAlways), owner: self, userInfo: nil); | |
self.addTrackingArea(trackingArea); | |
} | |
override public func mouseEntered(theEvent: NSEvent) { | |
self.closeButton?.isMouseOver = true; | |
self.miniaturizeButton?.isMouseOver = true; | |
self.zoomButton?.isMouseOver = true; | |
} | |
override public func mouseExited(theEvent: NSEvent) { | |
self.closeButton?.isMouseOver = false; | |
self.miniaturizeButton?.isMouseOver = false; | |
self.zoomButton?.isMouseOver = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment