Ariver
2026-06-02 69e20c361756bb8039fdb8b1e8a6ccd074b7d2b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import AppKit
import Darwin
import Foundation
 
enum TagLauncherProcessSingleton {
    private static let lockURL = AppIdentity.applicationSupportDirectory
        .appendingPathComponent("TagLauncher.lock")
    private static let activationNotification = Notification.Name("TagLauncherExternalActivationRequested")
    private static let duplicateLaunchSuppressReopenKey = "duplicateLaunchSuppressReopenAt"
 
    static func acquireOrHandOffAndExit() -> FileHandle {
        do {
            try FileManager.default.createDirectory(
                at: AppIdentity.applicationSupportDirectory,
                withIntermediateDirectories: true
            )
            if !FileManager.default.fileExists(atPath: lockURL.path) {
                _ = FileManager.default.createFile(atPath: lockURL.path, contents: nil)
            }
 
            let lockFile = try FileHandle(forUpdating: lockURL)
            if flock(lockFile.fileDescriptor, LOCK_EX | LOCK_NB) == 0 {
                return lockFile
            }
            try? lockFile.close()
        } catch {
            // If locking fails, avoiding a duplicate visible instance is safer than
            // letting another Dock tile appear.
        }
 
        handOffAndExit()
    }
 
    private static func handOffAndExit() -> Never {
        let arguments = CommandLine.arguments.dropFirst()
        let shouldShowOverlay = arguments.contains("--show-overlay")
        if !shouldShowOverlay {
            UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: duplicateLaunchSuppressReopenKey)
            UserDefaults.standard.synchronize()
            NSApplication.shared.setActivationPolicy(.accessory)
        }
        let ownerInstance = NSRunningApplication
            .runningApplications(withBundleIdentifier: AppIdentity.bundleIdentifier)
            .filter { $0.processIdentifier != getpid() && !$0.isTerminated }
            .sorted(by: prefersRunningInstance)
            .first
 
        for delay in [0.0, 0.15, 0.4] {
            if delay > 0 {
                Thread.sleep(forTimeInterval: delay)
            }
            DistributedNotificationCenter.default().postNotificationName(
                activationNotification,
                object: AppIdentity.bundleIdentifier,
                userInfo: ["showOverlay": shouldShowOverlay],
                deliverImmediately: true
            )
            if shouldShowOverlay {
                ownerInstance?.activate()
            }
        }
        exit(0)
    }
 
    private static func prefersRunningInstance(
        _ lhs: NSRunningApplication,
        _ rhs: NSRunningApplication
    ) -> Bool {
        let lhsLaunchDate = lhs.launchDate ?? .distantFuture
        let rhsLaunchDate = rhs.launchDate ?? .distantFuture
        if lhsLaunchDate != rhsLaunchDate {
            return lhsLaunchDate < rhsLaunchDate
        }
        return lhs.processIdentifier < rhs.processIdentifier
    }
}