Files
react-native/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp
T
Valentin Shergin 7186a65b13 Fabric: Changes in BaseTextShadowNode to remove usage of shared_from_this()
Summary:
This is another step on the journey of removing `enable_shared_from_this`.

It's unclear why we used that before but it's clear now that using shared_ptr here is not necessary because all computation around happens inside the single callstack, so by definition we don't have object life-time concerns here.

Changelog: [Internal] Small Fabric-specific optimization.

Reviewed By: sammy-SC

Differential Revision: D17973957

fbshipit-source-id: 09a65c78e22083ed21b041240307f4858379cc60
2019-10-21 09:44:25 -07:00

93 lines
2.5 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.
*/
#include "ParagraphShadowNode.h"
#include "ParagraphMeasurementCache.h"
#include "ParagraphState.h"
namespace facebook {
namespace react {
char const ParagraphComponentName[] = "Paragraph";
AttributedString ParagraphShadowNode::getAttributedString() const {
if (!cachedAttributedString_.has_value()) {
auto textAttributes = TextAttributes::defaultTextAttributes();
textAttributes.apply(getProps()->textAttributes);
cachedAttributedString_ =
BaseTextShadowNode::getAttributedString(textAttributes, *this);
}
return cachedAttributedString_.value();
}
void ParagraphShadowNode::setTextLayoutManager(
SharedTextLayoutManager textLayoutManager) {
ensureUnsealed();
textLayoutManager_ = textLayoutManager;
}
void ParagraphShadowNode::setMeasureCache(
ParagraphMeasurementCache const *cache) {
ensureUnsealed();
measureCache_ = cache;
}
void ParagraphShadowNode::updateStateIfNeeded() {
ensureUnsealed();
auto attributedString = getAttributedString();
auto const &state = getStateData();
assert(textLayoutManager_);
assert(
(!state.layoutManager || state.layoutManager == textLayoutManager_) &&
"`StateData` refers to a different `TextLayoutManager`");
if (state.attributedString == attributedString &&
state.layoutManager == textLayoutManager_) {
return;
}
setStateData(ParagraphState{attributedString, textLayoutManager_});
}
#pragma mark - LayoutableShadowNode
Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
AttributedString attributedString = getAttributedString();
if (attributedString.isEmpty()) {
return {0, 0};
}
ParagraphAttributes const paragraphAttributes =
getProps()->paragraphAttributes;
assert(measureCache_);
return measureCache_->get(
ParagraphMeasurementCacheKey{
attributedString, paragraphAttributes, layoutConstraints},
[&](ParagraphMeasurementCacheKey const &key) {
return textLayoutManager_->measure(
attributedString, paragraphAttributes, layoutConstraints);
});
return textLayoutManager_->measure(
attributedString, paragraphAttributes, layoutConstraints);
}
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
updateStateIfNeeded();
ConcreteViewShadowNode::layout(layoutContext);
}
} // namespace react
} // namespace facebook