punchnet-macos/punchnet/punchnetApp.swift

131 lines
3.6 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
private var noticeServer: UDPNoticeCenterServer
@State private var appContext: AppContext
@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 {
Window("登陆", id: "login") {
LoginRootView()
.navigationTitle("")
.environment(self.appContext)
.onAppear {
self.showPrivacy = !hasAcceptedPrivacy
}
.sheet(isPresented: $showPrivacy) {
PrivacyDetailView(showPrivacy: $showPrivacy)
//.interactiveDismissDisabled() //
}
}
.windowResizability(.contentSize)
.windowToolbarStyle(.unified)
.defaultPosition(.center)
Window("网络", id: "logined") {
SettingsView()
.environment(self.appContext)
.frame(width: 750, height: 500)
}
.windowResizability(.contentSize)
.defaultPosition(.center)
Window("设置", id: "settings") {
SettingsView()
.environment(self.appContext)
.frame(width: 750, height: 500)
}
.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
}
}