Skip to content

Commit fafb0eb

Browse files
committed
Add a open chat button to widget context menu
1 parent 41abb69 commit fafb0eb

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

Core/Sources/Service/GUI/GraphicalUserInterfaceController.swift.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ public final class GraphicalUserInterfaceController {
1010
private nonisolated init() {
1111
Task { @MainActor in
1212
suggestionWidget.dataSource = WidgetDataSource.shared
13+
suggestionWidget.onOpenChatClicked = {
14+
Task {
15+
let fileURL = try await Environment.fetchCurrentFileURL()
16+
await WidgetDataSource.shared.createChatIfNeeded(for: fileURL)
17+
let presenter = PresentInWindowSuggestionPresenter()
18+
presenter.presentChatRoom(fileURL: fileURL)
19+
}
20+
}
1321
}
1422
}
1523
}

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ struct PromptToCodePanel: View {
99
VStack(spacing: 0) {
1010
ScrollView {
1111
VStack(spacing: 0) {
12+
if !provider.errorMessage.isEmpty {
13+
Text(provider.errorMessage)
14+
.multilineTextAlignment(.leading)
15+
.frame(maxWidth: .infinity, alignment: .leading)
16+
.foregroundColor(.white)
17+
.padding()
18+
.background(Color.red)
19+
}
20+
1221
if provider.code.isEmpty {
1322
Text(
1423
provider.isResponding
@@ -40,15 +49,6 @@ struct PromptToCodePanel: View {
4049
.frame(maxWidth: .infinity)
4150
}
4251

43-
if !provider.errorMessage.isEmpty {
44-
Text(provider.errorMessage)
45-
.multilineTextAlignment(.leading)
46-
.frame(maxWidth: .infinity, alignment: .leading)
47-
.foregroundColor(.white)
48-
.padding()
49-
.background(Color.red)
50-
}
51-
5252
Spacer(minLength: 50)
5353
}
5454
}
@@ -213,3 +213,24 @@ struct PromptToCodePanel_Bright_Preview: PreviewProvider {
213213
.frame(width: 450, height: 400)
214214
}
215215
}
216+
217+
struct PromptToCodePanel_Error_Bright_Preview: PreviewProvider {
218+
static var previews: some View {
219+
PromptToCodePanel(provider: PromptToCodeProvider(
220+
code: """
221+
ForEach(0..<viewModel.suggestion.count, id: \\.self) { index in
222+
Text(viewModel.suggestion[index])
223+
.frame(maxWidth: .infinity, alignment: .leading)
224+
.multilineTextAlignment(.leading)
225+
}
226+
""",
227+
language: "swift",
228+
description: "Hello world",
229+
isResponding: false,
230+
startLineIndex: 8,
231+
errorMessage: "Error"
232+
))
233+
.preferredColorScheme(.light)
234+
.frame(width: 450, height: 400)
235+
}
236+
}

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public final class SuggestionWidgetController {
3636
it.contentView = NSHostingView(
3737
rootView: WidgetView(
3838
viewModel: widgetViewModel,
39-
panelViewModel: suggestionPanelViewModel
39+
panelViewModel: suggestionPanelViewModel,
40+
onOpenChatClicked: { [weak self] in
41+
self?.onOpenChatClicked()
42+
}
4043
)
4144
)
4245
it.setIsVisible(true)
@@ -96,7 +99,8 @@ public final class SuggestionWidgetController {
9699
private var sourceEditorMonitorTask: Task<Void, Error>?
97100
private var currentFileURL: URL?
98101
private var colorScheme: ColorScheme = .light
99-
102+
103+
public var onOpenChatClicked: () -> Void = {}
100104
public var dataSource: SuggestionWidgetDataSource?
101105

102106
public nonisolated init() {

Core/Sources/SuggestionWidget/WidgetView.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct WidgetView: View {
1616
@ObservedObject var panelViewModel: SuggestionPanelViewModel
1717
@State var isHovering: Bool = false
1818
@State var processingProgress: Double = 0
19+
var onOpenChatClicked: () -> Void = {}
1920

2021
var body: some View {
2122
Circle().fill(isHovering ? .white.opacity(0.8) : .white.opacity(0.3))
@@ -74,7 +75,11 @@ struct WidgetView: View {
7475
isHovering = yes
7576
}
7677
}.contextMenu {
77-
WidgetContextMenu(widgetViewModel: viewModel)
78+
WidgetContextMenu(
79+
widgetViewModel: viewModel,
80+
isChatOpen: panelViewModel.isPanelDisplayed && panelViewModel.chat != nil,
81+
onOpenChatClicked: onOpenChatClicked
82+
)
7883
}
7984
}
8085

@@ -101,9 +106,21 @@ struct WidgetContextMenu: View {
101106
@AppStorage(\.suggestionFeatureEnabledProjectList) var suggestionFeatureEnabledProjectList
102107
@ObservedObject var widgetViewModel: WidgetViewModel
103108
@State var projectPath: String?
109+
var isChatOpen: Bool
110+
var onOpenChatClicked: () -> Void = {}
104111

105112
var body: some View {
106113
Group {
114+
if !isChatOpen {
115+
Button(action: {
116+
onOpenChatClicked()
117+
}) {
118+
Text("Open Chat")
119+
}
120+
}
121+
122+
Divider()
123+
107124
Button(action: {
108125
useGlobalChat.toggle()
109126
}) {
@@ -184,7 +201,7 @@ struct WidgetContextMenu: View {
184201
updateProjectPath(fileURL: fileURL)
185202
}
186203
}
187-
204+
188205
func updateProjectPath(fileURL: URL?) {
189206
Task {
190207
let projectURL = try? await Environment.fetchCurrentProjectRootURL(fileURL)

0 commit comments

Comments
 (0)