From f02cd90933b0b63b04df00d46ddc730debffa5db Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 26 Jun 2023 10:14:35 -0700 Subject: [PATCH] Invalidate NSTextStorage on AttributedString change (#38070) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38070 changelog: [iOS][fix]: Correctly invalidate NSTextStorage when non layout related props change Fixes: https://github.com/facebook/react-native/issues/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 --- .../components/text/ParagraphLayoutManager.cpp | 14 ++++++++------ .../renderer/components/text/ParagraphShadowNode.h | 1 - 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphLayoutManager.cpp b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphLayoutManager.cpp index 623b17ab8131f8..7dbd216892eb31 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphLayoutManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphLayoutManager.cpp @@ -42,20 +42,22 @@ bool ParagraphLayoutManager::shoudMeasureString( AttributedString const &attributedString, ParagraphAttributes const ¶graphAttributes, 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; diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h index 0936994b5da60f..b44e8f416dc675 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h @@ -9,7 +9,6 @@ #include #include -#include #include #include #include