import ParcelAnimation import ParcelCore struct ExplicitFloatPlacement { let app: AppID let workspace: WorkspaceKey let area: Rect let requested: Rect var generation: UInt64? var correction: Rect? var adopting: Rect? } extension ParcelEngine { func prepareExplicitFloatPlacements(_ pending: [Pending], previous: State, next: State) { for window in Array(explicitFloatPlacements.keys) { guard let intent = explicitFloatPlacements[window] else { continue } if let actual = intent.adopting, next.frames[window] == actual, next.observedFrames[window] == actual { cancelExplicitFloatPlacement(window) } else if !floatPlacementIsCurrent(intent, window: window) { cancelMotion(window, remove: false) } } guard pending.contains(where: { item in switch item.event { case .commandIssued(.toggleFloat), .workspaceCommandIssued(.toggleFloat, _), .commandForWindow(_, .toggleFloat, _): true default: false } }) else { return } for (window, managed) in next.windows where previous.windows[window]?.isFloating == false && managed.isFloating && managed.managementReason == .user { guard let key = next.workspace(containing: window), let workspace = next.workspaces[key], let display = next.displays[key.display], display.hasUsableGeometry, next.isActive(key), let target = next.frames[window] else { continue } let area = display.frame.inset(by: workspace.reservedEdges).inset(by: workspace.gaps.outer) .roundedToWholePoints() explicitFloatPlacements[window] = ExplicitFloatPlacement( app: managed.app, workspace: key, area: area, requested: target) } } func bindExplicitFloatPlacement(_ window: WindowID, generation: UInt64) { guard var intent = explicitFloatPlacements[window], intent.generation == nil else { return } intent.generation = generation explicitFloatPlacements[window] = intent } func cancelExplicitFloatPlacement(_ window: WindowID) { guard let intent = explicitFloatPlacements.removeValue(forKey: window) else { return } if let manual = manualCommandFrames[window], manual == intent.requested || manual == intent.correction || manual == intent.adopting { manualCommandFrames[window] = nil } } func acceptExplicitFloatReadback(_ observed: WindowFrame, sent: MotionFrame) -> Bool { let window = observed.window guard var intent = explicitFloatPlacements[window] else { return false } guard intent.generation == sent.generation, floatPlacementIsCurrent(intent, window: window), Rect(sent.rect) == (intent.correction ?? intent.requested), !dragging.contains(window), !liveResizeWindows.contains(window), !isWaitingForTour, writesPermitted, !unmanageableApps.contains(intent.app), !wedgedApps.contains(intent.app), !suspendedDisplays.contains(intent.workspace.display) else { cancelExplicitFloatPlacement(window) return false } if intent.adopting != nil { return true } if intent.correction == nil, observed.frame.size != intent.requested.size { let centered = Rect( x: max(intent.area.minX, intent.area.center.xCoordinate - observed.frame.size.width / 2).rounded(), y: max(intent.area.minY, intent.area.center.yCoordinate - observed.frame.size.height / 2).rounded(), width: observed.frame.size.width, height: observed.frame.size.height) if !observed.frame.matches(centered) { intent.correction = centered explicitFloatPlacements[window] = intent manualCommandFrames[window] = centered targets[window] = nil needsRetile = true scheduleTurn() return true } } intent.adopting = observed.frame explicitFloatPlacements[window] = intent manualCommandFrames[window] = observed.frame frameAdjustments[window] = nil correctiveWrites[window] = nil minimumCandidates[window] = nil enqueue( observed.frame.size == intent.requested.size ? .windowMovedByUser(window, frame: observed.frame) : .windowResizedByUser(window, frame: observed.frame), explicitFloatReadback: (window, intent)) return true } func acceptsExplicitFloatReadback(_ pending: Pending, in state: State) -> Bool { guard let adoption = pending.explicitFloatReadback else { return true } guard let generation = adoption.intent.generation, explicitFloatPlacements[adoption.window]?.generation == generation, coalescer.generation(for: adoption.window.motionID) == generation else { return false } return floatPlacementIsCurrent(adoption.intent, window: adoption.window, state: state) } private func floatPlacementIsCurrent(_ intent: ExplicitFloatPlacement, window: WindowID, state: State? = nil) -> Bool { let state = state ?? snapshot.state guard let managed = state.windows[window], managed.app == intent.app, managed.isFloating, state.workspace(containing: window) == intent.workspace, state.isActive(intent.workspace), let workspace = state.workspaces[intent.workspace], let display = state.displays[intent.workspace.display], display.hasUsableGeometry, display.frame.inset(by: workspace.reservedEdges).inset(by: workspace.gaps.outer).roundedToWholePoints() == intent.area else { return false } return state.frames[window] == intent.requested || intent.adopting.map { state.frames[window] == $0 } == true } }