Skip to content

Commit

Permalink
Invalidate NSTextStorage on AttributedString change (facebook#38070)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#38070

changelog:
[iOS][fix]: Correctly invalidate NSTextStorage when non layout related props change

Fixes: facebook#37944

Problem:
NSTextStorage was not invalidated if non-layout props were changed. So for example 'color' dynamically changed, it wouldn't get invalidated and font of incorrect color would be rendered on screen.

Reviewed By: javache

Differential Revision: D47019250

fbshipit-source-id: ba33d542dad2eaa0c3effcf827ad03f37fb11cc0
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jun 26, 2023
1 parent eaafc26 commit f02cd90
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ bool ParagraphLayoutManager::shoudMeasureString(
AttributedString const &attributedString,
ParagraphAttributes const &paragraphAttributes,
LayoutConstraints layoutConstraints) const {
size_t newHash = folly::hash::hash_combine(
0,
textAttributedStringHashLayoutWise(attributedString),
paragraphAttributes);
size_t newParagraphInputHash =
folly::hash::hash_combine(0, attributedString, paragraphAttributes);

if (newHash != paragraphInputHash_) {
if (newParagraphInputHash != paragraphInputHash_) {
// AttributedString or ParagraphAttributes have changed.
// Must create new host text storage and trigger measure.
hostTextStorage_ = textLayoutManager_->getHostTextStorage(
attributedString, paragraphAttributes, layoutConstraints);
paragraphInputHash_ = newHash;
paragraphInputHash_ = newParagraphInputHash;

return true; // Must measure again.
}

// Detect the case when available width for Paragraph meaningfully changes.
// This is to prevent unnecessary re-creation of NSTextStorage on iOS.
// On Android, this is no-op.
bool hasMaximumSizeChanged =
layoutConstraints.maximumSize.width != lastAvailableWidth_;
Float threshold = 0.01f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <react/renderer/components/text/BaseTextShadowNode.h>
#include <react/renderer/components/text/ParagraphEventEmitter.h>
#include <react/renderer/components/text/ParagraphLayoutManager.h>
#include <react/renderer/components/text/ParagraphProps.h>
#include <react/renderer/components/text/ParagraphState.h>
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
Expand Down

0 comments on commit f02cd90

Please sign in to comment.