Skip to content

Commit d608bb9

Browse files
authored
Call will / did display node for ASTextNode. Fixes facebookarchive#1680 (facebookarchive#1893)
1 parent 9ef97f4 commit d608bb9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Source/ASTextNode.mm

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ @interface ASTextNodeDrawParameter : NSObject {
141141
BOOL _opaque;
142142
CGRect _bounds;
143143
ASPrimitiveTraitCollection _traitCollection;
144+
ASDisplayNodeContextModifier _willDisplayNodeContentWithRenderingContext;
145+
ASDisplayNodeContextModifier _didDisplayNodeContentWithRenderingContext;
144146
}
145147
@end
146148

@@ -152,7 +154,9 @@ - (instancetype)initWithRendererAttributes:(ASTextKitAttributes)rendererAttribut
152154
contentScale:(CGFloat)contentScale
153155
opaque:(BOOL)opaque
154156
bounds:(CGRect)bounds
155-
traitCollection: (ASPrimitiveTraitCollection)traitCollection
157+
traitCollection:(ASPrimitiveTraitCollection)traitCollection
158+
willDisplayNodeContentWithRenderingContext:(ASDisplayNodeContextModifier)willDisplayNodeContentWithRenderingContext
159+
didDisplayNodeContentWithRenderingContext:(ASDisplayNodeContextModifier)didDisplayNodeContentWithRenderingContext
156160
{
157161
self = [super init];
158162
if (self != nil) {
@@ -163,6 +167,8 @@ - (instancetype)initWithRendererAttributes:(ASTextKitAttributes)rendererAttribut
163167
_opaque = opaque;
164168
_bounds = bounds;
165169
_traitCollection = traitCollection;
170+
_willDisplayNodeContentWithRenderingContext = willDisplayNodeContentWithRenderingContext;
171+
_didDisplayNodeContentWithRenderingContext = didDisplayNodeContentWithRenderingContext;
166172
}
167173
return self;
168174
}
@@ -560,7 +566,9 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
560566
contentScale:_contentsScaleForDisplay
561567
opaque:self.isOpaque
562568
bounds:[self threadSafeBounds]
563-
traitCollection:self.primitiveTraitCollection];
569+
traitCollection:self.primitiveTraitCollection
570+
willDisplayNodeContentWithRenderingContext:self.willDisplayNodeContentWithRenderingContext
571+
didDisplayNodeContentWithRenderingContext:self.didDisplayNodeContentWithRenderingContext];
564572
}
565573

566574
+ (UIImage *)displayWithParameters:(id<NSObject>)parameters isCancelled:(NS_NOESCAPE asdisplaynode_iscancelled_block_t)isCancelled
@@ -574,12 +582,19 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameters isCancelled:(NS_NOES
574582
UIColor *backgroundColor = drawParameter->_backgroundColor;
575583
UIEdgeInsets textContainerInsets = drawParameter ? drawParameter->_textContainerInsets : UIEdgeInsetsZero;
576584
ASTextKitRenderer *renderer = [drawParameter rendererForBounds:drawParameter->_bounds];
585+
ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext = drawParameter->_willDisplayNodeContentWithRenderingContext;
586+
ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext = drawParameter->_didDisplayNodeContentWithRenderingContext;
577587

578588
UIImage *result = ASGraphicsCreateImage(drawParameter->_traitCollection, CGSizeMake(drawParameter->_bounds.size.width, drawParameter->_bounds.size.height), drawParameter->_opaque, drawParameter->_contentScale, nil, nil, ^{
579589
CGContextRef context = UIGraphicsGetCurrentContext();
580590
ASDisplayNodeAssert(context, @"This is no good without a context.");
581591

582592
CGContextSaveGState(context);
593+
594+
if (context && willDisplayNodeContentWithRenderingContext) {
595+
willDisplayNodeContentWithRenderingContext(context, drawParameter);
596+
}
597+
583598
CGContextTranslateCTM(context, textContainerInsets.left, textContainerInsets.top);
584599

585600
// Fill background
@@ -591,6 +606,11 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameters isCancelled:(NS_NOES
591606

592607
// Draw text
593608
[renderer drawInContext:context bounds:drawParameter->_bounds];
609+
610+
if (context && didDisplayNodeContentWithRenderingContext) {
611+
didDisplayNodeContentWithRenderingContext(context, drawParameter);
612+
}
613+
594614
CGContextRestoreGState(context);
595615
});
596616

0 commit comments

Comments
 (0)