mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Fabric: Introducing AttributedStringBox
Summary: The diff implements a new class called `AttributedStringBox` that represents an object storing a shared `AttributedString` *or* a shared pointer to some opaque platform-specific object that can be used as an attributed string. The class serves two main purposes: - Represent type-erased attributed string entity (which can be platform-specific or platform-independent); - Represent a container that can be copied with constant complexity. Why? Several reasons: - Sometimes it makes sense to keep an attributed string as a shared resource. This way we don't need to pay for expensive copying and we also implement a copy-on-write semantics on top of that if needed. - We need to extend a TextLayoutMeasure API to support measuring some platform-specific attributed string implementation to remove the necessity of converting a string back and forth between representations. That's especially important for TextInput because we will need to measure that very efficiently (and the source of measuring, in this case, is a platform attributed string). In other words, we need something to store inside TextInputState to measure and update very efficiently. The source of this data might be a native TextInput control or a data from React, to represent that kinda object we need this data structure (and interfaces that deal with it). Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D18670793 fbshipit-source-id: bc0164f801f28642f7c6da340af12acf33b85d24
This commit is contained in:
committed by
Facebook Github Bot
parent
70ec7e2add
commit
c4876d0313
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 "AttributedStringBox.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
AttributedStringBox::AttributedStringBox()
|
||||
: mode_(Mode::Value),
|
||||
value_(std::make_shared<AttributedString const>(AttributedString{})),
|
||||
opaquePointer_({}){};
|
||||
|
||||
AttributedStringBox::AttributedStringBox(AttributedString const &value)
|
||||
: mode_(Mode::Value),
|
||||
value_(std::make_shared<AttributedString const>(value)),
|
||||
opaquePointer_({}){};
|
||||
|
||||
AttributedStringBox::AttributedStringBox(
|
||||
std::shared_ptr<void> const &opaquePointer)
|
||||
: mode_(Mode::OpaquePointer), value_({}), opaquePointer_(opaquePointer) {}
|
||||
|
||||
AttributedStringBox::Mode AttributedStringBox::getMode() const {
|
||||
return mode_;
|
||||
}
|
||||
|
||||
AttributedString const &AttributedStringBox::getValue() const {
|
||||
assert(mode_ == AttributedStringBox::Mode::Value);
|
||||
assert(value_);
|
||||
return *value_;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> AttributedStringBox::getOpaquePointer() const {
|
||||
assert(mode_ == AttributedStringBox::Mode::OpaquePointer);
|
||||
assert(opaquePointer_);
|
||||
return opaquePointer_;
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 <memory>
|
||||
|
||||
#include <react/attributedstring/AttributedString.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
/*
|
||||
* Represents an object storing a shared `AttributedString` or a shared pointer
|
||||
* to some opaque platform-specific object that can be used as an attributed
|
||||
* string. The class serves two main purposes:
|
||||
* - Represent type-erased attributed string entity (which can be
|
||||
* platform-specific or platform-independent);
|
||||
* - Represent a container that can be copied with constant complexity.
|
||||
*/
|
||||
class AttributedStringBox final {
|
||||
public:
|
||||
enum class Mode { Value, OpaquePointer };
|
||||
|
||||
/*
|
||||
* Default constructor constructs an empty string.
|
||||
*/
|
||||
AttributedStringBox();
|
||||
|
||||
/*
|
||||
* Custom explicit constructors.
|
||||
*/
|
||||
explicit AttributedStringBox(AttributedString const &value);
|
||||
explicit AttributedStringBox(std::shared_ptr<void> const &opaquePointer);
|
||||
|
||||
/*
|
||||
* Movable, Copyable, Assignable.
|
||||
*/
|
||||
AttributedStringBox(AttributedStringBox const &other) = default;
|
||||
AttributedStringBox(AttributedStringBox &&other) noexcept = default;
|
||||
AttributedStringBox &operator=(AttributedStringBox const &other) = default;
|
||||
AttributedStringBox &operator=(AttributedStringBox &&other) = default;
|
||||
|
||||
/*
|
||||
* Getters.
|
||||
*/
|
||||
Mode getMode() const;
|
||||
AttributedString const &getValue() const;
|
||||
std::shared_ptr<void> getOpaquePointer() const;
|
||||
|
||||
private:
|
||||
Mode mode_;
|
||||
std::shared_ptr<AttributedString const> value_;
|
||||
std::shared_ptr<void> opaquePointer_;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "ParagraphShadowNode.h"
|
||||
|
||||
#include <react/attributedstring/AttributedStringBox.h>
|
||||
#include "ParagraphState.h"
|
||||
|
||||
namespace facebook {
|
||||
@@ -62,7 +63,9 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
|
||||
}
|
||||
|
||||
return textLayoutManager_->measure(
|
||||
attributedString, getProps()->paragraphAttributes, layoutConstraints);
|
||||
AttributedStringBox{attributedString},
|
||||
getProps()->paragraphAttributes,
|
||||
layoutConstraints);
|
||||
}
|
||||
|
||||
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
|
||||
|
||||
+4
-1
@@ -8,6 +8,7 @@
|
||||
#include "AndroidTextInputShadowNode.h"
|
||||
|
||||
#include <fb/fbjni.h>
|
||||
#include <react/attributedstring/AttributedStringBox.h>
|
||||
#include <react/attributedstring/TextAttributes.h>
|
||||
#include <react/components/text/BaseTextShadowNode.h>
|
||||
#include <react/core/LayoutConstraints.h>
|
||||
@@ -111,7 +112,9 @@ Size AndroidTextInputShadowNode::measure(
|
||||
}
|
||||
|
||||
return textLayoutManager_->measure(
|
||||
attributedString, getProps()->paragraphAttributes, layoutConstraints);
|
||||
AttributedStringBox{attributedString},
|
||||
getProps()->paragraphAttributes,
|
||||
layoutConstraints);
|
||||
}
|
||||
|
||||
void AndroidTextInputShadowNode::layout(LayoutContext layoutContext) {
|
||||
|
||||
@@ -23,9 +23,11 @@ void *TextLayoutManager::getNativeTextLayoutManager() const {
|
||||
}
|
||||
|
||||
Size TextLayoutManager::measure(
|
||||
AttributedString attributedString,
|
||||
AttributedStringBox attributedStringBox,
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const {
|
||||
auto &attributedString = attributedStringBox.getValue();
|
||||
|
||||
return measureCache_.get(
|
||||
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints},
|
||||
[&](MeasureCacheKey const &key) {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include <react/attributedstring/AttributedString.h>
|
||||
#include <react/attributedstring/AttributedStringBox.h>
|
||||
#include <react/attributedstring/ParagraphAttributes.h>
|
||||
#include <react/core/LayoutConstraints.h>
|
||||
#include <react/utils/ContextContainer.h>
|
||||
@@ -35,7 +36,7 @@ class TextLayoutManager {
|
||||
* Measures `attributedString` using native text rendering infrastructure.
|
||||
*/
|
||||
Size measure(
|
||||
AttributedString attributedString,
|
||||
AttributedStringBox attributedStringBox,
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <react/attributedstring/AttributedString.h>
|
||||
#include <react/attributedstring/AttributedStringBox.h>
|
||||
#include <react/attributedstring/ParagraphAttributes.h>
|
||||
#include <react/core/LayoutConstraints.h>
|
||||
#include <react/utils/ContextContainer.h>
|
||||
@@ -34,7 +34,7 @@ class TextLayoutManager {
|
||||
* Measures `attributedString` using native text rendering infrastructure.
|
||||
*/
|
||||
Size measure(
|
||||
AttributedString attributedString,
|
||||
AttributedStringBox attributedStringBox,
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const;
|
||||
|
||||
|
||||
@@ -30,10 +30,12 @@ void *TextLayoutManager::getNativeTextLayoutManager() const
|
||||
}
|
||||
|
||||
Size TextLayoutManager::measure(
|
||||
AttributedString attributedString,
|
||||
AttributedStringBox attributedStringBox,
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const
|
||||
{
|
||||
auto &attributedString = attributedStringBox.getValue();
|
||||
|
||||
return measureCache_.get(
|
||||
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints}, [&](MeasureCacheKey const &key) {
|
||||
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
|
||||
|
||||
Reference in New Issue
Block a user