Files
react-native/ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h
T
Valentin Shergin c5cc27f1e9 Fabric: Introducing ComponentDescriptor::Flavor
Summary:
Currently, the same ComponentDescriptor class cannot be registered as a responder for components with different names. However, we have marginal cases where we really need it. The examples are `UnimplementedView` or possible universal interop with the classic RN or any other UI framework.

This change adds a special optional argument to ComponentDescript constructor that allows implementing this functionality.

Reviewed By: fkgozali

Differential Revision: D17211915

fbshipit-source-id: 18f59e09fe06b875a8e8975b7b2ab423489238bb
2019-09-29 20:06:37 -07:00

75 lines
2.4 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/config/ReactNativeConfig.h>
#include <react/core/ConcreteComponentDescriptor.h>
#include <react/textlayoutmanager/TextLayoutManager.h>
#include <react/utils/ContextContainer.h>
namespace facebook {
namespace react {
/*
* Descriptor for <Paragraph> component.
*/
class ParagraphComponentDescriptor final
: public ConcreteComponentDescriptor<ParagraphShadowNode> {
public:
ParagraphComponentDescriptor(
EventDispatcher::Weak eventDispatcher,
ContextContainer::Shared const &contextContainer,
ComponentDescriptor::Flavor const &flavor = {})
: ConcreteComponentDescriptor<ParagraphShadowNode>(
eventDispatcher,
contextContainer,
flavor) {
// 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.
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
}
protected:
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_.get());
paragraphShadowNode->dirtyLayout();
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
// setup measure function.
paragraphShadowNode->enableMeasurement();
}
private:
SharedTextLayoutManager textLayoutManager_;
std::unique_ptr<ParagraphMeasurementCache const> measureCache_;
};
} // namespace react
} // namespace facebook