-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathAccountItemView.swift
More file actions
204 lines (177 loc) · 6.47 KB
/
AccountItemView.swift
File metadata and controls
204 lines (177 loc) · 6.47 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import SwiftUI
import Cache
public class AccountItemView: NSView {
private var target: AnyObject?
private var action: Selector?
private var isHovered = false
private var visualEffect: NSVisualEffectView
private let menuItemPadding: CGFloat = 6
private let topInset: CGFloat = 4 // Customize this value
private let bottomInset: CGFloat = 0
private var userName: String
private var nameLabel: NSTextField!
let avatarSize = 28.0
let horizontalPadding = 14.0
let verticalPadding = 8.0
public override func setFrameSize(_ newSize: NSSize) {
super.setFrameSize(newSize)
updateVisualEffectFrame()
}
public init(
target: AnyObject? = nil,
action: Selector? = nil,
userName: String = ""
) {
self.target = target
self.action = action
self.userName = userName
// Initialize visualEffect with zero frame - it will be updated in layout
self.visualEffect = NSVisualEffectView(frame: .zero)
self.visualEffect.material = .selection
self.visualEffect.state = .active
self.visualEffect.blendingMode = .withinWindow
self.visualEffect.isHidden = true
self.visualEffect.wantsLayer = true
self.visualEffect.layer?.cornerRadius = 4
self.visualEffect.layer?.backgroundColor = NSColor.controlAccentColor.cgColor
self.visualEffect.isEmphasized = true
// Initialize with a reasonable starting size
super.init(
frame: NSRect(
x: 0,
y: 0,
width: 240,
height: avatarSize+verticalPadding+topInset
)
)
// Set up autoresizing mask to allow the view to resize with its superview
self.autoresizingMask = [.width]
self.visualEffect.autoresizingMask = [.width, .height]
wantsLayer = true
addSubview(visualEffect)
// Create and configure subviews
setupSubviews()
}
private func setupSubviews() {
// Create avatar view with hover state
let avatarView = NSHostingView(rootView: AvatarView(userName: userName, isHovered: isHovered))
avatarView.frame = NSRect(
x: horizontalPadding,
y: 4,
width: avatarSize,
height: avatarSize
)
addSubview(avatarView)
// Store nameLabel as property and configure it
nameLabel = NSTextField(
labelWithString: userName.isEmpty ? "Sign In to GitHub Account" : userName
)
nameLabel.font =
.systemFont(ofSize: NSFont.systemFontSize, weight: .semibold)
nameLabel.frame = NSRect(
x: horizontalPadding*1.5 + avatarSize,
y: 0,
width: 180,
height: avatarSize
)
nameLabel.cell?.truncatesLastVisibleLine = true
nameLabel.cell?.lineBreakMode = .byTruncatingTail
nameLabel.textColor = .labelColor
addSubview(nameLabel)
// Make sure nameLabel resizes with the view
nameLabel.autoresizingMask = [.width]
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func mouseUp(with event: NSEvent) {
if let target = target, let action = action {
NSApp.sendAction(action, to: target, from: self)
}
}
public override func updateTrackingAreas() {
super.updateTrackingAreas()
trackingAreas.forEach { removeTrackingArea($0) }
let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeAlways]
let trackingArea = NSTrackingArea(rect: bounds, options: options, owner: self, userInfo: nil)
addTrackingArea(trackingArea)
}
public override func mouseEntered(with event: NSEvent) {
super.mouseEntered(with: event)
isHovered = true
visualEffect.isHidden = false
nameLabel.textColor = .white
if let avatarView = subviews.first(where: { $0 is NSHostingView<AvatarView> }) as? NSHostingView<AvatarView> {
avatarView.rootView = AvatarView(userName: userName, isHovered: true)
}
}
public override func mouseExited(with event: NSEvent) {
super.mouseExited(with: event)
isHovered = false
visualEffect.isHidden = true
nameLabel.textColor = .labelColor
if let avatarView = subviews.first(where: { $0 is NSHostingView<AvatarView> }) as? NSHostingView<AvatarView> {
avatarView.rootView = AvatarView(userName: userName, isHovered: false)
}
}
public override func resetCursorRects() {
addCursorRect(bounds, cursor: .pointingHand)
}
public override func layout() {
super.layout()
updateVisualEffectFrame()
}
private func updateVisualEffectFrame() {
var paddedFrame = bounds
paddedFrame.origin.x += menuItemPadding
paddedFrame.origin.y += bottomInset
paddedFrame.size.width -= menuItemPadding*2
paddedFrame.size.height -= (topInset + bottomInset)
visualEffect.frame = paddedFrame
}
}
struct AvatarView: View {
let userName: String
let isHovered: Bool
@ObservedObject private var viewModel = AvatarViewModel.shared
init(userName: String, isHovered: Bool = false) {
self.userName = userName
self.isHovered = isHovered
}
var body: some View {
Group {
if let avatarImage = viewModel.avatarImage {
avatarImage
.resizable()
.scaledToFit()
.clipShape(Circle())
} else if userName.isEmpty {
Image(systemName: "person.crop.circle")
.resizable()
.scaledToFit()
.foregroundStyle(isHovered ? .white : .primary)
} else {
ProgressView()
.clipShape(Circle())
}
}
}
}
struct NSViewPreview: NSViewRepresentable {
var userName: String = ""
func makeNSView(context: Context) -> NSView {
let NSView = AccountItemView(
userName: userName
)
return NSView
}
func updateNSView(_ nsView: NSView, context: Context) {
// Update as needed...
}
}
#Preview("Not Signed In") {
NSViewPreview().frame(width: 245, height: 52)
}
#Preview("Signed In, Active") {
NSViewPreview(userName: "xcode-test").frame(width: 245, height: 52)
}