|
| 1 | +import AppKit |
| 2 | +import FileMintCore |
| 3 | +import MetalKit |
| 4 | +import SpriteKit |
| 5 | + |
| 6 | +@MainActor |
| 7 | +final class FileDrawerGenieTransition: MTKView { |
| 8 | + typealias BackdropUpdate = @MainActor ([SIMD2<Float>], CGFloat) -> Void |
| 9 | + |
| 10 | + private struct Motion { |
| 11 | + let id = UUID() |
| 12 | + let from: Float |
| 13 | + let to: Float |
| 14 | + let duration: TimeInterval |
| 15 | + let completion: @MainActor () -> Void |
| 16 | + var firstFrame: TimeInterval? |
| 17 | + } |
| 18 | + |
| 19 | + private let renderer: SKRenderer |
| 20 | + private let queue: any MTLCommandQueue |
| 21 | + private let sprite: SKSpriteNode |
| 22 | + private let geometry: FileDrawerGenieGeometry |
| 23 | + private let updateBackdrop: BackdropUpdate? |
| 24 | + private var motion: Motion? |
| 25 | + private var completionTask: Task<Void, Never>? |
| 26 | + private(set) var progress: Float |
| 27 | + var restoredOpeningImage: (@MainActor () -> CGImage?)? |
| 28 | + |
| 29 | + init?(image: CGImage, frame: CGRect, side: FileDrawerPreferences.Side, opening: Bool, framesPerSecond: Int, |
| 30 | + updateBackdrop: BackdropUpdate?) { |
| 31 | + guard let device = MTLCreateSystemDefaultDevice(), let queue = device.makeCommandQueue() else { return nil } |
| 32 | + self.queue = queue |
| 33 | + renderer = SKRenderer(device: device) |
| 34 | + sprite = SKSpriteNode(texture: SKTexture(cgImage: image), size: frame.size) |
| 35 | + geometry = FileDrawerGenieGeometry(size: frame.size, side: side) |
| 36 | + self.updateBackdrop = updateBackdrop |
| 37 | + progress = opening ? 1 : 0 |
| 38 | + super.init(frame: frame, device: device) |
| 39 | + isPaused = true |
| 40 | + presentsWithTransaction = updateBackdrop != nil |
| 41 | + layer?.isOpaque = false |
| 42 | + clearColor = MTLClearColorMake(0, 0, 0, 0) |
| 43 | + if framesPerSecond > 0 { preferredFramesPerSecond = framesPerSecond } |
| 44 | + let scene = SKScene(size: frame.size) |
| 45 | + scene.backgroundColor = .clear |
| 46 | + scene.anchorPoint = CGPoint(x: 0.5, y: 0.5) |
| 47 | + sprite.subdivisionLevels = 0 |
| 48 | + scene.addChild(sprite) |
| 49 | + renderer.scene = scene |
| 50 | + deform(at: progress) |
| 51 | + } |
| 52 | + |
| 53 | + @available(*, unavailable) |
| 54 | + required init(coder: NSCoder) { fatalError("Use the drawer transition initializer") } |
| 55 | + |
| 56 | + override func hitTest(_ point: NSPoint) -> NSView? { nil } |
| 57 | + |
| 58 | + func play(opening: Bool, completion: @escaping @MainActor () -> Void) { |
| 59 | + let end: Float = opening ? 0 : 1 |
| 60 | + let duration = 0.5 * Double(abs(end - progress)) |
| 61 | + guard duration > 0 else { completion(); return } |
| 62 | + motion = Motion(from: progress, to: end, duration: duration, completion: completion) |
| 63 | + finishIfOccluded(after: duration) |
| 64 | + isPaused = false |
| 65 | + } |
| 66 | + |
| 67 | + override func draw(_ dirtyRect: NSRect) { |
| 68 | + guard let motion, let pass = currentRenderPassDescriptor, let drawable = currentDrawable, |
| 69 | + let buffer = queue.makeCommandBuffer() else { return } |
| 70 | + CATransaction.begin() |
| 71 | + CATransaction.setDisableActions(true) |
| 72 | + if motion.to == 0, let image = restoredOpeningImage?() { |
| 73 | + sprite.texture = SKTexture(cgImage: image) |
| 74 | + restoredOpeningImage = nil |
| 75 | + } |
| 76 | + if let firstFrame = motion.firstFrame { |
| 77 | + let amount = Float(min(1, (CACurrentMediaTime() - firstFrame) / motion.duration)) |
| 78 | + deform(at: amount == 1 ? motion.to : motion.from + (motion.to - motion.from) * amount) |
| 79 | + if amount == 1 { |
| 80 | + isPaused = true |
| 81 | + drawable.addPresentedHandler { [weak self, id = motion.id] _ in |
| 82 | + Task { @MainActor in |
| 83 | + guard let self, self.motion?.id == id else { return } |
| 84 | + self.finishMotion() |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + renderFrame(to: pass, using: buffer) |
| 90 | + if presentsWithTransaction { |
| 91 | + // Present the controls in the same transaction as the material's outline. |
| 92 | + buffer.commit() |
| 93 | + buffer.waitUntilScheduled() |
| 94 | + drawable.present() |
| 95 | + } else { |
| 96 | + buffer.present(drawable) |
| 97 | + buffer.commit() |
| 98 | + } |
| 99 | + CATransaction.commit() |
| 100 | + if motion.firstFrame == nil { |
| 101 | + self.motion?.firstFrame = CACurrentMediaTime() |
| 102 | + finishIfOccluded(after: motion.duration) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + func renderFrame(to pass: MTLRenderPassDescriptor, using buffer: any MTLCommandBuffer) { |
| 107 | + guard let texture = pass.colorAttachments[0].texture else { return } |
| 108 | + renderer.update(atTime: CACurrentMediaTime()) |
| 109 | + let viewport = CGRect(x: 0, y: 0, width: texture.width, height: texture.height) |
| 110 | + renderer.render(withViewport: viewport, commandBuffer: buffer, renderPassDescriptor: pass) |
| 111 | + } |
| 112 | + |
| 113 | + func discard() { |
| 114 | + completionTask?.cancel() |
| 115 | + motion = nil |
| 116 | + isPaused = true |
| 117 | + removeFromSuperview() |
| 118 | + } |
| 119 | + |
| 120 | + private func finishMotion() { |
| 121 | + let completion = motion?.completion |
| 122 | + motion = nil |
| 123 | + isPaused = true |
| 124 | + completionTask?.cancel() |
| 125 | + completionTask = nil |
| 126 | + completion?() |
| 127 | + } |
| 128 | + |
| 129 | + private func finishIfOccluded(after duration: TimeInterval) { |
| 130 | + completionTask?.cancel() |
| 131 | + completionTask = Task { [weak self] in |
| 132 | + // An occluded window may stop delivering draw or presentation callbacks. |
| 133 | + try? await Task.sleep(for: .seconds(duration + 0.35)) |
| 134 | + guard !Task.isCancelled else { return } |
| 135 | + self?.finishMotion() |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private func deform(at progress: Float) { |
| 140 | + self.progress = progress |
| 141 | + let opacity = CGFloat(geometry.opacity(at: progress)) |
| 142 | + let vertices = geometry.vertices(at: progress) |
| 143 | + sprite.alpha = opacity |
| 144 | + sprite.warpGeometry = SKWarpGeometryGrid(columns: FileDrawerGenieGeometry.columns, rows: FileDrawerGenieGeometry.rows, |
| 145 | + destinationPositions: vertices) |
| 146 | + updateBackdrop?(vertices, opacity) |
| 147 | + } |
| 148 | +} |
0 commit comments