Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnmrndn committed Nov 27, 2024
1 parent f417f25 commit 674c237
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 53 deletions.
5 changes: 3 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"location" : "https://github.com/ntnmrndn/Descriptors",
"state" : {
"branch" : "antoine/swift6",
"revision" : "f67005499aac61b4458ea536e62351d70b4647d6"
"revision" : "b1eb25aa85dbcac3149438c7ac8d13949317ba45"
}
},
{
Expand All @@ -16,9 +16,10 @@
"location" : "https://github.com/ntnmrndn/Texture.git",
"state" : {
"branch" : "antoine/swift6",
"revision" : "2a8c4b9ca4591229e0017c06083165cd1d3242ab"
"revision" : "5bd706009aeee6711656cc993fdb60131c4d04f0"
}
}
],
"version" : 3
}

2 changes: 1 addition & 1 deletion Sources/Components/Compositions/AnyDisplayNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ open class AnyDisplayNode: SafeAreaDisplayNode {
}

@available(*, unavailable)
open override func onDidLoad(_ body: @escaping ASDisplayNodeDidLoadBlock) {
open override func onDidLoad(_ body: @escaping ASDisplayNodeDidLoadBlock) {
super.onDidLoad(body)
}

Expand Down
102 changes: 55 additions & 47 deletions Sources/Components/Compositions/ShapeLayerNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fileprivate final class BackingShapeLayerNode : ASDisplayNode {
}

/// A node that displays shape with CAShapeLayer
public final class ShapeLayerNode : ASDisplayNode, @preconcurrency ShapeDisplaying, @unchecked Sendable {
public final class ShapeLayerNode : ASDisplayNode, MainActorShapeDisplaying, @unchecked Sendable {

private let backingNode = BackingShapeLayerNode()

Expand All @@ -38,23 +38,54 @@ public final class ShapeLayerNode : ASDisplayNode, @preconcurrency ShapeDisplay
}
}

public init(update: @escaping Update) {
self.updateClosure = update
super.init()
backgroundColor = .clear
backingNode.backgroundColor = .clear
backingNode.isLayerBacked = true
automaticallyManagesSubnodes = true
}

/// Warning: shape\* values will actually be set at didLoad
public convenience init (
update: @escaping Update,
shapeFillColor: UIColor = .clear,
shapeStrokeColor: UIColor = .clear,
shapeLineWidth: CGFloat = 0.0
) {
self.init(update: update)
backgroundColor = .clear
backingNode.backgroundColor = .clear
backingNode.isLayerBacked = true
automaticallyManagesSubnodes = true
self.onDidLoad({
let shapeLayerNode = ($0 as! ShapeLayerNode)
shapeLayerNode.shapeFillColor = shapeFillColor
shapeLayerNode.shapeStrokeColor = shapeStrokeColor
shapeLayerNode.shapeLineWidth = shapeLineWidth
})
}

public override var supportsLayerBacking: Bool {
return true
}

@MainActor
public var shapeLayer: CAShapeLayer {
/// Beware, direct access to lineWidth is not supported here when using usesInnerBorder, otherwise acess should be safe
public var unsafeShapeLayer: CAShapeLayer {
backingNode.layer
}

// To be thread-safe, using stored property
/// Should be set on mainThread, TODO: Discuss how to enforce the rule

/// cache value for bg thread access
private var _shapeLineWidth: CGFloat = .zero

@MainActor
public var shapeLineWidth: CGFloat = 0 {
didSet {
MainActor.assumeIsolated {
backingNode.layer.lineWidth = shapeLineWidth
setNeedsLayout()
}
_shapeLineWidth = shapeLineWidth
backingNode.layer.lineWidth = shapeLineWidth
setNeedsLayout()
}
}

Expand All @@ -64,18 +95,12 @@ public final class ShapeLayerNode : ASDisplayNode, @preconcurrency ShapeDisplay
return backingNode.layer.strokeColor.map { UIColor(cgColor: $0) }
}
set {
// Keep it for now since we might have non confirming implementation
ASPerformBlockOnMainThread {
MainActor.assumeIsolated {

CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.backingNode.layer.strokeColor = newValue?.cgColor
}
CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.backingNode.layer.strokeColor = newValue?.cgColor
}
}

Expand All @@ -85,21 +110,15 @@ public final class ShapeLayerNode : ASDisplayNode, @preconcurrency ShapeDisplay
return backingNode.layer.fillColor.map { UIColor(cgColor: $0) }
}
set {
// Keep it for now since we might have non confirming implementation
ASPerformBlockOnMainThread {
MainActor.assumeIsolated {

CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.backingNode.layer.fillColor = newValue?.cgColor
}
CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.backingNode.layer.fillColor = newValue?.cgColor
}
}

public override func layout() {
super.layout()
CATransaction.begin()
Expand All @@ -125,27 +144,16 @@ public final class ShapeLayerNode : ASDisplayNode, @preconcurrency ShapeDisplay
}
}

public init(
update: @escaping Update
) {
self.updateClosure = update
super.init()
backgroundColor = .clear
backingNode.backgroundColor = .clear
backingNode.isLayerBacked = true
automaticallyManagesSubnodes = true
}

public override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {

if usesInnerBorder {
return ASWrapperLayoutSpec(
layoutElement: ASInsetLayoutSpec(
insets: .init(
top: shapeLineWidth / 2,
left: shapeLineWidth / 2,
bottom: shapeLineWidth / 2,
right: shapeLineWidth / 2
top: _shapeLineWidth / 2,
left: _shapeLineWidth / 2,
bottom: _shapeLineWidth / 2,
right: _shapeLineWidth / 2
),
child: backingNode
)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Components/Compositions/StyledEdgeNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,15 @@ public class StyledEdgeNode<ContentNode: ASDisplayNode>: NamedDisplayNodeBase {

}

@MainActor
public override func didLoad() {
super.didLoad()

// to disable animation
updateBorder()
updateStrategy()

self.borderNode.shapeLayer.fillRule = .evenOdd
self.borderNode.unsafeShapeLayer.fillRule = .evenOdd
}

private func updateBorder() {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Components/Elements/GradientLayerNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ open class GradientLayerNode : ASDisplayNode, @unchecked Sendable {
backgroundColor = .clear
}

@MainActor
open override func didLoad() {
super.didLoad()

Expand All @@ -66,7 +67,7 @@ open class GradientLayerNode : ASDisplayNode, @unchecked Sendable {

}

/// why are we using both assert main thread and lock and dispatch ?
@MainActor
open func setDescriptor(descriptor: LinearGradientDescriptor) {

lock()
Expand Down
1 change: 0 additions & 1 deletion Sources/Components/Tools/NamedDisplayNodeBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ open class NamedDisplayNodeBase: ASDisplayNode, @unchecked Sendable {
return self
}

@preconcurrency
@MainActor
private func propagate(action: DisplayNodeAction) {
for handler in __actionHandlers {
Expand Down

0 comments on commit 674c237

Please sign in to comment.