Skip to content

Instantly share code, notes, and snippets.

@livingstonef
Created September 8, 2016 13:59
Show Gist options
  • Save livingstonef/04bc9344a501cf198db13d1e48a6addd to your computer and use it in GitHub Desktop.
Save livingstonef/04bc9344a501cf198db13d1e48a6addd to your computer and use it in GitHub Desktop.
//
// ToolbarCustomView.swift
// LabDoc
//
// Created by Livingstone Fultang on 24/07/2016.
// Copyright © 2016 Livingstone Fultang. All rights reserved.
//
import Cocoa
@IBDesignable class ToolbarCustomView: NSView {
var gradient:NSGradient! = nil
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
self.wantsLayer = true
self.needsDisplay = true;
self.layer?.masksToBounds = true
//The background
let startingColor:NSColor = NSColor(red: 244/256, green: 244/256, blue: 244/256, alpha: 1.0)
let endingColor:NSColor = NSColor(red: 203/256, green: 203/256, blue: 203/256, alpha: 1.0)
gradient = NSGradient(starting: startingColor, ending: endingColor)!;
if self.window?.isKeyWindow == true {
gradient.draw(in: self.bounds, angle: 270 )
}else{
self.layer?.backgroundColor = NSColor.controlBackgroundColor.cgColor
}
//The bottom border
self.layer?.borderColor = NSColor(red: 160/256, green: 160/256, blue: 160/256, alpha: 1).cgColor;
//Recieve window key status notification and force redraw of the custom toolbar to update the background
NotificationCenter.default.setObserver(self, selector: #selector(ToolbarCustomView.handleParentWindowStateChange(_:)) , name: NSNotification.Name.NSWindowDidBecomeKey.rawValue, object: self.window)
NotificationCenter.default.setObserver(self, selector: #selector(ToolbarCustomView.handleParentWindowStateChange(_:)) , name: NSNotification.Name.NSWindowDidResignKey.rawValue, object: self.window)
}
func handleParentWindowStateChange(_ notification:Notification) {
//The background
self.needsDisplay = true;
self.needsLayout = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment