153 lines
4.4 KiB
Swift
153 lines
4.4 KiB
Swift
//
|
||
// 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
|
||
|
||
private var noticeServer: UDPNoticeCenterServer
|
||
@State private var appContext: AppContext
|
||
@State private var userContext = UserContext()
|
||
@AppStorage("hasAcceptedPrivacy") var hasAcceptedPrivacy: Bool = false
|
||
// UI 控制状态:是否显示弹窗
|
||
@State private var showPrivacy: Bool = false
|
||
|
||
init() {
|
||
self.noticeServer = UDPNoticeCenterServer()
|
||
self.noticeServer.start()
|
||
self.appContext = AppContext(noticePort: self.noticeServer.port)
|
||
}
|
||
|
||
var body: some Scene {
|
||
WindowGroup(id: "main") {
|
||
// RootView()
|
||
SettingsView()
|
||
.navigationTitle("")
|
||
.environment(self.appContext)
|
||
.environment(self.userContext)
|
||
.onAppear {
|
||
self.showPrivacy = !hasAcceptedPrivacy
|
||
}
|
||
.sheet(isPresented: $showPrivacy) {
|
||
PrivacyDetailView(showPrivacy: $showPrivacy)
|
||
//.interactiveDismissDisabled() // 强制阅读
|
||
}
|
||
}
|
||
// .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)
|
||
.environment(self.appContext)
|
||
.frame(width: 750, height: 500)
|
||
}
|
||
.windowResizability(.contentSize)
|
||
.defaultPosition(.center)
|
||
|
||
Window("重置密码", id: "resetPassword") {
|
||
ResetPasswordRootView()
|
||
.environment(self.userContext)
|
||
.environment(self.appContext)
|
||
.frame(width: 500, height: 400)
|
||
}
|
||
.windowResizability(.contentSize)
|
||
.defaultPosition(.center)
|
||
|
||
Window("注册", id: "register") {
|
||
RegisterRootView()
|
||
.environment(self.userContext)
|
||
.environment(self.appContext)
|
||
.frame(width: 500, height: 400)
|
||
}
|
||
.windowResizability(.contentSize)
|
||
.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 {
|
||
print("call me applicationShouldTerminate")
|
||
Task {
|
||
do {
|
||
try await VPNManager.shared.disableVpn()
|
||
} catch let err {
|
||
print("退出时关闭 VPN 失败: \(err)")
|
||
}
|
||
|
||
print("call me here termi")
|
||
|
||
await MainActor.run {
|
||
sender.reply(toApplicationShouldTerminate: true)
|
||
}
|
||
}
|
||
|
||
return .terminateLater
|
||
}
|
||
|
||
}
|