punchnet-macos/punchnet/punchnetApp.swift
2026-01-06 16:33:02 +08:00

138 lines
4.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// punchnetApp.swift
// punchnet
//
// Created by on 2025/5/12.
//
import SwiftUI
import AppKit
import SwiftData
import Combine
@main
struct punchnetApp: App {
/*
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Item.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
*/
@Environment(\.openWindow) private var openWindow
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@AppStorage("token") var token: String = ""
@AppStorage("network_code") var networkCode: String = ""
@AppStorage("hostname") var hostname: String = ""
@ObservedObject var vpnManager = VPNManager.shared
private var noticeServer: UDPNoticeCenterServer
init() {
self.noticeServer = UDPNoticeCenterServer()
self.noticeServer.start()
}
var body: some Scene {
WindowGroup(id: "mainWindow") {
IndexView(noticeServer: self.noticeServer)
.onAppear {
//
guard let screenFrame = NSScreen.main?.frame else { return }
//
NSApplication.shared.windows.forEach { window in
//
let windowWidth = window.frame.width
let windowHeight = window.frame.height
let centerX = (screenFrame.width - windowWidth) / 2
let centerY = (screenFrame.height - windowHeight) / 2
//
window.setFrameOrigin(NSPoint(x: centerX, y: centerY))
}
}
.toolbar(.hidden)
.navigationTitle("")
}
.commands {
CommandGroup(replacing: .appInfo) {
Button {
openWindow(id: "abortPunchnet")
} label: {
Text("About Punchnet")
}
}
}
.windowResizability(.contentSize)
Window("", id: "abortPunchnet") {
AbortView()
.frame(minWidth: 300, maxWidth: 300, minHeight: 500, maxHeight: 500)
}
MenuBarExtra("punchnet", image: "logo_32") {
VStack {
Button(action: {
self.menuClick()
}, label: {
Text(vpnManager.title)
})
Divider()
Button(action: {
NSApplication.shared.terminate(nil)
}, label: { Text("退出") })
}
}
.menuBarExtraStyle(.menu)
}
private func menuClick() {
switch self.vpnManager.vpnStatus {
case .disconnected:
Task {
let clientId = SystemConfig.getClientId()
try await vpnManager.enableVpn(options: SystemConfig.getOptions(networkCode: self.networkCode, token: self.token, clientId: clientId, hostname: self.hostname, noticePort: self.noticeServer.port)!)
}
case .connected:
Task {
try await vpnManager.disableVpn()
}
}
}
}
// APP
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillFinishLaunching(_ notification: Notification) {
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
Task {
try await VPNManager.shared.disableVpn()
DispatchQueue.main.async {
sender.reply(toApplicationShouldTerminate: true)
}
}
return .terminateLater
}
}