-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathModelPickerButton.swift
More file actions
270 lines (234 loc) · 9.92 KB
/
ModelPickerButton.swift
File metadata and controls
270 lines (234 loc) · 9.92 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import AppKit
import SwiftUI
// MARK: - Model Picker Button (NSViewRepresentable)
struct ModelPickerButton: NSViewRepresentable {
let selectedModel: LLMModel?
let copilotModels: [LLMModel]
let byokModels: [LLMModel]
let isBYOKFFEnabled: Bool
let currentCache: ScopeCache
let fontScale: Double
func makeNSView(context: Context) -> NSView {
let container = ModelPickerContainerView(fontScale: fontScale)
container.translatesAutoresizingMaskIntoConstraints = false
let button = ClickThroughButton()
button.title = ""
button.bezelStyle = .inline
button.setButtonType(.momentaryPushIn)
button.isBordered = false
button.target = context.coordinator
button.action = #selector(Coordinator.buttonClicked(_:))
button.translatesAutoresizingMaskIntoConstraints = false
button.wantsLayer = true
let titleLabel = NSTextField(labelWithString: "")
titleLabel.isEditable = false
titleLabel.isBordered = false
titleLabel.backgroundColor = .clear
titleLabel.drawsBackground = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
titleLabel.alignment = .center
titleLabel.usesSingleLineMode = true
titleLabel.lineBreakMode = .byTruncatingMiddle
let chevronView = NSImageView()
let chevronImage = NSImage(
systemSymbolName: "chevron.down",
accessibilityDescription: nil
)
let symbolConfig = NSImage.SymbolConfiguration(
pointSize: 8 * fontScale, weight: .semibold
)
chevronView.image = chevronImage?.withSymbolConfiguration(symbolConfig)
chevronView.translatesAutoresizingMaskIntoConstraints = false
let stackView = NSStackView(views: [titleLabel, chevronView])
stackView.orientation = .horizontal
stackView.spacing = 2 * fontScale
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.alignment = .centerY
stackView.setHuggingPriority(.required, for: .horizontal)
button.addSubview(stackView)
container.addSubview(button)
NSLayoutConstraint.activate([
button.leadingAnchor.constraint(equalTo: container.leadingAnchor),
button.trailingAnchor.constraint(equalTo: container.trailingAnchor),
button.topAnchor.constraint(equalTo: container.topAnchor),
button.bottomAnchor.constraint(equalTo: container.bottomAnchor),
stackView.leadingAnchor.constraint(
equalTo: button.leadingAnchor, constant: 6 * fontScale
),
stackView.trailingAnchor.constraint(
equalTo: button.trailingAnchor, constant: -6 * fontScale
),
stackView.topAnchor.constraint(
equalTo: button.topAnchor, constant: 2 * fontScale
),
stackView.bottomAnchor.constraint(
equalTo: button.bottomAnchor, constant: -2 * fontScale
),
chevronView.widthAnchor.constraint(equalToConstant: 8 * fontScale),
chevronView.heightAnchor.constraint(equalToConstant: 8 * fontScale),
])
context.coordinator.button = button
context.coordinator.titleLabel = titleLabel
context.coordinator.chevronView = chevronView
// Setup tracking for hover
let trackingArea = NSTrackingArea(
rect: .zero,
options: [.mouseEnteredAndExited, .activeInActiveApp, .inVisibleRect],
owner: context.coordinator,
userInfo: nil
)
button.addTrackingArea(trackingArea)
context.coordinator.trackingArea = trackingArea
return container
}
func updateNSView(_ nsView: NSView, context: Context) {
guard let titleLabel = context.coordinator.titleLabel,
let button = context.coordinator.button,
let chevronView = context.coordinator.chevronView
else { return }
let label = selectedModelLabel
titleLabel.stringValue = label
titleLabel.font = NSFont.systemFont(ofSize: 13 * fontScale)
titleLabel.textColor = .labelColor
let chevronConfig = NSImage.SymbolConfiguration(
pointSize: 8 * fontScale, weight: .semibold
)
chevronView.image = NSImage(
systemSymbolName: "chevron.down",
accessibilityDescription: nil
)?.withSymbolConfiguration(chevronConfig)
chevronView.contentTintColor = .tertiaryLabelColor
// Update coordinator data
context.coordinator.selectedModel = selectedModel
context.coordinator.copilotModels = copilotModels
context.coordinator.byokModels = byokModels
context.coordinator.isBYOKFFEnabled = isBYOKFFEnabled
context.coordinator.currentCache = currentCache
context.coordinator.fontScale = fontScale
// Hover background
let isHovered = context.coordinator.isHovered
button.layer?.backgroundColor = isHovered
? NSColor.gray.withAlphaComponent(0.1).cgColor
: NSColor.clear.cgColor
button.layer?.cornerRadius = 5 * fontScale
button.layer?.cornerCurve = .continuous
// Ideal width based on text (allows shrinking when parent is tight)
let textWidth = labelWidth(label: label)
context.coordinator.widthConstraint?.constant = textWidth
if context.coordinator.widthConstraint == nil {
let wc = nsView.widthAnchor.constraint(lessThanOrEqualToConstant: textWidth)
wc.priority = .defaultHigh
wc.isActive = true
context.coordinator.widthConstraint = wc
}
// Report ideal width so SwiftUI can size us properly
if let container = nsView as? ModelPickerContainerView {
container.fontScale = fontScale
container.idealWidth = textWidth
container.invalidateIntrinsicContentSize()
}
}
func makeCoordinator() -> Coordinator {
Coordinator(
selectedModel: selectedModel,
copilotModels: copilotModels,
byokModels: byokModels,
isBYOKFFEnabled: isBYOKFFEnabled,
currentCache: currentCache,
fontScale: fontScale
)
}
private var selectedModelLabel: String {
let name = selectedModel?.displayName ?? selectedModel?.modelName ?? ""
if selectedModel?.degradationReason != nil {
return "\u{26A0} \(name)"
}
return name
}
private func labelWidth(label: String) -> CGFloat {
let font = NSFont.systemFont(ofSize: 13 * fontScale)
let attrs: [NSAttributedString.Key: Any] = [.font: font]
let textWidth = ceil((label as NSString).size(withAttributes: attrs).width)
// text + left padding(6) + right padding(6) + chevron(8) + stack spacing(2) + text field internal margin(6)
return textWidth + 28 * fontScale
}
// MARK: - Coordinator
class Coordinator: NSObject {
var selectedModel: LLMModel?
var copilotModels: [LLMModel]
var byokModels: [LLMModel]
var isBYOKFFEnabled: Bool
var currentCache: ScopeCache
var fontScale: Double
var button: NSButton?
var titleLabel: NSTextField?
var chevronView: NSImageView?
var trackingArea: NSTrackingArea?
var widthConstraint: NSLayoutConstraint?
var isHovered = false
init(
selectedModel: LLMModel?,
copilotModels: [LLMModel],
byokModels: [LLMModel],
isBYOKFFEnabled: Bool,
currentCache: ScopeCache,
fontScale: Double
) {
self.selectedModel = selectedModel
self.copilotModels = copilotModels
self.byokModels = byokModels
self.isBYOKFFEnabled = isBYOKFFEnabled
self.currentCache = currentCache
self.fontScale = fontScale
}
@objc func buttonClicked(_ sender: NSButton) {
let menuBuilder = ModelPickerMenu(
selectedModel: selectedModel,
copilotModels: copilotModels,
byokModels: byokModels,
isBYOKFFEnabled: isBYOKFFEnabled,
currentCache: currentCache,
fontScale: fontScale
)
menuBuilder.showMenu(relativeTo: sender)
}
@objc(mouseEntered:) func mouseEntered(with event: NSEvent) {
isHovered = true
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.15
button?.animator().layer?.backgroundColor = NSColor.gray
.withAlphaComponent(0.1).cgColor
}
NSCursor.pointingHand.push()
}
@objc(mouseExited:) func mouseExited(with event: NSEvent) {
isHovered = false
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.15
button?.animator().layer?.backgroundColor = NSColor.clear.cgColor
}
NSCursor.pop()
}
}
}
// MARK: - Container view that constrains intrinsic height
private class ModelPickerContainerView: NSView {
var fontScale: Double
var idealWidth: CGFloat = NSView.noIntrinsicMetric
init(fontScale: Double) {
self.fontScale = fontScale
super.init(frame: .zero)
setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
setContentHuggingPriority(.defaultHigh, for: .horizontal)
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: NSSize {
let height = 20 * fontScale
return NSSize(width: idealWidth, height: height)
}
}