Created
May 15, 2025 05:26
-
-
Save julianschiavo/6472bbbe10359133765e95d339e25fb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------------------------------- | |
// CGSSpace.swift by Julian Schiavo | |
// Originally from avaidyam/Parrot | |
// | |
// This file is a lightly modified derivative of the original CGSSpace.swift | |
// file from the “Parrot” repository by avaidyam, made available under the | |
// Mozilla Public License, Version 2.0 (“MPL‑2.0”). | |
// | |
// All modifications herein are governed by MPL‑2.0. You are granted the rights | |
// to use, modify, and redistribute this file in accordance with the license’s | |
// terms and conditions. Any use of this file, in source or binary form, must | |
// reproduce this header and include a copy of the full MPL‑2.0 text. | |
// | |
// Original Source: | |
// https://github.com/avaidyam/Parrot/ | |
// blob/6cf7ba419176c386ed8f18e838690a7272fe57ee/ | |
// MochaUI/CGSSpace.swift | |
// Full License Text: | |
// https://github.com/avaidyam/Parrot/ | |
// blob/6cf7ba419176c386ed8f18e838690a7272fe57ee/LICENSE.md | |
// | |
// When distributing public app releases, you must include a copy of the MPL‑2.0 | |
// license text and a link to (this) Github Gist in the distributed app. | |
// ----------------------------------------------------------------------------- | |
import AppKit | |
final class CGSSpace { | |
private let identifier: CGSSpaceID | |
public var windows: Set<NSWindow> = [] { | |
didSet { | |
let remove = oldValue.subtracting(windows) | |
let add = windows.subtracting(oldValue) | |
CGSRemoveWindowsFromSpaces( | |
_CGSDefaultConnection(), | |
remove.map { $0.windowNumber } as NSArray, | |
[identifier] | |
) | |
CGSAddWindowsToSpaces( | |
_CGSDefaultConnection(), | |
add.map { $0.windowNumber } as NSArray, | |
[identifier]) | |
} | |
} | |
public init(level: Int = 0) { | |
let flag = 0x1 // this value MUST be 1, otherwise, Finder decides to draw desktop icons | |
self.identifier = CGSSpaceCreate(_CGSDefaultConnection(), flag, nil) | |
CGSSpaceSetAbsoluteLevel(_CGSDefaultConnection(), identifier, level) | |
CGSShowSpaces(_CGSDefaultConnection(), [identifier]) | |
} | |
deinit { | |
CGSHideSpaces(_CGSDefaultConnection(), [self.identifier]) | |
CGSSpaceDestroy(_CGSDefaultConnection(), self.identifier) | |
} | |
} | |
fileprivate typealias CGSConnectionID = UInt | |
fileprivate typealias CGSSpaceID = UInt64 | |
@_silgen_name("_CGSDefaultConnection") | |
fileprivate func _CGSDefaultConnection() -> CGSConnectionID | |
@_silgen_name("CGSSpaceCreate") | |
fileprivate func CGSSpaceCreate(_ cid: CGSConnectionID, _ unknown: Int, _ options: NSDictionary?) -> CGSSpaceID | |
@_silgen_name("CGSSpaceDestroy") | |
fileprivate func CGSSpaceDestroy(_ cid: CGSConnectionID, _ space: CGSSpaceID) | |
@_silgen_name("CGSSpaceSetAbsoluteLevel") | |
fileprivate func CGSSpaceSetAbsoluteLevel(_ cid: CGSConnectionID, _ space: CGSSpaceID, _ level: Int) | |
@_silgen_name("CGSAddWindowsToSpaces") | |
fileprivate func CGSAddWindowsToSpaces(_ cid: CGSConnectionID, _ windows: NSArray, _ spaces: NSArray) | |
@_silgen_name("CGSRemoveWindowsFromSpaces") | |
fileprivate func CGSRemoveWindowsFromSpaces(_ cid: CGSConnectionID, _ windows: NSArray, _ spaces: NSArray) | |
@_silgen_name("CGSHideSpaces") | |
fileprivate func CGSHideSpaces(_ cid: CGSConnectionID, _ spaces: NSArray) | |
@_silgen_name("CGSShowSpaces") | |
fileprivate func CGSShowSpaces(_ cid: CGSConnectionID, _ spaces: NSArray) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment