Set nodes dirty when sizeThatFits changes
Summary: In YogaKit, nodes are not dirtied when `sizeThatFits` of leaf nodes change. This makes it difficult to reuse views like `UILabel` when their content (text) changes. I wrote a failing test to prove it and wrote a fix by caching the latest value of `sizeThatFits` in `YGNodeBridge` and checking for changes in `YGAttachNodesFromViewHierachy` (renamed to `YGUpdateNodesFromViewHierachy` to make it clear it's not simply reattaching nodes anymore). When a new size is detected, I call `YGNodeMarkDirty` on the leaf node. **Unfortunately**, this makes another test fail for no logical reason (`testThatViewNotIncludedInFirstLayoutPassAreIncludedInSecond`). Either there's something I don't understand or I've un-advertedly unearthed a bug in the C implementation, but in what case would setting a node dirty make the layout return a different size? Closes https://github.com/facebook/yoga/pull/308 Reviewed By: emilsjolander Differential Revision: D4378826 Pulled By: dshahidehpour fbshipit-source-id: cd0640bc863debb9e434370739416e3e33e57a08
This commit is contained in:
committed by
Facebook Github Bot
parent
1877a97898
commit
29c44e281c
@@ -11,6 +11,8 @@
|
||||
|
||||
#import <objc/runtime.h>
|
||||
|
||||
static const void *kYGNodeBridgeAssociatedKey = &kYGNodeBridgeAssociatedKey;
|
||||
|
||||
@interface YGNodeBridge : NSObject
|
||||
@property (nonatomic, assign, readonly) YGNodeRef cnode;
|
||||
@end
|
||||
@@ -39,6 +41,14 @@
|
||||
|
||||
@implementation UIView (Yoga)
|
||||
|
||||
- (void)yg_markDirty
|
||||
{
|
||||
YGNodeBridge *const bridge = objc_getAssociatedObject(self, kYGNodeBridgeAssociatedKey);
|
||||
if (bridge != nil && [self yg_isLeaf]) {
|
||||
YGNodeMarkDirty(bridge.cnode);
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)yg_usesYoga
|
||||
{
|
||||
NSNumber *usesYoga = objc_getAssociatedObject(self, @selector(yg_usesYoga));
|
||||
@@ -226,13 +236,12 @@
|
||||
|
||||
- (YGNodeRef)ygNode
|
||||
{
|
||||
YGNodeBridge *node = objc_getAssociatedObject(self, @selector(ygNode));
|
||||
YGNodeBridge *node = objc_getAssociatedObject(self, kYGNodeBridgeAssociatedKey);
|
||||
if (!node) {
|
||||
node = [YGNodeBridge new];
|
||||
YGNodeSetContext(node.cnode, (__bridge void *) self);
|
||||
objc_setAssociatedObject(self, @selector(ygNode), node, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
objc_setAssociatedObject(self, kYGNodeBridgeAssociatedKey, node, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
return node.cnode;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user