[web] Set touch-action:none in embedded views. (flutter/engine#53945)

This PR adds `touch-action:none` to `flutter-view` elements, so the browser lets the flutter engine fully handle all touch gestures.

This fix is more delicate than the first approach, which broke some merged taps when accessibility/semantics are enabled. 

## Issues

* Found while testing: https://github.com/flutter/flutter/issues/130950
* "More correct" fix for: https://github.com/flutter/engine/pull/53647

## Demos

* Flutter scroll: https://dit-multiview-scroll.web.app
* Semantics: https://dit-tests.web.app
* Scrollable platform views: https://dit-multiview-tests.web.app

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
David Iglesias
2024-07-16 19:08:37 -07:00
committed by GitHub
parent 0f762f543a
commit 1960bc9b52
4 changed files with 9 additions and 18 deletions

View File

@@ -968,20 +968,6 @@ class _PointerAdapter extends _BaseAdapter with _WheelEventListenerMixin {
@override
void setup() {
// Prevents the browser auto-canceling pointer events.
// Register into `_listener` directly so the event isn't forwarded to semantics.
_listeners.add(Listener.register(
event: 'touchstart',
target: _viewTarget,
handler: (DomEvent event) {
// This is one of the ways I've seen this done. PixiJS does a similar thing.
// ThreeJS seems to subscribe move/leave in the pointerdown handler instead?
if (event.cancelable) {
event.preventDefault();
}
},
));
_addPointerEventListener(_viewTarget, 'pointerdown', (DomPointerEvent event) {
final int device = _getPointerId(event);
final List<ui.PointerData> pointerData = <ui.PointerData>[];

View File

@@ -34,7 +34,10 @@ class CustomElementEmbeddingStrategy implements EmbeddingStrategy {
..style.height = '100%'
..style.display = 'block'
..style.overflow = 'hidden'
..style.position = 'relative';
..style.position = 'relative'
// This is needed so the browser lets flutter handle all pointer events
// it receives, without canceling them.
..style.touchAction = 'none';
hostElement.appendChild(rootElement);

View File

@@ -305,15 +305,15 @@ void testMain() {
},
);
test('prevents default on touchstart events', () async {
test('allows default on touchstart events', () async {
final event = createDomEvent('Event', 'touchstart');
rootElement.dispatchEvent(event);
expect(
event.defaultPrevented,
isTrue,
reason: 'touchstart events should be prevented so pointer events are not cancelled later.',
isFalse,
reason: 'touchstart events should NOT be prevented. That breaks semantic taps!',
);
});

View File

@@ -76,6 +76,8 @@ void doTests() {
reason: 'Should take 100% of the available height');
expect(styleAfter.overflow, 'hidden',
reason: 'Should hide the occasional oversized canvas elements.');
expect(styleAfter.touchAction, 'none',
reason: 'Should disable browser handling of touch events.');
});
});
}