fix UI
This commit is contained in:
parent
6a29b8bc85
commit
7872604857
@ -31,6 +31,11 @@ class AppContext {
|
|||||||
// 登陆凭证
|
// 登陆凭证
|
||||||
var loginCredit: Credit?
|
var loginCredit: Credit?
|
||||||
|
|
||||||
|
// 判断用户是否登陆
|
||||||
|
var isLogined: Bool {
|
||||||
|
return loginCredit != nil
|
||||||
|
}
|
||||||
|
|
||||||
enum Credit {
|
enum Credit {
|
||||||
case token(token: String, session: SDLAPIClient.NetworkSession)
|
case token(token: String, session: SDLAPIClient.NetworkSession)
|
||||||
case accountAndPasword(account: String, password: String, session: SDLAPIClient.NetworkSession)
|
case accountAndPasword(account: String, password: String, session: SDLAPIClient.NetworkSession)
|
||||||
@ -116,6 +121,8 @@ class AppContext {
|
|||||||
// 退出登陆
|
// 退出登陆
|
||||||
func logout() async throws {
|
func logout() async throws {
|
||||||
try await VPNManager.shared.disableVpn()
|
try await VPNManager.shared.disableVpn()
|
||||||
|
self.networkContext = .default()
|
||||||
|
self.loginCredit = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
68
punchnet/Views/MenuBar/MainMenuBar.swift
Normal file
68
punchnet/Views/MenuBar/MainMenuBar.swift
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
//
|
||||||
|
// MainMenuBar.swift
|
||||||
|
// punchnet
|
||||||
|
//
|
||||||
|
// Created by 安礼成 on 2026/3/24.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct MainMenuBar: View {
|
||||||
|
@State private var vpnManager = VPNManager.shared
|
||||||
|
@Environment(AppContext.self) private var appContext: AppContext
|
||||||
|
@Environment(\.openWindow) private var openWindow
|
||||||
|
@Environment(\.dismissWindow) private var dismissWindow
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack {
|
||||||
|
switch self.vpnManager.vpnStatus {
|
||||||
|
case .connected:
|
||||||
|
Button(action: {
|
||||||
|
Task { @MainActor in
|
||||||
|
try await vpnManager.disableVpn()
|
||||||
|
}
|
||||||
|
}, label: {
|
||||||
|
Text("停止")
|
||||||
|
})
|
||||||
|
case .disconnected:
|
||||||
|
Button(action: {
|
||||||
|
Task { @MainActor in
|
||||||
|
await self.startVPN()
|
||||||
|
}
|
||||||
|
}, label: {
|
||||||
|
Text("启动")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
Divider()
|
||||||
|
|
||||||
|
// Button("打开控制面板") {
|
||||||
|
// openWindow(id: appContext.isLoggedIn ? "logined" : "login")
|
||||||
|
// }
|
||||||
|
|
||||||
|
SettingsLink {
|
||||||
|
Text("设置")
|
||||||
|
}
|
||||||
|
.buttonStyle(.plain)
|
||||||
|
|
||||||
|
Divider()
|
||||||
|
|
||||||
|
Button(action: {
|
||||||
|
NSApplication.shared.terminate(nil)
|
||||||
|
}, label: {
|
||||||
|
Text("退出应用")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func startVPN() async {
|
||||||
|
if let options = appContext.vpnOptions {
|
||||||
|
try? await vpnManager.enableVpn(options: options)
|
||||||
|
dismissWindow(id: "login")
|
||||||
|
openWindow(id: "logined")
|
||||||
|
} else {
|
||||||
|
openWindow(id: "login")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -69,9 +69,7 @@ struct NetworkView: View {
|
|||||||
.frame(width: 160)
|
.frame(width: 160)
|
||||||
}
|
}
|
||||||
|
|
||||||
Button {
|
SettingsLink {
|
||||||
openWindow(id: "settings")
|
|
||||||
} label: {
|
|
||||||
Image(systemName: "slider.horizontal.3")
|
Image(systemName: "slider.horizontal.3")
|
||||||
.font(.system(size: 14))
|
.font(.system(size: 14))
|
||||||
.foregroundColor(.secondary)
|
.foregroundColor(.secondary)
|
||||||
|
|||||||
@ -28,7 +28,7 @@ struct SettingsAccountView: View {
|
|||||||
} else {
|
} else {
|
||||||
// 蓝色主按钮
|
// 蓝色主按钮
|
||||||
Button {
|
Button {
|
||||||
self.openMainWindow(id: "main")
|
self.openMainWindow(id: "login")
|
||||||
} label: {
|
} label: {
|
||||||
Text("登录")
|
Text("登录")
|
||||||
.fontWeight(.medium)
|
.fontWeight(.medium)
|
||||||
@ -116,20 +116,29 @@ extension SettingsAccountView {
|
|||||||
|
|
||||||
struct AccountCreditView: View {
|
struct AccountCreditView: View {
|
||||||
@Environment(AppContext.self) var appContext: AppContext
|
@Environment(AppContext.self) var appContext: AppContext
|
||||||
|
@Environment(\.openWindow) var openWindow
|
||||||
|
@Environment(\.dismissWindow) var dismissWindow
|
||||||
|
|
||||||
let username: String
|
let username: String
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
AccountRow(icon: "person.fill", title: "当前登录账号", subtitle: username, actions: AnyView(
|
AccountRow(icon: "person.fill", title: "当前登录账号", subtitle: username, actions: AnyView(
|
||||||
HStack(spacing: 12) {
|
HStack(spacing: 12) {
|
||||||
Button("修改密码") {
|
// Button("修改密码") {
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
.buttonStyle(.link)
|
// .buttonStyle(.link)
|
||||||
|
|
||||||
Button("退出登录") {
|
Button("退出登录") {
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
try await appContext.logout()
|
try await appContext.logout()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.dismissWindow(id: "logined")
|
||||||
|
self.dismissWindow(id: "settings")
|
||||||
|
|
||||||
|
self.openWindow(id: "login")
|
||||||
|
|
||||||
}
|
}
|
||||||
.buttonStyle(.bordered)
|
.buttonStyle(.bordered)
|
||||||
.foregroundColor(.red)
|
.foregroundColor(.red)
|
||||||
@ -140,6 +149,9 @@ extension SettingsAccountView {
|
|||||||
|
|
||||||
struct TokenCreditView: View {
|
struct TokenCreditView: View {
|
||||||
@Environment(AppContext.self) var appContext: AppContext
|
@Environment(AppContext.self) var appContext: AppContext
|
||||||
|
@Environment(\.openWindow) var openWindow
|
||||||
|
@Environment(\.dismissWindow) var dismissWindow
|
||||||
|
|
||||||
let token: String
|
let token: String
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@ -148,10 +160,16 @@ extension SettingsAccountView {
|
|||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
try await appContext.logout()
|
try await appContext.logout()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.dismissWindow(id: "logined")
|
||||||
|
self.dismissWindow(id: "settings")
|
||||||
|
|
||||||
|
self.openWindow(id: "login")
|
||||||
}
|
}
|
||||||
.buttonStyle(.bordered)
|
.buttonStyle(.bordered)
|
||||||
.foregroundColor(.red)
|
.foregroundColor(.red)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -69,7 +69,7 @@ struct punchnetApp: App {
|
|||||||
.windowResizability(.contentSize)
|
.windowResizability(.contentSize)
|
||||||
.defaultPosition(.center)
|
.defaultPosition(.center)
|
||||||
|
|
||||||
Window("设置", id: "settings") {
|
Settings {
|
||||||
SettingsView()
|
SettingsView()
|
||||||
.environment(self.appContext)
|
.environment(self.appContext)
|
||||||
.frame(width: 750, height: 500)
|
.frame(width: 750, height: 500)
|
||||||
@ -78,48 +78,12 @@ struct punchnetApp: App {
|
|||||||
.defaultPosition(.center)
|
.defaultPosition(.center)
|
||||||
|
|
||||||
MenuBarExtra("punchnet", image: "logo_32") {
|
MenuBarExtra("punchnet", image: "logo_32") {
|
||||||
VStack {
|
MainMenuBar()
|
||||||
|
.environment(appContext)
|
||||||
switch self.vpnManager.vpnStatus {
|
|
||||||
case .connected:
|
|
||||||
Button(action: {
|
|
||||||
Task { @MainActor in
|
|
||||||
try await vpnManager.disableVpn()
|
|
||||||
}
|
|
||||||
}, label: {
|
|
||||||
Text("停止")
|
|
||||||
})
|
|
||||||
case .disconnected:
|
|
||||||
Button(action: {
|
|
||||||
Task { @MainActor in
|
|
||||||
await self.startVPN()
|
|
||||||
}
|
|
||||||
}, label: {
|
|
||||||
Text("启动")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
Divider()
|
|
||||||
|
|
||||||
Button(action: {
|
|
||||||
NSApplication.shared.terminate(nil)
|
|
||||||
}, label: {
|
|
||||||
Text("退出应用")
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.menuBarExtraStyle(.menu)
|
.menuBarExtraStyle(.menu)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func startVPN() async {
|
|
||||||
if let options = appContext.vpnOptions {
|
|
||||||
try? await vpnManager.enableVpn(options: options)
|
|
||||||
} else {
|
|
||||||
openWindow(id: "login")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理APP的生命周期
|
// 处理APP的生命周期
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user