Skip to content

Commit

Permalink
Fabric: Passing size constraints as part of starting Surface
Browse files Browse the repository at this point in the history
Summary:
Size constraints are essential part of the running Surface, decoupling them from starting process means that we will have to perform additional commit later.
This and previous couple diffs fix a problem with initial zero size of the surface and following visible "jumpy" relayout.

Reviewed By: sahrens

Differential Revision: D10174280

fbshipit-source-id: 0ec48692cb814fd46cc3a1d044c5eb8ab9ecb031
  • Loading branch information
shergin authored and facebook-github-bot committed Oct 9, 2018
1 parent b8947c4 commit 4ce57cb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 35 deletions.
4 changes: 3 additions & 1 deletion React/Fabric/RCTScheduler.h
Expand Up @@ -41,7 +41,9 @@ NS_ASSUME_NONNULL_BEGIN

- (void)startSurfaceWithSurfaceId:(facebook::react::SurfaceId)surfaceId
moduleName:(NSString *)moduleName
initailProps:(NSDictionary *)initialProps;
initailProps:(NSDictionary *)initialProps
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints
layoutContext:(facebook::react::LayoutContext)layoutContext;

- (void)stopSurfaceWithSurfaceId:(facebook::react::SurfaceId)surfaceId;

Expand Down
6 changes: 5 additions & 1 deletion React/Fabric/RCTScheduler.mm
Expand Up @@ -60,11 +60,15 @@ - (void)dealloc
- (void)startSurfaceWithSurfaceId:(SurfaceId)surfaceId
moduleName:(NSString *)moduleName
initailProps:(NSDictionary *)initialProps
layoutConstraints:(LayoutConstraints)layoutConstraints
layoutContext:(LayoutContext)layoutContext;
{
_scheduler->startSurface(
surfaceId,
RCTStringFromNSString(moduleName),
convertIdToFollyDynamic(initialProps)
convertIdToFollyDynamic(initialProps),
layoutConstraints,
layoutContext
);
}

Expand Down
43 changes: 28 additions & 15 deletions React/Fabric/RCTSurfacePresenter.mm
Expand Up @@ -108,11 +108,14 @@ - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize
maximumSize:(CGSize)maximumSize
surface:(RCTFabricSurface *)surface
{
LayoutContext layoutContext;
layoutContext.pointScaleFactor = RCTScreenScale();
LayoutConstraints layoutConstraints = {};
layoutConstraints.minimumSize = RCTSizeFromCGSize(minimumSize);
layoutConstraints.maximumSize = RCTSizeFromCGSize(maximumSize);
LayoutContext layoutContext = {
.pointScaleFactor = RCTScreenScale()
};

LayoutConstraints layoutConstraints = {
.minimumSize = RCTSizeFromCGSize(minimumSize),
.maximumSize = RCTSizeFromCGSize(maximumSize)
};

return [self._scheduler measureSurfaceWithLayoutConstraints:layoutConstraints
layoutContext:layoutContext
Expand All @@ -123,11 +126,14 @@ - (void)setMinimumSize:(CGSize)minimumSize
maximumSize:(CGSize)maximumSize
surface:(RCTFabricSurface *)surface
{
LayoutContext layoutContext;
layoutContext.pointScaleFactor = RCTScreenScale();
LayoutConstraints layoutConstraints = {};
layoutConstraints.minimumSize = RCTSizeFromCGSize(minimumSize);
layoutConstraints.maximumSize = RCTSizeFromCGSize(maximumSize);
LayoutContext layoutContext = {
.pointScaleFactor = RCTScreenScale()
};

LayoutConstraints layoutConstraints = {
.minimumSize = RCTSizeFromCGSize(minimumSize),
.maximumSize = RCTSizeFromCGSize(maximumSize)
};

[self._scheduler constraintSurfaceLayoutWithLayoutConstraints:layoutConstraints
layoutContext:layoutContext
Expand Down Expand Up @@ -174,13 +180,20 @@ - (void)_startSurface:(RCTFabricSurface *)surface
{
[_mountingManager.componentViewRegistry dequeueComponentViewWithName:@"Root" tag:surface.rootTag];

LayoutContext layoutContext = {
.pointScaleFactor = RCTScreenScale()
};

LayoutConstraints layoutConstraints = {
.minimumSize = RCTSizeFromCGSize(surface.minimumSize),
.maximumSize = RCTSizeFromCGSize(surface.maximumSize)
};

[self._scheduler startSurfaceWithSurfaceId:surface.rootTag
moduleName:surface.moduleName
initailProps:surface.properties];

[self setMinimumSize:surface.minimumSize
maximumSize:surface.maximumSize
surface:surface];
initailProps:surface.properties
layoutConstraints:layoutConstraints
layoutContext:layoutContext];
}

- (void)_stopSurface:(RCTFabricSurface *)surface
Expand Down
8 changes: 5 additions & 3 deletions ReactCommon/fabric/uimanager/Scheduler.cpp
Expand Up @@ -54,11 +54,13 @@ Scheduler::~Scheduler() {
void Scheduler::startSurface(
SurfaceId surfaceId,
const std::string &moduleName,
const folly::dynamic &initialProps
const folly::dynamic &initialProps,
const LayoutConstraints &layoutConstraints,
const LayoutContext &layoutContext
) const {
std::lock_guard<std::mutex> lock(mutex_);

auto shadowTree = std::make_unique<ShadowTree>(surfaceId);
auto shadowTree = std::make_unique<ShadowTree>(surfaceId, layoutConstraints, layoutContext);
shadowTree->setDelegate(this);
shadowTreeRegistry_.emplace(surfaceId, std::move(shadowTree));

Expand Down Expand Up @@ -121,7 +123,7 @@ SchedulerDelegate *Scheduler::getDelegate() const {

void Scheduler::shadowTreeDidCommit(const ShadowTree &shadowTree, const ShadowViewMutationList &mutations) const {
if (delegate_) {
delegate_->schedulerDidFinishTransaction(shadowTree.getRootTag(), mutations);
delegate_->schedulerDidFinishTransaction(shadowTree.getSurfaceId(), mutations);
}
}

Expand Down
4 changes: 3 additions & 1 deletion ReactCommon/fabric/uimanager/Scheduler.h
Expand Up @@ -37,7 +37,9 @@ class Scheduler final:
void startSurface(
SurfaceId surfaceId,
const std::string &moduleName,
const folly::dynamic &initialProps
const folly::dynamic &initialProps,
const LayoutConstraints &layoutConstraints = {},
const LayoutContext &layoutContext = {}
) const;

void stopSurface(SurfaceId surfaceId) const;
Expand Down
27 changes: 19 additions & 8 deletions ReactCommon/fabric/uimanager/ShadowTree.cpp
Expand Up @@ -15,15 +15,26 @@
namespace facebook {
namespace react {

ShadowTree::ShadowTree(Tag rootTag):
rootTag_(rootTag) {
ShadowTree::ShadowTree(
SurfaceId surfaceId,
const LayoutConstraints &layoutConstraints,
const LayoutContext &layoutContext
):
surfaceId_(surfaceId) {

const auto noopEventEmitter = std::make_shared<const ViewEventEmitter>(nullptr, -1, std::shared_ptr<const EventDispatcher>());

const auto props = std::make_shared<const RootProps>(
*RootShadowNode::defaultSharedProps(),
layoutConstraints,
layoutContext
);

const auto noopEventEmitter = std::make_shared<const ViewEventEmitter>(nullptr, rootTag, std::shared_ptr<const EventDispatcher>());
rootShadowNode_ = std::make_shared<RootShadowNode>(
ShadowNodeFragment {
.tag = rootTag,
.rootTag = rootTag,
.props = RootShadowNode::defaultSharedProps(),
.tag = surfaceId,
.rootTag = surfaceId,
.props = props,
.eventEmitter = noopEventEmitter,
},
nullptr
Expand All @@ -34,8 +45,8 @@ ShadowTree::~ShadowTree() {
complete(std::make_shared<SharedShadowNodeList>(SharedShadowNodeList {}));
}

Tag ShadowTree::getRootTag() const {
return rootTag_;
Tag ShadowTree::getSurfaceId() const {
return surfaceId_;
}

SharedRootShadowNode ShadowTree::getRootShadowNode() const {
Expand Down
15 changes: 9 additions & 6 deletions ReactCommon/fabric/uimanager/ShadowTree.h
Expand Up @@ -26,17 +26,20 @@ class ShadowTree final {
public:

/*
* Creates a new shadow tree instance with given `rootTag`.
* Creates a new shadow tree instance.
*/
ShadowTree(Tag rootTag);
ShadowTree(
SurfaceId surfaceId,
const LayoutConstraints &layoutConstraints,
const LayoutContext &layoutContext
);

~ShadowTree();

/*
* Returns the rootTag associated with the shadow tree (the tag of the
* root shadow node).
* Returns the `SurfaceId` associated with the shadow tree.
*/
Tag getRootTag() const;
SurfaceId getSurfaceId() const;

/*
* Synchronously runs `function` when `commitMutex_` is acquired.
Expand Down Expand Up @@ -100,7 +103,7 @@ class ShadowTree final {
*/
SharedRootShadowNode getRootShadowNode() const;

const Tag rootTag_;
const SurfaceId surfaceId_;
mutable SharedRootShadowNode rootShadowNode_; // Protected by `commitMutex_`.
ShadowTreeDelegate const *delegate_;
mutable std::recursive_mutex commitMutex_;
Expand Down

0 comments on commit 4ce57cb

Please sign in to comment.