2018-09-11 15:27:47 -07:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-31 15:31:03 -07:00
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
2018-07-15 16:46:35 -07:00
|
|
|
#include <fabric/components/root/RootShadowNode.h>
|
2018-05-08 22:56:16 -07:00
|
|
|
#include <fabric/core/LayoutConstraints.h>
|
|
|
|
#include <fabric/core/ReactPrimitives.h>
|
|
|
|
#include <fabric/core/ShadowNode.h>
|
|
|
|
#include <fabric/uimanager/ShadowTreeDelegate.h>
|
2018-09-03 22:53:18 -07:00
|
|
|
#include <fabric/uimanager/ShadowViewMutation.h>
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Represents the shadow tree and its lifecycle.
|
|
|
|
*/
|
2018-09-03 22:53:28 -07:00
|
|
|
class ShadowTree final {
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/*
|
2018-10-09 16:25:08 -07:00
|
|
|
* Creates a new shadow tree instance.
|
2018-05-08 22:56:16 -07:00
|
|
|
*/
|
2018-10-09 16:25:08 -07:00
|
|
|
ShadowTree(
|
|
|
|
SurfaceId surfaceId,
|
|
|
|
const LayoutConstraints &layoutConstraints,
|
|
|
|
const LayoutContext &layoutContext
|
|
|
|
);
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-09-13 22:56:00 -07:00
|
|
|
~ShadowTree();
|
|
|
|
|
2018-05-08 22:56:16 -07:00
|
|
|
/*
|
2018-10-09 16:25:08 -07:00
|
|
|
* Returns the `SurfaceId` associated with the shadow tree.
|
2018-05-08 22:56:16 -07:00
|
|
|
*/
|
2018-10-09 16:25:08 -07:00
|
|
|
SurfaceId getSurfaceId() const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-10-09 16:25:03 -07:00
|
|
|
/*
|
|
|
|
* Synchronously runs `function` when `commitMutex_` is acquired.
|
|
|
|
* It is useful in cases when transactional consistency and/or successful
|
|
|
|
* commit are required. E.g. you might want to run `measure` and
|
|
|
|
* `constraintLayout` as part of a single congious transaction.
|
|
|
|
* Use this only if it is necessary. All public methods of the class are
|
|
|
|
* already thread-safe.
|
|
|
|
*/
|
|
|
|
void synchronize(std::function<void(void)> function) const;
|
|
|
|
|
2018-05-08 22:56:16 -07:00
|
|
|
#pragma mark - Layout
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Measures the shadow tree with given `layoutConstraints` and `layoutContext`.
|
|
|
|
* Can be called from any thread, side-effect-less.
|
|
|
|
*/
|
|
|
|
Size measure(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Applies given `layoutConstraints` and `layoutContext` and commit
|
|
|
|
* the new shadow tree.
|
2018-10-09 16:25:01 -07:00
|
|
|
* Returns `true` if the operation finished successfully.
|
2018-05-08 22:56:16 -07:00
|
|
|
* Can be called from any thread.
|
|
|
|
*/
|
2018-10-09 16:25:01 -07:00
|
|
|
bool constraintLayout(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
#pragma mark - Application
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new shadow tree with given `rootChildNodes` and commit.
|
|
|
|
* Can be called from any thread.
|
2018-10-09 16:25:01 -07:00
|
|
|
* Returns `true` if the operation finished successfully.
|
2018-05-08 22:56:16 -07:00
|
|
|
*/
|
2018-10-09 16:25:01 -07:00
|
|
|
bool complete(const SharedShadowNodeUnsharedList &rootChildNodes) const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
#pragma mark - Delegate
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets and gets the delegate.
|
|
|
|
* The delegate is stored as a raw pointer, so the owner must null
|
|
|
|
* the pointer before being destroyed.
|
|
|
|
*/
|
2018-09-26 10:02:04 -07:00
|
|
|
void setDelegate(ShadowTreeDelegate const *delegate);
|
|
|
|
ShadowTreeDelegate const *getDelegate() const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
|
|
|
private:
|
2018-10-09 16:25:10 -07:00
|
|
|
UnsharedRootShadowNode cloneRootShadowNode(
|
|
|
|
const SharedRootShadowNode &oldRootShadowNode,
|
|
|
|
const LayoutConstraints &layoutConstraints,
|
|
|
|
const LayoutContext &layoutContext
|
|
|
|
) const;
|
|
|
|
|
|
|
|
bool complete(
|
|
|
|
const SharedRootShadowNode &oldRootShadowNode,
|
|
|
|
const UnsharedRootShadowNode &newRootShadowNode
|
|
|
|
) const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-08-27 07:21:18 -07:00
|
|
|
bool commit(
|
|
|
|
const SharedRootShadowNode &oldRootShadowNode,
|
|
|
|
const SharedRootShadowNode &newRootShadowNode,
|
2018-09-03 22:53:18 -07:00
|
|
|
const ShadowViewMutationList &mutations
|
2018-10-09 16:24:57 -07:00
|
|
|
) const;
|
2018-10-09 16:25:10 -07:00
|
|
|
|
2018-10-09 16:24:57 -07:00
|
|
|
void toggleEventEmitters(const ShadowViewMutationList &mutations) const;
|
|
|
|
void emitLayoutEvents(const ShadowViewMutationList &mutations) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return `rootShadowNodeMutex_` protected by `commitMutex_`.
|
|
|
|
*/
|
|
|
|
SharedRootShadowNode getRootShadowNode() const;
|
2018-05-08 22:56:16 -07:00
|
|
|
|
2018-10-09 16:25:08 -07:00
|
|
|
const SurfaceId surfaceId_;
|
2018-10-09 16:24:57 -07:00
|
|
|
mutable SharedRootShadowNode rootShadowNode_; // Protected by `commitMutex_`.
|
2018-09-26 10:02:04 -07:00
|
|
|
ShadowTreeDelegate const *delegate_;
|
2018-10-09 16:25:03 -07:00
|
|
|
mutable std::recursive_mutex commitMutex_;
|
2018-05-08 22:56:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|