Skip to content

Commit 90cf7be

Browse files
Revert: [web] Keep the keyboard up during an iOS caret drag (flutter#190926)
Reverts: [[web] Keep the keyboard up during an iOS caret drag](flutter#190014) Initiated by: @jtmcdole Reason for reverting: Original PR Author: @flutter-zl Reviewed By: @Renzo-Olivares The original PR description is provided below: Fixes flutter#189744 **Problem** On iOS 27, long-press-dragging the selection caret in a Web TextField dismisses the keyboard mid-gesture. WebKit transiently blurs the hidden input with a null relatedTarget while the document keeps focus, then refocuses it a frame later,and the engine reacted to that blink on two listeners that each tore the input down. A plain input ignores the same blur, so the cause is Flutter's reaction, not a WebKit limitation. **Fix** On iOS both listeners now defer their teardown by 100ms and cancel it if the input refocuses, mirroring the existing flutter#155265 deferred close. Done and tap-away never refocus so they still close, and the deferral is narrowed to the exact drag signature so every other focus transition is unaffected. **Demo** Before: https://flutter-demo-52-before.web.app (keyboard dismisses mid caret drag) After: https://flutter-demo-52-after.web.app (keyboard stays up) Repro on iOS 27 Safari: tap the field to raise the keyboard, then long-press and drag the selection caret. Before dismisses; after stays up. Done and tap-away still dismiss.
1 parent b05fabd commit 90cf7be

4 files changed

Lines changed: 2 additions & 396 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher/view_focus_binding.dart‎

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import 'dart:js_interop';
88
import 'package:ui/src/engine.dart';
99
import 'package:ui/ui.dart' as ui;
1010

11-
/// Overrides `domDocument.hasFocus()` in [ViewFocusBinding] for tests, so the
12-
/// iOS caret-drag deferral tests do not depend on the headless browser
13-
/// reporting the test document as focused. Mirrors
14-
/// `DefaultTextEditingStrategy.debugDocumentHasFocusOverride`.
15-
bool? debugViewFocusDocumentHasFocusOverride;
16-
1711
/// Tracks the [FlutterView]s focus changes.
1812
final class ViewFocusBinding {
1913
ViewFocusBinding(this._viewManager, this._onViewFocusChange);
@@ -26,21 +20,6 @@ final class ViewFocusBinding {
2620

2721
StreamSubscription<int>? _onViewCreatedListener;
2822

29-
/// A deferred report of a `focusout` that named no element to gain focus.
30-
///
31-
/// A native iOS caret/selection drag transiently blurs the focused input to
32-
/// <body> and WebKit refocuses it a frame later. Deferring the report lets
33-
/// that refocus cancel it, so the view is not briefly reported unfocused.
34-
/// See: https://github.com/flutter/flutter/issues/189744
35-
Timer? _pendingFocusoutTimer;
36-
37-
/// Whether the document itself still has focus.
38-
///
39-
/// Reads [debugViewFocusDocumentHasFocusOverride] when set, so tests do not
40-
/// depend on the headless browser reporting the test document as focused.
41-
/// Mirrors `DefaultTextEditingStrategy._documentHasFocus`.
42-
bool get _documentHasFocus => debugViewFocusDocumentHasFocusOverride ?? domDocument.hasFocus();
43-
4423
void init() {
4524
// We need a global listener here to know if the user was pressing "shift"
4625
// when the Flutter view receives focus, to move the Flutter focus to the
@@ -57,7 +36,6 @@ final class ViewFocusBinding {
5736
domDocument.body?.removeEventListener(_keyDown, _handleKeyDown);
5837
domDocument.body?.removeEventListener(_keyUp, _handleKeyUp);
5938
_onViewCreatedListener?.cancel();
60-
_pendingFocusoutTimer?.cancel();
6139
}
6240

6341
void changeViewFocus(int viewId, ui.ViewFocusState state) {
@@ -76,9 +54,6 @@ final class ViewFocusBinding {
7654

7755
late final DomEventListener _handleFocusin = createDomEventListener((DomEvent event) {
7856
event as DomFocusEvent;
79-
// Focus returned, so a deferred `focusout` was a transient blur; drop it.
80-
_pendingFocusoutTimer?.cancel();
81-
_pendingFocusoutTimer = null;
8257
_handleFocusChange(event.target as DomElement?);
8358
});
8459

@@ -95,31 +70,7 @@ final class ViewFocusBinding {
9570
}
9671

9772
event as DomFocusEvent;
98-
final willGainFocus = event.relatedTarget as DomElement?;
99-
final target = event.target as DomElement?;
100-
101-
// On iOS, a native caret/selection drag transiently blurs Flutter's active
102-
// text-editing element to <body> (relatedTarget == null) while the document
103-
// still has focus, and WebKit refocuses it a frame later. Reporting the view
104-
// unfocused on that blink tears the text connection down and drops the
105-
// keyboard. Defer only that precise case, re-deriving from the live focus so
106-
// an immediate refocus is a no-op ([_handleFocusin] cancels the timer).
107-
// Anything else, a non-editing element, a genuine focus loss, or the page
108-
// itself losing focus, reports immediately.
109-
// https://github.com/flutter/flutter/issues/189744
110-
if (isIosSafari &&
111-
willGainFocus == null &&
112-
_documentHasFocus &&
113-
textEditing.isActiveTextEditingElement(target)) {
114-
_pendingFocusoutTimer?.cancel();
115-
_pendingFocusoutTimer = Timer(kTransientBlurSettleDelay, () {
116-
_pendingFocusoutTimer = null;
117-
_handleFocusChange(domDocument.activeElement);
118-
});
119-
return;
120-
}
121-
122-
_handleFocusChange(willGainFocus);
73+
_handleFocusChange(event.relatedTarget as DomElement?);
12374
});
12475

12576
late final DomEventListener _handleKeyDown = createDomEventListener((DomEvent event) {

‎engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart‎

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'package:meta/meta.dart';
1212
import 'package:ui/ui.dart' as ui;
1313
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
1414

15-
import '../browser_detection.dart' show isIosSafari;
1615
import '../configuration.dart';
1716
import '../dom.dart';
1817
import '../mouse/prevent_default.dart';
@@ -51,18 +50,6 @@ bool browserHasAutofillOverlay() =>
5150
/// transparent.
5251
const String transparentTextEditingClass = 'transparentTextEditing';
5352

54-
/// How long to wait before treating a blur that named no incoming element as a
55-
/// real focus loss.
56-
///
57-
/// Several browser behaviors blur transiently and restore focus a moment later:
58-
/// backgrounding a tab fires blur before `visibilitychange`, and on iOS a
59-
/// native caret or selection drag blurs the input mid-gesture before WebKit
60-
/// refocuses it. Waiting this long lets those settle before the engine acts.
61-
///
62-
/// Shared by [DefaultTextEditingStrategy.handleBlur] and [ViewFocusBinding],
63-
/// which defer the same gesture and must not disagree about the window.
64-
const Duration kTransientBlurSettleDelay = Duration(milliseconds: 100);
65-
6653
void _emptyCallback(dynamic _) {}
6754

6855
/// These style attributes are constant throughout the life time of an input
@@ -1875,7 +1862,7 @@ abstract class DefaultTextEditingStrategy
18751862
// When a browser tab is backgrounded, the input blur arrives before
18761863
// visibilitychange. Wait briefly so tab switches can keep the text
18771864
// connection alive, while ordinary window/iframe blurs still close it.
1878-
_pendingBlurConnectionCloseTimer = Timer(kTransientBlurSettleDelay, () {
1865+
_pendingBlurConnectionCloseTimer = Timer(const Duration(milliseconds: 100), () {
18791866
_pendingBlurConnectionCloseTimer = null;
18801867
if (_documentVisibilityState == 'hidden' || _documentHasFocus) {
18811868
return;
@@ -1884,32 +1871,6 @@ abstract class DefaultTextEditingStrategy
18841871
});
18851872
return;
18861873
}
1887-
// On iOS WebKit, a native caret or selection drag transiently blurs the
1888-
// hidden input mid-gesture with `relatedTarget == null` while the document
1889-
// still has focus, and WebKit refocuses the input a frame later. Closing
1890-
// the connection on that blink drops the keyboard; a plain <input> keeps
1891-
// it. Defer the close and skip it if the input has regained focus by the
1892-
// time the timer fires. A genuine blur, the Done button or tapping away,
1893-
// does not refocus, so it still closes. [ViewFocusBinding] defers the
1894-
// matching `focusout` the same way.
1895-
// https://github.com/flutter/flutter/issues/189744
1896-
if (isIosSafari) {
1897-
_pendingBlurConnectionCloseTimer?.cancel();
1898-
_pendingBlurConnectionCloseTimer = Timer(kTransientBlurSettleDelay, () {
1899-
_pendingBlurConnectionCloseTimer = null;
1900-
if (domDocument.activeElement == activeDomElement) {
1901-
// The input refocused: this was the transient mid-gesture blur.
1902-
return;
1903-
}
1904-
if (_documentVisibilityState == 'hidden') {
1905-
// The page was backgrounded (e.g. a tab switch) after the blur was
1906-
// scheduled; keep the connection alive, matching the branch above.
1907-
return;
1908-
}
1909-
textEditing.sendTextConnectionClosedToFrameworkIfAny();
1910-
});
1911-
return;
1912-
}
19131874
textEditing.sendTextConnectionClosedToFrameworkIfAny();
19141875
} else if (_viewForElement(willGainFocusElement) == activeDomElementView) {
19151876
// If the focus stays within the same FlutterView, ensure the focus stays
@@ -2793,16 +2754,6 @@ class HybridTextEditing {
27932754
/// Also used to define if a keyboard is needed.
27942755
bool isEditing = false;
27952756

2796-
/// Whether [element] is the DOM element currently receiving text input.
2797-
///
2798-
/// [ViewFocusBinding] uses this to recognize a `focusout` that originated
2799-
/// from the active text-editing element.
2800-
///
2801-
/// Prefer this over matching on [textEditingClass]. That class is
2802-
/// not guaranteed to be applied by all strategies.
2803-
bool isActiveTextEditingElement(DomElement? element) =>
2804-
isEditing && element != null && element == strategy.domElement;
2805-
28062757
InputConfiguration? configuration;
28072758

28082759
DefaultTextEditingStrategy? debugTextEditingStrategyOverride;

‎engine/src/flutter/lib/web_ui/test/engine/platform_dispatcher/view_focus_binding_test.dart‎

Lines changed: 0 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ void testMain() {
2424

2525
tearDown(() {
2626
EngineSemantics.instance.semanticsEnabled = false;
27-
endFakeTextEditing();
2827
});
2928

3029
test('The view is focusable and reachable by keyboard when registered', () async {
@@ -269,177 +268,9 @@ void testMain() {
269268
expect(dispatchedViewFocusEvents[0].state, ui.ViewFocusState.focused);
270269
expect(dispatchedViewFocusEvents[0].direction, ui.ViewFocusDirection.forward);
271270
});
272-
273-
// On iOS a native caret/selection drag transiently blurs Flutter's active
274-
// text-editing element to <body> (relatedTarget == null) while the document
275-
// still has focus, and WebKit refocuses it a frame later. The view-unfocused
276-
// report is deferred so that refocus cancels it.
277-
// Regression test for https://github.com/flutter/flutter/issues/189744
278-
test('drops the deferred view-unfocused report when the editing input '
279-
'refocuses on iOS', () async {
280-
final EngineFlutterView view = createAndRegisterView(dispatcher);
281-
final DomHTMLInputElement input = createDomHTMLInputElement();
282-
view.dom.rootElement.append(input);
283-
input.focusWithoutScroll();
284-
beginFakeTextEditing(input);
285-
dispatchedViewFocusEvents.clear();
286-
287-
debugEmulateIosSafari = true;
288-
debugViewFocusDocumentHasFocusOverride = true;
289-
try {
290-
// The null-relatedTarget focusout schedules the deferred report; the
291-
// immediate refocus, as WebKit does mid-drag, cancels it.
292-
input.blur();
293-
input.focusWithoutScroll();
294-
await Future<void>.delayed(const Duration(milliseconds: 150));
295-
expect(dispatchedViewFocusEvents, isEmpty);
296-
} finally {
297-
debugEmulateIosSafari = false;
298-
debugViewFocusDocumentHasFocusOverride = null;
299-
}
300-
});
301-
302-
// A genuine blur (Done button, tap-away) never refocuses, so the deferred
303-
// report must still fire, carrying the right view and direction.
304-
test('reports the view unfocused on iOS when the editing input does not '
305-
'refocus', () async {
306-
final EngineFlutterView view = createAndRegisterView(dispatcher);
307-
final DomHTMLInputElement input = createDomHTMLInputElement();
308-
view.dom.rootElement.append(input);
309-
input.focusWithoutScroll();
310-
beginFakeTextEditing(input);
311-
dispatchedViewFocusEvents.clear();
312-
313-
debugEmulateIosSafari = true;
314-
debugViewFocusDocumentHasFocusOverride = true;
315-
try {
316-
input.blur();
317-
await Future<void>.delayed(const Duration(milliseconds: 150));
318-
final Iterable<ui.ViewFocusEvent> unfocused = dispatchedViewFocusEvents.where(
319-
(ui.ViewFocusEvent e) => e.state == ui.ViewFocusState.unfocused,
320-
);
321-
expect(unfocused, hasLength(1));
322-
expect(unfocused.single.viewId, view.viewId);
323-
expect(unfocused.single.direction, ui.ViewFocusDirection.undefined);
324-
} finally {
325-
debugEmulateIosSafari = false;
326-
debugViewFocusDocumentHasFocusOverride = null;
327-
}
328-
});
329-
330-
// The deferral is scoped to Flutter's text-editing element. A null-target
331-
// focusout from any other element must report immediately, so a later
332-
// refocus cannot erase a real focus loss.
333-
test('reports immediately for a non-text-editing element on iOS', () {
334-
final EngineFlutterView view = createAndRegisterView(dispatcher);
335-
final DomElement other = createDomElement('input');
336-
view.dom.rootElement.append(other);
337-
other.focusWithoutScroll();
338-
dispatchedViewFocusEvents.clear();
339-
340-
debugEmulateIosSafari = true;
341-
// Report the document as focused so the only condition failing is that
342-
// `other` is not the active editing element. Without this the test could
343-
// pass because the headless browser reported the document unfocused,
344-
// which is a different branch than the one under test.
345-
debugViewFocusDocumentHasFocusOverride = true;
346-
try {
347-
other.blur();
348-
// Not deferred: the unfocused event is present synchronously.
349-
expect(
350-
dispatchedViewFocusEvents.where(
351-
(ui.ViewFocusEvent e) => e.state == ui.ViewFocusState.unfocused,
352-
),
353-
hasLength(1),
354-
);
355-
} finally {
356-
debugEmulateIosSafari = false;
357-
debugViewFocusDocumentHasFocusOverride = null;
358-
}
359-
});
360-
361-
// The deferral requires the document to still have focus. When focus has
362-
// left the document, such as a window, iframe, or app switch, the
363-
// null-target focusout from the editing element must report immediately so
364-
// the framework is not left believing the view is still focused.
365-
// Regression test for https://github.com/flutter/flutter/issues/189744
366-
test('reports immediately when the document is not focused on iOS', () {
367-
final EngineFlutterView view = createAndRegisterView(dispatcher);
368-
final DomHTMLInputElement input = createDomHTMLInputElement();
369-
view.dom.rootElement.append(input);
370-
input.focusWithoutScroll();
371-
beginFakeTextEditing(input);
372-
dispatchedViewFocusEvents.clear();
373-
374-
debugEmulateIosSafari = true;
375-
debugViewFocusDocumentHasFocusOverride = false;
376-
try {
377-
input.blur();
378-
// Not deferred: with the document unfocused the unfocused event is
379-
// present synchronously.
380-
expect(
381-
dispatchedViewFocusEvents.where(
382-
(ui.ViewFocusEvent e) => e.state == ui.ViewFocusState.unfocused,
383-
),
384-
hasLength(1),
385-
);
386-
} finally {
387-
debugEmulateIosSafari = false;
388-
debugViewFocusDocumentHasFocusOverride = null;
389-
}
390-
});
391-
392-
// The deferral must key off the engine's editing state, not the
393-
// `flt-text-editing` class, which is not guaranteed to be applied by all
394-
// text editing strategies. Matching on the class would leave the deferral
395-
// dead for strategies that do not apply it.
396-
// Regression test for https://github.com/flutter/flutter/issues/189744
397-
test('defers on iOS for an editing element with no flt-text-editing class', () async {
398-
final EngineFlutterView view = createAndRegisterView(dispatcher);
399-
final DomHTMLInputElement input = createDomHTMLInputElement();
400-
view.dom.rootElement.append(input);
401-
input.focusWithoutScroll();
402-
beginFakeTextEditing(input);
403-
expect(
404-
input.classList.contains(HybridTextEditing.textEditingClass),
405-
isFalse,
406-
reason: 'the semantics path never applies this class',
407-
);
408-
dispatchedViewFocusEvents.clear();
409-
410-
debugEmulateIosSafari = true;
411-
debugViewFocusDocumentHasFocusOverride = true;
412-
try {
413-
input.blur();
414-
input.focusWithoutScroll();
415-
await Future<void>.delayed(const Duration(milliseconds: 150));
416-
expect(dispatchedViewFocusEvents, isEmpty);
417-
} finally {
418-
debugEmulateIosSafari = false;
419-
debugViewFocusDocumentHasFocusOverride = null;
420-
}
421-
});
422271
});
423272
}
424273

425-
/// Makes [element] the engine's active text-editing element, which is what
426-
/// [HybridTextEditing.isActiveTextEditingElement] reports to [ViewFocusBinding].
427-
///
428-
/// Sets the real singleton state rather than applying
429-
/// [HybridTextEditing.textEditingClass], so these tests exercise the same signal
430-
/// production code reads. The class is not guaranteed to be applied by all text
431-
/// editing strategies, so keying tests off it would not reflect the production
432-
/// code.
433-
void beginFakeTextEditing(DomHTMLElement element) {
434-
textEditing.isEditing = true;
435-
textEditing.strategy.domElement = element;
436-
}
437-
438-
void endFakeTextEditing() {
439-
textEditing.isEditing = false;
440-
textEditing.strategy.domElement = null;
441-
}
442-
443274
EngineFlutterView createAndRegisterView(EnginePlatformDispatcher dispatcher) {
444275
final DomElement div = createDomElement('div');
445276
final view = EngineFlutterView(dispatcher, div);

0 commit comments

Comments
 (0)