From 418c940a240aac5a637929e2fc9c9ad6758dedfd Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 22 Jul 2021 17:51:01 -0700 Subject: [PATCH] [web] Fix inability to type in text fields in iOS (flutter/engine#27563) --- .../src/engine/text_editing/text_editing.dart | 20 ++++++++++++---- .../lib/web_ui/test/text_editing_test.dart | 24 +++++-------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart index 00b93a74fd..f9f285e9c0 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart @@ -1133,13 +1133,23 @@ class IOSTextEditingStrategy extends GloballyPositionedTextEditingStrategy { _addTapListener(); - // On iOS, blur is trigerred if the virtual keyboard is closed or the - // browser is sent to background or the browser tab is changed. + // On iOS, blur is trigerred in the following cases: // - // Since in all these cases, the connection needs to be closed, - // [domRenderer.windowHasFocus] is not checked in [IOSTextEditingStrategy]. + // 1. The browser app is sent to the background (or the tab is changed). In + // this case, the window loses focus (see [domRenderer.windowHasFocus]), + // so we close the input connection with the framework. + // 2. The user taps on another focusable element. In this case, we refocus + // the input field and wait for the framework to manage the focus change. + // 3. The virtual keyboard is closed by tapping "done". We can't detect this + // programmatically, so we end up refocusing the input field. This is + // okay because the virtual keyboard will hide, and as soon as the user + // taps the text field again, the virtual keyboard will come up. subscriptions.add(activeDomElement.onBlur.listen((_) { - owner.sendTextConnectionClosedToFrameworkIfAny(); + if (domRenderer.windowHasFocus) { + activeDomElement.focus(); + } else { + owner.sendTextConnectionClosedToFrameworkIfAny(); + } })); } diff --git a/engine/src/flutter/lib/web_ui/test/text_editing_test.dart b/engine/src/flutter/lib/web_ui/test/text_editing_test.dart index 476fc298a5..95468b2d33 100644 --- a/engine/src/flutter/lib/web_ui/test/text_editing_test.dart +++ b/engine/src/flutter/lib/web_ui/test/text_editing_test.dart @@ -584,24 +584,12 @@ void testMain() { // DOM element is blurred. textEditing!.strategy.domElement!.blur(); - // For ios-safari the connection is closed. - if (browserEngine == BrowserEngine.webkit && - operatingSystem == OperatingSystem.iOs) { - expect(spy.messages, hasLength(1)); - expect(spy.messages[0].channel, 'flutter/textinput'); - expect( - spy.messages[0].methodName, 'TextInputClient.onConnectionClosed'); - await Future.delayed(Duration.zero); - // DOM element loses the focus. - expect(defaultTextEditingRoot.activeElement, null); - } else { - // No connection close message sent. - expect(spy.messages, hasLength(0)); - await Future.delayed(Duration.zero); - // DOM element still keeps the focus. - expect(defaultTextEditingRoot.activeElement, - textEditing!.strategy.domElement); - } + // No connection close message sent. + expect(spy.messages, hasLength(0)); + await Future.delayed(Duration.zero); + // DOM element still keeps the focus. + expect(defaultTextEditingRoot.activeElement, + textEditing!.strategy.domElement); }, // TODO(nurhan): https://github.com/flutter/flutter/issues/50769 skip: browserEngine == BrowserEngine.edge);