Rewrite menu bar extra with AppKit

This commit is contained in:
世界
2026-01-05 19:19:11 +08:00
parent 91edd77b00
commit ce0d7ead88
17 changed files with 666 additions and 301 deletions
@@ -24,8 +24,6 @@ import SwiftUI
case .notDetermined:
pendingAuthorizationRequest = true
manager.requestAlwaysAuthorization()
case .authorized, .authorizedAlways:
onAuthorizationGranted?()
default:
break
}
@@ -69,10 +67,10 @@ public struct GlobalChecksModifier: ViewModifier {
handleImportRemoteProfile()
}
.onChangeCompat(of: importProfile.wrappedValue) { _ in
handleImportProfile()
Task { @MainActor in handleImportProfile() }
}
.onChangeCompat(of: importRemoteProfile.wrappedValue) { _ in
handleImportRemoteProfile()
Task { @MainActor in handleImportRemoteProfile() }
}
.onChangeCompat(of: environments.extensionProfile?.status) { status in
handleStatusChange(status)
@@ -84,10 +82,10 @@ public struct GlobalChecksModifier: ViewModifier {
#if os(macOS)
content
.onReceive(NotificationCenter.default.publisher(for: .extensionRequiresWIFIState)) { _ in
handleWiFiStateNotification()
Task { @MainActor in handleWiFiStateNotification() }
}
.onReceive(NotificationCenter.default.publisher(for: .extensionRequiresHelperService)) { _ in
handleHelperServiceNotification()
Task { @MainActor in handleHelperServiceNotification() }
}
#else
content
@@ -133,26 +131,24 @@ public struct GlobalChecksModifier: ViewModifier {
}
private func handleStatusChange(_ status: NEVPNStatus?) {
guard let status else { return }
switch status {
case .connected:
notStarted = false
Task {
Task { @MainActor in
guard let status else { return }
switch status {
case .connected:
notStarted = false
await checkDeprecatedNotes()
}
case .connecting:
notStarted = true
case .disconnected:
if #available(iOS 16.0, macOS 13.0, tvOS 17.0, *) {
if notStarted, let profile = environments.extensionProfile {
Task {
case .connecting:
notStarted = true
case .disconnected:
if #available(iOS 16.0, macOS 13.0, tvOS 17.0, *) {
if notStarted, let profile = environments.extensionProfile {
await checkLastDisconnectError(profile: profile)
}
}
notStarted = false
default:
break
}
notStarted = false
default:
break
}
}
@@ -63,7 +63,9 @@ public struct ConnectionListView: View {
viewModel.connect()
}
.onReceive(environments.commandClient.$connections) { connections in
viewModel.setConnections(connections)
Task { @MainActor in
viewModel.setConnections(connections)
}
}
.onChangeCompat(of: viewModel.connectionStateFilter) { filter in
environments.commandClient.connectionStateFilter = filter
@@ -108,20 +108,22 @@ public struct StartStopButton: View {
.disabled(!profile.status.isEnabled)
.alert($alert)
.onReceive(timer) { _ in
currentTime = Date()
Task { @MainActor in
currentTime = Date()
}
}
.onChangeCompat(of: profile.status) { status in
if isStarting {
if status == .disconnected {
isStarting = false
if #available(iOS 16.0, macOS 13.0, tvOS 17.0, *) {
Task {
Task { @MainActor in
if isStarting {
if status == .disconnected {
isStarting = false
if #available(iOS 16.0, macOS 13.0, tvOS 17.0, *) {
await checkStartupError()
}
} else if status.isConnectedStrict {
isStarting = false
environments.commandClient.connect()
}
} else if status.isConnectedStrict {
isStarting = false
environments.commandClient.connect()
}
}
}
@@ -17,6 +17,19 @@ public extension EnvironmentValues {
}
}
private struct menuBarExtraSpeedModeKey: EnvironmentKey {
static let defaultValue: Binding<Int> = .constant(1)
}
var menuBarExtraSpeedMode: Binding<Int> {
get {
self[menuBarExtraSpeedModeKey.self]
}
set {
self[menuBarExtraSpeedModeKey.self] = newValue
}
}
private struct selectionKey: EnvironmentKey {
static let defaultValue: Binding<NavigationPage> = .constant(.dashboard)
}
@@ -28,7 +28,9 @@ public struct GroupListView: View {
viewModel.connect()
}
.onReceive(environments.commandClient.$groups) { groups in
viewModel.setGroups(groups)
Task { @MainActor in
viewModel.setGroups(groups)
}
}
}
}
@@ -137,11 +137,11 @@ import SwiftUI
} label: {
Text(symbol)
.font(.system(.body, design: .monospaced).weight(.medium))
#if os(iOS)
#if os(iOS)
.frame(minWidth: symbol.count > 1 ? nil : 32, minHeight: 32)
#else
#else
.frame(minWidth: symbol.count > 1 ? nil : 24)
#endif
#endif
}
}
}
@@ -11,6 +11,7 @@ public struct AppView: View {
@State private var startAtLogin = false
@Environment(\.showMenuBarExtra) private var showMenuBarExtra
@Environment(\.menuBarExtraSpeedMode) private var menuBarExtraSpeedMode
@State private var menuBarExtraInBackground = false
#if os(macOS)
@@ -47,6 +48,17 @@ public struct AppView: View {
}
if showMenuBarExtra.wrappedValue {
Picker("Real-time Speed", selection: menuBarExtraSpeedMode) {
ForEach(MenuBarExtraSpeedMode.allCases, id: \.rawValue) { mode in
Text(mode.name).tag(mode.rawValue)
}
}
.onChangeCompat(of: menuBarExtraSpeedMode.wrappedValue) { newValue in
Task {
await SharedPreferences.menuBarExtraSpeedMode.set(newValue)
}
}
Toggle("Keep Menu Bar in Background", isOn: $menuBarExtraInBackground)
.onChangeCompat(of: menuBarExtraInBackground) { newValue in
Task {