Skip to content

Commit

Permalink
Update Bridging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Jul 5, 2024
1 parent 3c03c0d commit f063ee7
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions Ice/Bridging/Bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f063ee7

Please sign in to comment.