114 lines
3.3 KiB
Swift
114 lines
3.3 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)")
|
||
}
|
||
}()
|
||
*/
|
||
|
||
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
||
|
||
private var noticeServer: UDPNoticeCenterServer
|
||
@State private var appContext: AppContext
|
||
@AppStorage("hasAcceptedPrivacy") var hasAcceptedPrivacy: Bool = false
|
||
// UI 控制状态:是否显示弹窗
|
||
@State private var showPrivacy: Bool = false
|
||
|
||
@State private var vpnManager = VPNManager.shared
|
||
|
||
init() {
|
||
self.noticeServer = UDPNoticeCenterServer()
|
||
self.noticeServer.start()
|
||
self.appContext = AppContext(noticePort: self.noticeServer.port)
|
||
}
|
||
|
||
var body: some Scene {
|
||
Window("Punchnet", id: "main") {
|
||
AppRootView()
|
||
.navigationTitle("")
|
||
.environment(self.appContext)
|
||
.frame(
|
||
width: self.appContext.isLogined ? 900 : 380,
|
||
height: self.appContext.isLogined ? 600 : 500
|
||
)
|
||
// 增加动画:当状态改变时,窗口会像微信那样丝滑地变大/变小
|
||
.animation(.spring(response: 0.4, dampingFraction: 0.8), value: self.appContext.isLogined)
|
||
.onAppear {
|
||
self.showPrivacy = !hasAcceptedPrivacy
|
||
}
|
||
.sheet(isPresented: $showPrivacy) {
|
||
PrivacyDetailView(showPrivacy: $showPrivacy)
|
||
.interactiveDismissDisabled() // 强制阅读
|
||
}
|
||
}
|
||
.windowToolbarStyle(.unified)
|
||
.windowResizability(.contentSize)
|
||
.defaultPosition(.center)
|
||
.windowStyle(.hiddenTitleBar)
|
||
|
||
Settings {
|
||
SettingsView()
|
||
.environment(self.appContext)
|
||
.frame(width: 750, height: 450)
|
||
}
|
||
.windowResizability(.contentSize)
|
||
.defaultPosition(.center)
|
||
.windowStyle(.hiddenTitleBar)
|
||
|
||
MenuBarExtra("Punchnet", image: "logo_32") {
|
||
MainMenuBar()
|
||
.environment(appContext)
|
||
}
|
||
.menuBarExtraStyle(.menu)
|
||
}
|
||
|
||
}
|
||
|
||
// 处理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()
|
||
NSLog("vpn disabled")
|
||
} catch let err {
|
||
NSLog("退出时关闭 VPN 失败: \(err)")
|
||
}
|
||
|
||
await MainActor.run {
|
||
sender.reply(toApplicationShouldTerminate: true)
|
||
}
|
||
}
|
||
|
||
return .terminateLater
|
||
}
|
||
|
||
}
|