Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -2497,6 +2497,7 @@ @interface FlutterTextInputPlugin ()
// The current password-autofillable input fields that have yet to be saved.
@property(nonatomic, readonly)
NSMutableDictionary<NSString*, FlutterTextInputView*>* autofillContext;
@property(nonatomic, readonly) BOOL pendingInputHiderRemoval;
@property(nonatomic, retain) FlutterTextInputView* activeView;
@property(nonatomic, retain) FlutterTextInputViewAccessibilityHider* inputHider;
@property(nonatomic, readonly, weak) id<FlutterViewResponder> viewResponder;
Expand All @@ -2511,6 +2512,7 @@ @interface FlutterTextInputPlugin ()

@implementation FlutterTextInputPlugin {
NSTimer* _enableFlutterTextInputViewAccessibilityTimer;
BOOL _pendingInputHiderRemoval;
}

- (instancetype)initWithDelegate:(id<FlutterTextInputDelegate>)textInputDelegate {
Expand Down Expand Up @@ -2539,7 +2541,7 @@ - (void)handleKeyboardWillShow:(NSNotification*)notification {
}

- (void)dealloc {
[self hideTextInput];
[self reset];
}

- (void)removeEnableFlutterTextInputViewAccessibilityTimer {
Expand All @@ -2554,6 +2556,8 @@ - (void)removeEnableFlutterTextInputViewAccessibilityTimer {
}

- (void)reset {
[_autofillContext removeAllObjects];
[self clearTextInputClient];
[self hideTextInput];
}

Expand Down Expand Up @@ -2818,22 +2822,6 @@ - (void)startLiveTextInput {
- (void)showTextInput {
_activeView.viewResponder = _viewResponder;
[self addToInputParentViewIfNeeded:_activeView];
// Adds a delay to prevent the text view from receiving accessibility
// focus in case it is activated during semantics updates.
//
// One common case is when the app navigates to a page with an auto
// focused text field. The text field will activate the FlutterTextInputView
// with a semantics update sent to the engine. The voiceover will focus
// the newly attached active view while performing accessibility update.
// This results in accessibility focus stuck at the FlutterTextInputView.
if (!_enableFlutterTextInputViewAccessibilityTimer) {
_enableFlutterTextInputViewAccessibilityTimer =
[NSTimer scheduledTimerWithTimeInterval:kUITextInputAccessibilityEnablingDelaySeconds
target:[FlutterTimerProxy proxyWithTarget:self]
selector:@selector(enableActiveViewAccessibility)
userInfo:nil
repeats:NO];
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm hoping we can just remove these (at least the timer).

Per @chunhtai (if I recall correctly) this is for cases like navigating to a new route with an "autofocus: true" text field. But I think when that happens, the accessibility bridge would have already received the new semantics tree, especially with the current implementation where autofocus is scheduled in a post frame callback and semantics updates are sent synchronously to the accessibility bridge on iOS.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@chunhtai do you think this can be removed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, if we're worried about the timing with the semantics tree update, shouldn't accessibility bridge be the one that calls enableActiveViewAccessbility when it's ready, instead of the text input plugin tries to coordinate with the accessibility bridge?

@chunhtai chunhtai Sep 15, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current implementation was five years ago bf551a3.

Where this changed was introduced 4 years ago d2dacb3

I am not sure if this is still needed either though.

if we're worried about the timing with the semantics tree update, shouldn't accessibility bridge be the one that calls enableActiveViewAccessbility.

This is only an issue due to iOS internal implementation that it reuses UItextinput for text editing and a11y tree. From the framework perspective, there is nothing wrong that autofocus the textfield and then sends a11y tree update. The keyboard focus and a11y focus are separated concepts in the first place.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is removing this delay necessary for autofill to work?

@LongCatIsLooong LongCatIsLooong Sep 15, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is removing this delay necessary for autofill to work?

I would think not (it's more of a drive-by comment).

From the framework perspective, there is nothing wrong that autofocus the textfield and then sends a11y tree update. The keyboard focus and a11y focus are separated concepts in the first place.

Yeah the framework doesn't have to know about the details of what UIKit / the text input plugin does, but the iOS accessibility bridge is specialized for the iOS implementation, and to me it seems to be the perfect place to determining when to switch between an a11y node and the "real" input field (instead of using a timer in TextInputPlugin). But again I digress.

IIRC the PR moves the switch code from show/hide to setInputClient/clearInputClient, so the timing is a bit different and the app may call show/hide themselves to dismiss / reveal the virtual keyboard. Do you happen to know if that will break anything, or are there things we should test to make sure there's no regression.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the iOS accessibility bridge is specialized for the iOS implementation, and to me it seems to be the perfect place to determining when to switch between an a11y node and the "real" input field (instead of using a timer in TextInputPlugin). But again I digress.

Oh I misread the original question, yeah moving this to ios a11ybridge is fine. It just feels a bit weird that a11y needs to listen to textinputplugin activation and then set the attribute on textinputplugin, but I get it makes a bit more sense to structure the code this way

PR moves the switch code from show/hide to setInputClient/clearInputClient, so the timing is a bit different and the app may call show/hide themselves to dismiss / reveal the virtual keyboard. Do you happen to know if that will break anything, or are there things we should test to make sure there's no regression.

I think this is probably fine. to do a manual test just create a push a new route with autofocus textfield in voice over and see if you will see weird focus behavior

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds good I'll give that a try.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(trying the implementation out now).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tried out the patch and the behavior seems to be exactly the same in my test app:

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: TextButton(
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(builder: (context) {
              return Scaffold(
                //appBar: AppBar(title: const Text('My Page')),
                body: Column(
                  children: [
                    TextButton(
                      child: const Text('POP'),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                    TextField(
                      autofocus: true,
                    )
                  ],
                ),
              );
            }));
          },
          child: Text('123')),
    ));
  }
}

The only weirdness I observed: when I press the "123" button and wait for the route transition to finish, the accessibility focus indicator (the rectangle with black borders) would sometimes show over an empty area with no interactive UI elements and announce "one hundred and twenty three". I'm seeing that with and without this patch so I guess that's not related.

[_activeView becomeFirstResponder];
}

Expand All @@ -2845,11 +2833,7 @@ - (void)enableActiveViewAccessibility {
}

- (void)hideTextInput {
[self removeEnableFlutterTextInputViewAccessibilityTimer];
_activeView.accessibilityEnabled = NO;
[_activeView resignFirstResponder];
[_activeView removeFromSuperview];
[_inputHider removeFromSuperview];
}

- (void)triggerAutofillSave:(BOOL)saveEntries {
Expand All @@ -2866,6 +2850,14 @@ - (void)triggerAutofillSave:(BOOL)saveEntries {
}

[self cleanUpViewHierarchy:YES clearText:!saveEntries delayRemoval:NO];

// Trigger removal of input hider if needed.
if (_pendingInputHiderRemoval) {
[_activeView removeFromSuperview];
[_inputHider removeFromSuperview];
_pendingInputHiderRemoval = NO;
}

[self addToInputParentViewIfNeeded:_activeView];
}

Expand Down Expand Up @@ -2917,6 +2909,23 @@ - (void)setTextInputClient:(int)client withConfiguration:(NSDictionary*)configur
// text fields immediately (which seems to make the keyboard flicker).
// See: https://github.com/flutter/flutter/issues/64628.
[self cleanUpViewHierarchy:NO clearText:YES delayRemoval:YES];

// Adds a delay to prevent the text view from receiving accessibility
// focus in case it is activated during semantics updates.
//
// One common case is when the app navigates to a page with an auto
// focused text field. The text field will activate the FlutterTextInputView
// with a semantics update sent to the engine. The voiceover will focus
// the newly attached active view while performing accessibility update.
// This results in accessibility focus stuck at the FlutterTextInputView.
if (!_enableFlutterTextInputViewAccessibilityTimer) {
_enableFlutterTextInputViewAccessibilityTimer =
[NSTimer scheduledTimerWithTimeInterval:kUITextInputAccessibilityEnablingDelaySeconds
target:[FlutterTimerProxy proxyWithTarget:self]
selector:@selector(enableActiveViewAccessibility)
userInfo:nil
repeats:NO];
}
}

// Creates and shows an input field that is not password related and has no autofill
Expand Down Expand Up @@ -3099,6 +3108,17 @@ - (void)setTextInputEditingState:(NSDictionary*)state {
- (void)clearTextInputClient {
[_activeView setTextInputClient:0];
_activeView.frame = CGRectZero;

[self removeEnableFlutterTextInputViewAccessibilityTimer];
_activeView.accessibilityEnabled = NO;

if (_autofillContext.count == 0) {
[_activeView removeFromSuperview];
[_inputHider removeFromSuperview];
} else {
// If _autofillContext is not empty, triggerAutofillSave will be called to clean up the views.
_pendingInputHiderRemoval = YES;
}
}

- (void)updateConfig:(NSDictionary*)dictionary {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ @interface FlutterTextInputPlugin ()
@property(nonatomic, readonly) UIView* keyboardView;
@property(nonatomic, assign) UIView* cachedFirstResponder;
@property(nonatomic, readonly) CGRect keyboardRect;
@property(nonatomic, readonly) BOOL pendingInputHiderRemoval;
@property(nonatomic, readonly)
NSMutableDictionary<NSString*, FlutterTextInputView*>* autofillContext;

Expand Down Expand Up @@ -172,6 +173,14 @@ - (void)setClientId:(int)clientId configuration:(NSDictionary*)config {
}];
}

- (void)setClientClear {
FlutterMethodCall* clearClientCall =
[FlutterMethodCall methodCallWithMethodName:@"TextInput.clearClient" arguments:@[]];
[textInputPlugin handleMethodCall:clearClientCall
result:^(id _Nullable result){
}];
}

- (void)setTextInputShow {
FlutterMethodCall* setClientCall = [FlutterMethodCall methodCallWithMethodName:@"TextInput.show"
arguments:@[]];
Expand Down Expand Up @@ -974,6 +983,21 @@ - (void)testReplaceTestLocalAdjustSelectionAndMarkedTextRange {
XCTAssertEqual(inputView.markedTextRange, nil);
}

- (void)testFlutterTextInputViewIsNotClearWhenKeyboardShowAndHide {
// Regression test for https://github.com/flutter/flutter/issues/172250.
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
[inputView setMarkedText:@"test text" selectedRange:NSMakeRange(0, 5)];
XCTAssertEqualObjects(inputView.text, @"test text");

// Showing keyboard does not trigger clearing of marked text.
[self setTextInputShow];
XCTAssertEqualObjects(inputView.text, @"test text");

// Hiding keyboard does not trigger clearing of marked text.
[self setTextInputHide];
XCTAssertEqualObjects(inputView.text, @"test text");
}

- (void)testFlutterTextInputViewOnlyRespondsToInsertionPointColorBelowIOS17 {
FlutterTextInputView* inputView = [[FlutterTextInputView alloc] initWithOwner:textInputPlugin];
// [UITextInputTraits insertionPointColor] is non-public API, so @selector(insertionPointColor)
Expand Down Expand Up @@ -2676,6 +2700,42 @@ - (void)testAutofillInputViews {
withTag:@"field2"]);
}

- (void)testAutofillContextPersistsAfterClearClient {
NSMutableDictionary* field1 = self.mutableTemplateCopy;
[field1 setValue:@{
@"uniqueIdentifier" : @"field1",
@"hints" : @[ @"username" ],
@"editingValue" : @{@"text" : @""}
}
forKey:@"autofill"];

NSMutableDictionary* field2 = self.mutablePasswordTemplateCopy;
[field2 setValue:@{
@"uniqueIdentifier" : @"field2",
@"hints" : @[ @"password" ],
@"editingValue" : @{@"text" : @""}
}
forKey:@"autofill"];

NSMutableDictionary* config = [field1 mutableCopy];
[config setValue:@[ field1, field2 ] forKey:@"fields"];

// Verify initial state.
[self setClientId:123 configuration:config];
XCTAssertEqual(textInputPlugin.autofillContext.count, 2ul);
XCTAssertFalse(textInputPlugin.pendingInputHiderRemoval);

// Retain autofill context.
[self setClientClear];
XCTAssertEqual(textInputPlugin.autofillContext.count, 2ul);
XCTAssertTrue(textInputPlugin.pendingInputHiderRemoval);

// Consume autofill context.
[self commitAutofillContextAndVerify];
XCTAssertEqual(textInputPlugin.autofillContext.count, 0ul);
XCTAssertFalse(textInputPlugin.pendingInputHiderRemoval);
}

- (void)testPasswordAutofillHack {
NSDictionary* config = self.mutableTemplateCopy;
[config setValue:@"YES" forKey:@"obscureText"];
Expand Down Expand Up @@ -2812,24 +2872,58 @@ - (void)testInitialActiveViewCantAccessTextInputDelegate {
XCTAssertNil(textInputPlugin.activeView.textInputDelegate);
}

- (void)testAutoFillDoesNotTriggerOnShowAndHideKeyboard {
// Regression test for https://github.com/flutter/flutter/issues/145681.
NSMutableDictionary* configuration = self.mutableTemplateCopy;
[configuration setValue:@{
@"uniqueIdentifier" : @"field1",
@"hints" : @[ UITextContentTypePassword ],
@"editingValue" : @{@"text" : @""}
}
forKey:@"autofill"];
[configuration setValue:@[ [configuration copy] ] forKey:@"fields"];
[self setClientId:123 configuration:configuration];

[self setTextInputShow];
XCTAssertEqual(self.viewsVisibleToAutofill.count, 1ul);

// Hiding keyboard does not trigger showing autofill prompt.
[self setTextInputHide];
XCTAssertEqual(self.viewsVisibleToAutofill.count, 1ul);

[self commitAutofillContextAndVerify];
}

#pragma mark - Accessibility - Tests

- (void)testUITextInputAccessibilityNotHiddenWhenShowed {
- (void)testUITextInputAccessibilityNotHiddenWhenKeyboardIsShownAndHidden {
[self setClientId:123 configuration:self.mutableTemplateCopy];

// Send show text input method call.
[self setTextInputShow];
// Find all the FlutterTextInputViews we created.
NSArray<FlutterTextInputView*>* inputFields = self.installedInputViews;

// The input view should not be hidden.
XCTAssertEqual([inputFields count], 1u);

// Send show text input method call.
[self setTextInputShow];

inputFields = self.installedInputViews;

XCTAssertEqual([inputFields count], 1u);

// Send hide text input method call.
[self setTextInputHide];

inputFields = self.installedInputViews;

XCTAssertEqual([inputFields count], 1u);

// Send clear text client method call.
[self setClientClear];

inputFields = self.installedInputViews;

// The input view should be hidden.
XCTAssertEqual([inputFields count], 0u);
}
Expand Down