From 8d5350898a17521e4629c24c0ec3bf435df08b50 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 19 Aug 2022 12:04:22 +0800 Subject: [PATCH] iOS: refact layout use measure state to control when it self is most,but supernode is fit --- doric-iOS/Pod/Classes/Shader/DoricLayouts.m | 48 +++++++++++++-------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/doric-iOS/Pod/Classes/Shader/DoricLayouts.m b/doric-iOS/Pod/Classes/Shader/DoricLayouts.m index 94521779..cbee65c2 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricLayouts.m +++ b/doric-iOS/Pod/Classes/Shader/DoricLayouts.m @@ -92,6 +92,8 @@ - (DoricLayout *)doricLayout { @end @interface DoricLayout () +@property(nonatomic, assign) bool needRemeasure; +@property(nonatomic, assign) bool remeasuring; @end @implementation DoricLayout @@ -103,6 +105,8 @@ - (instancetype)init { _maxHeight = CGFLOAT_MAX; _minWidth = -1; _minHeight = -1; + _needRemeasure = NO; + _remeasuring = NO; } return self; } @@ -284,13 +288,15 @@ - (CGFloat)addHeightPadding:(CGFloat)size { - (bool)needFitWidth { return self.widthSpec == DoricLayoutFit - || (self.widthSpec == DoricLayoutMost && self.superLayout.needFitWidth) + || (self.widthSpec == DoricLayoutMost && self.superLayout.needFitWidth + && (!self.superLayout.remeasuring || self.inHLayout)) || (self.widthSpec == DoricLayoutJust && self.hasWidthWeight); } - (bool)needFitHeight { return self.heightSpec == DoricLayoutFit - || (self.heightSpec == DoricLayoutMost && self.superLayout.needFitHeight) + || (self.heightSpec == DoricLayoutMost && self.superLayout.needFitHeight + && (!self.superLayout.remeasuring || self.inVLayout)) || (self.heightSpec == DoricLayoutJust && self.hasHeightWeight); } @@ -369,6 +375,8 @@ - (void)measureSelf:(CGSize)remainingSize limitTo:(CGSize)limitSize { } - (void)measureSelf:(CGSize)remainingSize limitTo:(CGSize)limitSize restrain:(bool)needRestrain { + self.needRemeasure = NO; + self.remeasuring = NO; [self measureContent:[self removeSizePadding:remainingSize] limitTo:[self removeSizePadding:limitSize]]; @@ -404,12 +412,30 @@ - (void)measureSelf:(CGSize)remainingSize limitTo:(CGSize)limitSize restrain:(bo self.measuredHeight = self.height; } - if (needRestrain && [self restrain:limitSize] && self.layoutType != DoricUndefined) { + if (needRestrain + && [self restrain:limitSize] + && self.layoutType != DoricUndefined + ) { CGSize size = [self removeSizePadding:CGSizeMake( self.measuredWidth, self.measuredHeight)]; [self measureSelf:size limitTo:size restrain:NO]; } + + if (self.needRemeasure) { + CGSize size = [self removeSizePadding:CGSizeMake( + self.measuredWidth, + self.measuredHeight)]; + self.remeasuring = YES; + [self measureContent:size + limitTo:size]; + self.remeasuring = NO; + } + + if ((self.mostWidth && self.superLayout.needFitWidth) + || (self.mostHeight && self.superLayout.needFitHeight)) { + self.superLayout.needRemeasure = YES; + } } #pragma measureContent @@ -487,8 +513,7 @@ - (void)measureVLayoutContent:(CGSize)remaining limitTo:(CGSize)limit { if (had) { contentHeight -= self.spacing; } - - if (contentWeight > 0 && !self.fitHeight) { + if (contentWeight > 0 && !self.fitHeight && !self.hasHeightWeight) { CGFloat extra = remaining.height - contentHeight; contentWidth = 0; contentHeight = 0; @@ -677,12 +702,6 @@ - (void)layoutStack { if (layout.disabled) { continue; } - if (self.needFitWidth && layout.mostWidth) { - layout.measuredWidth = self.contentWidth - layout.marginLeft - layout.marginRight; - } - if (self.needFitHeight && layout.mostHeight) { - layout.measuredHeight = self.contentHeight - layout.marginTop - layout.marginBottom; - } [layout layout]; DoricGravity gravity = layout.alignment; if ((gravity & DoricGravityLeft) == DoricGravityLeft) { @@ -736,9 +755,6 @@ - (void)layoutVLayout { if (layout.disabled) { continue; } - if (self.needFitWidth && layout.mostWidth) { - layout.measuredWidth = self.contentWidth - layout.marginLeft - layout.marginRight; - } [layout layout]; DoricGravity gravity = layout.alignment | self.gravity; if ((gravity & DoricGravityLeft) == DoricGravityLeft) { @@ -779,10 +795,6 @@ - (void)layoutHLayout { continue; } - if (self.needFitHeight && layout.mostHeight) { - layout.measuredHeight = self.contentHeight - layout.marginTop - layout.marginBottom; - } - [layout layout]; DoricGravity gravity = layout.alignment | self.gravity;