diff --git a/Amethyst/Managers/Windows.swift b/Amethyst/Managers/Windows.swift index 73bb81fe..4f05fa61 100644 --- a/Amethyst/Managers/Windows.swift +++ b/Amethyst/Managers/Windows.swift @@ -207,22 +207,23 @@ extension WindowManager { LayoutWindow(id: $0.id(), frame: $0.frame(), isFocused: $0.isFocused()) } + let snapshotFloatingMap = floatingMap + let snapshotActiveIDCache = activeIDCache + let snapshotWindowsByID = Dictionary(uniqueKeysWithValues: windows.map { ($0.id(), $0) }) + return WindowSet( windows: layoutWindows, - isWindowWithIDActive: { [weak self] id -> Bool in - guard let window = self?.window(withID: id) else { + isWindowWithIDActive: { id -> Bool in + guard let window = snapshotWindowsByID[id] else { return false } - return self?.isWindowActive(window) ?? false + return window.isActive() && snapshotActiveIDCache.contains(window.cgID()) }, - isWindowWithIDFloating: { [weak self] windowID -> Bool in - guard let window = self?.window(withID: windowID) else { - return false - } - return self?.isWindowFloating(window) ?? false + isWindowWithIDFloating: { windowID -> Bool in + return snapshotFloatingMap[windowID] ?? false }, - windowForID: { [weak self] windowID -> Window? in - return self?.window(withID: windowID) + windowForID: { windowID -> Window? in + return snapshotWindowsByID[windowID] } ) } diff --git a/Amethyst/Model/Window.swift b/Amethyst/Model/Window.swift index 6e498a26..9be33b0e 100644 --- a/Amethyst/Model/Window.swift +++ b/Amethyst/Model/Window.swift @@ -162,19 +162,23 @@ final class AXWindowID: Hashable, Codable { } private let window: AXWindow + private let pid: pid_t + private let windowID: CGWindowID /// Equality for window IDs is based on the underlying CoreGraphics id and the owning pid, which (mostly) uniquely identifies a window. static func == (lhs: AXWindowID, rhs: AXWindowID) -> Bool { - return lhs.window.pid() == rhs.window.pid() && lhs.window.windowID() == rhs.window.windowID() + return lhs.pid == rhs.pid && lhs.windowID == rhs.windowID } func hash(into hasher: inout Hasher) { - hasher.combine(window.pid()) - hasher.combine(window.windowID()) + hasher.combine(pid) + hasher.combine(windowID) } fileprivate init(window: AXWindow) { self.window = window + self.pid = window.pid() + self.windowID = window.windowID() } init(from decoder: Decoder) throws { @@ -193,12 +197,14 @@ final class AXWindowID: Hashable, Codable { } self.window = AXWindow(axElement: window.axElementRef) + self.pid = pid + self.windowID = windowID } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(window.pid(), forKey: .pid) - try container.encode(window.windowID(), forKey: .windowID) + try container.encode(pid, forKey: .pid) + try container.encode(windowID, forKey: .windowID) } }