Files
epod-window-bar/Sources/Private/SpaceService.swift
Erhan Keseli 70d7ca5efe EPOD Window Bar: a window strip above the macOS Dock
EPOD Window Bar is a macOS agent app. It shows a thin strip above the Dock
with one tab per window of the frontmost application. One click raises the
window.

Build from source with XcodeGen and Xcode. Signing uses Config.xcconfig
(ad-hoc by default; a local Local.xcconfig can set a team ID). The app needs
Accessibility and Screen Recording permissions, and runs in a reduced mode
without Screen Recording.

See README.md and docs/ for details.
2026-11-21 21:41:06 +01:00

69 lines
2.7 KiB
Swift

import Foundation
import CoreGraphics
// SpaceService reports the Space (desktop) of each window. The service uses
// the private CGS API (PrivateAPI). If the private API is missing, the service
// reports no Spaces. The caller then treats all windows as the current Space
// (spec section 7.5). The service is main-actor isolated.
@MainActor
final class SpaceService {
private let api: PrivateAPI
init(api: PrivateAPI? = nil) {
self.api = api ?? .shared
}
// isAvailable is true if the Space API loaded.
var isAvailable: Bool {
return api.spacesAvailable
}
// spaces returns a map from window ID to its Space IDs. If the API is
// missing, the function returns an empty map.
func spaces(for windowIDs: [CGWindowID]) -> [CGWindowID: [UInt64]] {
guard isAvailable, windowIDs.isEmpty == false else { return [:] }
var result: [CGWindowID: [UInt64]] = [:]
// CGSCopySpacesForWindows returns the union of Spaces for the whole
// list. The service queries one window at a time to map each window to
// its own Space IDs.
for id in windowIDs {
if let spaces = api.spaces(forWindows: [id]) {
result[id] = spaces
}
}
return result
}
// currentSpaceIDs returns the current Space ID of each display. The service
// reads the managed display Space list. If the API is missing, the function
// returns an empty array.
func currentSpaceIDs() -> [UInt64] {
guard let displays = api.managedDisplaySpaces() else { return [] }
var ids: [UInt64] = []
for display in displays {
// Each display dictionary has a "Current Space" dictionary with the
// key "ManagedSpaceID" (UInt64). Source: yabai, AltTab.
if let current = display["Current Space"] as? [String: Any],
let id = current["ManagedSpaceID"] as? UInt64 {
ids.append(id)
} else if let current = display["Current Space"] as? [String: Any],
let number = current["ManagedSpaceID"] as? NSNumber {
ids.append(number.uint64Value)
}
}
return ids
}
// isOnCurrentSpace returns true if any Space ID of the window is a current
// Space ID. If the service has no data, the function returns true. The app
// then treats the window as a window on the current Space.
func isOnCurrentSpace(windowSpaceIDs: [UInt64], currentSpaceIDs: [UInt64]) -> Bool {
if windowSpaceIDs.isEmpty || currentSpaceIDs.isEmpty { return true }
for id in windowSpaceIDs where currentSpaceIDs.contains(id) {
return true
}
return false
}
}