punchnet-macos/punchnet/punchnetApp.swift
2026-03-10 13:35:45 +08:00

153 lines
4.5 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 = ""
private var noticeServer: UDPNoticeCenterServer
@State private var appContext: AppContext
@State private var userContext = UserContext()
init() {
self.noticeServer = UDPNoticeCenterServer()
self.noticeServer.start()
self.appContext = AppContext(noticePort: self.noticeServer.port)
}
var body: some Scene {
WindowGroup(id: "mainWindow") {
RootView()
.frame(width: 800, height: 500)
.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("")
.environment(self.appContext)
.environment(self.userContext)
}
.commands {
CommandGroup(replacing: .appInfo) {
Button {
openWindow(id: "abortPunchnet")
} label: {
Text("About Punchnet")
}
}
}
.windowResizability(.contentSize)
.windowToolbarStyle(.unified)
.defaultPosition(.center)
Window("设置", id: "settings") {
SettingsView()
.environment(self.userContext)
}
.defaultSize(width: 800, height: 500)
.defaultPosition(.center)
Window("重置密码", id: "resetPassword") {
ResetPasswordRootView()
.environment(self.userContext)
}
.defaultSize(width: 800, height: 500)
.defaultPosition(.center)
Window("注册", id: "register") {
ResetPasswordRootView()
.environment(self.userContext)
}
.defaultSize(width: 800, height: 500)
.defaultPosition(.center)
MenuBarExtra("punchnet", image: "logo_32") {
VStack {
Button(action: {
self.menuClick()
}, label: {
Text("启动")
})
Divider()
Button(action: {
NSApplication.shared.terminate(nil)
}, label: { Text("退出") })
}
}
.menuBarExtraStyle(.menu)
}
private func menuClick() {
}
}
// 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
}
}