-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathHoverButton.swift
More file actions
145 lines (121 loc) · 4.1 KB
/
HoverButton.swift
File metadata and controls
145 lines (121 loc) · 4.1 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import AppKit
class HoverButton: NSButton {
private var isLinkMode = false
override func awakeFromNib() {
super.awakeFromNib()
setupButton()
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setupButton()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupButton()
}
private func setupButton() {
self.wantsLayer = true
self.layer?.backgroundColor = NSColor.clear.cgColor
self.layer?.cornerRadius = 3
}
private func resetToDefaultState() {
self.layer?.backgroundColor = NSColor.clear.cgColor
if isLinkMode {
updateLinkAppearance(isHovered: false)
}
}
override func viewDidMoveToSuperview() {
super.viewDidMoveToSuperview()
DispatchQueue.main.async {
self.updateTrackingAreas()
}
}
override func layout() {
super.layout()
updateTrackingAreas()
}
func configureLinkMode() {
isLinkMode = true
self.isBordered = false
self.setButtonType(.momentaryChange)
self.layer?.backgroundColor = NSColor.clear.cgColor
}
func setLinkStyle(title: String, fontSize: CGFloat) {
configureLinkMode()
updateLinkAppearance(title: title, fontSize: fontSize, isHovered: false)
}
override func mouseEntered(with event: NSEvent) {
if isLinkMode {
updateLinkAppearance(isHovered: true)
} else {
self.layer?.backgroundColor = NSColor.labelColor.withAlphaComponent(0.15).cgColor
super.mouseEntered(with: event)
}
}
override func mouseExited(with event: NSEvent) {
if isLinkMode {
updateLinkAppearance(isHovered: false)
} else {
super.mouseExited(with: event)
resetToDefaultState()
}
}
private func updateLinkAppearance(title: String? = nil, fontSize: CGFloat? = nil, isHovered: Bool = false) {
let buttonTitle = title ?? self.title
let font = fontSize != nil ? NSFont.systemFont(ofSize: fontSize!, weight: .regular) : NSFont.systemFont(ofSize: 11)
let attributes: [NSAttributedString.Key: Any] = [
.foregroundColor: NSColor.controlAccentColor,
.font: font,
.underlineStyle: isHovered ? NSUnderlineStyle.single.rawValue : 0
]
let attributedTitle = NSAttributedString(string: buttonTitle, attributes: attributes)
self.attributedTitle = attributedTitle
}
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
// Reset state immediately after click
DispatchQueue.main.async {
self.resetToDefaultState()
}
}
override func mouseUp(with event: NSEvent) {
super.mouseUp(with: event)
// Ensure state is reset
DispatchQueue.main.async {
self.resetToDefaultState()
}
}
override func viewDidHide() {
super.viewDidHide()
// Reset state when view is hidden (like when menu closes)
resetToDefaultState()
}
override func viewDidUnhide() {
super.viewDidUnhide()
// Ensure clean state when view reappears
resetToDefaultState()
}
override func removeFromSuperview() {
super.removeFromSuperview()
// Reset state when removed from superview
resetToDefaultState()
}
override func updateTrackingAreas() {
super.updateTrackingAreas()
for trackingArea in self.trackingAreas {
self.removeTrackingArea(trackingArea)
}
guard self.bounds.width > 0 && self.bounds.height > 0 else { return }
let trackingArea = NSTrackingArea(
rect: self.bounds,
options: [
.mouseEnteredAndExited,
.activeAlways,
.inVisibleRect
],
owner: self,
userInfo: nil
)
self.addTrackingArea(trackingArea)
}
}