import AppKit public import Foundation public import ParcelConfig public import ParcelCore @MainActor public final class StackChipController { private(set) var panels: [LearningPanel] = [] private let orderPanels: Bool private let reduceMotion: @MainActor () -> Bool public private(set) var groups: [StackChipGroup] = [] public private(set) var dropTargets: [StackDropTarget] = [] public private(set) var isDragging = false public var onDragStateChanged: (@MainActor (Bool) -> Void)? private var cancellingDrag = false private var motionEnabled: Bool? private let focus: @MainActor (WindowID) -> Void private let close: @MainActor (WindowID) -> Void private let cycle: (@MainActor (WindowID, CycleDirection) -> Void)? private let move: (@MainActor (WindowID, WindowID) -> Void)? private var drag = StackChipDrag() private var sourceWindows: [WindowID] = [] private var sourceApp: AppID? private var sourceWorkspace: WorkspaceKey? private var preview: LearningPanel? private var configuration = MotionConfiguration() private let grips: StackGripController var usesGrip = false private var presentedWindow: WindowID? private var state = State() private var keyboardReturnWindow: (window: WindowID, app: AppID)? public func showChips(for window: WindowID) { guard presentedWindow != window, groups.contains(where: { $0.windows.contains(window) }) else { return } presentedWindow = window update(state: state, configuration: configuration) } public func hideChips() { guard presentedWindow != nil, drag.source == nil, !panels.contains(where: \.isKeyWindow) else { return } presentedWindow = nil keyboardReturnWindow = nil update(state: state, configuration: configuration) } private func dismissChips() { let previous = keyboardReturnWindow keyboardReturnWindow = nil for panel in panels { panel.orderOut(nil) } presentedWindow = nil update(state: state, configuration: configuration) if let previous, state.windows[previous.window]?.app == previous.app { focus(previous.window) } else if let window = state.focusedWindow { focus(window) } } private func refreshPresentation() { guard usesGrip, orderPanels else { return } for panel in panels { let windows = (panel.contentView as? StackChipView)?.windows ?? [] if presentedWindow.map(windows.contains) == true || isDragging || panel.isKeyWindow { if !panel.isVisible { if let layer = panel.contentView?.layer { LearningMotion.show(layer, token: .pop, configuration: configuration) } panel.orderFront(nil) } } else { panel.orderOut(nil) } } for panel in grips.panels { let windows = (panel.contentView as? StackGripView)?.grip.windows if panels.contains(where: { $0.isVisible && (($0.contentView as? StackChipView)?.windows == windows || $0.frame.intersects(panel.frame)) }) { panel.orderOut(nil) } else if !panel.isVisible { panel.orderFront(nil) } } } public convenience init( focus: @escaping @MainActor (WindowID) -> Void, close: @escaping @MainActor (WindowID) -> Void, cycle: (@MainActor (WindowID, CycleDirection) -> Void)? = nil, move: (@MainActor (WindowID, WindowID) -> Void)? = nil ) { self.init(focus: focus, close: close, cycle: cycle, move: move, orderPanels: true) } init( focus: @escaping @MainActor (WindowID) -> Void, close: @escaping @MainActor (WindowID) -> Void, cycle: (@MainActor (WindowID, CycleDirection) -> Void)? = nil, move: (@MainActor (WindowID, WindowID) -> Void)? = nil, orderPanels: Bool, reduceMotion: @escaping @MainActor () -> Bool = { NSWorkspace.shared.accessibilityDisplayShouldReduceMotion } ) { self.focus = focus self.close = close self.cycle = cycle self.move = move self.orderPanels = orderPanels self.reduceMotion = reduceMotion grips = StackGripController(orderPanels: orderPanels) } public func update(state: State, configuration: MotionConfiguration) { self.state = state let configurationChanged = self.configuration != configuration self.configuration = configuration if usesGrip { grips.select = focus grips.showControls = { [weak self] window in self?.showChips(for: window) self?.focusChips() } grips.update(state: state, configuration: configuration) } let geometry = usesGrip ? StackGrip.observedGeometry(in: state) : state let next = usesGrip && presentedWindow == nil ? StackChips.groups(in: geometry, compact: true) : StackChips.groups(in: geometry, expanding: presentedWindow) defer { refreshPresentation() } let nextTargets = StackChips.dropTargets(in: state) let targetsChanged = dropTargets != nextTargets dropTargets = nextTargets refreshDragContext() let motionEnabled = configuration.enabled && !reduceMotion() guard groups != next || targetsChanged || configurationChanged || self.motionEnabled != motionEnabled || (orderPanels && !usesGrip && panels.contains(where: { !$0.isVisible })) else { return } self.motionEnabled = motionEnabled var remaining = panels var updated: [LearningPanel] = [] for group in next { let panel: LearningPanel let destinations = dropTargets if let index = remaining.firstIndex(where: { ($0.contentView as? StackChipView)?.windows == group.windows }) { panel = remaining.remove(at: index) (panel.contentView as? StackChipView)?.update(group, targets: destinations) } else { panel = LearningPanel(title: group.accessibilityLabel, interactive: true) let moveAction: (@MainActor (WindowID, WindowID) -> Void)? if move != nil { moveAction = { [weak self] source, target in self?.moveWindow(source, target: target) } } else { moveAction = nil } let view = StackChipView( group: group, focus: focus, close: close, cycle: { [weak self] source, direction in self?.cycleStack(source, direction: direction) }, move: moveAction, dragBegan: { [weak self] source, point in self?.beginDrag(source, at: point) }, dragChanged: { [weak self] point in self?.updateDrag(at: point) }, dragEnded: { [weak self] point in self?.endDrag(at: point) }, dragCancelled: { [weak self] in self?.cancelDrag() }) view.update(group, targets: destinations) view.onDismiss = { [weak self] in self?.dismissChips() } panel.contentView = view if let layer = view.layer { LearningMotion.show(layer, token: .pop, configuration: configuration) } } if !motionEnabled { panel.contentView?.layer?.removeAnimation(forKey: "learningScale") } if !configuration.enabled { panel.contentView?.layer?.removeAllAnimations() } if !groups.contains(where: { $0.windows == group.windows && $0.frame == group.frame }) { panel.place(group.frame) } if orderPanels, !usesGrip, !panel.isVisible { panel.orderFront(nil) } updated.append(panel) } for panel in remaining { panel.close() } panels = updated groups = next } public func updateDragContext(state: State, configuration: MotionConfiguration) { self.state = state self.configuration = configuration presentedWindow = nil keyboardReturnWindow = nil grips.stop() for panel in panels { panel.close() } panels = [] groups = [] motionEnabled = nil dropTargets = StackChips.dropTargets(in: state) refreshDragContext() } private func sourceStack(for window: WindowID) -> (workspace: WorkspaceKey, windows: [WindowID])? { guard state.windows[window]?.isFloating == false, let key = state.workspace(containing: window), state.isActive(key), let stack = state.workspaces[key]?.tree?.stacks.first(where: { $0.windows.contains(window) }) else { return nil } return (key, stack.windows) } private func refreshDragContext() { guard let source = drag.source else { return } guard let stack = sourceStack(for: source), stack.windows == sourceWindows, stack.workspace == sourceWorkspace, state.windows[source]?.app == sourceApp else { cancelDrag() return } if let point = drag.point { drag.update(at: point, targets: dropTargets) updateDragState() updatePreview() } } public func frame(of window: WindowID) -> CGRect? { guard let group = groups.first(where: { $0.windows.contains(window) }), let chip = group.chips.first(where: { $0.window == window }) else { return nil } return chip.frame.offsetBy(dx: group.frame.minX, dy: group.frame.minY) } public func controlFrame(for window: WindowID, control: StackControl) -> CGRect? { guard let group = groups.first(where: { $0.windows.contains(window) }), group.active else { return nil } return group.frame(for: control).offsetBy(dx: group.frame.minX, dy: group.frame.minY) } public func focusChips() { guard orderPanels else { return } if usesGrip, presentedWindow == nil { let target = groups.first { group in state.focusedWindow.map(group.windows.contains) == true } ?? groups.first if let target { showChips(for: target.visible) } } let panel = panels.first { panel in presentedWindow.map { window in (panel.contentView as? StackChipView)?.windows.contains(window) == true } == true } ?? panels.first { ($0.contentView as? StackChipView)?.active == true } ?? panels.first guard let panel, let view = panel.contentView as? StackChipView else { return } if !panels.contains(where: \.isKeyWindow), let window = state.focusedWindow, let app = state.windows[window]?.app { keyboardReturnWindow = (window, app) } NSApplication.shared.activate() presentedWindow = view.windows.first refreshPresentation() panel.makeKeyAndOrderFront(nil) panel.makeFirstResponder(view.selectedButton) } public func stop() { presentedWindow = nil keyboardReturnWindow = nil grips.stop() cancelDrag() for panel in panels { panel.close() } panels = [] groups = [] dropTargets = [] motionEnabled = nil state = State() } private func cycleStack(_ source: WindowID, direction: CycleDirection) { guard let group = groups.first(where: { $0.windows.contains(source) }) else { return } if let cycle { cycle(group.visible, direction) } else if let index = group.windows.firstIndex(of: group.visible) { let offset = direction == .next ? 1 : group.windows.count - 1 focus(group.windows[(index + offset) % group.windows.count]) } } private func moveWindow(_ source: WindowID, target: WindowID) { guard sourceStack(for: source) != nil, dropTargets.contains(where: { $0.window == target && !$0.windows.contains(source) }) else { return } cancelDrag() move?(source, target) } public func beginDrag(_ source: WindowID, at point: CGPoint) { guard move != nil, let stack = sourceStack(for: source) else { return } cancelDrag() sourceWindows = stack.windows sourceWorkspace = stack.workspace sourceApp = state.windows[source]?.app drag.begin(source: source, at: point) } public func updateDrag(at point: CGPoint) { drag.update(at: point, targets: dropTargets) updateDragState() updatePreview() } public func endDrag(at point: CGPoint) { let action = drag.release(at: point, targets: dropTargets) cancelDrag() if let action { moveWindow(action.source, target: action.target) } } public func cancelDrag() { guard !cancellingDrag else { return } cancellingDrag = true defer { cancellingDrag = false } drag.cancel() sourceWindows = [] sourceWorkspace = nil sourceApp = nil preview?.close() preview = nil for panel in panels { (panel.contentView as? StackChipView)?.cancelPointerDrag() } updateDragState() } private func updateDragState() { guard isDragging != drag.hasDragged else { return } isDragging = drag.hasDragged onDragStateChanged?(isDragging) } private func updatePreview() { guard let target = drag.target else { preview?.close() preview = nil return } let panel: LearningPanel if let existing = preview { panel = existing } else { panel = LearningPanel(title: UIStrings.text("Move here"), interactive: false) let view = StackDropPreview(frame: CGRect(origin: .zero, size: target.frame.size)) panel.contentView = view preview = panel if let layer = view.layer { LearningMotion.show(layer, token: .pop, configuration: configuration) } } panel.contentView?.frame.size = target.frame.size panel.contentView?.needsDisplay = true if !configuration.enabled { panel.contentView?.layer?.removeAllAnimations() } if reduceMotion() { panel.contentView?.layer?.removeAnimation(forKey: "learningScale") } panel.place(target.frame) if orderPanels { panel.orderFront(nil) } } }