perf: cache screen uuids and frontmost app

This commit is contained in:
lwouis committed 2026-02-11 21:35:25 +01:00
1 parent 2bce0d2d40
commit a5135c5560
8 files changed
+27 -12

No files matched your search

+1
View File
@@ -3,6 +3,7 @@ import ApplicationServices
class Applications {
static var list = [Application]()
static var frontmostPid = NSWorkspace.shared.frontmostApplication?.processIdentifier
static func initialDiscovery() {
addInitialRunningApplications()
+17 -4
View File
@@ -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
+2 -2
View File
@@ -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 {
+3 -3
View File
@@ -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() {
@@ -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
}
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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 {