@@ -3,11 +3,22 @@ import SwiftUI
33
44@MainActor
55final class SuggestionPanelViewModel : ObservableObject {
6- @Published var startLineIndex : Int
7- @Published var suggestion : [ NSAttributedString ]
6+ struct Suggestion : Equatable {
7+ var startLineIndex : Int
8+ var code : [ NSAttributedString ]
9+ var suggestionCount : Int
10+ var currentSuggestionIndex : Int
11+
12+ static let empty = Suggestion (
13+ startLineIndex: 0 ,
14+ code: [ ] ,
15+ suggestionCount: 0 ,
16+ currentSuggestionIndex: 0
17+ )
18+ }
19+
20+ @Published var suggestion : Suggestion
821 @Published var isPanelDisplayed : Bool
9- @Published var suggestionCount : Int
10- @Published var currentSuggestionIndex : Int
1122 @Published var alignTopToAnchor = false
1223
1324 var onAcceptButtonTapped : ( ( ) -> Void ) ?
@@ -16,21 +27,15 @@ final class SuggestionPanelViewModel: ObservableObject {
1627 var onNextButtonTapped : ( ( ) -> Void ) ?
1728
1829 public init (
19- startLineIndex: Int = 0 ,
20- suggestion: [ NSAttributedString ] = [ ] ,
30+ suggestion: Suggestion = . empty,
2131 isPanelDisplayed: Bool = false ,
22- suggestionCount: Int = 0 ,
23- currentSuggestionIndex: Int = 0 ,
2432 onAcceptButtonTapped: ( ( ) -> Void ) ? = nil ,
2533 onRejectButtonTapped: ( ( ) -> Void ) ? = nil ,
2634 onPreviousButtonTapped: ( ( ) -> Void ) ? = nil ,
2735 onNextButtonTapped: ( ( ) -> Void ) ? = nil
2836 ) {
29- self . startLineIndex = startLineIndex
3037 self . suggestion = suggestion
3138 self . isPanelDisplayed = isPanelDisplayed
32- self . suggestionCount = suggestionCount
33- self . currentSuggestionIndex = currentSuggestionIndex
3439 self . onAcceptButtonTapped = onAcceptButtonTapped
3540 self . onRejectButtonTapped = onRejectButtonTapped
3641 self . onPreviousButtonTapped = onPreviousButtonTapped
@@ -40,7 +45,6 @@ final class SuggestionPanelViewModel: ObservableObject {
4045
4146struct SuggestionPanelView : View {
4247 @ObservedObject var viewModel : SuggestionPanelViewModel
43- @State var isHovering : Bool = false
4448 @State var codeHeight : Double = 0
4549 let backgroundColor = #colorLiteral( red: 0.1580096483 , green: 0.1730263829 , blue: 0.2026666105 , alpha: 1 )
4650
@@ -74,13 +78,7 @@ struct SuggestionPanelView: View {
7478 . stroke ( Color . white. opacity ( 0.2 ) , style: . init( lineWidth: 1 ) )
7579 . padding ( 1 )
7680 )
77-
78- . onHover { yes in
79- withAnimation ( . easeInOut( duration: 0.2 ) ) {
80- isHovering = yes
81- }
82- }
83- . allowsHitTesting ( viewModel. isPanelDisplayed && !viewModel. suggestion. isEmpty)
81+ . allowsHitTesting ( viewModel. isPanelDisplayed && !viewModel. suggestion. code. isEmpty)
8482 . preferredColorScheme ( . dark)
8583
8684 if viewModel. alignTopToAnchor {
@@ -91,32 +89,26 @@ struct SuggestionPanelView: View {
9189 }
9290 . opacity ( {
9391 guard viewModel. isPanelDisplayed else { return 0 }
94- guard !viewModel. suggestion. isEmpty else { return 0 }
92+ guard !viewModel. suggestion. code . isEmpty else { return 0 }
9593 return 1
9694 } ( ) )
95+ . animation ( . easeInOut( duration: 0.2 ) , value: viewModel. suggestion)
96+ . animation ( . easeInOut( duration: 0.2 ) , value: viewModel. isPanelDisplayed)
9797 }
9898}
9999
100100struct CodeBlock : View {
101- struct SizePreferenceKey : PreferenceKey {
102- public static var defaultValue : CGSize = . zero
103- public static func reduce( value: inout CGSize , nextValue: ( ) -> CGSize ) {
104- value = value. width + value. height > nextValue ( ) . width + nextValue( )
105- . height ? value : nextValue ( )
106- }
107- }
108-
109101 @ObservedObject var viewModel : SuggestionPanelViewModel
110102
111103 var body : some View {
112104 LazyVGrid ( columns: [
113105 GridItem ( . fixed( 30 ) , alignment: . top) ,
114106 GridItem ( . flexible( ) ) ,
115107 ] , spacing: 4 ) {
116- ForEach ( 0 ..< viewModel. suggestion. count , id: \. self) { index in
117- Text ( " \( index + viewModel. startLineIndex + 1 ) " )
108+ ForEach ( 0 ..< viewModel. suggestion. code . endIndex , id: \. self) { index in
109+ Text ( " \( index + viewModel. suggestion . startLineIndex + 1 ) " )
118110 . foregroundColor ( Color . white. opacity ( 0.6 ) )
119- Text ( AttributedString ( viewModel. suggestion [ index] ) )
111+ Text ( AttributedString ( viewModel. suggestion. code [ index] ) )
120112 . foregroundColor ( . white. opacity ( 0.1 ) )
121113 . frame ( maxWidth: . infinity, alignment: . leading)
122114 . multilineTextAlignment ( . leading)
@@ -126,9 +118,6 @@ struct CodeBlock: View {
126118 . foregroundColor ( . white)
127119 . font ( . system( size: 12 , design: . monospaced) )
128120 . padding ( )
129- . background ( GeometryReader ( content: { proxy in
130- Color . clear. preference ( key: SizePreferenceKey . self, value: proxy. size)
131- } ) )
132121 }
133122}
134123
@@ -149,8 +138,10 @@ struct ToolBar: View {
149138 Image ( systemName: " chevron.left " )
150139 } . buttonStyle ( . plain)
151140
152- Text ( " \( viewModel. currentSuggestionIndex + 1 ) / \( viewModel. suggestionCount) " )
153- . monospacedDigit ( )
141+ if let suggestion = viewModel. suggestion {
142+ Text ( " \( suggestion. currentSuggestionIndex + 1 ) / \( suggestion. suggestionCount) " )
143+ . monospacedDigit ( )
144+ }
154145
155146 Button ( action: {
156147 Task {
@@ -219,18 +210,21 @@ struct CommandButtonStyle: ButtonStyle {
219210struct SuggestionPanelView_Preview : PreviewProvider {
220211 static var previews : some View {
221212 SuggestionPanelView ( viewModel: . init(
222- startLineIndex: 8 ,
223- suggestion:
224- highlighted (
225- code: """
226- LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
227- ForEach(0..<viewModel.suggestion.count, id: \\ .self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
228- Text(viewModel.suggestion[index])
229- .frame(maxWidth: .infinity, alignment: .leading)
230- .multilineTextAlignment(.leading)
231- }
232- """ ,
233- language: " swift "
213+ suggestion: . init(
214+ startLineIndex: 8 ,
215+ code: highlighted (
216+ code: """
217+ LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
218+ ForEach(0..<viewModel.suggestion.count, id: \\ .self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
219+ Text(viewModel.suggestion[index])
220+ .frame(maxWidth: .infinity, alignment: .leading)
221+ .multilineTextAlignment(.leading)
222+ }
223+ """ ,
224+ language: " swift "
225+ ) ,
226+ suggestionCount: 2 ,
227+ currentSuggestionIndex: 0
234228 ) ,
235229 isPanelDisplayed: true
236230 ) )
0 commit comments