Split background menu

This commit is contained in:
世界
2023-07-20 06:44:39 +08:00
parent 7a616bbe15
commit fdbf50d875
6 changed files with 43 additions and 9 deletions
+20 -2
View File
@@ -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))
+3 -2
View File
@@ -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
}
+6
View File
@@ -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")
}