Skip to content

Commit 4b631e2

Browse files
committed
Add support for dark mode.
1 parent 7344753 commit 4b631e2

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

ComponentTextKit/CKTextComponentLayer.mm

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,30 @@ - (NSObject *)drawParameters
8585

8686
- (id)willDisplayAsynchronouslyWithDrawParameters:(id<NSObject>)drawParameters
8787
{
88-
return rasterContentsCache()->objectForKey({_renderer.attributes, _renderer.constrainedSize});
88+
NSInteger userInterfaceStyle = 1;
89+
if (@available(iOS 13.0, *)) {
90+
UIView *view = (UIView *)self.delegate;
91+
if ([view isKindOfClass:UIView.class]) {
92+
userInterfaceStyle = view.traitCollection.userInterfaceStyle;
93+
}
94+
}
95+
return rasterContentsCache()->objectForKey({userInterfaceStyle, _renderer.attributes, _renderer.constrainedSize});
8996
}
9097

9198
- (void)didDisplayAsynchronously:(id)newContents withDrawParameters:(id<NSObject>)drawParameters
9299
{
93100
if (newContents) {
101+
NSInteger userInterfaceStyle = 1;
102+
if (@available(iOS 13.0, *)) {
103+
UIView *view = (UIView *)self.delegate;
104+
if ([view isKindOfClass:UIView.class]) {
105+
userInterfaceStyle = view.traitCollection.userInterfaceStyle;
106+
}
107+
}
108+
94109
CGImageRef imageRef = (__bridge CGImageRef)newContents;
95110
NSUInteger bytes = CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);
96-
rasterContentsCache()->cacheObject({_renderer.attributes, _renderer.constrainedSize}, newContents, bytes);
111+
rasterContentsCache()->cacheObject({userInterfaceStyle, _renderer.attributes, _renderer.constrainedSize}, newContents, bytes);
97112
}
98113
}
99114

ComponentTextKit/CKTextComponentView.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ - (instancetype)initWithFrame:(CGRect)frame
4242
return self;
4343
}
4444

45+
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
46+
{
47+
[super traitCollectionDidChange:previousTraitCollection];
48+
49+
if (@available(iOS 13.0, *)) {
50+
if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
51+
[self.layer setNeedsDisplay];
52+
}
53+
}
54+
}
55+
4556
- (void)setBackgroundColor:(UIColor *)backgroundColor
4657
{
4758
if (![self.backgroundColor isEqual:backgroundColor]) {

ComponentTextKit/TextKit/CKTextKitRendererCache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ namespace CK {
6767
*/
6868
struct Key {
6969

70+
NSInteger userInterfaceStyle;
7071
CKTextKitAttributes attributes;
7172
CGSize constrainedSize;
7273

73-
Key(CKTextKitAttributes a, CGSize cs);
74+
Key(NSInteger userInterfaceStyle, CKTextKitAttributes a, CGSize cs);
7475

7576
size_t hash;
7677

@@ -79,7 +80,8 @@ namespace CK {
7980
// These comparisons are in a specific order to reduce the overall cost of this function.
8081
return hash == other.hash
8182
&& CGSizeEqualToSize(constrainedSize, other.constrainedSize)
82-
&& attributes == other.attributes;
83+
&& attributes == other.attributes
84+
&& userInterfaceStyle == other.userInterfaceStyle;
8385
}
8486
};
8587

ComponentTextKit/TextKit/CKTextKitRendererCache.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ void enteredBackgroundNotificationHandler(CFNotificationCenterRef center, void *
2626
}
2727

2828
namespace Renderer {
29-
Key::Key(CKTextKitAttributes a, CGSize cs) : attributes(a), constrainedSize(cs) {
29+
Key::Key(NSInteger u, CKTextKitAttributes a, CGSize cs) : userInterfaceStyle(u), attributes(a), constrainedSize(cs) {
3030
// Precompute hash to avoid paying cost every time getHash is called.
3131
NSUInteger subhashes[] = {
3232
attributes.hash(),
3333
std::hash<CGFloat>()(constrainedSize.width),
34-
std::hash<CGFloat>()(constrainedSize.height)
34+
std::hash<CGFloat>()(constrainedSize.height),
35+
std::hash<NSInteger>()(userInterfaceStyle)
3536
};
3637
hash = RCIntegerArrayHash(subhashes, CK_ARRAY_COUNT(subhashes));
3738
}

0 commit comments

Comments
 (0)