public import Foundation public import ParcelCore public enum CycleDirection: Equatable, Sendable { case previous, next } public enum StackControl: Int, Equatable, Sendable { case previous = -1 case next = -2 case move = -3 public var accessibilityLabel: String { switch self { case .previous: UIStrings.text("Previous stacked window") case .next: UIStrings.text("Next stacked window") case .move: UIStrings.text("Move Window to Tile…") } } } public struct StackDropTarget: Equatable, Sendable { public let window: WindowID public let windows: [WindowID] public let workspace: WorkspaceKey public let frame: CGRect public let title: String public var accessibilityLabel: String { String(localized: "Move to tile: \(title)", table: "UI", bundle: .module) } } public enum StackChipHit: Equatable, Sendable { case chip(WindowID) case control(StackControl) } extension StackChipGroup { public func frame(for control: StackControl) -> CGRect { guard active else { return .zero } let width = 22.0 let x: Double switch control { case .previous: x = 0 case .next: x = frame.width - width * 2 case .move: x = frame.width - width } return CGRect(x: x, y: 0, width: width, height: frame.height) } public func hit(at point: CGPoint) -> StackChipHit? { guard point.x.isFinite, point.y.isFinite else { return nil } if let chip = chips.first(where: { $0.frame.contains(point) }) { return .chip(chip.window) } for control in [StackControl.previous, .next, .move] where frame(for: control).contains(point) { return .control(control) } return nil } } extension StackChips { public static func dropTargets(in state: State) -> [StackDropTarget] { guard state.visibleWindows.allSatisfy({ state.observedFrames[$0] != nil }) else { return [] } let occupied = state.visibleWindows.compactMap { id -> (WindowID, CGRect)? in guard let frame = state.observedFrames[id]?.learningRect else { return nil } return (id, frame) } return state.workspaces.keys.sorted().filter(state.isActive).flatMap { key -> [StackDropTarget] in guard let workspace = state.workspaces[key], let display = state.displays[key.display]?.frame.learningRect else { return [] } return (workspace.tree?.stacks ?? []).compactMap { stack in guard state.windows[stack.visible]?.isFloating == false, let frame = state.frames[stack.visible]?.learningRect, state.observedFrames[stack.visible]?.learningRect == frame, frame.isFiniteAndPositive, display.contains(frame), !occupied.contains(where: { $0.0 != stack.visible && $0.1.intersects(frame) }) else { return nil } let title = state.windows[stack.visible]?.title ?? "" return StackDropTarget( window: stack.visible, windows: stack.windows, workspace: key, frame: frame, title: title.isEmpty ? UIStrings.text("Untitled window") : title) } } } } enum StackScrollPhase: Sendable { case none, began, changed, ended, cancelled, momentum } struct StackScroll: Sendable { private var accumulated = 0.0 private var consumedGesture = false private var lastAction = -Double.infinity mutating func receive( deltaX: Double, deltaY: Double, precise: Bool, phase: StackScrollPhase, at time: Double ) -> CycleDirection? { guard deltaX.isFinite, deltaY.isFinite, time.isFinite, time >= 0 else { return nil } switch phase { case .momentum: return nil case .ended, .cancelled: accumulated = 0 consumedGesture = false return nil case .began: accumulated = 0 consumedGesture = false case .none, .changed: break } guard !consumedGesture else { return nil } let delta = abs(deltaY) >= abs(deltaX) ? deltaY : deltaX guard delta != 0 else { return nil } if !precise { guard time - lastAction >= 0.12 else { return nil } lastAction = time return delta < 0 ? .next : .previous } if accumulated * delta < 0 { accumulated = 0 } accumulated += delta guard abs(accumulated) >= 24, time - lastAction >= 0.12 else { return nil } let direction: CycleDirection = accumulated < 0 ? .next : .previous accumulated = 0 lastAction = time consumedGesture = phase != .none return direction } } struct StackMove: Equatable, Sendable { let source: WindowID let target: WindowID } struct StackChipDrag: Sendable { private(set) var source: WindowID? private(set) var target: StackDropTarget? private(set) var point: CGPoint? private(set) var hasDragged = false private var origin: CGPoint? mutating func begin(source: WindowID, at point: CGPoint) { cancel() guard point.x.isFinite, point.y.isFinite else { return } self.source = source origin = point self.point = point } mutating func update(at point: CGPoint, targets: [StackDropTarget]) { guard let source, let origin, point.x.isFinite, point.y.isFinite else { target = nil return } self.point = point if hypot(point.x - origin.x, point.y - origin.y) >= 4 { hasDragged = true } target = hasDragged ? targets.first { !$0.windows.contains(source) && $0.frame.contains(point) } : nil } mutating func release(at point: CGPoint, targets: [StackDropTarget]) -> StackMove? { update(at: point, targets: targets) defer { cancel() } guard let source, let target else { return nil } return StackMove(source: source, target: target.window) } mutating func cancel() { source = nil target = nil point = nil origin = nil hasDragged = false } }