-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColoredButton.swift
65 lines (55 loc) · 2.36 KB
/
ColoredButton.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
//
// ColoredButton.swift
// TestButton
//
// Created by Mike Griebling on 2023-03-01.
//
import Cocoa
class ColoredButton: NSButton {
@IBInspectable var bgColor: NSColor = .darkGray { didSet { needsDisplay = true } }
@IBInspectable var foreColor: NSColor = .labelColor { didSet { needsDisplay = true } }
func configure() {
// self.appearance = NSAppearance(named: .vibrantDark)
let highlightColor: NSColor = bgColor.highlight(withLevel: 0.8)!
if !isHighlighted {
self.layer?.backgroundColor = bgColor.cgColor
} else {
self.layer?.backgroundColor = highlightColor.cgColor
// } else {
// self.layer?.backgroundColor = bgColor.highlight(withLevel: -0.5)!.cgColor
}
self.layer?.borderColor = NSColor.black.cgColor
self.layer?.borderWidth = 1
// let attributedString = NSAttributedString(string: title,
// attributes: [NSAttributedString.Key.foregroundColor: foreColor])
// self.attributedTitle = attributedString
// let titleParagraphStyle = NSMutableParagraphStyle()
// titleParagraphStyle.alignment = alignment
//
// let attributes: [NSAttributedString.Key : Any] = [.foregroundColor: foreColor, .paragraphStyle: titleParagraphStyle]
// if !self.attributedTitle.attributeKeys.isEmpty {
// var mutableAttributes = self.attributedTitle.mutableArrayValue(forKey: NSAttributedString.Key.foregroundColor.rawValue)
// // mutableAttributes[NSAttributedString.Key.foregroundColor] = foreColor
// } else {
// self.attributedTitle = NSAttributedString(string: self.title, attributes: attributes)
// }
}
override func updateLayer() {
configure()
}
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// let highlightColor: NSColor = bgColor.highlight(withLevel: 0.8)!
// if !isHighlighted {
// self.layer?.backgroundColor = bgColor.cgColor
// } else {
// self.layer?.backgroundColor = highlightColor.cgColor
//// } else {
//// self.layer?.backgroundColor = bgColor.highlight(withLevel: -0.5)!.cgColor
// }
}
// override var allowsVibrancy: Bool { true }
override func awakeFromNib() {
configure()
}
}