Skip to content

Commit

Permalink
Fix cursor moving while typing quickly and autocorrection triggered i…
Browse files Browse the repository at this point in the history
…n controlled single line TextInput on iOS (New Arch) (facebook#46970)

Summary:

Fixes facebook#44157

This one is a bit of a doozy...

During auto-correct in UITextField (used for single line TextInput) iOS will mutate the buffer in two parts, non-atomically. After the first part, after iOS triggers `textFieldDidChange`, selection is in the wrong position. If we set full new AttributedText at this point, we propagate the incorrect cursor position, and it is never restored.

In the common case, where we are not mutating text in the controlled component, we shouldn't need to be setting AttributedString in the first place, and we do have an equality comparison there currently. But it is defeated because attributes are not identical. There are a few sources of that:
1. NSParagraphStyle is present in backing input, but not the AttributedString we are setting. 
2. Backing text has an NSShadow with no color (does not render) not in the AttributedText
3. Event emitter attributes change on each update, and new text does not inherit the attributes.

The first two are part of the backing input `typingAttributes`, even if we set a dictionary without them. To solve for them, we make attribute comparison insensitive to the attribute values in a default initialized control. There is code around here fully falling back to attribute insensitive comparison, which we would ideally fix to instead role into this "effective" attribute comparison.

The event emitter attributes being misaligned is a real problem. We fix in a couple ways.
1. We treat the attribute values as equal if the backing event emitter is the same
2. We set paragraph level event emitter as a default attribute so the first typed character receives it

After these fixes, scenario in facebook/react-native-website#4247 no longer repros in new arch. Typing in debug build also subjectively seems faster? (we are not doing second invalidation of the control on every keypress).

Changes which do mutate content may be susceptible to the same style of issue, though on web/`react-dom` in Chrome, this seems to not try to preserve selection at all if the selection is uncontrolled, so this seems like less of an issue. 

I haven't yet looked at old arch, but my guess is we have similar issues there, and could be fixed in similar ways (though, we've been trying to avoid changing it as much as possible, and 0.76+ has new arch as default, so not sure if worth fixing in old impl as well if this is very long running issue).

Changelog:
[iOS][Fixed] - Fix cursor moving in iOS controlled single line TextInput on Autocorrection (New Arch)

Differential Revision: D64121570
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Oct 14, 2024
1 parent 0902b0a commit 86c90d9
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign, readonly) CGFloat zoomScale;
@property (nonatomic, assign, readonly) CGPoint contentOffset;
@property (nonatomic, assign, readonly) UIEdgeInsets contentInset;
@property (nullable, nonatomic, copy) NSDictionary<NSAttributedStringKey, id> *typingAttributes;

// This protocol disallows direct access to `selectedTextRange` property because
// unwise usage of it can break the `delegate` behavior. So, we always have to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import <react/renderer/components/iostextinput/TextInputComponentDescriptor.h>
#import <react/renderer/textlayoutmanager/RCTAttributedTextUtils.h>
#import <react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h>
#import <react/renderer/textlayoutmanager/TextLayoutManager.h>

#import <React/RCTBackedTextInputViewProtocol.h>
Expand Down Expand Up @@ -61,6 +62,13 @@ @implementation RCTTextInputComponentView {
*/
BOOL _comingFromJS;
BOOL _didMoveToWindow;

/*
* Newly initialized default typing attributes contain a no-op NSParagraphStyle and NSShadow. These cause inequality
* between the AttributedString backing the input and those generated from state. We store these attributes to make
* later comparison insensitive to them.
*/
NSDictionary<NSAttributedStringKey, id> *_originalTypingAttributes;
}

#pragma mark - UIView overrides
Expand All @@ -79,11 +87,26 @@ - (instancetype)initWithFrame:(CGRect)frame

[self addSubview:_backedTextInputView];
[self initializeReturnKeyType];

_originalTypingAttributes = [_backedTextInputView.typingAttributes copy];
}

return self;
}

- (void)updateEventEmitter:(const EventEmitter::Shared &)eventEmitter
{
[super updateEventEmitter:eventEmitter];

NSMutableDictionary<NSAttributedStringKey, id> *defaultAttributes = [_backedTextInputView.defaultTextAttributes mutableCopy];

RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new];
eventEmitterWrapper.eventEmitter = _eventEmitter;
defaultAttributes[RCTAttributedStringEventEmitterKey] = eventEmitterWrapper;

_backedTextInputView.defaultTextAttributes = defaultAttributes;
}

- (void)didMoveToWindow
{
[super didMoveToWindow];
Expand Down Expand Up @@ -236,8 +259,11 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
}

if (newTextInputProps.textAttributes != oldTextInputProps.textAttributes) {
_backedTextInputView.defaultTextAttributes =
NSMutableDictionary<NSAttributedStringKey, id> *defaultAttributes =
RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier()));
defaultAttributes[RCTAttributedStringEventEmitterKey] =
_backedTextInputView.defaultTextAttributes[RCTAttributedStringEventEmitterKey];
_backedTextInputView.defaultTextAttributes = defaultAttributes;
}

if (newTextInputProps.selectionColor != oldTextInputProps.selectionColor) {
Expand Down Expand Up @@ -732,9 +758,10 @@ - (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldTe
_backedTextInputView.markedTextRange || _backedTextInputView.isSecureTextEntry || fontHasBeenUpdatedBySystem;

if (shouldFallbackToBareTextComparison) {
return ([newText.string isEqualToString:oldText.string]);
return [newText.string isEqualToString:oldText.string];
} else {
return ([newText isEqualToAttributedString:oldText]);
return RCTIsAttributedStringEffectivelySame(
newText, oldText, _originalTypingAttributes, static_cast<const TextInputProps &>(*_props).textAttributes);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NSString *const RCTTextAttributesAccessibilityRoleAttributeName = @"Accessibilit
/*
* Creates `NSTextAttributes` from given `facebook::react::TextAttributes`
*/
NSDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttributes(
NSMutableDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttributes(
const facebook::react::TextAttributes &textAttributes);

/*
Expand All @@ -41,6 +41,17 @@ NSString *RCTNSStringFromStringApplyingTextTransform(NSString *string, facebook:

void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText);

/*
* Whether two `NSAttributedString` lead to the same underlying displayed text, even if they are not strictly equal.
* I.e. is one string substitutable for the other when backing a control (which may have some ignorable attributes
* provided).
*/
BOOL RCTIsAttributedStringEffectivelySame(
NSAttributedString *text1,
NSAttributedString *text2,
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes,
const facebook::react::TextAttributes &textAttributes);

@interface RCTWeakEventEmitterWrapper : NSObject
@property (nonatomic, assign) facebook::react::SharedEventEmitter eventEmitter;
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ - (void)dealloc
_weakEventEmitter.reset();
}

- (BOOL)isEqual:(id)object
{
// We consider the underlying EventEmitter as the identity
if (![object isKindOfClass:[self class]]) {
return NO;
}

auto thisEventEmitter = [self eventEmitter];
auto otherEventEmitter = [((RCTWeakEventEmitterWrapper *)object) eventEmitter];
return thisEventEmitter == otherEventEmitter;
}

- (NSUInteger)hash
{
// We consider the underlying EventEmitter as the identity
return (NSUInteger)_weakEventEmitter.lock().get();
}

@end

inline static UIFontWeight RCTUIFontWeightFromInteger(NSInteger fontWeight)
Expand Down Expand Up @@ -156,7 +174,8 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex

inline static UIColor *RCTEffectiveForegroundColorFromTextAttributes(const TextAttributes &textAttributes)
{
UIColor *effectiveForegroundColor = RCTUIColorFromSharedColor(textAttributes.foregroundColor) ?: [UIColor blackColor];
UIColor *effectiveForegroundColor =
RCTPlatformColorFromColor(*textAttributes.foregroundColor) ?: [UIColor blackColor];

if (!isnan(textAttributes.opacity)) {
effectiveForegroundColor = [effectiveForegroundColor
Expand All @@ -168,7 +187,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex

inline static UIColor *RCTEffectiveBackgroundColorFromTextAttributes(const TextAttributes &textAttributes)
{
UIColor *effectiveBackgroundColor = RCTUIColorFromSharedColor(textAttributes.backgroundColor);
UIColor *effectiveBackgroundColor = RCTPlatformColorFromColor(*textAttributes.backgroundColor);

if (effectiveBackgroundColor && !isnan(textAttributes.opacity)) {
effectiveBackgroundColor = [effectiveBackgroundColor
Expand All @@ -178,7 +197,8 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
return effectiveBackgroundColor ?: [UIColor clearColor];
}

NSDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttributes(const TextAttributes &textAttributes)
NSMutableDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttributes(
const TextAttributes &textAttributes)
{
NSMutableDictionary<NSAttributedStringKey, id> *attributes = [NSMutableDictionary dictionaryWithCapacity:10];

Expand Down Expand Up @@ -256,7 +276,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
NSUnderlineStyle style = RCTNSUnderlineStyleFromTextDecorationStyle(
textAttributes.textDecorationStyle.value_or(TextDecorationStyle::Solid));

UIColor *textDecorationColor = RCTUIColorFromSharedColor(textAttributes.textDecorationColor);
UIColor *textDecorationColor = RCTPlatformColorFromColor(*textAttributes.textDecorationColor);

// Underline
if (textDecorationLineType == TextDecorationLineType::Underline ||
Expand Down Expand Up @@ -285,7 +305,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
NSShadow *shadow = [NSShadow new];
shadow.shadowOffset = CGSize{textShadowOffset.width, textShadowOffset.height};
shadow.shadowBlurRadius = textAttributes.textShadowRadius;
shadow.shadowColor = RCTUIColorFromSharedColor(textAttributes.textShadowColor);
shadow.shadowColor = RCTPlatformColorFromColor(*textAttributes.textShadowColor);
attributes[NSShadowAttributeName] = shadow;
}

Expand All @@ -302,7 +322,7 @@ inline static CGFloat RCTEffectiveFontSizeMultiplierFromTextAttributes(const Tex
attributes[RCTTextAttributesAccessibilityRoleAttributeName] = [NSString stringWithUTF8String:roleStr.c_str()];
}

return [attributes copy];
return attributes;
}

void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
Expand Down Expand Up @@ -466,3 +486,147 @@ AttributedStringBox RCTAttributedStringBoxFromNSAttributedString(NSAttributedStr
return string;
}
}

static BOOL RCTIsParagraphStyleEffectivelySame(
NSParagraphStyle *style1,
NSParagraphStyle *style2,
const TextAttributes &baseTextAttributes)
{
if (style1 == nil || style2 == nil) {
return style1 == nil && style2 == nil;
}

// The NSParagraphStyle included as part of typingAttributes may eventually resolve "natural" directions to
// physical direction, so we should compare resolved directions
auto naturalAlignment =
baseTextAttributes.layoutDirection.value_or(LayoutDirection::LeftToRight) == LayoutDirection::LeftToRight
? NSTextAlignmentLeft
: NSTextAlignmentRight;

NSWritingDirection naturalBaseWritingDirection = baseTextAttributes.baseWritingDirection.has_value()
? RCTNSWritingDirectionFromWritingDirection(baseTextAttributes.baseWritingDirection.value())
: [NSParagraphStyle defaultWritingDirectionForLanguage:nil];

if (style1.alignment == NSTextAlignmentNatural || style1.baseWritingDirection == NSWritingDirectionNatural) {
NSMutableParagraphStyle *mutableStyle1 = [style1 mutableCopy];
style1 = mutableStyle1;

if (mutableStyle1.alignment == NSTextAlignmentNatural) {
mutableStyle1.alignment = naturalAlignment;
}

if (mutableStyle1.baseWritingDirection == NSWritingDirectionNatural) {
mutableStyle1.baseWritingDirection = naturalBaseWritingDirection;
}
}

if (style2.alignment == NSTextAlignmentNatural || style2.baseWritingDirection == NSWritingDirectionNatural) {
NSMutableParagraphStyle *mutableStyle2 = [style2 mutableCopy];
style2 = mutableStyle2;

if (mutableStyle2.alignment == NSTextAlignmentNatural) {
mutableStyle2.alignment = naturalAlignment;
}

if (mutableStyle2.baseWritingDirection == NSWritingDirectionNatural) {
mutableStyle2.baseWritingDirection = naturalBaseWritingDirection;
}
}

return [style1 isEqual:style2];
}

static BOOL RCTIsAttributeEffectivelySame(
NSAttributedStringKey attributeKey,
NSDictionary<NSAttributedStringKey, id> *attributes1,
NSDictionary<NSAttributedStringKey, id> *attributes2,
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes,
const TextAttributes &baseTextAttributes)
{
id attribute1 = attributes1[attributeKey] ?: insensitiveAttributes[attributeKey];
id attribute2 = attributes2[attributeKey] ?: insensitiveAttributes[attributeKey];

// Normalize attributes which can inexact but still effectively the same
if (attributeKey == NSParagraphStyleAttributeName) {
return RCTIsParagraphStyleEffectivelySame(attribute1, attribute2, baseTextAttributes);
}

// Otherwise rely on built-in comparison
return [attribute1 isEqual:attribute2];
}

BOOL RCTIsAttributedStringEffectivelySame(
NSAttributedString *text1,
NSAttributedString *text2,
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes,
const TextAttributes &baseTextAttributes)
{
if (![text1.string isEqualToString:text2.string]) {
return NO;
}

// We check that for every fragment in the old string
// 1. The new string's fragment overlapping the first spans the same characters
// 2. The attributes of each matching fragment are the same, ignoring those which match insensitive attibutes
__block BOOL areAttributesSame = YES;
[text1 enumerateAttributesInRange:NSMakeRange(0, text1.length)
options:0
usingBlock:^(
NSDictionary<NSAttributedStringKey, id> *text1Attributes,
NSRange text1Range,
BOOL *text1Stop) {
[text2 enumerateAttributesInRange:text1Range
options:0
usingBlock:^(
NSDictionary<NSAttributedStringKey, id> *text2Attributes,
NSRange text2Range,
BOOL *text2Stop) {
if (!NSEqualRanges(text1Range, text2Range)) {
areAttributesSame = NO;
*text1Stop = YES;
*text2Stop = YES;
return;
}

// Compare every attribute in text1 to the corresponding attribute
// in text2, or the set of insensitive attributes if not present
for (NSAttributedStringKey key in text1Attributes) {
if (!RCTIsAttributeEffectivelySame(
key,
text1Attributes,
text2Attributes,
insensitiveAttributes,
baseTextAttributes)) {
areAttributesSame = NO;
*text1Stop = YES;
*text2Stop = YES;
return;
}
}

for (NSAttributedStringKey key in text2Attributes) {
// We have already compared this attribute if it is present in
// both
if (text1Attributes[key] != nil) {
continue;
}

// But we still need to compare attributes if it is only present
// in text 2, to compare against insensitive attributes
if (!RCTIsAttributeEffectivelySame(
key,
text1Attributes,
text2Attributes,
insensitiveAttributes,
baseTextAttributes)) {
areAttributesSame = NO;
*text1Stop = YES;
*text2Stop = YES;
return;
}
}
}];
}];

return areAttributesSame;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#import <UIKit/UIKit.h>

#include <react/renderer/graphics/RCTPlatformColorUtils.h>
#include <react/renderer/textlayoutmanager/RCTFontProperties.h>
#include <react/renderer/textlayoutmanager/RCTFontUtils.h>
#import <react/renderer/graphics/RCTPlatformColorUtils.h>
#import <react/renderer/textlayoutmanager/RCTFontProperties.h>
#import <react/renderer/textlayoutmanager/RCTFontUtils.h>

inline static NSTextAlignment RCTNSTextAlignmentFromTextAlignment(facebook::react::TextAlignment textAlignment)
{
Expand Down Expand Up @@ -112,9 +112,3 @@ inline static NSUnderlineStyle RCTNSUnderlineStyleFromTextDecorationStyle(
return NSUnderlinePatternDot | NSUnderlineStyleSingle;
}
}

// TODO: this file has some duplicates method, we can remove it
inline static UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor)
{
return RCTPlatformColorFromColor(*sharedColor);
}

0 comments on commit 86c90d9

Please sign in to comment.