Skip to content

Commit 6798594

Browse files
committed
Merge branch 'release/0.8.5'
2 parents d0df63c + f89b190 commit 6798594

File tree

7 files changed

+321
-318
lines changed

7 files changed

+321
-318
lines changed

Core/Sources/SuggestionWidget/SuggestionPanelView.swift

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@ import SwiftUI
33

44
@MainActor
55
final 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

4146
struct 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,44 +89,36 @@ 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

100100
struct 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 {
112-
LazyVGrid(columns: [
113-
GridItem(.fixed(30), alignment: .top),
114-
GridItem(.flexible()),
115-
], spacing: 4) {
116-
ForEach(0..<viewModel.suggestion.count, id: \.self) { index in
117-
Text("\(index + viewModel.startLineIndex + 1)")
118-
.foregroundColor(Color.white.opacity(0.6))
119-
Text(AttributedString(viewModel.suggestion[index]))
120-
.foregroundColor(.white.opacity(0.1))
121-
.frame(maxWidth: .infinity, alignment: .leading)
122-
.multilineTextAlignment(.leading)
123-
.lineSpacing(4)
104+
VStack {
105+
ForEach(0..<viewModel.suggestion.code.endIndex, id: \.self) { index in
106+
HStack(alignment: .firstTextBaseline) {
107+
Text("\(index + viewModel.suggestion.startLineIndex + 1)")
108+
.multilineTextAlignment(.trailing)
109+
.foregroundColor(Color.white.opacity(0.6))
110+
.frame(minWidth: 40)
111+
Text(AttributedString(viewModel.suggestion.code[index]))
112+
.foregroundColor(.white.opacity(0.1))
113+
.frame(maxWidth: .infinity, alignment: .leading)
114+
.multilineTextAlignment(.leading)
115+
.lineSpacing(4)
116+
}
124117
}
125118
}
126119
.foregroundColor(.white)
127120
.font(.system(size: 12, design: .monospaced))
128121
.padding()
129-
.background(GeometryReader(content: { proxy in
130-
Color.clear.preference(key: SizePreferenceKey.self, value: proxy.size)
131-
}))
132122
}
133123
}
134124

@@ -149,8 +139,10 @@ struct ToolBar: View {
149139
Image(systemName: "chevron.left")
150140
}.buttonStyle(.plain)
151141

152-
Text("\(viewModel.currentSuggestionIndex + 1) / \(viewModel.suggestionCount)")
153-
.monospacedDigit()
142+
Text(
143+
"\(viewModel.suggestion.currentSuggestionIndex + 1) / \(viewModel.suggestion.suggestionCount)"
144+
)
145+
.monospacedDigit()
154146

155147
Button(action: {
156148
Task {
@@ -219,18 +211,21 @@ struct CommandButtonStyle: ButtonStyle {
219211
struct SuggestionPanelView_Preview: PreviewProvider {
220212
static var previews: some View {
221213
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"
214+
suggestion: .init(
215+
startLineIndex: 8,
216+
code: highlighted(
217+
code: """
218+
LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
219+
ForEach(0..<viewModel.suggestion.count, id: \\.self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
220+
Text(viewModel.suggestion[index])
221+
.frame(maxWidth: .infinity, alignment: .leading)
222+
.multilineTextAlignment(.leading)
223+
}
224+
""",
225+
language: "swift"
226+
),
227+
suggestionCount: 2,
228+
currentSuggestionIndex: 0
234229
),
235230
isPanelDisplayed: true
236231
))

0 commit comments

Comments
 (0)