From b40f0562f5f2db8beebd127b30771f68cbc7560e Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Tue, 3 Mar 2020 04:10:31 -0800 Subject: [PATCH] Rename measure to measureContent and pass it LayoutContext Summary: In order to build dynamic text sizing, `LayoutableShadowNode::measure` needs to accept `LayoutContext` Changelog: [Internal] Reviewed By: shergin Differential Revision: D20184598 fbshipit-source-id: 8928b59d51948caf3654f40049212a89a91dceb6 --- Libraries/Wrapper/RCTWrapperView.m | 2 +- React/Fabric/RCTSurfacePresenter.mm | 13 ++++++++++--- .../fabric/components/slider/SliderShadowNode.cpp | 2 +- .../fabric/components/slider/SliderShadowNode.h | 2 +- .../androidswitch/AndroidSwitchShadowNode.cpp | 5 +++-- .../switch/androidswitch/AndroidSwitchShadowNode.h | 3 ++- .../text/paragraph/ParagraphShadowNode.cpp | 3 +-- .../components/text/paragraph/ParagraphShadowNode.h | 2 +- .../androidtextinput/AndroidTextInputShadowNode.cpp | 5 +++-- .../androidtextinput/AndroidTextInputShadowNode.h | 2 +- .../textinput/iostextinput/TextInputShadowNode.cpp | 3 ++- .../textinput/iostextinput/TextInputShadowNode.h | 2 +- .../view/yoga/YogaLayoutableShadowNode.cpp | 3 ++- .../fabric/core/layout/LayoutableShadowNode.cpp | 2 +- .../fabric/core/layout/LayoutableShadowNode.h | 2 +- .../fabric/core/shadownode/ShadowNodeTraits.h | 2 +- 16 files changed, 32 insertions(+), 21 deletions(-) diff --git a/Libraries/Wrapper/RCTWrapperView.m b/Libraries/Wrapper/RCTWrapperView.m index cbc0a9e1b31..fc045f58e22 100644 --- a/Libraries/Wrapper/RCTWrapperView.m +++ b/Libraries/Wrapper/RCTWrapperView.m @@ -76,7 +76,7 @@ [super invalidateIntrinsicContentSize]; // Setting `intrinsicContentSize` dirties the Yoga node and - // enforce Yoga to call `measure` function (backed to `measureBlock`). + // enforce Yoga to call `measureContent` function (backed to `measureBlock`). [_bridge.uiManager setIntrinsicContentSize:self.intrinsicContentSize forView:self]; } diff --git a/React/Fabric/RCTSurfacePresenter.mm b/React/Fabric/RCTSurfacePresenter.mm index b7b2ad07790..c96763a3928 100644 --- a/React/Fabric/RCTSurfacePresenter.mm +++ b/React/Fabric/RCTSurfacePresenter.mm @@ -36,6 +36,12 @@ using namespace facebook::react; +static LayoutContext RCTCurrentLayoutContext() { + return { + .pointScaleFactor = RCTScreenScale(), + }; +} + @interface RCTSurfacePresenter () @end @@ -68,6 +74,7 @@ using namespace facebook::react; _observers = [NSMutableArray array]; _scheduler = [self _createScheduler]; + } return self; @@ -135,7 +142,7 @@ using namespace facebook::react; surface:(RCTFabricSurface *)surface { std::shared_lock lock(_schedulerMutex); - LayoutContext layoutContext = {.pointScaleFactor = RCTScreenScale()}; + LayoutContext layoutContext = RCTCurrentLayoutContext(); LayoutConstraints layoutConstraints = {.minimumSize = RCTSizeFromCGSize(minimumSize), .maximumSize = RCTSizeFromCGSize(maximumSize)}; return [_scheduler measureSurfaceWithLayoutConstraints:layoutConstraints @@ -146,7 +153,7 @@ using namespace facebook::react; - (void)setMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize surface:(RCTFabricSurface *)surface { std::shared_lock lock(_schedulerMutex); - LayoutContext layoutContext = {.pointScaleFactor = RCTScreenScale()}; + LayoutContext layoutContext = RCTCurrentLayoutContext(); LayoutConstraints layoutConstraints = {.minimumSize = RCTSizeFromCGSize(minimumSize), .maximumSize = RCTSizeFromCGSize(maximumSize)}; [_scheduler constraintSurfaceLayoutWithLayoutConstraints:layoutConstraints @@ -255,7 +262,7 @@ using namespace facebook::react; tag:surface.rootTag]; }); - LayoutContext layoutContext = {.pointScaleFactor = RCTScreenScale()}; + LayoutContext layoutContext = RCTCurrentLayoutContext(); LayoutConstraints layoutConstraints = {.minimumSize = RCTSizeFromCGSize(surface.minimumSize), .maximumSize = RCTSizeFromCGSize(surface.maximumSize)}; diff --git a/ReactCommon/fabric/components/slider/SliderShadowNode.cpp b/ReactCommon/fabric/components/slider/SliderShadowNode.cpp index 488fe566783..aa195b776df 100644 --- a/ReactCommon/fabric/components/slider/SliderShadowNode.cpp +++ b/ReactCommon/fabric/components/slider/SliderShadowNode.cpp @@ -83,7 +83,7 @@ ImageSource SliderShadowNode::getThumbImageSource() const { #pragma mark - LayoutableShadowNode -Size SliderShadowNode::measure(LayoutConstraints layoutConstraints) const { +Size SliderShadowNode::measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const { if (SliderMeasurementsManager::shouldMeasureSlider()) { return measurementsManager_->measure(getSurfaceId(), layoutConstraints); } diff --git a/ReactCommon/fabric/components/slider/SliderShadowNode.h b/ReactCommon/fabric/components/slider/SliderShadowNode.h index dc44d5c96b1..13cedd0ce17 100644 --- a/ReactCommon/fabric/components/slider/SliderShadowNode.h +++ b/ReactCommon/fabric/components/slider/SliderShadowNode.h @@ -40,7 +40,7 @@ class SliderShadowNode final : public ConcreteViewShadowNode< #pragma mark - LayoutableShadowNode - Size measure(LayoutConstraints layoutConstraints) const override; + Size measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const override; void layout(LayoutContext layoutContext) override; private: diff --git a/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.cpp b/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.cpp index b717418afd6..0a443fc0643 100644 --- a/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.cpp +++ b/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.cpp @@ -21,8 +21,9 @@ void AndroidSwitchShadowNode::setAndroidSwitchMeasurementsManager( #pragma mark - LayoutableShadowNode -Size AndroidSwitchShadowNode::measure( - LayoutConstraints layoutConstraints) const { +Size AndroidSwitchShadowNode::measureContent( + LayoutConstraints layoutConstraints, + LayoutContext layoutContext) const { return measurementsManager_->measure(getSurfaceId(), layoutConstraints); } diff --git a/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.h b/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.h index 66df81a2a49..70517418a10 100644 --- a/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.h +++ b/ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchShadowNode.h @@ -11,6 +11,7 @@ #include #include +#include #include namespace facebook { @@ -35,7 +36,7 @@ class AndroidSwitchShadowNode final : public ConcreteViewShadowNode< #pragma mark - LayoutableShadowNode - Size measure(LayoutConstraints layoutConstraints) const override; + Size measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const override; private: std::shared_ptr measurementsManager_; diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp index ad7570c97b6..04b64d81289 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp @@ -56,9 +56,8 @@ void ParagraphShadowNode::updateStateIfNeeded() { #pragma mark - LayoutableShadowNode -Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const { +Size ParagraphShadowNode::measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const { AttributedString attributedString = getAttributedString(); - if (attributedString.isEmpty()) { return layoutConstraints.clamp({0, 0}); } diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.h b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.h index c6513462cf9..4c28dc820e1 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.h +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.h @@ -59,7 +59,7 @@ class ParagraphShadowNode : public ConcreteViewShadowNode< #pragma mark - LayoutableShadowNode void layout(LayoutContext layoutContext) override; - Size measure(LayoutConstraints layoutConstraints) const override; + Size measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const override; private: /* diff --git a/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.cpp b/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.cpp index daba6efc7bb..fefac135877 100644 --- a/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.cpp +++ b/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.cpp @@ -153,8 +153,9 @@ void AndroidTextInputShadowNode::updateStateIfNeeded() { #pragma mark - LayoutableShadowNode -Size AndroidTextInputShadowNode::measure( - LayoutConstraints layoutConstraints) const { +Size AndroidTextInputShadowNode::measureContent( + LayoutConstraints layoutConstraints, + LayoutContext layoutContext) const { // Layout is called right after measure. // Measure is marked as `const`, and `layout` is not; so State can be updated // during layout, but not during `measure`. If State is out-of-date in layout, diff --git a/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.h b/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.h index 288545367b8..25071d46bb1 100644 --- a/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.h +++ b/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputShadowNode.h @@ -49,7 +49,7 @@ class AndroidTextInputShadowNode : public ConcreteViewShadowNode< #pragma mark - LayoutableShadowNode - Size measure(LayoutConstraints layoutConstraints) const override; + Size measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const override; void layout(LayoutContext layoutContext) override; private: diff --git a/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.cpp b/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.cpp index 5678cfc9b24..4116ab16f40 100644 --- a/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.cpp +++ b/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.cpp @@ -55,6 +55,7 @@ AttributedString TextInputShadowNode::getAttributedString() const { attributedString.appendAttributedString( BaseTextShadowNode::getAttributedString(textAttributes, *this)); + return attributedString; } @@ -78,7 +79,7 @@ void TextInputShadowNode::updateStateIfNeeded() { #pragma mark - LayoutableShadowNode -Size TextInputShadowNode::measure(LayoutConstraints layoutConstraints) const { +Size TextInputShadowNode::measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const { return textLayoutManager_->measure( attributedStringBoxToMeasure(), getConcreteProps().getEffectiveParagraphAttributes(), diff --git a/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.h b/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.h index 435e5b7da9f..fe975de277d 100644 --- a/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.h +++ b/ReactCommon/fabric/components/textinput/iostextinput/TextInputShadowNode.h @@ -47,7 +47,7 @@ class TextInputShadowNode : public ConcreteViewShadowNode< #pragma mark - LayoutableShadowNode - Size measure(LayoutConstraints layoutConstraints) const override; + Size measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const override; void layout(LayoutContext layoutContext) override; private: diff --git a/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp b/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp index 0c87c9094ec..71ddc9c5fbc 100644 --- a/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp +++ b/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp @@ -359,7 +359,8 @@ YGSize YogaLayoutableShadowNode::yogaNodeMeasureCallbackConnector( break; } - auto size = shadowNodeRawPtr->measure({minimumSize, maximumSize}); + auto size = shadowNodeRawPtr->measureContent({minimumSize, maximumSize}, + *static_cast(layoutContext)); return YGSize{yogaFloatFromFloat(size.width), yogaFloatFromFloat(size.height)}; diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp index 16346746658..515f3004dea 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp @@ -156,7 +156,7 @@ LayoutableShadowNode::getLayoutableChildNodes() const { return layoutableChildren; } -Size LayoutableShadowNode::measure(LayoutConstraints layoutConstraints) const { +Size LayoutableShadowNode::measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const { return Size(); } diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.h b/ReactCommon/fabric/core/layout/LayoutableShadowNode.h index f19ec6e4419..279d9c4a8f8 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.h +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.h @@ -66,7 +66,7 @@ class LayoutableShadowNode : public ShadowNode { * given constrains and relying on possible layout. * Default implementation returns zero size. */ - virtual Size measure(LayoutConstraints layoutConstraints) const; + virtual Size measureContent(LayoutConstraints layoutConstraints, LayoutContext layoutContext) const; /* * Computes layout recursively. diff --git a/ReactCommon/fabric/core/shadownode/ShadowNodeTraits.h b/ReactCommon/fabric/core/shadownode/ShadowNodeTraits.h index 46ad563d712..abd5b093b11 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNodeTraits.h +++ b/ReactCommon/fabric/core/shadownode/ShadowNodeTraits.h @@ -54,7 +54,7 @@ class ShadowNodeTraits { // leaf. LeafYogaNode = 1 << 10, - // Inherits `LayoutableShadowNode` and calls `measure()`. + // Inherits `LayoutableShadowNode` and calls `measureContent()`. HasMeasure = 1 << 11, // Internal to `ShadowNode`; do not use it outside.