diff --git a/Ice/Bridging/Bridging.swift b/Ice/Bridging/Bridging.swift index 6bc3b589..c42abe84 100644 --- a/Ice/Bridging/Bridging.swift +++ b/Ice/Bridging/Bridging.swift @@ -261,36 +261,50 @@ extension Bridging { // MARK: - CGSSpace extension Bridging { + /// Options that determine the space identifiers to return in a space list. + enum SpaceListOption { + case allSpaces + case visibleSpaces + } + /// The identifier of the active space. - static var activeSpaceID: Int { + static var activeSpaceID: CGSSpaceID { CGSGetActiveSpace(CGSMainConnectionID()) } - /// Returns a Boolean value that indicates whether the window with the - /// given identifier is on the active space. + /// Returns an array of identifiers for the spaces containing the window with + /// the given identifier. /// /// - Parameter windowID: An identifier for a window. - static func isWindowOnActiveSpace(_ windowID: CGWindowID) -> Bool { - guard let spaces = CGSCopySpacesForWindows( - CGSMainConnectionID(), - CGSSpaceMask.allSpaces, - [windowID] as CFArray - ) else { + static func getSpaceList(for windowID: CGWindowID, option: SpaceListOption) -> [CGSSpaceID] { + let mask: CGSSpaceMask = switch option { + case .allSpaces: .allSpaces + case .visibleSpaces: .allVisibleSpaces + } + guard let spaces = CGSCopySpacesForWindows(CGSMainConnectionID(), mask, [windowID] as CFArray) else { Logger.bridging.error("CGSCopySpacesForWindows failed") - return false + return [] } guard let spaceIDs = spaces.takeRetainedValue() as? [CGSSpaceID] else { Logger.bridging.error("CGSCopySpacesForWindows returned array of unexpected type") - return false + return [] } - return Set(spaceIDs).contains(activeSpaceID) + return spaceIDs + } + + /// Returns a Boolean value that indicates whether the window with the + /// given identifier is on the active space. + /// + /// - Parameter windowID: An identifier for a window. + static func isWindowOnActiveSpace(_ windowID: CGWindowID) -> Bool { + getSpaceList(for: windowID, option: .allSpaces).contains(activeSpaceID) } /// Returns a Boolean value that indicates whether the space with the given /// identifier is a fullscreen space. /// /// - Parameter spaceID: An identifier for a space. - static func isSpaceFullscreen(_ spaceID: Int) -> Bool { + static func isSpaceFullscreen(_ spaceID: CGSSpaceID) -> Bool { let type = CGSSpaceGetType(CGSMainConnectionID(), spaceID) return type == .fullscreen }