Skip to content

Commit a6b3d78

Browse files
committed
Add settings to widget context menu
1 parent e3096e2 commit a6b3d78

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ extension SuggestionWidgetController {
311311
}
312312
guard fileURL != currentFileURL else { continue }
313313
currentFileURL = fileURL
314+
widgetViewModel.currentFileURL = currentFileURL
314315
await updateContentForActiveEditor(fileURL: fileURL)
315316
}
316317

Core/Sources/SuggestionWidget/WidgetView.swift

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import Environment
12
import SwiftUI
23

34
@MainActor
45
final class WidgetViewModel: ObservableObject {
56
@Published var isProcessing: Bool
7+
@Published var currentFileURL: URL?
68

79
init(isProcessing: Bool = false) {
810
self.isProcessing = isProcessing
@@ -72,7 +74,7 @@ struct WidgetView: View {
7274
isHovering = yes
7375
}
7476
}.contextMenu {
75-
WidgetContextMenu()
77+
WidgetContextMenu(widgetViewModel: viewModel)
7678
}
7779
}
7880

@@ -95,6 +97,10 @@ struct WidgetContextMenu: View {
9597
@AppStorage(\.acceptSuggestionWithAccessibilityAPI) var acceptSuggestionWithAccessibilityAPI
9698
@AppStorage(\.hideCommonPrecedingSpacesInSuggestion) var hideCommonPrecedingSpacesInSuggestion
9799
@AppStorage(\.forceOrderWidgetToFront) var forceOrderWidgetToFront
100+
@AppStorage(\.disableSuggestionFeatureGlobally) var disableSuggestionFeatureGlobally
101+
@AppStorage(\.suggestionFeatureEnabledProjectList) var suggestionFeatureEnabledProjectList
102+
@ObservedObject var widgetViewModel: WidgetViewModel
103+
@State var projectPath: String?
98104

99105
var body: some View {
100106
Group {
@@ -115,7 +121,7 @@ struct WidgetContextMenu: View {
115121
Image(systemName: "checkmark")
116122
}
117123
}
118-
124+
119125
Button(action: {
120126
acceptSuggestionWithAccessibilityAPI.toggle()
121127
}, label: {
@@ -124,7 +130,7 @@ struct WidgetContextMenu: View {
124130
Image(systemName: "checkmark")
125131
}
126132
})
127-
133+
128134
Button(action: {
129135
hideCommonPrecedingSpacesInSuggestion.toggle()
130136
}, label: {
@@ -133,7 +139,7 @@ struct WidgetContextMenu: View {
133139
Image(systemName: "checkmark")
134140
}
135141
})
136-
142+
137143
Button(action: {
138144
forceOrderWidgetToFront.toggle()
139145
}, label: {
@@ -142,15 +148,52 @@ struct WidgetContextMenu: View {
142148
Image(systemName: "checkmark")
143149
}
144150
})
145-
151+
152+
if let projectPath, disableSuggestionFeatureGlobally {
153+
let matchedPath = suggestionFeatureEnabledProjectList.first { path in
154+
projectPath.hasPrefix(path)
155+
}
156+
Button(action: {
157+
if matchedPath != nil {
158+
suggestionFeatureEnabledProjectList
159+
.removeAll { path in path == matchedPath }
160+
} else {
161+
suggestionFeatureEnabledProjectList.append(projectPath)
162+
}
163+
}) {
164+
if matchedPath == nil {
165+
Text("Add to Suggestion-Enabled Project List")
166+
} else {
167+
Text("Remove from Suggestion-Enabled Project List")
168+
}
169+
}
170+
}
171+
146172
Divider()
147-
173+
148174
Button(action: {
149175
exit(0)
150176
}) {
151177
Text("Quit")
152178
}
153179
}
180+
.onAppear {
181+
updateProjectPath(fileURL: widgetViewModel.currentFileURL)
182+
}
183+
.onChange(of: widgetViewModel.currentFileURL) { fileURL in
184+
updateProjectPath(fileURL: fileURL)
185+
}
186+
}
187+
188+
func updateProjectPath(fileURL: URL?) {
189+
Task {
190+
let projectURL = try? await Environment.fetchCurrentProjectRootURL(fileURL)
191+
if let projectURL {
192+
Task { @MainActor in
193+
self.projectPath = projectURL.path
194+
}
195+
}
196+
}
154197
}
155198
}
156199

0 commit comments

Comments
 (0)