Files
react-native/ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h
T
Joshua GrossandFacebook Github Bot 28b8b8e370 Use feature flags in Fabric C++ core, in particular for paragraph measurement caching
Summary: The goal is to be able to use MobileConfig params inside of core React Native C++ code. This works on Catalyst iOS now and Wilde; need to add support for FB4A and Catalyst Android.

Reviewed By: fkgozali

Differential Revision: D13883007

fbshipit-source-id: 115fe6cc884d2a0b9ca26dadf867a5f0ae99f262
2019-02-01 18:19:11 -08:00

85 lines
2.8 KiB
C++

/**
* 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 "ParagraphMeasurementCache.h"
#include "ParagraphShadowNode.h"
#include <folly/container/EvictingCacheMap.h>
#include <react/core/ConcreteComponentDescriptor.h>
#include <react/textlayoutmanager/TextLayoutManager.h>
#include <react/uimanager/ContextContainer.h>
namespace facebook {
namespace react {
/*
* Descriptor for <Paragraph> component.
*/
class ParagraphComponentDescriptor final
: public ConcreteComponentDescriptor<ParagraphShadowNode> {
public:
ParagraphComponentDescriptor(
SharedEventDispatcher eventDispatcher,
const SharedContextContainer &contextContainer)
: ConcreteComponentDescriptor<ParagraphShadowNode>(eventDispatcher) {
// Every single `ParagraphShadowNode` will have a reference to
// a shared `TextLayoutManager`.
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer);
// Every single `ParagraphShadowNode` will have a reference to
// a shared `EvictingCacheMap`, a simple LRU cache for Paragraph
// measurements.
#ifdef ANDROID
auto paramName = "react_fabric:enabled_paragraph_measure_cache_android";
#else
auto paramName = "react_fabric:enabled_paragraph_measure_cache_ios";
#endif
// TODO: T39927960 - get rid of this if statement
bool enableCache =
(contextContainer != nullptr
? contextContainer
->getInstance<std::shared_ptr<const ReactNativeConfig>>(
"ReactNativeConfig")
->getBool(paramName)
: false);
if (enableCache) {
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
} else {
measureCache_ = nullptr;
}
}
void adopt(UnsharedShadowNode shadowNode) const override {
ConcreteComponentDescriptor::adopt(shadowNode);
assert(std::dynamic_pointer_cast<ParagraphShadowNode>(shadowNode));
auto paragraphShadowNode =
std::static_pointer_cast<ParagraphShadowNode>(shadowNode);
// `ParagraphShadowNode` uses `TextLayoutManager` to measure text content
// and communicate text rendering metrics to mounting layer.
paragraphShadowNode->setTextLayoutManager(textLayoutManager_);
// `ParagraphShadowNode` uses this to cache the results of text rendering
// measurements.
paragraphShadowNode->setMeasureCache(
measureCache_ ? measureCache_.get() : nullptr);
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
// setup measure function.
paragraphShadowNode->enableMeasurement();
}
private:
SharedTextLayoutManager textLayoutManager_;
std::unique_ptr<const ParagraphMeasurementCache> measureCache_;
};
} // namespace react
} // namespace facebook