From: Roopesh Chander Date: Wed, 22 May 2019 12:01:05 +0000 (+0530) Subject: macOS: Ignore bogus reopen because of login item helper X-Git-Tag: 0.0.20190531-9~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=493c7b102e4095664860304e5e5d5d151ff7507c;p=thirdparty%2Fwireguard-apple.git macOS: Ignore bogus reopen because of login item helper The bogus reopen occurs because the SMLoginItemSetEnabled actually runs the helper app immediately. The helper app attempts to launch the main app, causing a reopen Apple event (rapp) to be sent. Signed-off-by: Roopesh Chander --- diff --git a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift index c897111..453a152 100644 --- a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift +++ b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift @@ -58,6 +58,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { } func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows: Bool) -> Bool { + if let appleEvent = NSAppleEventManager.shared().currentAppleEvent { + if LaunchedAtLoginDetector.isReopenedByLoginItemHelper(reopenAppleEvent: appleEvent) { + return false + } + } if hasVisibleWindows { return true } diff --git a/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift b/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift index 185aced..0d8e3d8 100644 --- a/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift +++ b/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift @@ -4,14 +4,25 @@ import Cocoa class LaunchedAtLoginDetector { + static let launchCode = "LaunchedByWireGuardLoginItemHelper" + 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 } + + static func isReopenedByLoginItemHelper(reopenAppleEvent: NSAppleEventDescriptor) -> Bool { + guard isReopenEvent(reopenAppleEvent) else { return false } + guard let propData = reopenAppleEvent.paramDescriptor(forKeyword: keyAEPropData) else { return false } + return propData.stringValue == launchCode + } } private func isOpenEvent(_ event: NSAppleEventDescriptor) -> Bool { return event.eventClass == kCoreEventClass && event.eventID == kAEOpenApplication } + +private func isReopenEvent(_ event: NSAppleEventDescriptor) -> Bool { + return event.eventClass == kCoreEventClass && event.eventID == kAEReopenApplication +}