From a5135c5560566f7bba8a4b01da6bbb20b759be9e Mon Sep 17 00:00:00 2001 From: lwouis Date: Sun, 8 Feb 2026 11:03:57 +0100 Subject: [PATCH] perf: cache screen uuids and frontmost app --- src/logic/Applications.swift | 1 + src/logic/Screens.swift | 21 +++++++++++++++++---- src/logic/Window.swift | 4 ++-- src/logic/Windows.swift | 6 +++--- src/logic/events/AccessibilityEvents.swift | 1 + src/logic/events/ScreensEvents.swift | 2 +- src/logic/events/SpacesEvents.swift | 2 +- src/ui/App.swift | 2 +- 8 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/logic/Applications.swift b/src/logic/Applications.swift index 7fb07b68..6323877a 100644 --- a/src/logic/Applications.swift +++ b/src/logic/Applications.swift @@ -3,6 +3,7 @@ import ApplicationServices class Applications { static var list = [Application]() + static var frontmostPid = NSWorkspace.shared.frontmostApplication?.processIdentifier static func initialDiscovery() { addInitialRunningApplications() diff --git a/src/logic/Screens.swift b/src/logic/Screens.swift index 1f6293e9..87897c2a 100644 --- a/src/logic/Screens.swift +++ b/src/logic/Screens.swift @@ -5,8 +5,9 @@ class Screens { static func refresh() { all.removeAll() + NSScreen.clearUuidCache() for screen in NSScreen.screens { - guard let uuid = screen.uuid() else { continue } + guard let uuid = screen.cachedUuid() else { continue } all[uuid] = screen } } @@ -14,6 +15,7 @@ class Screens { extension NSScreen { static var preferred = NSScreen.screens.first! + private static var uuidCache = [ObjectIdentifier: ScreenUuid]() static func updatePreferred() { preferred = detectPreferred() ?? NSScreen.screens.first! @@ -34,7 +36,7 @@ extension NSScreen { /// * if NSScreen.screensHaveSeparateSpaces == false, and key window is on another screen than screens[0], it still returns screens[0] /// we find the screen with the key window ourselves manually static func active() -> NSScreen? { - guard let frontmostPid = NSWorkspace.shared.frontmostApplication?.processIdentifier, + guard let frontmostPid = Applications.frontmostPid, // we avoid Applications.findOrCreate() here, because it active() is called very early during launch // we are not ready to create applications yet let frontmostApp = (Applications.list.first { $0.pid == frontmostPid }) else { return nil } @@ -47,7 +49,7 @@ extension NSScreen { /// there is only 1 active menubar. Other screens will show their menubar dimmed static func withActiveMenubar() -> NSScreen? { - return NSScreen.screens.first { CGSCopyActiveMenuBarDisplayIdentifier(CGS_CONNECTION) == $0.uuid() } + return NSScreen.screens.first { CGSCopyActiveMenuBarDisplayIdentifier(CGS_CONNECTION) == $0.cachedUuid() } } static func withMouse() -> NSScreen? { @@ -74,6 +76,18 @@ extension NSScreen { return uuid } + static func clearUuidCache() { + uuidCache.removeAll(keepingCapacity: true) + } + + func cachedUuid() -> ScreenUuid? { + let key = ObjectIdentifier(self) + if let uuid = NSScreen.uuidCache[key] { return uuid } + guard let uuid = uuid() else { return nil } + NSScreen.uuidCache[key] = uuid + return uuid + } + // periphery:ignore func refreshRate() -> Double? { return number().flatMap { CGDisplayCopyDisplayMode($0)?.refreshRate } @@ -102,4 +116,3 @@ extension NSScreen { } typealias ScreenUuid = CFString - diff --git a/src/logic/Window.swift b/src/logic/Window.swift index 80723a3a..b16ff38f 100644 --- a/src/logic/Window.swift +++ b/src/logic/Window.swift @@ -267,13 +267,13 @@ class Window { } private func updateScreenId() { - screenId = NSScreen.screens.first { isOnScreen($0) }?.uuid() + screenId = NSScreen.screens.first { isOnScreen($0) }?.cachedUuid() } /// window may not be visible on that screen (e.g. the window is not on the current Space) func isOnScreen(_ screen: NSScreen) -> Bool { if NSScreen.screensHaveSeparateSpaces { - if let screenUuid = screen.uuid(), let screenSpaces = Spaces.screenSpacesMap[screenUuid] { + if let screenUuid = screen.cachedUuid(), let screenSpaces = Spaces.screenSpacesMap[screenUuid] { return screenSpaces.contains { screenSpace in spaceIds.contains { $0 == screenSpace } } } } else { diff --git a/src/logic/Windows.swift b/src/logic/Windows.swift index 683d6a26..f8958697 100644 --- a/src/logic/Windows.swift +++ b/src/logic/Windows.swift @@ -136,8 +136,8 @@ class Windows { ($0.hide == .always || (window.isWindowlessApp && $0.hide != .none)) } } ?? false) && - !(Preferences.appsToShow[App.app.shortcutIndex] == .active && window.application.pid != NSWorkspace.shared.frontmostApplication?.processIdentifier) && - !(Preferences.appsToShow[App.app.shortcutIndex] == .nonActive && window.application.pid == NSWorkspace.shared.frontmostApplication?.processIdentifier) && + !(Preferences.appsToShow[App.app.shortcutIndex] == .active && window.application.pid != Applications.frontmostPid) && + !(Preferences.appsToShow[App.app.shortcutIndex] == .nonActive && window.application.pid == Applications.frontmostPid) && !(!(Preferences.showHiddenWindows[App.app.shortcutIndex] != .hide) && window.isHidden) && ((Preferences.showWindowlessApps[App.app.shortcutIndex] != .hide && window.isWindowlessApp) || !window.isWindowlessApp && @@ -191,7 +191,7 @@ class Windows { hoveredWindowIndex = nil ThumbnailsView.highlight(oldIndex) } - if let frontmostPid = NSWorkspace.shared.frontmostApplication?.processIdentifier, + if let frontmostPid = Applications.frontmostPid, let frontmostApp = Applications.findOrCreate(frontmostPid), (frontmostApp.focusedWindow == nil || Preferences.windowOrder[App.app.shortcutIndex] != .recentlyFocused), let lastFocusedOrderWindowIndex = getLastFocusedOrderWindowIndex() { diff --git a/src/logic/events/AccessibilityEvents.swift b/src/logic/events/AccessibilityEvents.swift index 0806bfdc..edc4bb0f 100644 --- a/src/logic/events/AccessibilityEvents.swift +++ b/src/logic/events/AccessibilityEvents.swift @@ -35,6 +35,7 @@ class AccessibilityEvents { } private static func applicationActivated(_ app: Application, _ pid: pid_t, _ type: String, _ appFocusedWindow: AXUIElement?) { + Applications.frontmostPid = pid if app.hasBeenActiveOnce != true { app.hasBeenActiveOnce = true } diff --git a/src/logic/events/ScreensEvents.swift b/src/logic/events/ScreensEvents.swift index e1e77d0d..a0bb32d6 100644 --- a/src/logic/events/ScreensEvents.swift +++ b/src/logic/events/ScreensEvents.swift @@ -39,7 +39,7 @@ final class Debouncer { // ScreensEvents: a screen added or removed can shuffle windows around Spaces; we refresh them // SpacesEvents: if UI was kept open during Space transition, the Spaces may be obsolete; we refresh them App.app.refreshOpenUiAfterExternalEvent(Windows.list) - Logger.info { "screens:\(NSScreen.screens.map { ($0.uuid() ?? "nil" as CFString, $0.frame) })" } + Logger.info { "screens:\(NSScreen.screens.map { ($0.cachedUuid() ?? "nil" as CFString, $0.frame) })" } Logger.info { "currentSpace:\(Spaces.currentSpaceIndex) (id:\(Spaces.currentSpaceId)) spaces:\(Spaces.screenSpacesMap)" } }) workItem = item diff --git a/src/logic/events/SpacesEvents.swift b/src/logic/events/SpacesEvents.swift index d682ab67..a6a0ef14 100644 --- a/src/logic/events/SpacesEvents.swift +++ b/src/logic/events/SpacesEvents.swift @@ -13,7 +13,7 @@ class SpacesEvents { // this window doesn't emit resize/move events. It doesn't pass isActualWindow on creation. It's added on focusedWindowChanged // for such cases, we refresh isFullscreen on Space change Windows.updateIsFullscreenOnCurrentSpace() - if let frontmostPid = NSWorkspace.shared.frontmostApplication?.processIdentifier, + if let frontmostPid = Applications.frontmostPid, let frontmostApp = Applications.findOrCreate(frontmostPid), let focusedWindow = frontmostApp.focusedWindow { App.app.checkIfShortcutsShouldBeDisabled(focusedWindow, nil) diff --git a/src/ui/App.swift b/src/ui/App.swift index e62c6ebb..b7aadd03 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -186,7 +186,7 @@ class App: AppCenterApplication { if let window = selectedWindow, MissionControl.state() == .inactive || MissionControl.state() == .showDesktop { window.focus() if Preferences.cursorFollowFocus == .always || ( - Preferences.cursorFollowFocus == .differentScreen && (Spaces.screenSpacesMap.first { $0.value.contains { space in window.spaceIds.contains(space) } })?.key != NSScreen.active()?.uuid()) { + Preferences.cursorFollowFocus == .differentScreen && (Spaces.screenSpacesMap.first { $0.value.contains { space in window.spaceIds.contains(space) } })?.key != NSScreen.active()?.cachedUuid()) { moveCursorToSelectedWindow(window) } } else {