mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Fabric: Migrate ScrollView from LocalData to State
Summary: Seems we need this now to enable future improvements in ScrollView such as correct measure, pull-to-refresh and so on. Reviewed By: mdvacca Differential Revision: D15323687 fbshipit-source-id: fae37431ccbbf2faec9c84752396153689b873ef
This commit is contained in:
committed by
Facebook Github Bot
parent
4001ffb7ce
commit
cd231da27a
@@ -10,8 +10,8 @@
|
||||
#import <React/RCTAssert.h>
|
||||
#import <react/components/scrollview/ScrollViewComponentDescriptor.h>
|
||||
#import <react/components/scrollview/ScrollViewEventEmitter.h>
|
||||
#import <react/components/scrollview/ScrollViewLocalData.h>
|
||||
#import <react/components/scrollview/ScrollViewProps.h>
|
||||
#import <react/components/scrollview/ScrollViewState.h>
|
||||
#import <react/graphics/Geometry.h>
|
||||
|
||||
#import "RCTConversions.h"
|
||||
@@ -28,7 +28,8 @@ using namespace facebook::react;
|
||||
@implementation RCTScrollViewComponentView {
|
||||
RCTEnhancedScrollView *_Nonnull _scrollView;
|
||||
UIView *_Nonnull _contentView;
|
||||
SharedScrollViewLocalData _scrollViewLocalData;
|
||||
ScrollViewShadowNode::ConcreteState::Shared _state;
|
||||
CGSize _contentSize;
|
||||
}
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame
|
||||
@@ -101,11 +102,18 @@ using namespace facebook::react;
|
||||
// MAP_SCROLL_VIEW_PROP(snapToAlignment);
|
||||
}
|
||||
|
||||
- (void)updateLocalData:(SharedLocalData)localData oldLocalData:(SharedLocalData)oldLocalData
|
||||
- (void)updateState:(State::Shared)state oldState:(State::Shared)oldState
|
||||
{
|
||||
assert(std::dynamic_pointer_cast<const ScrollViewLocalData>(localData));
|
||||
_scrollViewLocalData = std::static_pointer_cast<const ScrollViewLocalData>(localData);
|
||||
CGSize contentSize = RCTCGSizeFromSize(_scrollViewLocalData->getContentSize());
|
||||
assert(std::dynamic_pointer_cast<ScrollViewShadowNode::ConcreteState const>(state));
|
||||
_state = std::static_pointer_cast<ScrollViewShadowNode::ConcreteState const>(state);
|
||||
|
||||
CGSize contentSize = RCTCGSizeFromSize(_state->getData().getContentSize());
|
||||
|
||||
if (CGSizeEqualToSize(_contentSize, contentSize)) {
|
||||
return;
|
||||
}
|
||||
|
||||
_contentSize = contentSize;
|
||||
_contentView.frame = CGRect{CGPointZero, contentSize};
|
||||
_scrollView.contentSize = contentSize;
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "ScrollViewLocalData.h"
|
||||
|
||||
#include <react/debug/debugStringConvertibleUtils.h>
|
||||
#include <react/graphics/conversions.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
ScrollViewLocalData::ScrollViewLocalData(Rect contentBoundingRect)
|
||||
: contentBoundingRect(contentBoundingRect) {}
|
||||
|
||||
Size ScrollViewLocalData::getContentSize() const {
|
||||
return Size{contentBoundingRect.getMaxX(), contentBoundingRect.getMaxY()};
|
||||
}
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
#if RN_DEBUG_STRING_CONVERTIBLE
|
||||
std::string ScrollViewLocalData::getDebugName() const {
|
||||
return "ScrollViewLocalData";
|
||||
}
|
||||
|
||||
SharedDebugStringConvertibleList ScrollViewLocalData::getDebugProps() const {
|
||||
return {
|
||||
debugStringConvertibleItem("contentBoundingRect", contentBoundingRect)};
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <react/core/LocalData.h>
|
||||
#include <react/graphics/Geometry.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class ScrollViewLocalData;
|
||||
|
||||
using SharedScrollViewLocalData = std::shared_ptr<const ScrollViewLocalData>;
|
||||
|
||||
/*
|
||||
* LocalData for <ScrollView> component.
|
||||
*/
|
||||
class ScrollViewLocalData : public LocalData {
|
||||
public:
|
||||
ScrollViewLocalData(Rect contentBoundingRect);
|
||||
|
||||
/*
|
||||
* Compound size of all nested (first level only) components;
|
||||
* is used for computing `contentSize`.
|
||||
*/
|
||||
const Rect contentBoundingRect;
|
||||
|
||||
#pragma mark - Getters
|
||||
|
||||
Size getContentSize() const;
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
#if RN_DEBUG_STRING_CONVERTIBLE
|
||||
std::string getDebugName() const override;
|
||||
SharedDebugStringConvertibleList getDebugProps() const override;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -9,14 +9,12 @@
|
||||
|
||||
#include <react/core/LayoutMetrics.h>
|
||||
|
||||
#include "ScrollViewLocalData.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
const char ScrollViewComponentName[] = "ScrollView";
|
||||
|
||||
void ScrollViewShadowNode::updateLocalData() {
|
||||
void ScrollViewShadowNode::updateStateIfNeeded() {
|
||||
ensureUnsealed();
|
||||
|
||||
auto contentBoundingRect = Rect{};
|
||||
@@ -24,16 +22,19 @@ void ScrollViewShadowNode::updateLocalData() {
|
||||
contentBoundingRect.unionInPlace(childNode->getLayoutMetrics().frame);
|
||||
}
|
||||
|
||||
const auto &localData =
|
||||
std::make_shared<const ScrollViewLocalData>(contentBoundingRect);
|
||||
setLocalData(localData);
|
||||
auto state = getStateData();
|
||||
|
||||
if (state.contentBoundingRect != contentBoundingRect) {
|
||||
state.contentBoundingRect = contentBoundingRect;
|
||||
setStateData(std::move(state));
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - LayoutableShadowNode
|
||||
|
||||
void ScrollViewShadowNode::layout(LayoutContext layoutContext) {
|
||||
ConcreteViewShadowNode::layout(layoutContext);
|
||||
updateLocalData();
|
||||
updateStateIfNeeded();
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <react/components/scrollview/ScrollViewEventEmitter.h>
|
||||
#include <react/components/scrollview/ScrollViewProps.h>
|
||||
#include <react/components/scrollview/ScrollViewState.h>
|
||||
#include <react/components/view/ConcreteViewShadowNode.h>
|
||||
#include <react/core/LayoutContext.h>
|
||||
|
||||
@@ -23,7 +24,8 @@ extern const char ScrollViewComponentName[];
|
||||
class ScrollViewShadowNode final : public ConcreteViewShadowNode<
|
||||
ScrollViewComponentName,
|
||||
ScrollViewProps,
|
||||
ScrollViewEventEmitter> {
|
||||
ScrollViewEventEmitter,
|
||||
ScrollViewState> {
|
||||
public:
|
||||
using ConcreteViewShadowNode::ConcreteViewShadowNode;
|
||||
|
||||
@@ -32,7 +34,7 @@ class ScrollViewShadowNode final : public ConcreteViewShadowNode<
|
||||
void layout(LayoutContext layoutContext) override;
|
||||
|
||||
private:
|
||||
void updateLocalData();
|
||||
void updateStateIfNeeded();
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "ScrollViewState.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
Size ScrollViewState::getContentSize() const {
|
||||
return Size{contentBoundingRect.getMaxX(), contentBoundingRect.getMaxY()};
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <react/graphics/Geometry.h>
|
||||
|
||||
#include <folly/dynamic.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
/*
|
||||
* State for <ScrollView> component.
|
||||
*/
|
||||
class ScrollViewState final {
|
||||
public:
|
||||
Point contentOffset;
|
||||
Rect contentBoundingRect;
|
||||
|
||||
/*
|
||||
* Returns size of scrollable area.
|
||||
*/
|
||||
Size getContentSize() const;
|
||||
|
||||
#ifdef ANDROID
|
||||
ScrollViewState() = default;
|
||||
ScrollViewState(folly::dynamic data){};
|
||||
folly::dynamic getDynamic() const {
|
||||
return {};
|
||||
};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
Reference in New Issue
Block a user