Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fabric] use state in paragraph component #24873

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#import "RCTParagraphComponentView.h"

#import <react/components/text/ParagraphComponentDescriptor.h>
#import <react/components/text/ParagraphLocalData.h>
#import <react/components/text/ParagraphState.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are not alphabetically sorted now.

If you can run clang format on that with config from the repo, that would be great. Otherwise I can do it for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shergin ran yarn clang-format (complains about a lot of files!) but weirdly, nothing to say about the ordering. manually fixed it.

#import <react/components/text/ParagraphProps.h>
#import <react/components/text/RawTextComponentDescriptor.h>
#import <react/components/text/TextComponentDescriptor.h>
Expand All @@ -21,7 +21,7 @@
using namespace facebook::react;

@implementation RCTParagraphComponentView {
SharedParagraphLocalData _paragraphLocalData;
ParagraphShadowNode::ConcreteState::Shared _state;
ParagraphAttributes _paragraphAttributes;
}

Expand Down Expand Up @@ -63,32 +63,32 @@ - (void)updateProps:(SharedProps)props oldProps:(SharedProps)oldProps
_paragraphAttributes = paragraphProps->paragraphAttributes;
}

- (void)updateLocalData:(SharedLocalData)localData oldLocalData:(SharedLocalData)oldLocalData
- (void)updateState:(State::Shared)state oldState:(State::Shared)oldState
{
_paragraphLocalData = std::static_pointer_cast<const ParagraphLocalData>(localData);
assert(_paragraphLocalData);
_state = std::static_pointer_cast<ParagraphShadowNode::ConcreteState const>(state);
ericlewis marked this conversation as resolved.
Show resolved Hide resolved
assert(_state);
[self setNeedsDisplay];
}

- (void)prepareForRecycle
{
[super prepareForRecycle];
_paragraphLocalData.reset();
_state.reset();
}

- (void)drawRect:(CGRect)rect
{
if (!_paragraphLocalData) {
if (!_state) {
return;
}

SharedTextLayoutManager textLayoutManager = _paragraphLocalData->getTextLayoutManager();
SharedTextLayoutManager textLayoutManager = _state->getData().getTextLayoutManager();
RCTTextLayoutManager *nativeTextLayoutManager =
(__bridge RCTTextLayoutManager *)textLayoutManager->getNativeTextLayoutManager();

CGRect frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());

[nativeTextLayoutManager drawAttributedString:_paragraphLocalData->getAttributedString()
[nativeTextLayoutManager drawAttributedString:_state->getData().getAttributedString()
paragraphAttributes:_paragraphAttributes
frame:frame];
}
Expand All @@ -102,26 +102,26 @@ - (NSString *)accessibilityLabel
return superAccessibilityLabel;
}

if (!_paragraphLocalData) {
if (!_state) {
return nil;
}

return RCTNSStringFromString(_paragraphLocalData->getAttributedString().getString());
return RCTNSStringFromString(_state->getData().getAttributedString().getString());
}

- (SharedTouchEventEmitter)touchEventEmitterAtPoint:(CGPoint)point
{
if (!_paragraphLocalData) {
if (!_state) {
return _eventEmitter;
}

SharedTextLayoutManager textLayoutManager = _paragraphLocalData->getTextLayoutManager();
SharedTextLayoutManager textLayoutManager = _state->getData().getTextLayoutManager();
RCTTextLayoutManager *nativeTextLayoutManager =
(__bridge RCTTextLayoutManager *)textLayoutManager->getNativeTextLayoutManager();
CGRect frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());

SharedEventEmitter eventEmitter =
[nativeTextLayoutManager getEventEmitterWithAttributeString:_paragraphLocalData->getAttributedString()
[nativeTextLayoutManager getEventEmitterWithAttributeString:_state->getData().getAttributedString()
paragraphAttributes:_paragraphAttributes
frame:frame
atPoint:point];
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

#include "ParagraphShadowNode.h"
#include "ParagraphLocalData.h"
#include "ParagraphState.h"
#include "ParagraphMeasurementCache.h"

namespace facebook {
Expand Down Expand Up @@ -38,21 +38,18 @@ void ParagraphShadowNode::setMeasureCache(
measureCache_ = cache;
}

void ParagraphShadowNode::updateLocalDataIfNeeded() {
void ParagraphShadowNode::updateStateIfNeeded() {
ensureUnsealed();

auto attributedString = getAttributedString();
auto currentLocalData =
std::static_pointer_cast<const ParagraphLocalData>(getLocalData());
if (currentLocalData &&
currentLocalData->getAttributedString() == attributedString) {
auto state = getStateData();
ericlewis marked this conversation as resolved.
Show resolved Hide resolved
if (state.getAttributedString() == attributedString) {
return;
}

auto localData = std::make_shared<ParagraphLocalData>();
localData->setAttributedString(std::move(attributedString));
localData->setTextLayoutManager(textLayoutManager_);
setLocalData(localData);
state.setAttributedString(std::move(attributedString));
ericlewis marked this conversation as resolved.
Show resolved Hide resolved
state.setTextLayoutManager(textLayoutManager_);
setStateData(std::move(state));
}

#pragma mark - LayoutableShadowNode
Expand Down Expand Up @@ -83,7 +80,7 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
}

void ParagraphShadowNode::layout(LayoutContext layoutContext) {
updateLocalDataIfNeeded();
updateStateIfNeeded();
ConcreteViewShadowNode::layout(layoutContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <folly/Optional.h>
#include <react/components/text/ParagraphMeasurementCache.h>
#include <react/components/text/ParagraphProps.h>
#include <react/components/text/ParagraphState.h>
#include <react/components/text/TextShadowNode.h>
#include <react/components/view/ConcreteViewShadowNode.h>
#include <react/core/ConcreteShadowNode.h>
Expand All @@ -32,7 +33,8 @@ using ParagraphEventEmitter = ViewEventEmitter;
class ParagraphShadowNode : public ConcreteViewShadowNode<
ParagraphComponentName,
ParagraphProps,
ParagraphEventEmitter>,
ParagraphEventEmitter,
ParagraphState>,
public BaseTextShadowNode {
public:
using ConcreteViewShadowNode::ConcreteViewShadowNode;
Expand All @@ -45,7 +47,7 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
/*
* Associates a shared TextLayoutManager with the node.
* `ParagraphShadowNode` uses the manager to measure text content
* and construct `ParagraphLocalData` objects.
* and construct `ParagraphState` objects.
*/
void setTextLayoutManager(SharedTextLayoutManager textLayoutManager);

Expand All @@ -65,10 +67,10 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<

private:
/*
* Creates a `LocalData` object (with `AttributedText` and
* Creates a `State` object (with `AttributedText` and
* `TextLayoutManager`) if needed.
*/
void updateLocalDataIfNeeded();
void updateStateIfNeeded();

SharedTextLayoutManager textLayoutManager_;
const ParagraphMeasurementCache *measureCache_;
Expand Down
43 changes: 43 additions & 0 deletions ReactCommon/fabric/components/text/paragraph/ParagraphState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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 "ParagraphState.h"

#include <react/components/text/conversions.h>
#include <react/debug/debugStringConvertibleUtils.h>

namespace facebook {
namespace react {

AttributedString ParagraphState::getAttributedString() const {
return attributedString_;
}

void ParagraphState::setAttributedString(const AttributedString &attributedString) const {
assert(!attributedStringIsInitialized_);
ericlewis marked this conversation as resolved.
Show resolved Hide resolved
attributedString_ = attributedString;
attributedStringIsInitialized_ = true;
}

SharedTextLayoutManager ParagraphState::getTextLayoutManager() const {
return textLayoutManager_;
}

void ParagraphState::setTextLayoutManager(const SharedTextLayoutManager &textLayoutManager) const {
textLayoutManager_ = textLayoutManager;
}

#ifdef ANDROID

folly::dynamic ParagraphState::getDynamic() const {
return toDynamic(*this);
}

#endif

} // namespace react
} // namespace facebook
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,39 @@
#pragma once

#include <react/attributedstring/AttributedString.h>
#include <react/core/LocalData.h>
#include <react/textlayoutmanager/TextLayoutManager.h>

namespace facebook {
namespace react {

class ParagraphLocalData;

using SharedParagraphLocalData = std::shared_ptr<const ParagraphLocalData>;


/*
* LocalData for <Paragraph> component.
* State for <Paragraph> component.
* Represents what to render and how to render.
*/
class ParagraphLocalData : public LocalData {
class ParagraphState final {
public:
/*
* All content of <Paragraph> component represented as an `AttributedString`.
*/
AttributedString getAttributedString() const;
void setAttributedString(AttributedString attributedString);
void setAttributedString(AttributedString const &attributedString) const;

/*
* `TextLayoutManager` provides a connection to platform-specific
* text rendering infrastructure which is capable to render the
* `AttributedString`.
*/
SharedTextLayoutManager getTextLayoutManager() const;
void setTextLayoutManager(SharedTextLayoutManager textLayoutManager);
void setTextLayoutManager(SharedTextLayoutManager const &textLayoutManager) const;

#ifdef ANDROID
folly::dynamic getDynamic() const override;
#endif

#pragma mark - DebugStringConvertible

#if RN_DEBUG_STRING_CONVERTIBLE
std::string getDebugName() const override;
SharedDebugStringConvertibleList getDebugProps() const override;
folly::dynamic getDynamic() const;
#endif

private:
AttributedString attributedString_;
SharedTextLayoutManager textLayoutManager_;
mutable AttributedString attributedString_;
ericlewis marked this conversation as resolved.
Show resolved Hide resolved
mutable SharedTextLayoutManager textLayoutManager_;
mutable bool attributedStringIsInitialized_;
};

} // namespace react
Expand Down
14 changes: 7 additions & 7 deletions ReactCommon/fabric/components/text/paragraph/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

#include <folly/dynamic.h>
#include <react/attributedstring/conversions.h>
#include <react/components/text/ParagraphLocalData.h>
#include <react/components/text/ParagraphState.h>

namespace facebook {
namespace react {

#ifdef ANDROID

inline folly::dynamic toDynamic(const ParagraphLocalData &paragraphLocalData) {
folly::dynamic newLocalData = folly::dynamic::object();
newLocalData["attributedString"] =
toDynamic(paragraphLocalData.getAttributedString());
newLocalData["hash"] = newLocalData["attributedString"]["hash"];
return newLocalData;
inline folly::dynamic toDynamic(const ParagraphState &paragraphState) {
folly::dynamic newState = folly::dynamic::object();
newState["attributedString"] =
toDynamic(paragraphState->getData().getAttributedString());
newState["hash"] = newState["attributedString"]["hash"];
return newState;
}

#endif
Expand Down