Skip to content

Commit a52d4a1

Browse files
authored
refactor(ios): hippy shadowView optimization (Tencent#4467)
* refactor(ios): hippy shadowView optimization * refactor(ios): hippy shadowView optimization 2
1 parent 76ae9ab commit a52d4a1

File tree

5 files changed

+206
-165
lines changed

5 files changed

+206
-165
lines changed

renderer/native/ios/renderer/HippyUIManager.mm

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ - (UIView *)createViewForShadowListItem:(HippyShadowView *)shadowView {
517517
// until the next `cellForItemAtIndexPath` call.
518518
// we currently resolve this issue by setting the CreationType synchronously.
519519
// TODO: CreationType's further optimization is needed in the future
520-
[shadowView synchronousRecusivelySetCreationTypeToInstant];
520+
[shadowView synchronousRecursivelySetCreationTypeToInstant];
521521
UIView *listItemView = [self createViewRecursiveFromRenderObjectWithNOLock:shadowView];
522522

523523
[self.viewRegistry generateTempCacheBeforeAcquireAllStoredWeakComponentsForRootTag:shadowView.rootTag];
@@ -545,7 +545,7 @@ - (UIView *)createViewRecursiveFromRenderObjectWithNOLock:(HippyShadowView *)sha
545545
if (view) {
546546
// First of all, mark shadowView as dirty recursively,
547547
// so that we can collect ui blocks to amend correctly.
548-
[shadowView dirtyPropagation:NativeRenderUpdateLifecycleAllDirtied];
548+
[shadowView markLayoutDirty];
549549

550550
// Special handling of lazy list, which is a cellView
551551
// because lazy loading list needs to be re-layout
@@ -641,7 +641,7 @@ - (void)updateView:(nonnull NSNumber *)componentTag
641641
HippyComponentData *componentData = [self componentDataForViewName:renderObject.viewName];
642642
NSDictionary *newProps = [renderObject mergeProps:props];
643643
[componentData setProps:newProps forShadowView:renderObject];
644-
[renderObject dirtyPropagation:NativeRenderUpdateLifecyclePropsDirtied];
644+
[renderObject markLayoutDirty];
645645
[self addUIBlock:^(__unused HippyUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
646646
UIView *view = viewRegistry[componentTag];
647647
[componentData setProps:newProps forView:view];
@@ -891,7 +891,7 @@ - (void)deleteRenderNodesIds:(std::vector<std::shared_ptr<hippy::DomNode>> &&)no
891891
for (auto dom_node : nodes) {
892892
int32_t tag = dom_node->GetRenderInfo().id;
893893
HippyShadowView *renderObject = [currentRegistry objectForKey:@(tag)];
894-
[renderObject dirtyPropagation:NativeRenderUpdateLifecycleLayoutDirtied];
894+
[renderObject markLayoutDirty];
895895
if (renderObject) {
896896
[renderObject removeFromHippySuperview];
897897
[self purgeChildren:@[renderObject] onRootTag:rootTag fromRegistry:_shadowViewRegistry];
@@ -961,8 +961,8 @@ - (void)renderMoveViews:(const std::vector<int32_t> &&)ids
961961
[view removeFromHippySuperview];
962962
[toShadowView insertHippySubview:view atIndex:nodeRenderIndex];
963963
}
964-
[fromShadowView dirtyPropagation:NativeRenderUpdateLifecycleLayoutDirtied];
965-
[toShadowView dirtyPropagation:NativeRenderUpdateLifecycleLayoutDirtied];
964+
[fromShadowView markLayoutDirty];
965+
[toShadowView markLayoutDirty];
966966
[fromShadowView didUpdateHippySubviews];
967967
[toShadowView didUpdateHippySubviews];
968968
auto strongTags = std::move(ids);
@@ -1000,7 +1000,7 @@ - (void)renderMoveNodes:(std::vector<std::shared_ptr<hippy::DomNode>> &&)nodes
10001000
int32_t index = node->GetRenderInfo().index;
10011001
int32_t componentTag = node->GetId();
10021002
HippyShadowView *objectView = [_shadowViewRegistry componentForTag:@(componentTag) onRootTag:@(rootTag)];
1003-
[objectView dirtyPropagation:NativeRenderUpdateLifecycleLayoutDirtied];
1003+
[objectView markLayoutDirty];
10041004
HippyAssert(!parentObjectView || parentObjectView == [objectView parent], @"parent not same!");
10051005
if (!parentObjectView) {
10061006
parentObjectView = (HippyShadowView *)[objectView parent];
@@ -1044,7 +1044,7 @@ - (void)updateNodesLayout:(const std::vector<std::tuple<int32_t, hippy::LayoutRe
10441044
CGRect frame = CGRectMakeFromLayoutResult(layoutResult);
10451045
HippyShadowView *shadowView = [_shadowViewRegistry componentForTag:componentTag onRootTag:rootTag];
10461046
if (shadowView) {
1047-
[shadowView dirtyPropagation:NativeRenderUpdateLifecycleLayoutDirtied];
1047+
[shadowView markLayoutDirty];
10481048
shadowView.frame = frame;
10491049
shadowView.nodeLayoutResult = layoutResult;
10501050
[self addUIBlock:^(__unused HippyUIManager *uiManager, NSDictionary<NSNumber *,__kindof UIView *> *viewRegistry) {
@@ -1548,7 +1548,7 @@ - (void)onFontChangedFromNative:(NSNotification *)notification {
15481548
((HippyShadowText *)shadowView).fontSizeMultiplier = fontSizeMultiplier;
15491549
}
15501550
[shadowView dirtyText:NO];
1551-
[shadowView dirtyPropagation:NativeRenderUpdateLifecycleLayoutDirtied];
1551+
[shadowView markLayoutDirty];
15521552
}
15531553
}
15541554
// do layout and refresh UI

renderer/native/ios/renderer/component/text/HippyShadowText.mm

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ - (void)updateAttachmentsFrame:(NSMutableSet<NativeRenderApplierBlock> * _Nonnul
256256
options:0
257257
usingBlock:^(HippyShadowView *child, NSRange range, __unused BOOL *_) {
258258
if (child) {
259-
float width = child.width, height = child.height;
259+
// Get width and height from layout style
260+
CGSize styleSize = [child getStyleSize];
261+
float width = styleSize.width;
262+
float height = styleSize.height;
260263
if (isnan(width) || isnan(height)) {
261264
HippyLogError(@"Views nested within a <Text> must have a width and height");
262265
}
@@ -343,8 +346,8 @@ - (void)updateAttachmentsFrame:(NSMutableSet<NativeRenderApplierBlock> * _Nonnul
343346

344347
// Nested <Text> inside <Text> should not call amendLayoutBeforeMount again,
345348
// so only call amendXxx when subcomponent is not a <Text>.
346-
if (NativeRenderUpdateLifecycleComputed != _propagationLifecycle) {
347-
_propagationLifecycle = NativeRenderUpdateLifecycleComputed;
349+
if (!_isLayoutComputed) {
350+
_isLayoutComputed = YES;
348351
for (HippyShadowView *shadowView in self.hippySubviews) {
349352
if (![shadowView isKindOfClass:HippyShadowText.class]) {
350353
[shadowView amendLayoutBeforeMount:blocks];
@@ -506,8 +509,7 @@ - (BOOL)isTextDirty {
506509

507510
- (void)recomputeText {
508511
[self attributedString];
509-
[self setTextComputed];
510-
[self dirtyPropagation:NativeRenderUpdateLifecycleAllDirtied];
512+
[self markLayoutDirty];
511513
}
512514

513515
#pragma mark - AttributeString
@@ -591,23 +593,19 @@ - (NSAttributedString *)_attributedStringWithStyleInfo:(HippyAttributedStringSty
591593
childInfo.isNestedText = styleInfo.isNestedText;
592594
NSAttributedString *subStr = [childShadowText _attributedStringWithStyleInfo:childInfo];
593595
[attributedString appendAttributedString:subStr];
594-
[child setTextComputed];
595596
} else {
596-
float width = 0, height = 0;
597-
auto domManager = [child domManager].lock();
598-
if (domManager) {
599-
int32_t componentTag = [child.hippyTag intValue];
600-
auto domNode = domManager->GetNode(child.rootNode, componentTag);
601-
if (domNode) {
602-
width = domNode->GetLayoutNode()->GetStyleWidth();
603-
height = domNode->GetLayoutNode()->GetStyleHeight();
604-
CGRect frame = child.frame;
605-
frame.size.width = width;
606-
frame.size.height = height;
607-
child.frame = frame;
608-
}
609-
}
610-
if (isnan(width) || isnan(height)) {
597+
// Get width and height from layout style
598+
CGSize styleSize = [child getStyleSize];
599+
float width = styleSize.width;
600+
float height = styleSize.height;
601+
602+
// Update child's frame with the style size
603+
if (!isnan(width) && !isnan(height)) {
604+
CGRect frame = child.frame;
605+
frame.size.width = width;
606+
frame.size.height = height;
607+
child.frame = frame;
608+
} else {
611609
HippyLogError(@"Views nested within a <Text> must have a width and height");
612610
}
613611
// take margin into account
@@ -644,8 +642,7 @@ - (NSAttributedString *)_attributedStringWithStyleInfo:(HippyAttributedStringSty
644642
heightOfTallestSubview = height;
645643
}
646644
_hasAttachment = YES;
647-
// Don't call setTextComputed on this child. HippyTextManager takes care of
648-
// processing inline UIViews.
645+
// Note: HippyTextManager takes care of processing inline UIViews.
649646
}
650647
}
651648

renderer/native/ios/renderer/component/textinput/HippyShadowTextView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ - (void)dirtyText:(BOOL)needToDoLayout {
188188
}
189189

190190
- (void)amendLayoutBeforeMount:(NSMutableSet<NativeRenderApplierBlock> *)blocks {
191-
if (NativeRenderUpdateLifecycleComputed != _propagationLifecycle) {
191+
if (!_isLayoutComputed) {
192192
//Set needs layout for font change event, etc.
193193
NSNumber *currentTag = self.hippyTag;
194194
[blocks addObject:^(NSDictionary<NSNumber *, UIView *> *viewRegistry, UIView * _Nullable lazyCreatedView) {

0 commit comments

Comments
 (0)