@@ -2,7 +2,6 @@ import MarkdownUI
22import SwiftUI
33
44struct ChatPanel : View {
5- @ObservedObject var viewModel : SuggestionPanelViewModel
65 @ObservedObject var chat : ChatProvider
76 @Namespace var inputAreaNamespace
87 @State var typedMessage = " "
@@ -13,8 +12,7 @@ struct ChatPanel: View {
1312 Divider ( )
1413 ChatPanelMessages (
1514 chat: chat,
16- inputAreaNamespace: inputAreaNamespace,
17- colorScheme: viewModel. colorScheme
15+ inputAreaNamespace: inputAreaNamespace
1816 )
1917 Divider ( )
2018 ChatPanelInputArea (
@@ -30,10 +28,18 @@ struct ChatPanel: View {
3028}
3129
3230struct ChatPanelToolbar : View {
33- let chat : ChatProvider
31+ @ObservedObject var chat : ChatProvider
32+ @AppStorage ( \. useGlobalChat) var useGlobalChat
3433
3534 var body : some View {
3635 HStack {
36+ Toggle ( isOn: . init( get: {
37+ useGlobalChat
38+ } , set: { _ in
39+ chat. switchContext ( )
40+ } ) ) { EmptyView ( ) }
41+ . toggleStyle ( GlobalChatSwitchToggleStyle ( ) )
42+
3743 Spacer ( )
3844
3945 Button ( action: {
@@ -45,7 +51,8 @@ struct ChatPanelToolbar: View {
4551 }
4652 . buttonStyle ( . plain)
4753 }
48- . padding ( . horizontal, 4 )
54+ . padding ( . leading, 8 )
55+ . padding ( . trailing, 4 )
4956 . padding ( . vertical, 4 )
5057 . background ( . regularMaterial)
5158 }
@@ -54,7 +61,7 @@ struct ChatPanelToolbar: View {
5461struct ChatPanelMessages : View {
5562 @ObservedObject var chat : ChatProvider
5663 var inputAreaNamespace : Namespace . ID
57- var colorScheme : ColorScheme
64+ @ Environment ( \ . colorScheme ) var colorScheme
5865 @AppStorage ( \. disableLazyVStack) var disableLazyVStack
5966
6067 @ViewBuilder
@@ -74,7 +81,7 @@ struct ChatPanelMessages: View {
7481 ScrollView {
7582 vstack {
7683 let r = 6 as Double
77-
84+
7885 Spacer ( )
7986
8087 if chat. isReceivingMessage {
@@ -112,7 +119,6 @@ struct ChatPanelMessages: View {
112119 ForEach ( chat. history. reversed ( ) , id: \. id) { message in
113120 let text = message. text. isEmpty && !message. isUser ? " ... " : message
114121 . text
115-
116122
117123 if message. isUser {
118124 Markdown ( text)
@@ -310,6 +316,42 @@ struct RoundedCorners: Shape {
310316 }
311317}
312318
319+ struct GlobalChatSwitchToggleStyle : ToggleStyle {
320+ func makeBody( configuration: Configuration ) -> some View {
321+ HStack ( spacing: 4 ) {
322+ RoundedRectangle ( cornerRadius: 10 , style: . circular)
323+ . foregroundColor ( configuration. isOn ? Color . indigo : . gray. opacity ( 0.5 ) )
324+ . frame ( width: 30 , height: 20 , alignment: . center)
325+ . overlay (
326+ Circle ( )
327+ . fill ( . regularMaterial)
328+ . padding ( . all, 2 )
329+ . overlay (
330+ Image (
331+ systemName: configuration
332+ . isOn ? " globe " : " doc.circle "
333+ )
334+ . resizable ( )
335+ . aspectRatio ( contentMode: . fit)
336+ . frame ( width: 12 , height: 12 , alignment: . center)
337+ . foregroundStyle ( . secondary)
338+ )
339+ . offset ( x: configuration. isOn ? 5 : - 5 , y: 0 )
340+ . animation ( . linear( duration: 0.1 ) , value: configuration. isOn)
341+
342+ )
343+ . onTapGesture { configuration. isOn. toggle ( ) }
344+ . overlay {
345+ RoundedRectangle ( cornerRadius: 10 , style: . circular)
346+ . stroke ( . black. opacity ( 0.2 ) , lineWidth: 1 )
347+ }
348+
349+ Text ( configuration. isOn ? " Global Chat " : " File Chat " )
350+ . foregroundStyle ( . tertiary)
351+ }
352+ }
353+ }
354+
313355// MARK: - Previews
314356
315357struct ChatPanel_Preview : PreviewProvider {
@@ -343,9 +385,7 @@ struct ChatPanel_Preview: PreviewProvider {
343385 ]
344386
345387 static var previews : some View {
346- ChatPanel ( viewModel: . init(
347- isPanelDisplayed: true
348- ) , chat: . init(
388+ ChatPanel ( chat: . init(
349389 history: ChatPanel_Preview . history,
350390 isReceivingMessage: true
351391 ) )
@@ -356,9 +396,7 @@ struct ChatPanel_Preview: PreviewProvider {
356396
357397struct ChatPanel_EmptyChat_Preview : PreviewProvider {
358398 static var previews : some View {
359- ChatPanel ( viewModel: . init(
360- isPanelDisplayed: true
361- ) , chat: . init(
399+ ChatPanel ( chat: . init(
362400 history: [ ] ,
363401 isReceivingMessage: false
364402 ) )
@@ -388,9 +426,7 @@ struct ChatCodeSyntaxHighlighter: CodeSyntaxHighlighter {
388426
389427struct ChatPanel_InputText_Preview : PreviewProvider {
390428 static var previews : some View {
391- ChatPanel ( viewModel: . init(
392- isPanelDisplayed: true
393- ) , chat: . init(
429+ ChatPanel ( chat: . init(
394430 history: ChatPanel_Preview . history,
395431 isReceivingMessage: false
396432 ) )
@@ -403,9 +439,6 @@ struct ChatPanel_InputText_Preview: PreviewProvider {
403439struct ChatPanel_InputMultilineText_Preview : PreviewProvider {
404440 static var previews : some View {
405441 ChatPanel (
406- viewModel: . init(
407- isPanelDisplayed: true
408- ) ,
409442 chat: . init(
410443 history: ChatPanel_Preview . history,
411444 isReceivingMessage: false
@@ -420,10 +453,7 @@ struct ChatPanel_InputMultilineText_Preview: PreviewProvider {
420453
421454struct ChatPanel_Light_Preview : PreviewProvider {
422455 static var previews : some View {
423- ChatPanel ( viewModel: . init(
424- isPanelDisplayed: true ,
425- colorScheme: . light
426- ) , chat: . init(
456+ ChatPanel ( chat: . init(
427457 history: ChatPanel_Preview . history,
428458 isReceivingMessage: true
429459 ) )
0 commit comments