Skip to content

Commit 6ba7156

Browse files
committed
Use pseudo command handler in suggestion panel
1 parent 54ffb03 commit 6ba7156

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
import AppKit
22
import SuggestionWidget
3+
import Environment
34

45
@MainActor
56
public final class GraphicalUserInterfaceController {
67
public nonisolated static let shared = GraphicalUserInterfaceController()
78
nonisolated let realtimeSuggestionIndicatorController = RealtimeSuggestionIndicatorController()
89
nonisolated let suggestionWidget = SuggestionWidgetController()
9-
private nonisolated init() {}
10+
private nonisolated init() {
11+
Task { @MainActor in
12+
suggestionWidget.onNextButtonTapped = {
13+
Task { @ServiceActor in
14+
let handler = PseudoCommandHandler()
15+
await handler.presentNextSuggestion()
16+
}
17+
}
18+
19+
suggestionWidget.onPreviousButtonTapped = {
20+
Task { @ServiceActor in
21+
let handler = PseudoCommandHandler()
22+
await handler.presentPreviousSuggestion()
23+
}
24+
}
25+
26+
suggestionWidget.onRejectButtonTapped = {
27+
Task { @ServiceActor in
28+
let handler = PseudoCommandHandler()
29+
await handler.rejectSuggestions()
30+
}
31+
}
32+
33+
suggestionWidget.onAcceptButtonTapped = {
34+
Task {
35+
try await Environment.triggerAction("Accept Suggestion")
36+
}
37+
}
38+
}
39+
}
1040
}

Core/Sources/SuggestionWidget/SuggestionPanelView.swift

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,31 @@ final class SuggestionPanelViewModel: ObservableObject {
99
@Published var suggestionCount: Int
1010
@Published var currentSuggestionIndex: Int
1111

12+
var onAcceptButtonTapped: (() -> Void)?
13+
var onRejectButtonTapped: (() -> Void)?
14+
var onPreviousButtonTapped: (() -> Void)?
15+
var onNextButtonTapped: (() -> Void)?
16+
1217
public init(
1318
startLineIndex: Int = 0,
1419
suggestion: [String] = [],
1520
isPanelDisplayed: Bool = false,
1621
suggestionCount: Int = 0,
17-
currentSuggestionIndex: Int = 0
22+
currentSuggestionIndex: Int = 0,
23+
onAcceptButtonTapped: (() -> Void)? = nil,
24+
onRejectButtonTapped: (() -> Void)? = nil,
25+
onPreviousButtonTapped: (() -> Void)? = nil,
26+
onNextButtonTapped: (() -> Void)? = nil
1827
) {
1928
self.startLineIndex = startLineIndex
2029
self.suggestion = suggestion
2130
self.isPanelDisplayed = isPanelDisplayed
2231
self.suggestionCount = suggestionCount
2332
self.currentSuggestionIndex = currentSuggestionIndex
33+
self.onAcceptButtonTapped = onAcceptButtonTapped
34+
self.onRejectButtonTapped = onRejectButtonTapped
35+
self.onPreviousButtonTapped = onPreviousButtonTapped
36+
self.onNextButtonTapped = onNextButtonTapped
2437
}
2538
}
2639

@@ -107,35 +120,47 @@ struct CodeBlock: View {
107120
}))
108121
}
109122
}
110-
123+
111124
struct ToolBar: View {
112125
@ObservedObject var viewModel: SuggestionPanelViewModel
113126

114127
var body: some View {
115128
HStack {
116129
Button(action: {
117130
Task {
131+
if let block = viewModel.onPreviousButtonTapped {
132+
block()
133+
return
134+
}
118135
try await Environment.triggerAction("Previous Suggestion")
119136
}
120137
}) {
121138
Image(systemName: "chevron.left")
122139
}.buttonStyle(.plain)
123-
140+
124141
Text("\(viewModel.currentSuggestionIndex + 1) / \(viewModel.suggestionCount)")
125142
.monospacedDigit()
126-
143+
127144
Button(action: {
128145
Task {
146+
if let block = viewModel.onNextButtonTapped {
147+
block()
148+
return
149+
}
129150
try await Environment.triggerAction("Next Suggestion")
130151
}
131152
}) {
132-
Image(systemName: "chevron.right")
153+
Image(systemName: "chevron.right")
133154
}.buttonStyle(.plain)
134155

135156
Spacer()
136157

137158
Button(action: {
138159
Task {
160+
if let block = viewModel.onRejectButtonTapped {
161+
block()
162+
return
163+
}
139164
try await Environment.triggerAction("Reject Suggestion")
140165
}
141166
}) {
@@ -144,6 +169,10 @@ struct ToolBar: View {
144169

145170
Button(action: {
146171
Task {
172+
if let block = viewModel.onAcceptButtonTapped {
173+
block()
174+
return
175+
}
147176
try await Environment.triggerAction("Accept Suggestion")
148177
}
149178
}) {

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ public final class SuggestionWidgetController {
7070
private var xcode: NSRunningApplication?
7171
private var suggestionForFiles: [URL: Suggestion] = [:]
7272

73+
public var onAcceptButtonTapped: (() -> Void)? {
74+
get { suggestionPanelViewModel.onAcceptButtonTapped }
75+
set { suggestionPanelViewModel.onAcceptButtonTapped = newValue }
76+
}
77+
78+
public var onRejectButtonTapped: (() -> Void)? {
79+
get { suggestionPanelViewModel.onRejectButtonTapped }
80+
set { suggestionPanelViewModel.onRejectButtonTapped = newValue }
81+
}
82+
83+
public var onPreviousButtonTapped: (() -> Void)? {
84+
get { suggestionPanelViewModel.onPreviousButtonTapped }
85+
set { suggestionPanelViewModel.onPreviousButtonTapped = newValue }
86+
}
87+
88+
public var onNextButtonTapped: (() -> Void)? {
89+
get { suggestionPanelViewModel.onNextButtonTapped }
90+
set { suggestionPanelViewModel.onNextButtonTapped = newValue }
91+
}
92+
7393
enum Suggestion {
7494
case code([String], startLineIndex: Int, currentSuggestionIndex: Int, suggestionCount: Int)
7595
}

0 commit comments

Comments
 (0)