punchnet-macos/punchnet/punchnetApp.swift

117 lines
3.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)")
}
}()
*/
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@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.appContext = AppContext()
}
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)
Window("settings", id: "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) {
let shared = UserDefaults(suiteName: "group.com.jihe.punchnetmac")
shared?.set("App says hello", forKey: "test_msg")
shared?.synchronize()
SDLNotificationCenter.shared.addObserver(for: .vpnStatusChanged) { name in
NSLog("DarwinNotificationCenter get message: \(name)")
}
}
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
}
}