Skip to content

Commit 33a0adc

Browse files
authored
support colors with light and dark modes (#121)
1 parent 8921b1f commit 33a0adc

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

Sources/Components/Compositions/ShapeLayerNode.swift

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,57 @@ fileprivate final class BackingShapeLayerNode : ASDisplayNode {
2323
return shape
2424
}
2525
}
26+
27+
fileprivate var shapeStrokeColor: UIColor? {
28+
didSet {
29+
ASPerformBlockOnMainThread {
30+
CATransaction.begin()
31+
CATransaction.setDisableActions(true)
32+
defer {
33+
CATransaction.commit()
34+
}
35+
self.layer.strokeColor = self.shapeStrokeColor?
36+
.resolvedColor(with: .init(userInterfaceStyle: self.asyncTraitCollection().userInterfaceStyle))
37+
.cgColor
38+
}
39+
}
40+
}
41+
42+
public var shapeFillColor: UIColor? {
43+
didSet {
44+
ASPerformBlockOnMainThread {
45+
CATransaction.begin()
46+
CATransaction.setDisableActions(true)
47+
defer {
48+
CATransaction.commit()
49+
}
50+
self.layer.fillColor = self.shapeFillColor?
51+
.resolvedColor(with: .init(userInterfaceStyle: self.asyncTraitCollection().userInterfaceStyle))
52+
.cgColor
53+
}
54+
}
55+
}
56+
57+
override func asyncTraitCollectionDidChange(
58+
withPreviousTraitCollection previousTraitCollection: ASPrimitiveTraitCollection
59+
) {
60+
super.asyncTraitCollectionDidChange(withPreviousTraitCollection: previousTraitCollection)
61+
lazy var userInterfaceStyle = asyncTraitCollection().userInterfaceStyle
62+
guard
63+
isNodeLoaded,
64+
previousTraitCollection.userInterfaceStyle != userInterfaceStyle
65+
else {
66+
return
67+
}
68+
ASPerformBlockOnMainThread {
69+
self.layer.strokeColor = self.shapeStrokeColor?
70+
.resolvedColor(with: .init(userInterfaceStyle: userInterfaceStyle))
71+
.cgColor
72+
self.layer.fillColor = self.shapeFillColor?
73+
.resolvedColor(with: .init(userInterfaceStyle: userInterfaceStyle))
74+
.cgColor
75+
}
76+
}
2677
}
2778

2879
/// A node that displays shape with CAShapeLayer
@@ -53,36 +104,22 @@ public final class ShapeLayerNode : ASDisplayNode, ShapeDisplaying {
53104
setNeedsLayout()
54105
}
55106
}
56-
107+
57108
public var shapeStrokeColor: UIColor? {
58109
get {
59-
return backingNode.layer.strokeColor.map { UIColor(cgColor: $0) }
110+
return backingNode.shapeStrokeColor
60111
}
61112
set {
62-
ASPerformBlockOnMainThread {
63-
CATransaction.begin()
64-
CATransaction.setDisableActions(true)
65-
defer {
66-
CATransaction.commit()
67-
}
68-
self.backingNode.layer.strokeColor = newValue?.cgColor
69-
}
113+
backingNode.shapeStrokeColor = newValue
70114
}
71115
}
72116

73117
public var shapeFillColor: UIColor? {
74118
get {
75-
return backingNode.layer.fillColor.map { UIColor(cgColor: $0) }
119+
return backingNode.shapeFillColor
76120
}
77121
set {
78-
ASPerformBlockOnMainThread {
79-
CATransaction.begin()
80-
CATransaction.setDisableActions(true)
81-
defer {
82-
CATransaction.commit()
83-
}
84-
self.backingNode.layer.fillColor = newValue?.cgColor
85-
}
122+
backingNode.shapeFillColor = newValue
86123
}
87124
}
88125

0 commit comments

Comments
 (0)