import AppKit import ParcelAnimation import ParcelConfig import QuartzCore enum AppWindowPresentation { static let level = NSWindow.Level.floating @MainActor static func configure(_ window: NSWindow) { window.level = level window.hidesOnDeactivate = false window.animationBehavior = .none if let panel = window as? NSPanel { panel.isFloatingPanel = true } } } struct LearningPanelPresentation { let setupWindow: Bool let activatesApplication: Bool init(setupWindow: Bool, activatesApplication: Bool) { self.setupWindow = setupWindow self.activatesApplication = setupWindow || activatesApplication } var styleMask: NSWindow.StyleMask { if setupWindow { [.titled, .closable, .resizable, .fullSizeContentView] } else if activatesApplication { [.borderless] } else { [.borderless, .nonactivatingPanel] } } var becomesKeyOnlyIfNeeded: Bool { !setupWindow } func suppressesDefaultAction(keyCode: UInt16, isRepeat: Bool) -> Bool { setupWindow && isRepeat && (keyCode == 36 || keyCode == 76) } } @MainActor final class LearningPanel: NSPanel { let interactive: Bool private let presentation: LearningPanelPresentation let activatesApplication: Bool var onSetupClose: (@MainActor () -> Void)? init(title: String, interactive: Bool, setupWindow: Bool = false, activatesApplication: Bool = false) { let presentation = LearningPanelPresentation( setupWindow: setupWindow, activatesApplication: activatesApplication) self.interactive = interactive self.presentation = presentation self.activatesApplication = presentation.activatesApplication super.init( contentRect: .zero, styleMask: presentation.styleMask, backing: .buffered, defer: false) self.title = title isReleasedWhenClosed = false isOpaque = setupWindow backgroundColor = setupWindow ? .windowBackgroundColor : .clear hasShadow = setupWindow titlebarAppearsTransparent = setupWindow titleVisibility = setupWindow ? .hidden : .visible ignoresMouseEvents = !interactive becomesKeyOnlyIfNeeded = presentation.becomesKeyOnlyIfNeeded AppWindowPresentation.configure(self) collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .stationary] collectionBehavior.insert(interactive ? .participatesInCycle : .ignoresCycle) setAccessibilityLabel(title) if !interactive { setAccessibilityElement(false) } } override var canBecomeKey: Bool { interactive } override var canBecomeMain: Bool { presentation.setupWindow } override func performClose(_ sender: Any?) { if presentation.setupWindow { onSetupClose?() } else { super.performClose(sender) } } override func cancelOperation(_ sender: Any?) { if presentation.setupWindow { onSetupClose?() } else { super.cancelOperation(sender) } } override func performKeyEquivalent(with event: NSEvent) -> Bool { if presentation.suppressesDefaultAction(keyCode: event.keyCode, isRepeat: event.isARepeat) { return true } return super.performKeyEquivalent(with: event) } func place(_ rect: CGRect) { let height = NSScreen.screens.first?.frame.maxY ?? 0 let frame = CGRect(x: rect.minX, y: height - rect.maxY, width: rect.width, height: rect.height) if self.frame != frame { setFrame(frame, display: false) } } } @MainActor enum LearningMotion { static func resize(_ panel: NSPanel, to frame: CGRect, configuration: MotionConfiguration) { resize(panel, to: frame, motion: OverlayMotion(configuration: configuration, accessibility: .system)) } static func resize(_ panel: NSPanel, to frame: CGRect, motion: OverlayMotion) { guard panel.isVisible, motion.behavior == .animate else { panel.setFrame(frame, display: true) return } NSAnimationContext.runAnimationGroup { context in context.duration = motion.spec(.drop).duration context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) panel.animator().setFrame(frame, display: true) } } static func apply(_ motion: OverlayMotion, to layer: CALayer) { switch motion.behavior { case .instant: layer.removeAllAnimations() case .fade: layer.removeAnimation(forKey: "learningScale") case .animate: break } } static func show(_ layer: CALayer, token: MotionToken, configuration: MotionConfiguration) { show(layer, token: token, motion: OverlayMotion(configuration: configuration, accessibility: .system)) } static func show(_ layer: CALayer, token: MotionToken, motion: OverlayMotion) { layer.removeAllAnimations() let duration: Double switch motion.behavior { case .instant: return case .fade(let value): duration = value case .animate: let spec = motion.spec(token) duration = spec.duration if token == .pop || token == .drop { let spring = CASpringAnimation(keyPath: "transform.scale") if case .scale(let from) = spec.accent { spring.fromValue = from } else { spring.fromValue = 1 - spec.overshootLimit * 0.16 } spring.toValue = 1 spring.mass = spec.spring.mass spring.stiffness = spec.spring.stiffness spring.damping = spec.spring.damping spring.duration = duration layer.add(spring, forKey: "learningScale") } } let fade = CABasicAnimation(keyPath: "opacity") fade.fromValue = 0 fade.toValue = 1 fade.duration = duration layer.add(fade, forKey: "learningFade") } }