import Foundation public import ParcelCore extension Event { var isSetupUndo: Bool { switch self { case .commandIssued(.undo), .commandForWindow(_, .undo, _): true default: false } } } extension ParcelEngine { public func beginSetupOpening(_ setup: SavedSetup) throws { guard isRunning else { throw SetupOperationError.engineUnavailable } guard !setup.memory.records.contains(where: { suspendedDisplays.contains($0.workspace.key.display) }) else { throw SetupOperationError.destinationsUnavailable } if !isFlushing, !queue.isEmpty { flush() } cancelSetupOpening() setupOpening = try SetupOpening(setup: setup, state: snapshot.state) setupOpeningReady = false stopRestoringWorkspaces(Set(setup.memory.records.map { $0.workspace.key })) let generation = lifetime setupOpeningTimeout = scheduler.schedule(after: 20) { [weak self] in guard let self, self.lifetime == generation else { return } self.finishSetupOpening(timedOut: true) } transactSetupOpening(finishing: false, cancelling: false) } public func completeSetupLaunches() { setupOpeningReady = true if !isFlushing, !queue.isEmpty { flush() } if setupOpening?.unresolvedCount == 0 { finishSetupOpening(timedOut: false) } } public func cancelSetupOpening() { guard setupOpening != nil else { return } transactSetupOpening(finishing: false, cancelling: true) } public func removeSetupHiddenApplications(_ pids: Set) { for window in snapshot.state.windows.values where pids.contains(window.app.pid) { send(.windowDisappeared(window.id)) } if !isFlushing, !queue.isEmpty { flush() } } public func removeSetupHiddenApps(_ bundles: Set) { for window in snapshot.state.windows.values where bundles.contains(window.app.bundleID) { send(.windowDisappeared(window.id)) } if !isFlushing, !queue.isEmpty { flush() } } public func flushSetupState() { if isRunning, !isFlushing, !queue.isEmpty { flush() } } func applyPendingSetup(to state: inout State, events: [Event], effects: inout [[Effect]]) -> Set { guard var opening = setupOpening else { return [] } for event in events { opening.observe(event) } let previous = state let output = opening.apply(to: &state) applySetupDispositions(to: &state, previous: previous) setupOpening = opening let manual = setupFrameWindows(output) if !effects.isEmpty { effects[effects.count - 1].append(contentsOf: output) } prepareSetupFrames(manual, state: state) return manual } func publishSetupProgress() { guard setupOpening != nil else { return } if setupOpeningReady, setupOpening?.unresolvedCount == 0 { finishSetupOpening(timedOut: false) } else { onSetupProgress?(setupOpening, false) } } private func finishSetupOpening(timedOut: Bool) { guard setupOpening != nil else { return } transactSetupOpening(finishing: true, cancelling: false, timedOut: timedOut) } private func transactSetupOpening(finishing: Bool, cancelling: Bool, timedOut: Bool = false) { guard var opening = setupOpening else { return } let previous = snapshot.state var state = previous let effects: [Effect] if cancelling { effects = opening.cancel(in: &state) } else if finishing { effects = opening.finish(in: &state) } else { effects = opening.apply(to: &state) } applySetupDispositions(to: &state, previous: previous) revision += 1 snapshot = EngineSnapshot( revision: revision, state: state, readbackFrames: snapshot.readbackFrames, displaySuspensions: snapshot.displaySuspensions) let manual = setupFrameWindows(effects).union(opening.matchedWindows.values) prepareSetupFrames(manual, state: state) if finishing || cancelling { setupOpeningTimeout?.cancel() setupOpeningTimeout = nil setupOpening = nil setupOpeningReady = false } else { setupOpening = opening } publishPlacementState() markWorkspaceMemoryChanges(previous: previous, next: state, events: [.relayout]) planFrames(manualWindows: manual) rememberWorkspaceState() execute(effects) for subscriber in subscribers.values { subscriber.yield(snapshot) } onSetupProgress?(finishing || cancelling ? opening : setupOpening, timedOut) } private func setupFrameWindows(_ effects: [Effect]) -> Set { Set( effects.flatMap { effect -> [WindowID] in if case .setFrames(let frames) = effect { return frames.map(\.window) } return [] }) } private func applySetupDispositions(to state: inout State, previous: State) { for id in state.windows.keys where previous.windows[id]?.isFloating != state.windows[id]?.isFloating { state.windows[id]?.managementReason = .user } } private func prepareSetupFrames(_ windows: Set, state: State) { for window in windows { frameAdjustments[window] = nil manualFloating[window] = state.windows[window]?.isFloating if !isAutomaticTilingEnabled { manualCommandFrames[window] = state.frames[window] } else { manualCommandFrames[window] = nil } } } }