Skip to content

Commit

Permalink
Disable Implicit Animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakr233 committed Aug 16, 2024
1 parent e2681e5 commit c6840c4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Sources/ColorfulX/CALayerAnimationsDisablingDelegate.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 9 additions & 1 deletion Sources/ColorfulX/MetalLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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)
Expand All @@ -65,5 +71,7 @@ class MetalLink: DisplayLinkDelegate {
metalLayer.drawableSize = CGSize(width: width, height: height)
}

func synchronization() { onSynchronizationUpdate?() }
func synchronization() {
onSynchronizationUpdate?()
}
}
11 changes: 11 additions & 0 deletions Sources/ColorfulX/MulticolorGradientView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 }
Expand Down

0 comments on commit c6840c4

Please sign in to comment.