-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathCheckBox.swift
66 lines (50 loc) · 1.51 KB
/
CheckBox.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
A layer-backed custom check box that is IBDesignable and IBInspectable.
*/
import Cocoa
@IBDesignable public class CheckBox: NSButton {
// MARK: Properties
@IBInspectable public var tintColor: NSColor {
get {
return NSColor(CGColor: checkBoxLayer.tintColor)!
}
set {
checkBoxLayer.tintColor = newValue.CGColor
}
}
@IBInspectable public var isChecked: Bool {
get {
return checkBoxLayer.isChecked
}
set {
checkBoxLayer.isChecked = newValue
}
}
private var checkBoxLayer: CheckBoxLayer {
return layer as! CheckBoxLayer
}
override public var intrinsicContentSize: NSSize {
return NSSize(width: 40, height: 40)
}
// MARK: View Life Cycle
override public func awakeFromNib() {
super.awakeFromNib()
wantsLayer = true
layer = CheckBoxLayer()
layer!.setNeedsDisplay()
}
// MARK: Events
override public func mouseDown(event: NSEvent) {
isChecked = !isChecked
cell!.performClick(self)
}
override public func viewDidChangeBackingProperties() {
super.viewDidChangeBackingProperties()
if let window = window {
layer?.contentsScale = window.backingScaleFactor
}
}
}