From 7ded01c2770692f7a2380f2be30622c9384a2ba2 Mon Sep 17 00:00:00 2001 From: lwouis Date: Mon, 29 Dec 2025 15:41:23 +0100 Subject: [PATCH] perf: don't compute diffs of running-apps; directly get new/old --- src/api-wrappers/HelperExtensions.swift | 2 +- .../events/RunningApplicationsEvents.swift | 22 ++++++++----------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/api-wrappers/HelperExtensions.swift b/src/api-wrappers/HelperExtensions.swift index c593b593..1d2368ee 100644 --- a/src/api-wrappers/HelperExtensions.swift +++ b/src/api-wrappers/HelperExtensions.swift @@ -315,7 +315,7 @@ extension DispatchTimeInterval { } extension NSRunningApplication { - var id: String { "pid:\(String(describing: processIdentifier)) app:\(bundleIdentifier ?? bundleURL?.absoluteString ?? executableURL?.absoluteString ?? localizedName ?? "nil")" } + func debugId() -> String { "pid:\(String(describing: processIdentifier)) app:\(bundleIdentifier ?? bundleURL?.absoluteString ?? executableURL?.absoluteString ?? localizedName ?? "nil")" } } // 250ms is similar to human delay in processing changes on screen diff --git a/src/logic/events/RunningApplicationsEvents.swift b/src/logic/events/RunningApplicationsEvents.swift index 6c16159e..579f35e9 100644 --- a/src/logic/events/RunningApplicationsEvents.swift +++ b/src/logic/events/RunningApplicationsEvents.swift @@ -2,26 +2,22 @@ import Cocoa class RunningApplicationsEvents { private static var appsObserver: NSKeyValueObservation! - private static var previousValueOfRunningApps: Set! static func observe() { - previousValueOfRunningApps = Set(NSWorkspace.shared.runningApplications) // we can't observe NSWorkspace.didLaunchApplicationNotification or NSWorkspace.didTerminateApplicationNotification // these only trigger for some apps, mostly GUI app. We need to track all processes as any could spawn a window - appsObserver = NSWorkspace.shared.observe(\.runningApplications, options: [.old, .new], changeHandler: { (_, _) in handleEvent() }) + appsObserver = NSWorkspace.shared.observe(\.runningApplications, options: [.old, .new], changeHandler: { (_, change) in handleEvent(change) }) } - private static func handleEvent() { - let workspaceApps = Set(NSWorkspace.shared.runningApplications) - let added = workspaceApps.subtracting(previousValueOfRunningApps) - let removed = previousValueOfRunningApps.subtracting(workspaceApps) - Logger.info("added:", added.map { $0.id }, "removed:", removed.map { $0.id }) - if !added.isEmpty { - Applications.addRunningApplications(Array(added)) + private static func handleEvent(_ change: NSKeyValueObservedChange<[NSRunningApplication]>) { + let added = change.newValue + let removed = change.oldValue + Logger.debug("added:", added?.map { $0.debugId() }, "removed:", removed?.map { $0.debugId() }) + if let added { + Applications.addRunningApplications(added) } - if !removed.isEmpty { - Applications.removeRunningApplications(Array(removed)) + if let removed { + Applications.removeRunningApplications(removed) } - previousValueOfRunningApps = workspaceApps } }