forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralSettingsView.swift
More file actions
141 lines (131 loc) · 4.88 KB
/
GeneralSettingsView.swift
File metadata and controls
141 lines (131 loc) · 4.88 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
import ComposableArchitecture
import SwiftUI
struct GeneralSettingsView: View {
@AppStorage(\.extensionPermissionShown) var extensionPermissionShown: Bool
@AppStorage(\.quitXPCServiceOnXcodeAndAppQuit) var quitXPCServiceOnXcodeAndAppQuit: Bool
@State private var shouldPresentExtensionPermissionAlert = false
@State private var shouldShowRestartXcodeAlert = false
let store: StoreOf<General>
var accessibilityPermissionSubtitle: String {
switch store.isAccessibilityPermissionGranted {
case .granted:
return "Granted"
case .notGranted:
return "Enable accessibility in system preferences"
case .unknown:
return ""
}
}
var extensionPermissionSubtitle: any View {
switch store.isExtensionPermissionGranted {
case .notGranted:
return HStack(spacing: 0) {
Text("Enable ")
Text(
"Extensions \(Image(systemName: "puzzlepiece.extension.fill")) → Xcode Source Editor \(Image(systemName: "info.circle")) → GitHub Copilot for Xcode"
)
.bold()
.foregroundStyle(.primary)
Text(" for faster and full-featured code completion.")
}
case .disabled:
return Text("Quit and restart Xcode to enable extension")
case .granted:
return Text("Granted")
case .unknown:
return Text("")
}
}
var extensionPermissionBadge: BadgeItem? {
switch store.isExtensionPermissionGranted {
case .notGranted:
return .init(text: "Not Granted", level: .danger)
case .disabled:
return .init(text: "Disabled", level: .danger)
default:
return nil
}
}
var extensionPermissionAction: ()->Void {
switch store.isExtensionPermissionGranted {
case .disabled:
return { shouldShowRestartXcodeAlert = true }
default:
return NSWorkspace.openXcodeExtensionsPreferences
}
}
var body: some View {
SettingsSection(title: "General") {
SettingsToggle(
title: "Quit GitHub Copilot when Xcode App is closed",
isOn: $quitXPCServiceOnXcodeAndAppQuit
)
Divider()
SettingsLink(
url: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility",
title: "Accessibility Permission",
subtitle: accessibilityPermissionSubtitle,
badge: store.isAccessibilityPermissionGranted == .notGranted ?
.init(
text: "Not Granted",
level: .danger
) : nil
)
Divider()
SettingsLink(
action: extensionPermissionAction,
title: "Extension Permission",
subtitle: extensionPermissionSubtitle,
badge: extensionPermissionBadge
)
} footer: {
HStack {
Spacer()
Button("?") {
NSWorkspace.shared.open(
URL(string: "https://github.com/github/CopilotForXcode/blob/main/TROUBLESHOOTING.md")!
)
}
.clipShape(Circle())
}
}
.alert(
"Enable Extension Permission",
isPresented: $shouldPresentExtensionPermissionAlert
) {
Button(
"Open System Preferences",
action: {
NSWorkspace.openXcodeExtensionsPreferences()
}).keyboardShortcut(.defaultAction)
Button("View How-to Guide", action: {
let url = "https://github.com/github/CopilotForXcode/blob/main/TROUBLESHOOTING.md#extension-permission"
NSWorkspace.shared.open(URL(string: url)!)
})
Button("Close", role: .cancel, action: {})
} message: {
Text("To enable faster and full-featured code completion, navigate to:\nExtensions → Xcode Source Editor → GitHub Copilot for Xcode.")
}
.task {
if extensionPermissionShown { return }
extensionPermissionShown = true
shouldPresentExtensionPermissionAlert = true
}
.alert(
"Restart Xcode?",
isPresented: $shouldShowRestartXcodeAlert
) {
Button("Restart Now") {
NSWorkspace.restartXcode()
}.keyboardShortcut(.defaultAction)
Button("Cancel", role: .cancel) {}
} message: {
Text("Quit and restart Xcode to enable Github Copilot for Xcode extension.")
}
}
}
#Preview {
GeneralSettingsView(
store: .init(initialState: .init(), reducer: { General() })
)
}