-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVPNMenu.swift
106 lines (100 loc) · 4.06 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
import SwiftUI
struct VPNMenu<VPN: VPNService, S: Session>: View {
@EnvironmentObject var vpn: VPN
@EnvironmentObject var session: S
@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("CoderVPN")
.frame(maxWidth: .infinity, alignment: .leading)
.font(.body.bold())
.foregroundColor(.primary)
}.toggleStyle(.switch)
.disabled(vpnDisabled)
}
Divider()
Text("Workspace Agents")
.font(.headline)
.foregroundColor(.gray)
VPNState<VPN, S>()
}.padding([.horizontal, .top], Theme.Size.trayInset)
Agents<VPN, S>()
// Trailing stack
VStack(alignment: .leading, spacing: 3) {
TrayDivider()
if session.hasSession {
Link(destination: session.baseAccessURL!.appending(path: "templates")) {
ButtonRowView {
Text("Create workspace")
EmptyView()
}
}.buttonStyle(.plain)
TrayDivider()
}
if vpn.state == .failed(.systemExtensionError(.needsUserApproval)) {
Button {
openSystemExtensionSettings()
} label: {
ButtonRowView { Text("Approve in System Settings") }
}.buttonStyle(.plain)
} else {
AuthButton<VPN, S>()
}
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(session)
.onReceive(inspection.notice) { inspection.visit(self, $0) } // ViewInspector
}
private var vpnDisabled: Bool {
!session.hasSession ||
vpn.state == .connecting ||
vpn.state == .disconnecting ||
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")!)
}
#Preview {
VPNMenu<PreviewVPN, PreviewSession>().frame(width: 256)
.environmentObject(PreviewVPN())
.environmentObject(PreviewSession())
}