-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathChatWindowView.swift
More file actions
561 lines (499 loc) · 18.5 KB
/
ChatWindowView.swift
File metadata and controls
561 lines (499 loc) · 18.5 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import ActiveApplicationMonitor
import ConversationTab
import AppKit
import ChatTab
import ComposableArchitecture
import SwiftUI
import SharedUIComponents
import GitHubCopilotViewModel
import Status
import ChatService
import Workspace
private let r: Double = 8
struct ChatWindowView: View {
let store: StoreOf<ChatPanelFeature>
let toggleVisibility: (Bool) -> Void
@State private var isChatHistoryVisible: Bool = false
@ObservedObject private var statusObserver = StatusObserver.shared
var body: some View {
WithPerceptionTracking {
// Force re-evaluation when workspace state changes
let currentWorkspace = store.currentChatWorkspace
let _ = currentWorkspace?.selectedTabId
ZStack {
if statusObserver.observedAXStatus == .notGranted {
ChatNoAXPermissionView()
} else {
switch statusObserver.authStatus.status {
case .loggedIn:
if currentWorkspace == nil || (currentWorkspace?.tabInfo.isEmpty ?? true) {
ChatNoWorkspaceView()
} else if isChatHistoryVisible {
ChatHistoryViewWrapper(store: store, isChatHistoryVisible: $isChatHistoryVisible)
} else {
ChatView(store: store, isChatHistoryVisible: $isChatHistoryVisible)
}
case .notLoggedIn:
ChatLoginView(viewModel: GitHubCopilotViewModel.shared)
case .notAuthorized:
ChatNoSubscriptionView(viewModel: GitHubCopilotViewModel.shared)
case .unknown:
ChatLoginView(viewModel: GitHubCopilotViewModel.shared)
}
}
}
.onChange(of: store.isPanelDisplayed) { isDisplayed in
toggleVisibility(isDisplayed)
}
.preferredColorScheme(store.colorScheme)
}
}
}
struct ChatView: View {
let store: StoreOf<ChatPanelFeature>
@Binding var isChatHistoryVisible: Bool
var body: some View {
VStack(spacing: 0) {
Rectangle()
.fill(Color.chatWindowBackgroundColor)
.scaledFrame(height: 28)
VStack(spacing: 0) {
ChatBar(store: store, isChatHistoryVisible: $isChatHistoryVisible)
.scaledFrame(height: 32)
.scaledPadding(.leading, 16)
.scaledPadding(.trailing, 8)
Divider()
ChatTabContainer(store: store)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.xcodeStyleFrame()
.ignoresSafeArea(edges: .top)
}
}
struct ChatHistoryViewWrapper: View {
let store: StoreOf<ChatPanelFeature>
@Binding var isChatHistoryVisible: Bool
var body: some View {
WithPerceptionTracking {
VStack(spacing: 0) {
Rectangle()
.fill(Color.chatWindowBackgroundColor)
.scaledFrame(height: 28)
ChatHistoryView(
store: store,
isChatHistoryVisible: $isChatHistoryVisible
)
.background(Color.chatWindowBackgroundColor)
.frame(
maxWidth: .infinity,
maxHeight: .infinity
)
}
.xcodeStyleFrame()
.ignoresSafeArea(edges: .top)
.preferredColorScheme(store.colorScheme)
.focusable()
.onExitCommand(perform: {
isChatHistoryVisible = false
})
}
}
}
struct ChatLoadingView: View {
var body: some View {
VStack(alignment: .center) {
Spacer()
VStack(spacing: 24) {
Instruction(isAgentMode: .constant(false))
ProgressView("Loading...")
}
.frame(maxWidth: .infinity, alignment: .center)
// keep same as chat view
.padding(.top, 20) // chat bar
Spacer()
}
.xcodeStyleFrame()
.ignoresSafeArea(edges: .top)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(.ultraThinMaterial)
}
}
struct ChatTitleBar: View {
let store: StoreOf<ChatPanelFeature>
@State var isHovering = false
@AppStorage(\.autoAttachChatToXcode) var autoAttachChatToXcode
var body: some View {
WithPerceptionTracking {
HStack(spacing: 6) {
Button(action: {
store.send(.closeActiveTabClicked)
}) {
EmptyView()
}
.opacity(0)
.keyboardShortcut("w", modifiers: [.command])
Button(
action: {
store.send(.hideButtonClicked)
}
) {
Image(systemName: "minus")
.foregroundStyle(.black.opacity(0.5))
.scaledFont(Font.system(size: 8).weight(.heavy))
}
.opacity(0)
.keyboardShortcut("m", modifiers: [.command])
Spacer()
if !autoAttachChatToXcode {
TrafficLightButton(
isHovering: isHovering,
isActive: store.isDetached,
color: Color(nsColor: .systemCyan),
action: {
store.send(.toggleChatPanelDetachedButtonClicked)
}
) {
Image(systemName: "pin.fill")
.foregroundStyle(.black.opacity(0.5))
.scaledFont(Font.system(size: 6).weight(.black))
.transformEffect(.init(translationX: 0, y: 0.5))
}
}
}
.buttonStyle(.plain)
.padding(.trailing, 8)
.onHover(perform: { hovering in
isHovering = hovering
})
}
}
struct TrafficLightButton<Icon: View>: View {
let isHovering: Bool
let isActive: Bool
let color: Color
let action: () -> Void
let icon: () -> Icon
@Environment(\.controlActiveState) var controlActiveState
var body: some View {
Button(action: {
action()
}) {
Circle()
.fill(
controlActiveState == .key && isActive
? color
: Color(nsColor: .separatorColor)
)
.scaledFrame(
width: Style.trafficLightButtonSize,
height: Style.trafficLightButtonSize
)
.overlay {
Circle().stroke(lineWidth: 0.5).foregroundColor(.black.opacity(0.2))
}
.overlay {
if isHovering {
icon()
}
}
}
.focusable(false)
}
}
}
private extension View {
func hideScrollIndicator() -> some View {
scrollIndicators(.hidden)
}
}
struct ChatBar: View {
let store: StoreOf<ChatPanelFeature>
@Binding var isChatHistoryVisible: Bool
struct TabBarState: Equatable {
var tabInfo: IdentifiedArray<String, ChatTabInfo>
var selectedTabId: String
}
var body: some View {
WithPerceptionTracking {
HStack(spacing: 8) {
if store.chatHistory.selectedWorkspaceName != nil {
ChatWindowHeader(store: store)
}
Spacer()
CreateButton(store: store)
ChatHistoryButton(store: store, isChatHistoryVisible: $isChatHistoryVisible)
SettingsButton(store: store)
}
}
}
struct Tabs: View {
let store: StoreOf<ChatPanelFeature>
@Environment(\.chatTabPool) var chatTabPool
var body: some View {
WithPerceptionTracking {
let tabInfo = store.currentChatWorkspace?.tabInfo
let selectedTabId = store.currentChatWorkspace?.selectedTabId
?? store.currentChatWorkspace?.tabInfo.first?.id
?? ""
ScrollViewReader { proxy in
ScrollView(.horizontal) {
HStack(spacing: 0) {
ForEach(tabInfo!, id: \.id) { info in
if let tab = chatTabPool.getTab(of: info.id) {
ChatTabBarButton(
store: store,
info: info,
content: { tab.tabItem },
icon: { tab.icon },
isSelected: info.id == selectedTabId
)
.contextMenu {
tab.menu
}
.id(info.id)
} else {
EmptyView()
}
}
}
}
.hideScrollIndicator()
.onChange(of: selectedTabId) { id in
withAnimation(.easeInOut(duration: 0.2)) {
proxy.scrollTo(id)
}
}
}
}
}
}
struct ChatWindowHeader: View {
let store: StoreOf<ChatPanelFeature>
var body: some View {
WithPerceptionTracking {
HStack(spacing: 0) {
Image("XcodeIcon")
.resizable()
.renderingMode(.original)
.scaledToFit()
.scaledFrame(width: 24, height: 24)
Text(store.chatHistory.selectedWorkspaceName!)
.scaledFont(size: 13, weight: .bold)
.scaledPadding(.leading, 4)
.truncationMode(.tail)
.scaledFrame(maxWidth: 192, alignment: .leading)
.help(store.chatHistory.selectedWorkspacePath!)
}
}
}
}
struct CreateButton: View {
let store: StoreOf<ChatPanelFeature>
var body: some View {
WithPerceptionTracking {
Button(action: {
store.send(.createNewTapButtonClicked(kind: nil))
}) {
Image(systemName: "plus.bubble")
.scaledFont(.body)
}
.buttonStyle(HoverButtonStyle())
.help("New Chat")
.accessibilityLabel("New Chat")
}
}
}
struct ChatHistoryButton: View {
let store: StoreOf<ChatPanelFeature>
@Binding var isChatHistoryVisible: Bool
var body: some View {
WithPerceptionTracking {
Button(action: {
isChatHistoryVisible = true
}) {
if #available(macOS 15.0, *) {
Image(systemName: "clock.arrow.trianglehead.counterclockwise.rotate.90")
.scaledFont(.body)
} else {
Image(systemName: "clock.arrow.circlepath")
.scaledFont(.body)
}
}
.buttonStyle(HoverButtonStyle())
.help("Show Chats...")
.accessibilityLabel("Show Chats...")
}
}
}
struct SettingsButton: View {
let store: StoreOf<ChatPanelFeature>
var body: some View {
WithPerceptionTracking {
Button(action: {
store.send(.openSettings)
}) {
Image(systemName: "gearshape")
.scaledFont(.body)
}
.buttonStyle(HoverButtonStyle())
.help("Open Settings")
.accessibilityLabel("Open Settings")
}
}
}
}
struct ChatTabBarButton<Content: View, Icon: View>: View {
let store: StoreOf<ChatPanelFeature>
let info: ChatTabInfo
let content: () -> Content
let icon: () -> Icon
let isSelected: Bool
@State var isHovered: Bool = false
var body: some View {
if self.isSelected {
HStack(spacing: 0) {
HStack(spacing: 0) {
icon()
.buttonStyle(.plain)
}
.font(.callout)
.lineLimit(1)
}
.frame(maxHeight: .infinity)
}
}
}
struct ChatTabContainer: View {
let store: StoreOf<ChatPanelFeature>
@Environment(\.chatTabPool) var chatTabPool
@State private var pasteMonitor: Any?
var body: some View {
WithPerceptionTracking {
let tabInfoArray = store.currentChatWorkspace?.tabInfo
let selectedTabId = store.currentChatWorkspace?.selectedTabId
?? store.currentChatWorkspace?.tabInfo.first?.id
?? ""
if let tabInfoArray = tabInfoArray, !tabInfoArray.isEmpty {
activeTabsView(
tabInfoArray: tabInfoArray,
selectedTabId: selectedTabId
)
} else {
// Fallback view for empty state (rarely seen in practice)
EmptyView().frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.onAppear {
setupPasteMonitor()
}
.onDisappear {
removePasteMonitor()
}
}
// View displayed when there are active tabs
private func activeTabsView(
tabInfoArray: IdentifiedArray<String, ChatTabInfo>,
selectedTabId: String
) -> some View {
GeometryReader { geometry in
if tabInfoArray[id: selectedTabId] != nil,
let tab = chatTabPool.getTab(of: selectedTabId) {
tab.body
.frame(
width: geometry.size.width,
height: geometry.size.height
)
} else {
// Fallback if selected tab is not found
EmptyView()
}
}
}
private func setupPasteMonitor() {
pasteMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event in
guard event.modifierFlags.contains(.command),
event.charactersIgnoringModifiers?.lowercased() == "v" else {
return event
}
// Find the active chat tab and forward paste event to it
if let activeConversationTab = getActiveConversationTab() {
if !activeConversationTab.handlePasteEvent() {
return event
}
}
return nil
}
}
private func removePasteMonitor() {
if let monitor = pasteMonitor {
NSEvent.removeMonitor(monitor)
pasteMonitor = nil
}
}
private func getActiveConversationTab() -> ConversationTab? {
guard let selectedTabId = store.currentChatWorkspace?.selectedTabId,
let chatTab = chatTabPool.getTab(of: selectedTabId) as? ConversationTab else {
return nil
}
return chatTab
}
}
struct CreateOtherChatTabMenuStyle: MenuStyle {
func makeBody(configuration: Configuration) -> some View {
Image(systemName: "chevron.down")
.resizable()
.scaledFrame(width: 7, height: 4)
.frame(maxHeight: .infinity)
.padding(.leading, 4)
.padding(.trailing, 8)
.foregroundColor(.secondary)
}
}
struct ChatWindowView_Previews: PreviewProvider {
static let pool = ChatTabPool([
"2": EmptyChatTab(id: "2"),
"3": EmptyChatTab(id: "3"),
"4": EmptyChatTab(id: "4"),
"5": EmptyChatTab(id: "5"),
"6": EmptyChatTab(id: "6"),
"7": EmptyChatTab(id: "7"),
])
static func createStore() -> StoreOf<ChatPanelFeature> {
StoreOf<ChatPanelFeature>(
initialState: .init(
chatHistory: .init(
workspaces: [
.init(
id: .init(path: "p", username: "u"),
tabInfo: [
.init(id: "2", title: "Empty-2", workspacePath: "path", username: "username"),
.init(id: "3", title: "Empty-3", workspacePath: "path", username: "username"),
.init(id: "4", title: "Empty-4", workspacePath: "path", username: "username"),
.init(id: "5", title: "Empty-5", workspacePath: "path", username: "username"),
.init(id: "6", title: "Empty-6", workspacePath: "path", username: "username"),
.init(id: "7", title: "Empty-7", workspacePath: "path", username: "username"),
] as IdentifiedArray<String, ChatTabInfo>,
selectedTabId: "2"
) { _ in }
] as IdentifiedArray<WorkspaceIdentifier, ChatWorkspace>,
selectedWorkspacePath: "activeWorkspacePath",
selectedWorkspaceName: "activeWorkspacePath"
),
isPanelDisplayed: true
),
reducer: { ChatPanelFeature() }
)
}
static var previews: some View {
ChatWindowView(store: createStore(), toggleVisibility: { _ in })
.xcodeStyleFrame()
.padding()
.environment(\.chatTabPool, pool)
}
}
struct ChatLoadingView_Previews: PreviewProvider {
static var previews: some View {
ChatLoadingView()
}
}