From a82ebeb6f35601dd3babb9117ec2be43019faa3e Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 31 Oct 2019 21:23:43 -0700 Subject: [PATCH] Fabric: Use padding instead of size for SafeAreaView Summary: Use padding instead of setting size of SafeAreaView, this should make it more consistent with Paper component. changelog: [internal] Reviewed By: shergin Differential Revision: D18225793 fbshipit-source-id: 08dccbdae0e4f7a7847501a06e17d4c26473462a --- .../RCTSafeAreaViewComponentView.mm | 39 ++++--------------- .../SafeAreaViewComponentDescriptor.h | 7 +--- .../safeareaview/SafeAreaViewState.h | 6 +-- .../view/yoga/YogaLayoutableShadowNode.cpp | 12 ++++++ .../view/yoga/YogaLayoutableShadowNode.h | 2 + 5 files changed, 26 insertions(+), 40 deletions(-) diff --git a/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm b/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm index fa016edbaa8..57714d09037 100644 --- a/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm @@ -13,27 +13,6 @@ using namespace facebook::react; -static UIScrollView *findScrollView(UIView *view, uint recursionDepth = 0) -{ - if (recursionDepth >= 3) { - return NULL; - } - if ([view isKindOfClass:[UIScrollView class]]) { - return (UIScrollView *)view; - } - - if (view.subviews.count >= 1) { - for (UIView *subview in view.subviews) { - UIScrollView *scrollView = findScrollView(subview.subviews.firstObject, recursionDepth + 1); - if (scrollView) { - return scrollView; - } - } - } - - return NULL; -} - @implementation RCTSafeAreaViewComponentView { SafeAreaViewShadowNode::ConcreteState::Shared _state; } @@ -61,16 +40,14 @@ static UIScrollView *findScrollView(UIView *view, uint recursionDepth = 0) - (void)layoutSubviews { [super layoutSubviews]; - UIScrollView *scrollView = findScrollView(self); - if (scrollView && CGSizeEqualToSize(scrollView.bounds.size, self.bounds.size)) { - [scrollView setContentInset:self._safeAreaInsets]; - } else { - if (_state != nullptr) { - CGSize size = self.bounds.size; - size.height -= self._safeAreaInsets.bottom; - auto newState = SafeAreaViewState{RCTSizeFromCGSize(size)}; - _state->updateState(std::move(newState)); - } +} + +- (void)safeAreaInsetsDidChange +{ + [super safeAreaInsetsDidChange]; + if (_state != nullptr) { + auto newState = SafeAreaViewState{RCTEdgeInsetsFromUIEdgeInsets(self._safeAreaInsets)}; + _state->updateState(std::move(newState)); } } diff --git a/ReactCommon/fabric/components/safeareaview/SafeAreaViewComponentDescriptor.h b/ReactCommon/fabric/components/safeareaview/SafeAreaViewComponentDescriptor.h index c303e3f0eb4..aac036f2d01 100644 --- a/ReactCommon/fabric/components/safeareaview/SafeAreaViewComponentDescriptor.h +++ b/ReactCommon/fabric/components/safeareaview/SafeAreaViewComponentDescriptor.h @@ -35,12 +35,7 @@ class SafeAreaViewComponentDescriptor final shadowNode->getState()); auto stateData = state->getData(); - if (stateData.safeAreaSize.width != 0 && - stateData.safeAreaSize.height != 0) { - layoutableShadowNode->setSize( - Size{stateData.safeAreaSize.width, stateData.safeAreaSize.height}); - layoutableShadowNode->setPositionType(YGPositionTypeAbsolute); - } + layoutableShadowNode->setPadding(stateData.padding); ConcreteComponentDescriptor::adopt(shadowNode); } diff --git a/ReactCommon/fabric/components/safeareaview/SafeAreaViewState.h b/ReactCommon/fabric/components/safeareaview/SafeAreaViewState.h index 0509331303c..9fc152cea4f 100644 --- a/ReactCommon/fabric/components/safeareaview/SafeAreaViewState.h +++ b/ReactCommon/fabric/components/safeareaview/SafeAreaViewState.h @@ -19,10 +19,10 @@ class SafeAreaViewState final { public: using Shared = std::shared_ptr; - SafeAreaViewState(){}; - SafeAreaViewState(Size screenSize_) : safeAreaSize(screenSize_){}; + SafeAreaViewState() = default; + SafeAreaViewState(EdgeInsets padding_) : padding(padding_){}; - const Size safeAreaSize{}; + const EdgeInsets padding{}; #pragma mark - Getters }; diff --git a/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp b/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp index 180e0e4aae9..27d3d62554a 100644 --- a/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp +++ b/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.cpp @@ -138,6 +138,18 @@ void YogaLayoutableShadowNode::setSize(Size size) const { yogaNode_.setDirty(true); } +void YogaLayoutableShadowNode::setPadding(RectangleEdges padding) const { + ensureUnsealed(); + + auto style = yogaNode_.getStyle(); + style.padding()[YGEdgeTop] = yogaStyleValueFromFloat(padding.top); + style.padding()[YGEdgeLeft] = yogaStyleValueFromFloat(padding.left); + style.padding()[YGEdgeRight] = yogaStyleValueFromFloat(padding.right); + style.padding()[YGEdgeBottom] = yogaStyleValueFromFloat(padding.bottom); + yogaNode_.setStyle(style); + yogaNode_.setDirty(true); +} + void YogaLayoutableShadowNode::setPositionType( YGPositionType positionType) const { ensureUnsealed(); diff --git a/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.h b/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.h index cd5d0aba295..f0842dfda18 100644 --- a/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.h +++ b/ReactCommon/fabric/components/view/yoga/YogaLayoutableShadowNode.h @@ -69,6 +69,8 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode, */ void setSize(Size size) const; + void setPadding(RectangleEdges padding) const; + /** * Sets position type of Yoga node (relative, absolute). */