diff options
author | Roopesh Chander <roop@roopc.net> | 2019-05-20 14:04:47 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2019-05-20 16:42:28 +0530 |
commit | 40b1f0bac804985d1fceaef9422eb4c5f0ad204e (patch) | |
tree | 4308b55c1441dd6a636cb40a050a78710e0abdaf /WireGuard/WireGuard | |
parent | macOS: Login item: Get helper app version from xcconfig (diff) | |
download | wireguard-apple-40b1f0bac804985d1fceaef9422eb4c5f0ad204e.tar.xz wireguard-apple-40b1f0bac804985d1fceaef9422eb4c5f0ad204e.zip |
macOS: Don't show manage window when launched at login
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard')
4 files changed, 40 insertions, 7 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift index a68e08a..0860166 100644 --- a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift +++ b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift @@ -18,7 +18,14 @@ class AppDelegate: NSObject, NSApplicationDelegate { Logger.configureGlobal(tagged: "APP", withFilePath: FileManager.logFileURL?.path) registerLoginItem(shouldLaunchAtLogin: true) - NSApp.setActivationPolicy(.regular) + var isLaunchedAtLogin = false + if let appleEvent = NSAppleEventManager.shared().currentAppleEvent { + isLaunchedAtLogin = LaunchedAtLoginDetector.isLaunchedAtLogin(openAppleEvent: appleEvent) + } + + if !isLaunchedAtLogin { + NSApp.setActivationPolicy(.regular) + } NSApp.mainMenu = MainMenu() TunnelsManager.create { [weak self] result in @@ -42,7 +49,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { self.tunnelsTracker = tunnelsTracker self.statusItemController = statusItemController - self.showManageTunnelsWindow(completion: nil) + if !isLaunchedAtLogin { + self.showManageTunnelsWindow(completion: nil) + } } } } diff --git a/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift b/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift new file mode 100644 index 0000000..53a4dae --- /dev/null +++ b/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved. + +import Cocoa + +class LaunchedAtLoginDetector { + static func isLaunchedAtLogin(openAppleEvent: NSAppleEventDescriptor) -> Bool { + let launchCode = "LaunchedByWireGuardLoginItemHelper" + guard isOpenEvent(openAppleEvent) else { return false } + guard let propData = openAppleEvent.paramDescriptor(forKeyword: keyAEPropData) else { return false } + return propData.stringValue == launchCode + } +} + +private func isOpenEvent(_ event: NSAppleEventDescriptor) -> Bool { + if let eventClassDescriptor = event.attributeDescriptor(forKeyword: keyEventClassAttr), + let eventIdDescriptor = event.attributeDescriptor(forKeyword: keyEventIDAttr) { + return eventClassDescriptor.typeCodeValue == kCoreEventClass && eventIdDescriptor.typeCodeValue == kAEOpenApplication + } + return false +} diff --git a/WireGuard/WireGuard/UI/macOS/LoginItemHelper/Info.plist b/WireGuard/WireGuard/UI/macOS/LoginItemHelper/Info.plist index f7bcf8d..7ddff91 100644 --- a/WireGuard/WireGuard/UI/macOS/LoginItemHelper/Info.plist +++ b/WireGuard/WireGuard/UI/macOS/LoginItemHelper/Info.plist @@ -30,5 +30,7 @@ <string>NSApplication</string> <key>LSBackgroundOnly</key> <true/> + <key>com.wireguard.macos.app_id</key> + <string>$(APP_ID_MACOS)</string> </dict> </plist> diff --git a/WireGuard/WireGuard/UI/macOS/LoginItemHelper/main.m b/WireGuard/WireGuard/UI/macOS/LoginItemHelper/main.m index 51b73fd..1010b49 100644 --- a/WireGuard/WireGuard/UI/macOS/LoginItemHelper/main.m +++ b/WireGuard/WireGuard/UI/macOS/LoginItemHelper/main.m @@ -5,12 +5,13 @@ int main(int argc, char *argv[]) { - NSURL *bundleURL = [NSBundle.mainBundle bundleURL]; + NSString *appIdInfoDictionaryKey = @"com.wireguard.macos.app_id"; + NSString *appId = [NSBundle.mainBundle objectForInfoDictionaryKey:appIdInfoDictionaryKey]; - // From <path>/WireGuard.app/Contents/Library/LoginItems/WireGuardLoginItemHelper.app, derive <path>/WireGuard.app - for (int i = 0; i < 4; ++i) - bundleURL = [bundleURL URLByDeletingLastPathComponent]; + NSString *launchCode = @"LaunchedByWireGuardLoginItemHelper"; + NSAppleEventDescriptor *paramDescriptor = [NSAppleEventDescriptor descriptorWithString:launchCode]; - [NSWorkspace.sharedWorkspace launchApplication:[bundleURL path]]; + [NSWorkspace.sharedWorkspace launchAppWithBundleIdentifier:appId options:NSWorkspaceLaunchWithoutActivation + additionalEventParamDescriptor:paramDescriptor launchIdentifier:NULL]; return 0; } |