forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralView.swift
More file actions
380 lines (343 loc) · 12.6 KB
/
GeneralView.swift
File metadata and controls
380 lines (343 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import Client
import ComposableArchitecture
import KeyboardShortcuts
import LaunchAgentManager
import Preferences
import SwiftUI
struct GeneralView: View {
let store: StoreOf<General>
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
AppInfoView()
Divider()
ExtensionServiceView(store: store)
Divider()
LaunchAgentView()
Divider()
GeneralSettingsView()
}
}
.onAppear {
store.send(.appear)
}
}
}
struct AppInfoView: View {
@State var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
@Environment(\.updateChecker) var updateChecker
var body: some View {
VStack(alignment: .leading) {
HStack(alignment: .top) {
Text(
Bundle.main
.object(forInfoDictionaryKey: "HOST_APP_NAME") as? String
?? "Copilot for Xcode"
)
.font(.title)
Text(appVersion ?? "")
.font(.footnote)
.foregroundColor(.secondary)
Spacer()
Button(action: {
updateChecker.checkForUpdates()
}) {
HStack(spacing: 2) {
Image(systemName: "arrow.up.right.circle.fill")
Text("Check for Updates")
}
}
}
HStack(spacing: 16) {
Link(
destination: URL(string: "https://github.com/intitni/CopilotForXcode")!
) {
HStack(spacing: 2) {
Image(systemName: "link")
Text("GitHub")
}
}
.focusable(false)
.foregroundColor(.accentColor)
Link(destination: URL(string: "https://www.buymeacoffee.com/intitni")!) {
HStack(spacing: 2) {
Image(systemName: "cup.and.saucer.fill")
Text("Buy Me A Coffee")
}
}
.foregroundColor(.accentColor)
.focusable(false)
}
}.padding()
}
}
struct ExtensionServiceView: View {
let store: StoreOf<General>
var body: some View {
VStack(alignment: .leading) {
WithViewStore(store, observe: { $0.xpcServiceVersion }) { viewStore in
Text("Extension Service Version: \(viewStore.state ?? "Loading..")")
}
WithViewStore(store, observe: { $0.isAccessibilityPermissionGranted }) { viewStore in
let grantedStatus: String = {
guard let granted = viewStore.state else { return "Loading.." }
return granted ? "Granted" : "Not Granted"
}()
Text("Accessibility Permission: \(grantedStatus)")
}
HStack {
WithViewStore(store, observe: { $0.isReloading }) { viewStore in
Button(action: { viewStore.send(.reloadStatus) }) {
Text("Refresh")
}.disabled(viewStore.state)
}
Button(action: {
Task {
let workspace = NSWorkspace.shared
let url = Bundle.main.bundleURL
.appendingPathComponent("Contents")
.appendingPathComponent("Applications")
.appendingPathComponent("CopilotForXcodeExtensionService.app")
workspace.activateFileViewerSelecting([url])
}
}) {
Text("Reveal Extension Service in Finder")
}
Button(action: {
let url = URL(
string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"
)!
NSWorkspace.shared.open(url)
}) {
Text("Accessibility Settings")
}
Button(action: {
let url = URL(
string: "x-apple.systempreferences:com.apple.ExtensionsPreferences"
)!
NSWorkspace.shared.open(url)
}) {
Text("Extensions Settings")
}
}
}
.padding()
}
}
struct LaunchAgentView: View {
@Environment(\.toast) var toast
@State var isDidRemoveLaunchAgentAlertPresented = false
@State var isDidSetupLaunchAgentAlertPresented = false
@State var isDidRestartLaunchAgentAlertPresented = false
var body: some View {
VStack(alignment: .leading) {
HStack {
Button(action: {
Task {
do {
try await LaunchAgentManager().setupLaunchAgent()
isDidSetupLaunchAgentAlertPresented = true
} catch {
toast(error.localizedDescription, .error)
}
}
}) {
Text("Set Up Launch Agent")
}
.alert(isPresented: $isDidSetupLaunchAgentAlertPresented) {
.init(
title: Text("Finished Launch Agent Setup"),
message: Text(
"Please refresh the Copilot status. (The first refresh may fail)"
),
dismissButton: .default(Text("OK"))
)
}
Button(action: {
Task {
do {
try await LaunchAgentManager().removeLaunchAgent()
isDidRemoveLaunchAgentAlertPresented = true
} catch {
toast(error.localizedDescription, .error)
}
}
}) {
Text("Remove Launch Agent")
}
.alert(isPresented: $isDidRemoveLaunchAgentAlertPresented) {
.init(
title: Text("Launch Agent Removed"),
dismissButton: .default(Text("OK"))
)
}
Button(action: {
Task {
do {
try await LaunchAgentManager().reloadLaunchAgent()
isDidRestartLaunchAgentAlertPresented = true
} catch {
toast(error.localizedDescription, .error)
}
}
}) {
Text("Reload Launch Agent")
}.alert(isPresented: $isDidRestartLaunchAgentAlertPresented) {
.init(
title: Text("Launch Agent Reloaded"),
dismissButton: .default(Text("OK"))
)
}
}
}
.padding()
}
}
struct GeneralSettingsView: View {
class Settings: ObservableObject {
@AppStorage(\.quitXPCServiceOnXcodeAndAppQuit)
var quitXPCServiceOnXcodeAndAppQuit
@AppStorage(\.suggestionWidgetPositionMode)
var suggestionWidgetPositionMode
@AppStorage(\.widgetColorScheme)
var widgetColorScheme
@AppStorage(\.preferWidgetToStayInsideEditorWhenWidthGreaterThan)
var preferWidgetToStayInsideEditorWhenWidthGreaterThan
@AppStorage(\.hideCircularWidget)
var hideCircularWidget
@AppStorage(\.showHideWidgetShortcutGlobally)
var showHideWidgetShortcutGlobally
}
@StateObject var settings = Settings()
@Environment(\.updateChecker) var updateChecker
var body: some View {
Form {
Toggle(isOn: $settings.quitXPCServiceOnXcodeAndAppQuit) {
Text("Quit service when Xcode and host app are terminated")
}
Toggle(isOn: .init(
get: { updateChecker.automaticallyChecksForUpdates },
set: { updateChecker.automaticallyChecksForUpdates = $0 }
)) {
Text("Automatically Check for Update")
}
Picker(selection: $settings.suggestionWidgetPositionMode) {
ForEach(SuggestionWidgetPositionMode.allCases, id: \.rawValue) {
switch $0 {
case .fixedToBottom:
Text("Fixed to Bottom").tag($0)
case .alignToTextCursor:
Text("Follow Text Cursor").tag($0)
}
}
} label: {
Text("Widget position")
}
Picker(selection: $settings.widgetColorScheme) {
ForEach(WidgetColorScheme.allCases, id: \.rawValue) {
switch $0 {
case .system:
Text("System").tag($0)
case .light:
Text("Light").tag($0)
case .dark:
Text("Dark").tag($0)
}
}
} label: {
Text("Widget color scheme")
}
HStack(alignment: .firstTextBaseline) {
TextField(text: .init(get: {
"\(Int(settings.preferWidgetToStayInsideEditorWhenWidthGreaterThan))"
}, set: {
settings
.preferWidgetToStayInsideEditorWhenWidthGreaterThan =
Double(Int($0) ?? 0)
})) {
Text("Prefer widget to be inside editor\nwhen width greater than")
.multilineTextAlignment(.trailing)
}
.textFieldStyle(.roundedBorder)
Text("pt")
}
KeyboardShortcuts.Recorder("Hotkey to Toggle Widgets", name: .showHideWidget) { _ in
// It's not used in this app!
KeyboardShortcuts.disable(.showHideWidget)
}
Toggle(isOn: $settings.showHideWidgetShortcutGlobally) {
Text("Enable the Hotkey Globally")
}
Toggle(isOn: $settings.hideCircularWidget) {
Text("Hide circular widget")
}
}.padding()
}
}
struct WidgetPositionIcon: View {
var position: SuggestionWidgetPositionMode
var isSelected: Bool
var body: some View {
ZStack {
Rectangle()
.fill(Color(nsColor: .textBackgroundColor))
Rectangle()
.fill(Color.accentColor.opacity(0.2))
.frame(width: 120, height: 20)
}
.frame(width: 120, height: 80)
}
}
struct LargeIconPicker<
Data: RandomAccessCollection,
ID: Hashable,
Content: View,
Label: View
>: View {
@Binding var selection: Data.Element
var data: Data
var id: KeyPath<Data.Element, ID>
var builder: (Data.Element, _ isSelected: Bool) -> Content
var label: () -> Label
@ViewBuilder
var content: some View {
HStack {
ForEach(data, id: id) { item in
let isSelected = selection[keyPath: id] == item[keyPath: id]
Button(action: {
selection = item
}) {
builder(item, isSelected)
.clipShape(RoundedRectangle(cornerRadius: 8))
.overlay {
RoundedRectangle(cornerRadius: 8)
.stroke(
isSelected ? Color.accentColor : Color.primary.opacity(0.1),
style: .init(lineWidth: 2)
)
}
}.buttonStyle(.plain)
}
}
}
var body: some View {
if #available(macOS 13.0, *) {
LabeledContent {
content
} label: {
label()
}
} else {
VStack {
label()
content
}
}
}
}
struct GeneralView_Previews: PreviewProvider {
static var previews: some View {
GeneralView(store: .init(initialState: .init(), reducer: General()))
.frame(height: 800)
}
}