diff --git a/Sources/ColorfulX/CALayerAnimationsDisablingDelegate.swift b/Sources/ColorfulX/CALayerAnimationsDisablingDelegate.swift new file mode 100644 index 0000000..63f737e --- /dev/null +++ b/Sources/ColorfulX/CALayerAnimationsDisablingDelegate.swift @@ -0,0 +1,23 @@ +// +// CALayerAnimationsDisablingDelegate.swift +// ColorfulX +// +// Created by 秋星桥 on 2024/8/16. +// + +#if canImport(UIKit) + import UIKit +#endif + +#if canImport(AppKit) + import AppKit +#endif + +class CALayerAnimationsDisablingDelegate: NSObject, CALayerDelegate { + static let shared = CALayerAnimationsDisablingDelegate() + private let null = NSNull() + + func action(for _: CALayer, forKey _: String) -> CAAction? { + null + } +} diff --git a/Sources/ColorfulX/MetalLink.swift b/Sources/ColorfulX/MetalLink.swift index 38f18c7..8608b81 100644 --- a/Sources/ColorfulX/MetalLink.swift +++ b/Sources/ColorfulX/MetalLink.swift @@ -14,6 +14,7 @@ class MetalLink: DisplayLinkDelegate { let metalLayer: CAMetalLayer let commandQueue: MTLCommandQueue let displayLink: DisplayLink = .init() + let disableLayerAnimationDelegate = CALayerAnimationsDisablingDelegate() typealias SynchornizationUpdate = () -> Void var onSynchronizationUpdate: SynchornizationUpdate? @@ -39,6 +40,11 @@ class MetalLink: DisplayLinkDelegate { metalLayer.device = metalDevice metalLayer.framebufferOnly = false metalLayer.isOpaque = false + metalLayer.actions = [ + "sublayers": NSNull(), + "content": NSNull(), + ] + metalLayer.delegate = disableLayerAnimationDelegate self.metalLayer = metalLayer displayLink.delegatingObject(self) @@ -65,5 +71,7 @@ class MetalLink: DisplayLinkDelegate { metalLayer.drawableSize = CGSize(width: width, height: height) } - func synchronization() { onSynchronizationUpdate?() } + func synchronization() { + onSynchronizationUpdate?() + } } diff --git a/Sources/ColorfulX/MulticolorGradientView.swift b/Sources/ColorfulX/MulticolorGradientView.swift index 711ce40..763ddc2 100644 --- a/Sources/ColorfulX/MulticolorGradientView.swift +++ b/Sources/ColorfulX/MulticolorGradientView.swift @@ -21,6 +21,13 @@ open class MulticolorGradientView: MetalView { public var currentTexture: MTLTexture? { currentDrawable?.texture } public var captureImage: CGImage? { currentTexture?.capture() } + public enum RenderExecutionStatus { + case normal + case temporaryStopped + } + + public var renderExecutionStatus: RenderExecutionStatus = .normal + override public init() { super.init() @@ -55,6 +62,10 @@ open class MulticolorGradientView: MetalView { guard lock.try() else { return } defer { lock.unlock() } + if renderExecutionStatus == .temporaryStopped { + return + } + guard let metalLink, let computePipelineState else { return }