Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113import Foundationpublic import ParcelCore
public enum EngineRoomFeedbackError: Error, Sendable, Equatable { case busy case systemConflict(SystemWindowConflict) case suspendedDisplay(DisplayID)}
extension ParcelEngine { public func resolveRoom(for window: WindowID, workspace: WorkspaceKey, action: RoomResolution) async throws -> EngineCommandResult { try checkRoomResolution(window: window, workspace: workspace, action: action) try Task.checkCancellation() let id = UUID() return try await withTaskCancellationHandler { try await withCheckedThrowingContinuation { reply in guard !Task.isCancelled else { reply.resume(throwing: CancellationError()) return } queue.append( Pending( event: .resolveRoom(window: window, workspace: workspace, action: action), reply: reply, commandID: id)) scheduleTurn() } } onCancel: { Task { @MainActor [weak self] in self?.cancelCommand(id) } } }
func takeRoomFeedbackBatch() -> [Pending] { while let index = queue.firstIndex(where: { if case .resolveRoom = $0.event { true } else { false } }) { if index > 0 { let pending = Array(queue.prefix(index)) queue.removeFirst(index) return pending } let pending = queue.removeFirst() guard case .resolveRoom(let window, let workspace, let action) = pending.event else { preconditionFailure("Expected a room resolution") } do { try checkRoomResolution(window: window, workspace: workspace, action: action) // Reconciliations after this event must see its manual classification override. return [pending] } catch { for detection in pending.detections ?? [] { detection.dequeue(.dropped) } if let reply = pending.reply { reply.resume(throwing: error) } else { log.record(.internalFailure(error)) } } } let pending = queue queue.removeAll(keepingCapacity: true) return pending }
func prepareRoomResolution(_ window: WindowID, workspace: WorkspaceKey, action: RoomResolution, state: inout State) { guard action == .keepFloating, state.windows[window] != nil, state.workspace(containing: window) == workspace, state.isActive(workspace), let frame = actualFrames[window] else { return } // Write receipts are newer than the reducer's last user-frame observation. state.observedFrames[window] = frame }
func finishRoomResolution(_ pending: Pending?, effects: [Effect]) -> Bool { guard let pending, case .resolveRoom(let window, _, let action) = pending.event else { return false } let reports = effects.compactMap { if case .report(let report) = $0 { report } else { nil } } let accepted: Bool switch action { case .keepFloating: accepted = !reports.contains(where: \.isFailure) if accepted { manualFloating[window] = true intrinsicFloating[window] = true if classifications[window] != nil { classifications[window]?.disposition = .float classifications[window]?.reason = .manual } cancelMotion(window, remove: false) } case .useWindowCap(let cap): accepted = effects.contains(.announce(.windowCapChanged(cap))) case .moveToDisplay(let display): accepted = effects.contains(.announce(.movedToDisplay(window, display))) } pending.reply?.resume( returning: EngineCommandResult(revision: revision, reports: reports, isAccepted: accepted)) return true }
private func checkRoomResolution(window: WindowID, workspace: WorkspaceKey, action: RoomResolution) throws { guard isRunning, writesPermitted else { throw EngineError.stopped } guard !spaceCommandRunning else { throw EngineRoomFeedbackError.busy } if let systemWindowConflict { throw EngineRoomFeedbackError.systemConflict(systemWindowConflict) } var affectedDisplays = [workspace.display] if case .moveToDisplay(let display) = action { affectedDisplays.append(display) } if let display = affectedDisplays.first(where: suspendedDisplays.contains) { throw EngineRoomFeedbackError.suspendedDisplay(display) } if operatingWindows.contains(window) { throw EngineWindowCommandError.busy(window) } // An in-flight write can still change the frame that Keep Floating promises to preserve. if let app = snapshot.state.windows[window]?.app, busyApps.contains(app.motionID) { throw EngineRoomFeedbackError.busy } }}