From fe50f6c942ec52d0c8d1dd5b1c7c34d9b2c96e64 Mon Sep 17 00:00:00 2001 From: Huan Lin Date: Mon, 31 Jul 2023 10:30:06 -0700 Subject: [PATCH 1/2] [ios][autocorrection]disable auto-correction highlight in iOS 17 --- .../Source/FlutterTextInputPlugin.mm | 19 +++++++++++++++---- .../Source/FlutterTextInputPluginTest.mm | 10 ++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index 9c16ec3070242..ddaa4475cc4d1 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -1632,10 +1632,21 @@ - (CGRect)firstRectForRange:(UITextRange*)range { if (_scribbleInteractionStatus == FlutterScribbleInteractionStatusNone && _scribbleFocusStatus == FlutterScribbleFocusStatusUnfocused) { - [self.textInputDelegate flutterTextInputView:self - showAutocorrectionPromptRectForStart:start - end:end - withClient:_textInputClient]; + if (@available(iOS 17.0, *)) { + // Disable auto-correction highlight feature for iOS 17. + // In iOS 17, whenever a character is inserted or deleted, the system will always query + // the rect for every single character of the current word. + // GitHub Issue: https://github.com/flutter/flutter/issues/128406 + } else { + // This tells the framework to show the highlight for incorrectly spelled word that is + // about to be auto-corrected. + // There is no other UITextInput API that informs about the auto-correction highlight. + // So we simply add the call here as a workaround. + [self.textInputDelegate flutterTextInputView:self + showAutocorrectionPromptRectForStart:start + end:end + withClient:_textInputClient]; + } } NSUInteger first = start; diff --git a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index 6fae2c6d76a8f..9ea51dcfb065e 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -314,6 +314,11 @@ - (void)testSettingKeyboardTypeNoneDisablesSystemKeyboard { } - (void)testAutocorrectionPromptRectAppears { + // Auto-correction prompt is disabled in iOS 17. + if (@available(iOS 17.0, *)) { + return; + } + FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView firstRectForRange:[FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)]]; @@ -353,6 +358,11 @@ - (void)testIgnoresSelectionChangeIfSelectionIsDisabled { } - (void)testAutocorrectionPromptRectDoesNotAppearDuringScribble { + // Auto-correction prompt is disabled in iOS 17. + if (@available(iOS 17.0, *)) { + return; + } + if (@available(iOS 14.0, *)) { FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; From 675610dfa69d25e439ef95541c2cf41e8759b029 Mon Sep 17 00:00:00 2001 From: Huan Lin Date: Tue, 1 Aug 2023 10:52:12 -0700 Subject: [PATCH 2/2] update tests --- .../Source/FlutterTextInputPlugin.mm | 4 +-- .../Source/FlutterTextInputPluginTest.mm | 26 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index ddaa4475cc4d1..a1ec6bdc85b04 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -1633,8 +1633,8 @@ - (CGRect)firstRectForRange:(UITextRange*)range { if (_scribbleInteractionStatus == FlutterScribbleInteractionStatusNone && _scribbleFocusStatus == FlutterScribbleFocusStatusUnfocused) { if (@available(iOS 17.0, *)) { - // Disable auto-correction highlight feature for iOS 17. - // In iOS 17, whenever a character is inserted or deleted, the system will always query + // Disable auto-correction highlight feature for iOS 17+. + // In iOS 17+, whenever a character is inserted or deleted, the system will always query // the rect for every single character of the current word. // GitHub Issue: https://github.com/flutter/flutter/issues/128406 } else { diff --git a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm index 9ea51dcfb065e..f39168fdeb87a 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterTextInputPluginTest.mm @@ -313,20 +313,22 @@ - (void)testSettingKeyboardTypeNoneDisablesSystemKeyboard { XCTAssertNil(textInputPlugin.activeView.inputViewController); } -- (void)testAutocorrectionPromptRectAppears { - // Auto-correction prompt is disabled in iOS 17. - if (@available(iOS 17.0, *)) { - return; - } - +- (void)testAutocorrectionPromptRectAppearsBeforeIOS17AndDoesNotAppearAfterIOS17 { FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin]; [inputView firstRectForRange:[FlutterTextRange rangeWithNSRange:NSMakeRange(0, 1)]]; - // Verify behavior. - OCMVerify([engine flutterTextInputView:inputView - showAutocorrectionPromptRectForStart:0 - end:1 - withClient:0]); + if (@available(iOS 17.0, *)) { + // Auto-correction prompt is disabled in iOS 17+. + OCMVerify(never(), [engine flutterTextInputView:inputView + showAutocorrectionPromptRectForStart:0 + end:1 + withClient:0]); + } else { + OCMVerify([engine flutterTextInputView:inputView + showAutocorrectionPromptRectForStart:0 + end:1 + withClient:0]); + } } - (void)testIgnoresSelectionChangeIfSelectionIsDisabled { @@ -358,7 +360,7 @@ - (void)testIgnoresSelectionChangeIfSelectionIsDisabled { } - (void)testAutocorrectionPromptRectDoesNotAppearDuringScribble { - // Auto-correction prompt is disabled in iOS 17. + // Auto-correction prompt is disabled in iOS 17+. if (@available(iOS 17.0, *)) { return; }