Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159import Foundationpublic import ParcelConfigpublic import ParcelCoreimport ParcelIPCpublic import ParcelPlatform
@MainActorpublic protocol EngineSpaceWorld: AnyObject { func spaceSnapshot() async throws -> [NativeDisplaySpaces] func executeSpaceCommand( _ destination: SpaceDestination, display: DisplayID, window: WindowID?, follow: Bool ) async throws -> SpaceCommandResult}
enum NativeSpaceMapping { static func currentSpaces(_ native: [NativeDisplaySpaces], displays: [DisplayID]) throws -> [DisplayID: SpaceID] { try Dictionary( uniqueKeysWithValues: displays.map { display in guard let entry = native.first(where: { $0.displayID.caseInsensitiveCompare(display.rawValue) == .orderedSame }) ?? (native.count == 1 && native.first?.displayID == "Main" ? native.first : nil), entry.currentSpace != 0, entry.spaces.contains(where: { $0.id == entry.currentSpace }) else { throw WindowServer.ReadError.currentSpaceUnavailable(displayID: display.rawValue) } return (display, SpaceID(rawValue: entry.currentSpace)) }) }}
extension EngineCommandParser { public static func spaceCommand(_ name: String, args: [String] = []) throws -> ConfigCommand? { if args.isEmpty, let command = ConfigCommand(rawValue: name), command.isSpaceCommand { return command } let isMove = name == "window-to-space" || name == "window-to-space-next" || name == "window-to-space-previous" var values = args let follow = values.last == "--follow" if follow && isMove { values.removeLast() } let command: ConfigCommand switch name { case "space-focus", "window-to-space": guard values.count == 1, let number = Int(values[0]), (1...16).contains(number), String(number) == values[0], !follow || isMove else { throw invalidSpaceArguments() } command = name == "space-focus" ? .spaceFocus(number) : .windowToSpace(number, follow: follow) case "space-next", "space-previous", "space-back-and-forth", "window-to-space-next", "window-to-space-previous": guard values.isEmpty, !follow || isMove else { throw invalidSpaceArguments() } switch name { case "space-next": command = .spaceNext case "space-previous": command = .spacePrevious case "space-back-and-forth": command = .spaceBackAndForth case "window-to-space-next": command = .windowToSpaceNext(follow: follow) default: command = .windowToSpacePrevious(follow: follow) } default: return nil } return command }
private static func invalidSpaceArguments() -> IPCError { IPCError( code: "invalid_command", message: String(localized: "The command or its arguments are invalid.", bundle: .module)) }}
extension ParcelEngine { public func querySpaces() async throws -> [NativeDisplaySpaces] { guard let spaceWorld else { throw spaceFailure(.displayUnavailable) } do { return try await spaceWorld.spaceSnapshot() } catch { throw spaceFailure(error) } }
public func performConfigured(_ command: ConfigCommand, activitySource: ActivitySource = .other) async throws -> EngineCommandResult { guard command.isSpaceCommand else { return try await perform(EngineCommandParser.command(command), activitySource: activitySource) } guard isRunning, writesPermitted else { throw EngineError.stopped } guard !spaceCommandRunning else { throw spaceFailure(.busy) } guard let spaceWorld, let display = snapshot.state.activeDisplay else { throw spaceFailure(.displayUnavailable) } let destination: SpaceDestination let move: Bool let follow: Bool switch command { case .spaceFocus(let number): (destination, move, follow) = (.desktop(number), false, true) case .spaceNext: (destination, move, follow) = (.next, false, true) case .spacePrevious: (destination, move, follow) = (.previous, false, true) case .spaceBackAndForth: (destination, move, follow) = (.backAndForth, false, true) case .windowToSpace(let number, let following): (destination, move, follow) = (.desktop(number), true, following) case .windowToSpaceNext(let following): (destination, move, follow) = (.next, true, following) case .windowToSpacePrevious(let following): (destination, move, follow) = (.previous, true, following) default: preconditionFailure("Expected a native Space command.") } let window = move ? snapshot.focusedWindow : nil if move && window == nil { throw spaceFailure(.windowUnavailable) } beginSpaceOperation() defer { endSpaceOperation() } do { let deadline = ContinuousClock.now.advanced(by: .milliseconds(750)) while !busyApps.isEmpty || focusOperations > 0 || !windowOperations.isEmpty { guard ContinuousClock.now < deadline else { throw SpaceCommandError.busy } try await Task.sleep(for: .milliseconds(10)) } try Task.checkCancellation() guard isRunning else { throw EngineError.stopped } let operation = Task { @MainActor in try await spaceWorld.executeSpaceCommand(destination, display: display, window: window, follow: follow) } spaceOperation = operation lastSpaceCommandResult = try await withTaskCancellationHandler { try await operation.value } onCancel: { operation.cancel() } spaceOperation = nil guard isRunning else { throw EngineError.stopped } let displays = try await spaceWorld.spaceSnapshot() for native in displays { let ids = snapshot.state.displays.keys.filter { $0.rawValue.caseInsensitiveCompare(native.displayID) == .orderedSame || native.displayID == "Main" } for id in ids { enqueue(.activeSpaceChanged(display: id, space: SpaceID(rawValue: native.currentSpace))) } } world.reconcile(Set(snapshot.state.windows.values.map(\.app))) return EngineCommandResult(revision: snapshot.revision, reports: [], isAccepted: true) } catch { spaceOperation = nil log.record(.internalFailure(error)) if error is CancellationError { throw error } throw spaceFailure(error) } }
func beginSpaceOperation() { spaceCommandRunning = true cancelMouseGesture() suspendMotion() }
func endSpaceOperation() { spaceCommandRunning = false guard isRunning, isAutomaticTilingEnabled else { return } snapNextPass = true needsRetile = true scheduleTurn() }
func spaceFailure(_ error: SpaceCommandError) -> IPCError { spaceFailure(error as any Error) }
func spaceFailure(_ error: any Error) -> IPCError { IPCError(code: "space_command_failed", message: SpaceNotice.message(for: error)) }}