Skip to content

Commit 7e115a9

Browse files
authored
[web] Remove the a11y placeholder only after the gesture ends (flutter#194109)
Fixes flutter#194104 ## Problem On mobile, the engine covers each view with a semantics placeholder and removes it after 20 events that do not activate a screen reader. On non-WebKit browsers it removed the placeholder on the very next event, usually a `pointermove` in the middle of a drag. That placeholder is the drag's target, and when the page lets the browser pan and cancels `touchmove`, Chrome on Android stops delivering the rest of the drag, including its `pointerup`, so the framework keeps the pointer down forever. ## Fix Remove the placeholder only on `touchend`, `pointerup`, or `click`, once the gesture that targets it has ended, on every browser engine. WebKit has used this rule since the placeholder was added. Accessibility was already given up on, so the only change is that the placeholder goes away when the finger lifts instead of mid-gesture. ## Demo On an Android phone in Chrome, reload the page, do one long, slow drag on the list, and lift the finger. Before, "Pointers down" stays at 1: https://flutter-demo-77-before.web.app After, "Pointers down" returns to 0 and the list is idle: https://flutter-demo-77-after.web.app
1 parent 2f89b8d commit 7e115a9

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics_helper.dart‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,15 @@ class MobileSemanticsEnabler extends SemanticsEnabler {
354354

355355
/// Instructs [tryEnableSemantics] to remove the placeholders.
356356
///
357-
/// For Blink browser engine the placeholders are removed upon any next event.
357+
/// The placeholders are removed upon the next "touchend", "pointerup",
358+
/// "touchcancel", "pointercancel", or "click" event, once the gesture that
359+
/// targets them has ended or was canceled. Removing them in the middle of a
360+
/// gesture can take the rest of its events with them. Safari swallows those
361+
/// events, and Chrome stops delivering them to the window when the page lets
362+
/// the browser pan and cancels "touchmove", so the framework never sees the
363+
/// pointer go up.
358364
///
359-
/// For Webkit browser engine the placeholders are removed upon the next
360-
/// "touchend" event. This is to prevent Safari from swallowing the event
361-
/// that happens on an element that's being removed. Blink doesn't have
362-
/// this issue.
365+
/// See https://github.com/flutter/flutter/issues/194104
363366
bool _schedulePlaceholderRemoval = false;
364367

365368
/// Hosts the placeholder inside the view it belongs to.
@@ -386,9 +389,10 @@ class MobileSemanticsEnabler extends SemanticsEnabler {
386389
if (_schedulePlaceholderRemoval) {
387390
// The event type can also be click for VoiceOver.
388391
final bool removeNow =
389-
ui_web.browser.browserEngine != ui_web.BrowserEngine.webkit ||
390392
event.type == 'touchend' ||
391393
event.type == 'pointerup' ||
394+
event.type == 'touchcancel' ||
395+
event.type == 'pointercancel' ||
392396
event.type == 'click';
393397
if (removeNow) {
394398
removeAllPlaceholders();

‎engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_helper_test.dart‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,49 @@ void testMain() {
293293
expect(shouldForwardToFramework, isTrue);
294294
});
295295

296+
test('keeps the placeholder until the gesture that targets it ends', () {
297+
// Giving up on semantics schedules the placeholder for removal. A drag
298+
// that started on the placeholder is still in progress, and removing
299+
// its target now could lose the drag's pointerup.
300+
// See https://github.com/flutter/flutter/issues/194104
301+
mobileSemanticsEnabler.semanticsActivationAttempts = kMaxSemanticsActivationAttempts - 1;
302+
expect(
303+
mobileSemanticsEnabler.tryEnableSemantics(createDomPointerEvent('pointermove')),
304+
isTrue,
305+
);
306+
307+
expect(
308+
mobileSemanticsEnabler.tryEnableSemantics(createDomPointerEvent('pointermove')),
309+
isTrue,
310+
);
311+
expect(placeholder!.isConnected, isTrue);
312+
expect(mobileSemanticsEnabler.placeholders, <DomElement>[placeholder!]);
313+
314+
expect(
315+
mobileSemanticsEnabler.tryEnableSemantics(createDomPointerEvent('pointerup')),
316+
isTrue,
317+
);
318+
expect(placeholder!.isConnected, isFalse);
319+
expect(mobileSemanticsEnabler.placeholders, isEmpty);
320+
});
321+
322+
for (final cancelType in <String>['pointercancel', 'touchcancel']) {
323+
test('removes the placeholder when the gesture ends with $cancelType', () {
324+
// A gesture the browser takes over, such as a page pan, ends with a
325+
// cancel instead of a pointerup or touchend.
326+
mobileSemanticsEnabler.semanticsActivationAttempts = kMaxSemanticsActivationAttempts - 1;
327+
mobileSemanticsEnabler.tryEnableSemantics(createDomPointerEvent('pointermove'));
328+
expect(placeholder!.isConnected, isTrue);
329+
330+
expect(
331+
mobileSemanticsEnabler.tryEnableSemantics(createDomEvent('Event', cancelType)),
332+
isTrue,
333+
);
334+
expect(placeholder!.isConnected, isFalse);
335+
expect(mobileSemanticsEnabler.placeholders, isEmpty);
336+
});
337+
}
338+
296339
test('Can update placeholder label', () {
297340
const testLabel = 'Test label for placeholder';
298341
mobileSemanticsEnabler.updatePlaceholderLabel(testLabel);

0 commit comments

Comments
 (0)