Skip to content

Commit

Permalink
support colors with light and dark modes (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnEstropia authored Jan 16, 2025
1 parent 8921b1f commit 33a0adc
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions Sources/Components/Compositions/ShapeLayerNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,57 @@ fileprivate final class BackingShapeLayerNode : ASDisplayNode {
return shape
}
}

fileprivate var shapeStrokeColor: UIColor? {
didSet {
ASPerformBlockOnMainThread {
CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.layer.strokeColor = self.shapeStrokeColor?
.resolvedColor(with: .init(userInterfaceStyle: self.asyncTraitCollection().userInterfaceStyle))
.cgColor
}
}
}

public var shapeFillColor: UIColor? {
didSet {
ASPerformBlockOnMainThread {
CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.layer.fillColor = self.shapeFillColor?
.resolvedColor(with: .init(userInterfaceStyle: self.asyncTraitCollection().userInterfaceStyle))
.cgColor
}
}
}

override func asyncTraitCollectionDidChange(
withPreviousTraitCollection previousTraitCollection: ASPrimitiveTraitCollection
) {
super.asyncTraitCollectionDidChange(withPreviousTraitCollection: previousTraitCollection)
lazy var userInterfaceStyle = asyncTraitCollection().userInterfaceStyle
guard
isNodeLoaded,
previousTraitCollection.userInterfaceStyle != userInterfaceStyle
else {
return
}
ASPerformBlockOnMainThread {
self.layer.strokeColor = self.shapeStrokeColor?
.resolvedColor(with: .init(userInterfaceStyle: userInterfaceStyle))
.cgColor
self.layer.fillColor = self.shapeFillColor?
.resolvedColor(with: .init(userInterfaceStyle: userInterfaceStyle))
.cgColor
}
}
}

/// A node that displays shape with CAShapeLayer
Expand Down Expand Up @@ -53,36 +104,22 @@ public final class ShapeLayerNode : ASDisplayNode, ShapeDisplaying {
setNeedsLayout()
}
}

public var shapeStrokeColor: UIColor? {
get {
return backingNode.layer.strokeColor.map { UIColor(cgColor: $0) }
return backingNode.shapeStrokeColor
}
set {
ASPerformBlockOnMainThread {
CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.backingNode.layer.strokeColor = newValue?.cgColor
}
backingNode.shapeStrokeColor = newValue
}
}

public var shapeFillColor: UIColor? {
get {
return backingNode.layer.fillColor.map { UIColor(cgColor: $0) }
return backingNode.shapeFillColor
}
set {
ASPerformBlockOnMainThread {
CATransaction.begin()
CATransaction.setDisableActions(true)
defer {
CATransaction.commit()
}
self.backingNode.layer.fillColor = newValue?.cgColor
}
backingNode.shapeFillColor = newValue
}
}

Expand Down

0 comments on commit 33a0adc

Please sign in to comment.