fix: prevent initial window lastFocusOrder overlaps

The `sortByLevel` method in the Windows class has been updated to determine window z-order properly across all visible spaces. It now sets the `lastFocusOrder` for each window, using a map from window IDs to their z-order position. Windows not present in the list are set to have `lastFocusOrder` of `Int.max`. Afterwards, windows are sorted by their `lastFocusOrder`, and these values are ensured to be monotonic, starting from 0.
This commit is contained in:
Ben Delaney authored and lwouis committed 2024-12-24 02:17:58 +01:00
1 parent 2a0a8585df
commit d32f9533e3
1 file changed
+14 -14
+14 -14
View File
@@ -240,24 +240,24 @@ class Windows {
}
}
/// Updates windows "lastFocusOrder" to ensure unique values based on window z-order.
/// Windows are ordered by their position in Spaces.windowsInSpaces() results,
/// with topmost windows first.
static func sortByLevel() {
var windowLevelMap = [CGWindowID: Int]()
for (index, cgWindowId) in Spaces.windowsInSpaces([Spaces.currentSpaceId]).enumerated() {
var windowLevelMap = [CGWindowID?: Int]()
for (index, cgWindowId) in Spaces.windowsInSpaces(Spaces.visibleSpaces).enumerated() {
windowLevelMap[cgWindowId] = index
}
var sortedTuples = Windows.list
.filter { $0.cgWindowId != nil }
.map { (windowLevelMap[$0.cgWindowId!], $0) }
sortedTuples.sort(by: {
if $0.0 == nil {
return false
var orderForUnlistedWindows = Int.max
for window in list {
if let listedOrder = windowLevelMap[window.cgWindowId] {
window.lastFocusOrder = listedOrder
} else {
window.lastFocusOrder = orderForUnlistedWindows
orderForUnlistedWindows -= 1
}
if $1.0 == nil {
return true
}
return $0.0! < $1.0!
})
Windows.list = sortedTuples.map { $0.1 }
}
list.sort { $0.lastFocusOrder < $1.lastFocusOrder }
}
static func refreshThumbnailsAsync(_ screen: NSScreen, _ currentIndex: Int) {