import ParcelCore import Synchronization final class NativeFullscreenCancellation: Sendable { private let cancelled = Mutex(false) func cancel() { cancelled.withLock { $0 = true } } func check() throws { if cancelled.withLock({ $0 }) { throw CancellationError() } } } @MainActor func performNativeFullscreenTransition( window: WindowID, retain: @MainActor () async throws -> Request, change: @MainActor (Request, NativeFullscreenCancellation) async throws -> Bool, read: @MainActor (Request) async throws -> Bool, release: @MainActor (Request) async -> Void, timeout: Duration = .seconds(3) ) async throws { let cancellation = NativeFullscreenCancellation() try await withTaskCancellationHandler { try Task.checkCancellation() let request = try await retain() do { try Task.checkCancellation() let target = try await change(request, cancellation) let deadline = ContinuousClock.now.advanced(by: timeout) repeat { try Task.checkCancellation() let actual = try await read(request) try Task.checkCancellation() if actual == target { await release(request) return } try await Task.sleep(for: .milliseconds(25)) } while ContinuousClock.now < deadline throw EngineWindowCommandError.fullscreenUnchanged(window) } catch { await release(request) throw error } } onCancel: { // A queued AX operation runs outside the caller's task context. cancellation.cancel() } }