From 21d38da4dd0115adb30e36ca80ed021b9b36efa6 Mon Sep 17 00:00:00 2001 From: decodism Date: Thu, 21 Sep 2023 17:21:07 +0200 Subject: [PATCH] Filter level when getting window by location --- Rectangle/AccessibilityElement.swift | 2 +- Rectangle/Utilities/CFExtension.swift | 4 --- Rectangle/Utilities/WindowUtil.swift | 52 ++++++++++++++++----------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/Rectangle/AccessibilityElement.swift b/Rectangle/AccessibilityElement.swift index 1d46f0163..72f9219e5 100644 --- a/Rectangle/AccessibilityElement.swift +++ b/Rectangle/AccessibilityElement.swift @@ -297,7 +297,7 @@ extension AccessibilityElement { } private static func getWindowInfo(_ location: CGPoint) -> WindowInfo? { - let infos = WindowUtil.getWindowList().filter { !["Dock", "WindowManager"].contains($0.processName) } + let infos = WindowUtil.getWindowList().filter { $0.level < 1000 && !["Dock", "WindowManager"].contains($0.processName) } if let info = (infos.first { $0.frame.contains(location) }) { return info } diff --git a/Rectangle/Utilities/CFExtension.swift b/Rectangle/Utilities/CFExtension.swift index f0700435c..3ed970d2c 100644 --- a/Rectangle/Utilities/CFExtension.swift +++ b/Rectangle/Utilities/CFExtension.swift @@ -21,8 +21,4 @@ extension CFDictionary { func getValue(_ key: CFString) -> T { return unsafeBitCast(CFDictionaryGetValue(self, unsafeBitCast(key, to: UnsafeRawPointer.self)), to: T.self) } - - func toRect() -> CGRect? { - return CGRect(dictionaryRepresentation: self) - } } diff --git a/Rectangle/Utilities/WindowUtil.swift b/Rectangle/Utilities/WindowUtil.swift index c216915ac..2b773c6ed 100644 --- a/Rectangle/Utilities/WindowUtil.swift +++ b/Rectangle/Utilities/WindowUtil.swift @@ -11,31 +11,42 @@ class WindowUtil { private static var windowListCache = TimeoutCache<[CGWindowID]?, [WindowInfo]>(timeout: 100) static func getWindowList(ids: [CGWindowID]? = nil, all: Bool = false) -> [WindowInfo] { - if let infos = windowListCache[ids] { return infos } + if let infos = windowListCache[ids] { + return infos + } var infos = [WindowInfo]() - var array: CFArray? - if let ids = ids { - let ptr = UnsafeMutablePointer.allocate(capacity: ids.count) - for i in 0...allocate(capacity: ids.count) + for (i, id) in ids.enumerated() { + values[i] = UnsafeRawPointer(bitPattern: UInt(id)) } - let ids = CFArrayCreate(kCFAllocatorDefault, ptr, ids.count, nil) - array = CGWindowListCreateDescriptionFromArray(ids) + let rawIds = CFArrayCreate(kCFAllocatorDefault, values, ids.count, nil) + rawInfos = CGWindowListCreateDescriptionFromArray(rawIds) } else { - array = CGWindowListCopyWindowInfo([all ? .optionAll : .optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) + rawInfos = CGWindowListCopyWindowInfo([all ? .optionAll : .optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) } - if let array = array { - let count = array.getCount() + if let rawInfos { + let count = rawInfos.getCount() for i in 0..