import AppKit import ParcelConfig import ParcelCore @MainActor final class StackGripController { private(set) var groups: [StackGrip] = [] private(set) var panels: [LearningPanel] = [] var select: @MainActor (WindowID) -> Void = { _ in } var showControls: @MainActor (WindowID) -> Void = { _ in } private let orderPanels: Bool init(orderPanels: Bool = true) { self.orderPanels = orderPanels } func update(state: State, configuration: MotionConfiguration, accessibility: OverlayAccessibility = .system) { let motion = OverlayMotion(configuration: configuration, accessibility: accessibility) let next = StackGrip.groups(in: state) var remaining = panels var updated: [LearningPanel] = [] for grip in next { let panel: LearningPanel if let index = remaining.firstIndex(where: { ($0.contentView as? StackGripView)?.grip.windows == grip.windows }) { panel = remaining.remove(at: index) (panel.contentView as? StackGripView)?.update(grip) } else { panel = LearningPanel(title: grip.accessibilityLabel, interactive: true) let view = StackGripView( grip: grip, select: { [weak self] in self?.select($0) }, showControls: { [weak self] in guard let self, let current = self.groups.first(where: { $0.windows == grip.windows }) else { return } self.showControls(current.visible) }) panel.contentView = view if let layer = view.layer { LearningMotion.show(layer, token: .pop, motion: motion) } } if let layer = panel.contentView?.layer { LearningMotion.apply(motion, to: layer) } panel.title = grip.accessibilityLabel panel.setAccessibilityLabel(grip.accessibilityLabel) let height = NSScreen.screens.first?.frame.maxY ?? 0 let frame = CGRect( x: grip.frame.minX, y: height - grip.frame.maxY, width: grip.frame.width, height: grip.frame.height) if panel.frame != frame { LearningMotion.resize(panel, to: frame, motion: motion) } if orderPanels, !panel.isVisible { panel.orderFront(nil) } updated.append(panel) } for panel in remaining { panel.close() } panels = updated groups = next } func frame(of window: WindowID) -> CGRect? { guard let grip = groups.first(where: { $0.windows.contains(window) }), let chip = grip.chips.first(where: { $0.window == window }) else { return nil } return chip.frame.offsetBy(dx: grip.frame.minX, dy: grip.frame.minY) } func stop() { for panel in panels { panel.close() } panels = [] groups = [] } isolated deinit { for panel in panels { panel.close() } } }