2026-05-26 19:38:47 +08:00

80 lines
2.1 KiB
Swift

//
// MainMenuBar.swift
// punchnet
//
// Created by on 2026/3/24.
//
import SwiftUI
struct MainMenuBar: View {
@State private var vpnManager = VPNManager.shared
@Environment(AppContext.self) private var appContext: AppContext
@Environment(\.openWindow) private var openWindow
var body: some View {
VStack {
switch self.vpnManager.vpnStatus {
case .connected:
Button(action: {
Task { @MainActor in
try await appContext.stopTun()
}
}, label: {
Text("停止")
})
case .connecting:
Button(action: {
}, label: {
Text("连接中...")
})
.disabled(true)
case .disconnecting:
Button(action: {
}, label: {
Text("断开中...")
})
.disabled(true)
case .disconnected:
Button(action: {
Task { @MainActor in
await self.startVPN()
}
}, label: {
Text("启动")
})
}
Divider()
Button("打开控制面板") {
AppWindow.open(id: AppWindow.main, using: openWindow)
print("call me open main")
}
Button("设置") {
AppWindow.open(id: AppWindow.settings, using: openWindow)
}
Divider()
Button("退出应用") {
NSApplication.shared.terminate(nil)
}
}
}
private func startVPN() async {
do {
if appContext.networkContext == nil {
try await appContext.connectNetwork()
}
try await appContext.startTun()
} catch {
NSLog("menu start vpn failed: \(error)")
}
}
}