diff --git a/ApplicationLibrary/Views/Dashboard/ExtensionStatusView.swift b/ApplicationLibrary/Views/Dashboard/ExtensionStatusView.swift index 5f71137..d1cca88 100644 --- a/ApplicationLibrary/Views/Dashboard/ExtensionStatusView.swift +++ b/ApplicationLibrary/Views/Dashboard/ExtensionStatusView.swift @@ -28,15 +28,10 @@ public struct ExtensionStatusView: View { StatusItem("Downlink", LibboxFormatBytes(message.downlink) + "/s") StatusItem("Uplink Total", LibboxFormatBytes(message.uplinkTotal)) StatusItem("Downlink Total", LibboxFormatBytes(message.downlinkTotal)) - } else { - #if os(macOS) - StatusItem("Connections", "\(message.connectionsOut)") - #endif } } else { StatusItem("Memory", "Loading...") StatusItem("Goroutines", "Loading...") - StatusItem("Connections", "Loading...") } }.background { GeometryReader { geometry in diff --git a/ApplicationLibrary/Views/Setting/SettingView.swift b/ApplicationLibrary/Views/Setting/SettingView.swift index 69db4e9..256d48c 100644 --- a/ApplicationLibrary/Views/Setting/SettingView.swift +++ b/ApplicationLibrary/Views/Setting/SettingView.swift @@ -17,6 +17,7 @@ public struct SettingView: View { #if os(macOS) @State private var startAtLogin = false @Environment(\.showMenuBarExtra) private var showMenuBarExtra + @State private var keepMenuBarInBackground = false #endif @State private var disableMemoryLimit = false @@ -51,8 +52,19 @@ public struct SettingView: View { .onChange(of: showMenuBarExtra.wrappedValue) { newValue in Task.detached { SharedPreferences.showMenuBarExtra = newValue + if !newValue { + keepMenuBarInBackground = false + } } } + if showMenuBarExtra.wrappedValue { + Toggle("Keep Menu Bar in Background", isOn: $keepMenuBarInBackground) + .onChange(of: keepMenuBarInBackground) { newValue in + Task.detached { + SharedPreferences.menuBarExtraInBackground = newValue + } + } + } } #endif Section("Packet Tunnel") { @@ -132,6 +144,7 @@ public struct SettingView: View { private func loadSettings() async { #if os(macOS) startAtLogin = SMAppService.mainApp.status == .enabled + keepMenuBarInBackground = SharedPreferences.menuBarExtraInBackground #endif disableMemoryLimit = SharedPreferences.disableMemoryLimit version = LibboxVersion() diff --git a/Library/Database/SharedPreferences.swift b/Library/Database/SharedPreferences.swift index 6c4efc5..fb40cc9 100644 --- a/Library/Database/SharedPreferences.swift +++ b/Library/Database/SharedPreferences.swift @@ -14,6 +14,7 @@ public enum SharedPreferences { #if os(macOS) @Preference("show_menu_bar_extra", defaultValue: true) public static var showMenuBarExtra + @Preference("menu_bar_extra_in_background", defaultValue: false) public static var menuBarExtraInBackground @Preference("started_by_user", defaultValue: false) public static var startedByUser #endif diff --git a/SFM/Application.swift b/SFM/Application.swift index c43fb7f..ed1f14b 100644 --- a/SFM/Application.swift +++ b/SFM/Application.swift @@ -61,14 +61,32 @@ struct Application: App { .menuBarExtraAccess(isPresented: $isMenuPresented) } - private func initialize() async { + private func initialize() { let initialShowMenuBarExtra = SharedPreferences.showMenuBarExtra - await MainActor.run { + DispatchQueue.main.async { showMenuBarExtra = initialShowMenuBarExtra } } private func hide(closeApp: Bool) { + Task.detached { + if SharedPreferences.menuBarExtraInBackground { + DispatchQueue.main.async { + hide0(closeApp: closeApp) + } + } else { + DispatchQueue.main.async { + if closeApp { + NSApp.terminate(nil) + } else { + NSApp.keyWindow?.close() + } + } + } + } + } + + private func hide0(closeApp: Bool) { if closeApp || NSApp.keyWindow?.identifier?.rawValue == "main" { let transformState = ProcessApplicationTransformState(kProcessTransformToUIElementApplication) var psn = ProcessSerialNumber(highLongOfPSN: 0, lowLongOfPSN: UInt32(kCurrentProcess)) diff --git a/SFM/ApplicationDelegate.swift b/SFM/ApplicationDelegate.swift index 716362e..850ffd8 100644 --- a/SFM/ApplicationDelegate.swift +++ b/SFM/ApplicationDelegate.swift @@ -12,7 +12,7 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate { let launchedAsLogInItem = event?.eventID == kAEOpenApplication && event?.paramDescriptor(forKeyword: keyAEPropData)?.enumCodeValue == keyAELaunchedAsLogInItem - if !launchedAsLogInItem || !SharedPreferences.showMenuBarExtra { + if !launchedAsLogInItem || !SharedPreferences.showMenuBarExtra || !SharedPreferences.menuBarExtraInBackground { NSApp.setActivationPolicy(.regular) NSApp.activate(ignoringOtherApps: true) } else { @@ -39,12 +39,13 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate { } func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool { - !SharedPreferences.showMenuBarExtra + !SharedPreferences.menuBarExtraInBackground } func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows flag: Bool) -> Bool { if !flag, NSApp.activationPolicy() == .accessory { NSApp.setActivationPolicy(.regular) + NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dock").first?.activate() } return true } diff --git a/SFM/MenuView.swift b/SFM/MenuView.swift index 4a42dfb..cf9bcc3 100644 --- a/SFM/MenuView.swift +++ b/SFM/MenuView.swift @@ -40,6 +40,12 @@ struct MenuView: View { MenuCommand { NSApp.setActivationPolicy(.regular) openWindow(id: "main") + if let dockApp = NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dock").first { + dockApp.activate() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { + NSApp.activate(ignoringOtherApps: true) + } + } } label: { Text("Open") }