]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
macOS: Ignore bogus reopen because of login item helper
authorRoopesh Chander <roop@roopc.net>
Wed, 22 May 2019 12:01:05 +0000 (17:31 +0530)
committerRoopesh Chander <roop@roopc.net>
Wed, 22 May 2019 14:22:21 +0000 (19:52 +0530)
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 <roop@roopc.net>
WireGuard/WireGuard/UI/macOS/AppDelegate.swift
WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift

index c897111b0e06137e84e442ead27a7ba9e03d402f..453a152a096ad69895a01f5e40694f2e686d4bcd 100644 (file)
@@ -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
         }
index 185aced37bd7d29fb8f53e1962bd9db57828557c..0d8e3d86f58095a42c54618127f4cf56b734037a 100644 (file)
@@ -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
+}