1- import Environment
21import SwiftUI
2+ import XPCShared
33
44@MainActor
55final class SuggestionPanelViewModel : ObservableObject {
@@ -20,6 +20,7 @@ final class SuggestionPanelViewModel: ObservableObject {
2020 @Published var suggestion : Suggestion
2121 @Published var isPanelDisplayed : Bool
2222 @Published var alignTopToAnchor = false
23+ @Published var colorScheme : ColorScheme
2324
2425 var onAcceptButtonTapped : ( ( ) -> Void ) ?
2526 var onRejectButtonTapped : ( ( ) -> Void ) ?
@@ -29,13 +30,15 @@ final class SuggestionPanelViewModel: ObservableObject {
2930 public init (
3031 suggestion: Suggestion = . empty,
3132 isPanelDisplayed: Bool = false ,
33+ colorScheme: ColorScheme = . dark,
3234 onAcceptButtonTapped: ( ( ) -> Void ) ? = nil ,
3335 onRejectButtonTapped: ( ( ) -> Void ) ? = nil ,
3436 onPreviousButtonTapped: ( ( ) -> Void ) ? = nil ,
3537 onNextButtonTapped: ( ( ) -> Void ) ? = nil
3638 ) {
3739 self . suggestion = suggestion
3840 self . isPanelDisplayed = isPanelDisplayed
41+ self . colorScheme = colorScheme
3942 self . onAcceptButtonTapped = onAcceptButtonTapped
4043 self . onRejectButtonTapped = onRejectButtonTapped
4144 self . onPreviousButtonTapped = onPreviousButtonTapped
@@ -46,7 +49,8 @@ final class SuggestionPanelViewModel: ObservableObject {
4649struct SuggestionPanelView : View {
4750 @ObservedObject var viewModel : SuggestionPanelViewModel
4851 @State var codeHeight : Double = 0
49- let backgroundColor = #colorLiteral( red: 0.1580096483 , green: 0.1730263829 , blue: 0.2026666105 , alpha: 1 )
52+ // get color scheme
53+ @Environment ( \. colorScheme) var colorScheme
5054
5155 var body : some View {
5256 VStack {
@@ -60,8 +64,18 @@ struct SuggestionPanelView: View {
6064 VStack ( spacing: 0 ) {
6165 ScrollView {
6266 CodeBlock ( viewModel: viewModel)
67+ . frame ( maxWidth: . infinity)
6368 }
64- . background ( Color ( nsColor: backgroundColor) )
69+ . background ( Color ( nsColor: {
70+ switch viewModel. colorScheme {
71+ case . dark:
72+ return #colorLiteral( red: 0.1580096483 , green: 0.1730263829 , blue: 0.2026666105 , alpha: 1 )
73+ case . light:
74+ return . white
75+ @unknown default :
76+ return . white
77+ }
78+ } ( ) ) )
6579
6680 ToolBar ( viewModel: viewModel)
6781 }
@@ -79,7 +93,7 @@ struct SuggestionPanelView: View {
7993 . padding ( 1 )
8094 )
8195 . allowsHitTesting ( viewModel. isPanelDisplayed && !viewModel. suggestion. code. isEmpty)
82- . preferredColorScheme ( . dark )
96+ . preferredColorScheme ( viewModel . colorScheme )
8397
8498 if viewModel. alignTopToAnchor {
8599 Spacer ( )
@@ -106,7 +120,7 @@ struct CodeBlock: View {
106120 HStack ( alignment: . firstTextBaseline) {
107121 Text ( " \( index + viewModel. suggestion. startLineIndex + 1 ) " )
108122 . multilineTextAlignment ( . trailing)
109- . foregroundColor ( Color . white . opacity ( 0.6 ) )
123+ . foregroundColor ( . secondary )
110124 . frame ( minWidth: 40 )
111125 Text ( AttributedString ( viewModel. suggestion. code [ index] ) )
112126 . foregroundColor ( . white. opacity ( 0.1 ) )
@@ -128,13 +142,7 @@ struct ToolBar: View {
128142 var body : some View {
129143 HStack {
130144 Button ( action: {
131- Task {
132- if let block = viewModel. onPreviousButtonTapped {
133- block ( )
134- return
135- }
136- try await Environment . triggerAction ( " Previous Suggestion " )
137- }
145+ viewModel. onPreviousButtonTapped ? ( )
138146 } ) {
139147 Image ( systemName: " chevron.left " )
140148 } . buttonStyle ( . plain)
@@ -145,39 +153,21 @@ struct ToolBar: View {
145153 . monospacedDigit ( )
146154
147155 Button ( action: {
148- Task {
149- if let block = viewModel. onNextButtonTapped {
150- block ( )
151- return
152- }
153- try await Environment . triggerAction ( " Next Suggestion " )
154- }
156+ viewModel. onNextButtonTapped ? ( )
155157 } ) {
156158 Image ( systemName: " chevron.right " )
157159 } . buttonStyle ( . plain)
158160
159161 Spacer ( )
160162
161163 Button ( action: {
162- Task {
163- if let block = viewModel. onRejectButtonTapped {
164- block ( )
165- return
166- }
167- try await Environment . triggerAction ( " Reject Suggestion " )
168- }
164+ viewModel. onRejectButtonTapped ? ( )
169165 } ) {
170166 Text ( " Reject " )
171167 } . buttonStyle ( CommandButtonStyle ( color: . gray) )
172168
173169 Button ( action: {
174- Task {
175- if let block = viewModel. onAcceptButtonTapped {
176- block ( )
177- return
178- }
179- try await Environment . triggerAction ( " Accept Suggestion " )
180- }
170+ viewModel. onAcceptButtonTapped ? ( )
181171 } ) {
182172 Text ( " Accept " )
183173 } . buttonStyle ( CommandButtonStyle ( color: . indigo) )
@@ -208,7 +198,41 @@ struct CommandButtonStyle: ButtonStyle {
208198 }
209199}
210200
211- struct SuggestionPanelView_Preview : PreviewProvider {
201+ struct SuggestionPanelView_Dark_Preview : PreviewProvider {
202+ static var previews : some View {
203+ SuggestionPanelView ( viewModel: . init(
204+ suggestion: . init(
205+ startLineIndex: 8 ,
206+ code: highlighted (
207+ code: """
208+ LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
209+ ForEach(0..<viewModel.suggestion.count, id: \\ .self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
210+ Text(viewModel.suggestion[index])
211+ .frame(maxWidth: .infinity, alignment: .leading)
212+ .multilineTextAlignment(.leading)
213+ }
214+ """ ,
215+ language: " swift " ,
216+ brightMode: false
217+ ) ,
218+ suggestionCount: 2 ,
219+ currentSuggestionIndex: 0
220+ ) ,
221+ isPanelDisplayed: true ,
222+ colorScheme: . dark
223+ ) )
224+ . frame ( width: 450 , height: 400 )
225+ . background {
226+ HStack {
227+ Color . red
228+ Color . green
229+ Color . blue
230+ }
231+ }
232+ }
233+ }
234+
235+ struct SuggestionPanelView_Bright_Preview : PreviewProvider {
212236 static var previews : some View {
213237 SuggestionPanelView ( viewModel: . init(
214238 suggestion: . init(
@@ -222,12 +246,14 @@ struct SuggestionPanelView_Preview: PreviewProvider {
222246 .multilineTextAlignment(.leading)
223247 }
224248 """ ,
225- language: " swift "
249+ language: " swift " ,
250+ brightMode: true
226251 ) ,
227252 suggestionCount: 2 ,
228253 currentSuggestionIndex: 0
229254 ) ,
230- isPanelDisplayed: true
255+ isPanelDisplayed: true ,
256+ colorScheme: . light
231257 ) )
232258 . frame ( width: 450 , height: 400 )
233259 . background {
0 commit comments