-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVPNMenu.swift
115 lines (108 loc) · 4.43 KB
/
VPNMenu.swift
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
import SwiftUI
struct VPNMenu<VPN: VPNService>: View {
@EnvironmentObject var vpn: VPN
@EnvironmentObject var state: AppState
@Environment(\.openSettings) private var openSettings
let inspection = Inspection<Self>()
var body: some View {
// Main stack
VStackLayout(alignment: .leading) {
// CoderVPN Stack
VStack(alignment: .leading, spacing: Theme.Size.trayPadding) {
HStack {
Toggle(isOn: Binding(
get: { vpn.state == .connected || vpn.state == .connecting },
set: { isOn in Task {
if isOn { await vpn.start() } else { await vpn.stop() }
}
}
)) {
Text("Coder Connect")
.frame(maxWidth: .infinity, alignment: .leading)
.font(.body.bold())
.foregroundColor(.primary)
}.toggleStyle(.switch)
.disabled(vpnDisabled)
}
Divider()
Text("Workspaces")
.font(.headline)
.foregroundColor(.secondary)
VPNState<VPN>()
}.padding([.horizontal, .top], Theme.Size.trayInset)
Agents<VPN>()
// Trailing stack
VStack(alignment: .leading, spacing: 3) {
TrayDivider()
if vpn.state == .connected, !vpn.menuState.invalidAgents.isEmpty {
InvalidAgentsButton<VPN>()
}
if state.hasSession {
Link(destination: state.baseAccessURL!.appending(path: "templates")) {
ButtonRowView {
Text("Create workspace")
}
}.buttonStyle(.plain)
TrayDivider()
}
if vpn.state == .failed(.systemExtensionError(.needsUserApproval)) {
Button {
openSystemExtensionSettings()
} label: {
ButtonRowView { Text("Approve in System Settings") }
}.buttonStyle(.plain)
} else {
AuthButton<VPN>()
}
Button {
openSettings()
appActivate()
} label: {
ButtonRowView { Text("Settings") }
}.buttonStyle(.plain)
Button {
About.open()
} label: {
ButtonRowView {
Text("About")
}
}.buttonStyle(.plain)
TrayDivider()
Button {
NSApp.terminate(nil)
} label: {
ButtonRowView {
Text("Quit")
}
}.buttonStyle(.plain)
}.padding([.horizontal, .bottom], Theme.Size.trayMargin)
}.padding(.bottom, Theme.Size.trayMargin)
.environmentObject(vpn)
.environmentObject(state)
.onReceive(inspection.notice) { inspection.visit(self, $0) } // ViewInspector
}
private var vpnDisabled: Bool {
!state.hasSession ||
vpn.state == .connecting ||
vpn.state == .disconnecting ||
// Prevent starting the VPN before the user has approved the system extension.
vpn.state == .failed(.systemExtensionError(.needsUserApproval))
}
}
func openSystemExtensionSettings() {
// Sourced from:
// https://gist.github.com/rmcdongit/f66ff91e0dad78d4d6346a75ded4b751?permalink_comment_id=5261757
// We'll need to ensure this continues to work in future macOS versions
// swiftlint:disable:next line_length
NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.ExtensionsPreferences?extensionPointIdentifier=com.apple.system_extension.network_extension.extension-point")!)
}
#if DEBUG
#Preview {
let appState = AppState(persistent: false)
appState.login(baseAccessURL: URL(string: "http://127.0.0.1:8080")!, sessionToken: "")
// appState.clearSession()
return VPNMenu<PreviewVPN>().frame(width: 256)
.environmentObject(PreviewVPN())
.environmentObject(appState)
}
#endif