Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve separation of code for layout method types #1305

Merged
merged 2 commits into from
Jan 28, 2019

Conversation

maicki
Copy link
Contributor

@maicki maicki commented Jan 7, 2019

Currently most of the layout spec code is tightly coupled to ASDisplayNode and yoga related layout logic is guarded by multiple compiler flag.

This PR tries to separate layout code logic a bit by moving it into different files and categories on ASDisplayNode. This is a first small step to separate and should give us a good base for even separate them more.

}

// Manual size calculation via calculateSizeThatFits:
CGSize size = [self calculateSizeThatFits:constrainedSize.max];
Copy link
Contributor Author

@maicki maicki Jan 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@appleguy Is this the right logic to fallback to calculateSizeThatFits: or should we call through to the layout spec implementation: calculateLayoutLayoutSpec?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely fall back to calculateLayoutThatFits:, as there are some subclasses that implement that directly. By contrast it can also fall back to calculateSizeThatFits: when appropriate, but the reverse is not true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep what I thought. Should be resolved. Hopefully in the future we can drop this though.

Copy link
Member

@Adlai-Holler Adlai-Holler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work

@maicki maicki force-pushed the MSLayoutTypesSeparation branch 2 times, most recently from ecf51ba to 9d1234a Compare January 10, 2019 08:01
Copy link
Member

@appleguy appleguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great change indeed, thanks for working on it!

Let's test it internally soon (before landing), so we can build confidence that there aren't any behavior changes.

@@ -143,19 +144,19 @@ - (NSString *)asciiArtName

@implementation ASDisplayNode (ASLayout)

- (void)setLayoutSpecBlock:(ASLayoutSpecBlock)layoutSpecBlock
- (ASLayoutType)layoutType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! What do you think about ASLayoutEngineType instead?

ASLayoutType sounds a bit too close to describing the actually-produced ASLayout objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, changed the name.

}

// Manual size calculation via calculateSizeThatFits:
CGSize size = [self calculateSizeThatFits:constrainedSize.max];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely fall back to calculateLayoutThatFits:, as there are some subclasses that implement that directly. By contrast it can also fall back to calculateSizeThatFits: when appropriate, but the reverse is not true.

layout = [layout filteredNodeLayoutTree];

return layout;
return nil;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than have default: and the nil fallthrough, what do you think about having an early-return check for Yoga and falling through to ASLayoutSpec in all other cases?

Although that wouldn't be quite as nice if we supported 3 or 4 layout engines, for the time being, we support 2 with the possibility of compiling out one of them. So, it would be especially clean to #if gate the YOGA condition and also avoid the semi-unusual possibility of falling through to this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look into the switch statement we will always fall through to the default case. Before I had an assertion in here but now we will break to hit the assertion I added at the end and to point developers to the comment about the supported layout engine.

In theory the nil case at the end should never be reached in the first place as a node's layout engine should always be supported.

I don't think we should change the logic in here. I further added a comment to make this clear and an assertion at the end.

If you think the switch will add some overhead for the non yoga case we could compile out the whole switch statement, but I think for complexity purposes we should keep it as it is.

NSTimeInterval _layoutComputationTotalTime;
NSInteger _layoutComputationNumberOfPasses;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like too much spacing. Since there's already a comment, should we keep the pattern of 1 blank line + the comment? At most 2 blank?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with 3 spaces, that was unintended, should have been 2.

To make the code more readable I split up different sections for instance variables though. Two spaces between sections and one space between subsections with a comment.

@maicki
Copy link
Contributor Author

maicki commented Jan 14, 2019

@appleguy Want to give that another look please. Thanks!

- Delegate to layout spec engine if the node is a layout spec node but yoga engine was asked for calculate the layout
- Change ASLayoutType to ASLayoutEngineType
- Improve layout engine fall through code
@ghost
Copy link

ghost commented Jan 26, 2019

1 Warning
⚠️ This is a big PR, please consider splitting it up to ease code review.

Generated by 🚫 Danger

@maicki maicki merged commit d4efe95 into master Jan 28, 2019
@nguyenhuy
Copy link
Member

Post-review: LGTM and I'm excited to have a cleaner/better separation between the 2 layout engines. We'll test this internally soon.

@maicki maicki deleted the MSLayoutTypesSeparation branch March 7, 2019 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants