Skip to content

Commit 592d984

Browse files
harryterkelsensuryaKommana2662
authored andcommitted
[web] Capture pointers to the Flutter view so drags survive platform view iframes (flutter#195187)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> On Flutter Web, a drag that starts on Flutter content stops receiving `pointermove` events when the pointer moves over an `<iframe>` platform view, because the browser routes those events to the iframe document instead of the host window. As a result, the dragged widget freezes over the iframe and jumps when the pointer re-enters Flutter content. This PR captures the pointer to the `<flutter-view>` root on `pointerdown` when the press targets Flutter content (`_viewTarget` or `flt-glass-pane`, to which events inside the scene's shadow root are retargeted), and cancels still-pressed pointers if capture on the view root is involuntarily lost (`lostpointercapture`). Presses on platform views, the text-editing host, and the semantics host are excluded so native HTML interactions, text selection, and `ClickDebouncer` accessibility clicks continue to work on their original targets. This change ships separately from automatic pointer interceptors so that pointer-routing changes for all Flutter web apps can be reviewed and reverted independently. **Review hot spots** - **Boundary events during a captured drag:** `pointerover`/`out`/`enter`/`leave` on other elements (including `pointerleave` on the view root when dragging outside the window) are suppressed until release. - **Involuntary capture loss (`lostpointercapture`):** cancels active presses if capture on `_viewTarget` is lost before `pointerup`/`pointercancel` (e.g. window blur or `releasePointerCapture`), while ignoring `lostpointercapture` after normal release or bubbling from platform view descendants. - **Clicks and semantics:** after a captured press, `click` lands on the view root. `semanticsHost` is excluded from capture so `ClickDebouncer` and assistive-technology `flt-tappable` activation remain unchanged (drags starting on semantics nodes remain uncaptured over iframes). - **Touch, pen, and multi-view:** touch already has implicit capture (moving it to the view root is benign), pen drags gain capture alongside mouse drags, and each view captures to its own root. Fixes flutter#141307 Related: flutter#157968, flutter#134652 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent d68d45b commit 592d984

2 files changed

Lines changed: 409 additions & 10 deletions

File tree

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

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,9 @@ class _SanitizedDetails {
931931
class _ButtonSanitizer {
932932
int _pressedButtons = 0;
933933

934+
/// Whether any button of the pointer is currently pressed.
935+
bool get isPressed => _pressedButtons != 0;
936+
934937
/// Transform [DomPointerEvent.buttons] to Flutter's PointerEvent buttons.
935938
int _htmlButtonsToFlutterButtons(int buttons) {
936939
// Flutter's button definition conveniently matches that of JavaScript
@@ -1095,6 +1098,55 @@ class _PointerAdapter extends _BaseAdapter with _WheelEventListenerMixin {
10951098
);
10961099
}
10971100

1101+
/// Captures the pointer of [event] to [_viewTarget] if the press landed on
1102+
/// Flutter content.
1103+
///
1104+
/// While a pointer is captured, the browser sends all of its events to
1105+
/// [_viewTarget], even when the pointer moves over an `<iframe>` platform
1106+
/// view, so drags that start on Flutter content keep receiving move events.
1107+
/// The browser automatically releases the capture on `pointerup` and
1108+
/// `pointercancel`, so `releasePointerCapture` does not need to be called
1109+
/// manually. See https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
1110+
/// See https://github.com/flutter/flutter/issues/141307
1111+
///
1112+
/// Presses on platform view content, the text editing host and the semantics
1113+
/// host are not captured, so those elements keep receiving their own pointer
1114+
/// events.
1115+
void _capturePointerIfFlutterContent(DomPointerEvent event) {
1116+
// Only capture when `event.target` is the view root (`<flutter-view>`) or
1117+
// the glass pane (`_view.dom.platformViewsHost`, i.e. `<flt-glass-pane>`).
1118+
//
1119+
// Despite its field name, `platformViewsHost` (`<flt-glass-pane>`) is also
1120+
// the shadow host for the scene's `#shadow-root` (`renderingHost`). Events
1121+
// from Flutter content inside that shadow root (such as hit-test proxies in
1122+
// `<flt-scene-host>`) are retargeted by the browser across the shadow
1123+
// boundary to `<flt-glass-pane>`.
1124+
//
1125+
// By contrast, platform views (`<flt-platform-view>` and their inner DOM
1126+
// elements) are light-DOM children of `<flt-glass-pane>` projected via
1127+
// `<slot>`, so the browser does not retarget their events to
1128+
// `<flt-glass-pane>`—`event.target` remains the platform view element
1129+
// itself. Likewise, presses on text fields or semantics nodes have an
1130+
// `event.target` inside `textEditingHost` or `semanticsHost`. All of those
1131+
// satisfy `target != _viewTarget && target != _view.dom.platformViewsHost`
1132+
// and return early without capturing.
1133+
final DomEventTarget? target = event.target;
1134+
if (target != _viewTarget && target != _view.dom.platformViewsHost) {
1135+
return;
1136+
}
1137+
final double? pointerId = event.pointerId;
1138+
if (pointerId == null) {
1139+
return;
1140+
}
1141+
try {
1142+
_viewTarget.setPointerCapture(pointerId);
1143+
} catch (_) {
1144+
// Capture is best-effort. `setPointerCapture` throws if the pointer is
1145+
// not active (as with synthetic events) or the element is disconnected.
1146+
// The press itself has already been reported either way.
1147+
}
1148+
}
1149+
10981150
@override
10991151
void setup() {
11001152
_addPointerEventListener(_viewTarget, 'pointerdown', (DomPointerEvent event) {
@@ -1116,6 +1168,7 @@ class _PointerAdapter extends _BaseAdapter with _WheelEventListenerMixin {
11161168
_downTouchDevices.add(device);
11171169
}
11181170
_callback(event, pointerData);
1171+
_capturePointerIfFlutterContent(event);
11191172

11201173
if (event.target == _viewTarget) {
11211174
// Ensure smooth focus transitions between text fields within the Flutter view.
@@ -1134,16 +1187,12 @@ class _PointerAdapter extends _BaseAdapter with _WheelEventListenerMixin {
11341187
}
11351188
});
11361189

1137-
// Move event listeners should be added to `_globalTarget` instead of
1138-
// `_viewTarget`. This is because `_viewTarget` (the root) captures pointers
1139-
// by default, meaning a pointer that starts within `_viewTarget` continues
1140-
// sending move events to its listener even when dragged outside.
1141-
//
1142-
// In contrast, `_globalTarget` (a regular <div>) stops sending move events
1143-
// when the pointer moves outside its bounds and resumes them only when the
1144-
// pointer re-enters.
1145-
//
1146-
// For demonstration, see this fiddle: https://jsfiddle.net/ditman/7towxaqp
1190+
// Move event listeners are attached to `_globalTarget` rather than
1191+
// `_viewTarget`. When a pointer is captured to `_viewTarget` on pointerdown,
1192+
// its move events are routed to `_viewTarget` even over iframes and bubble
1193+
// to `_globalTarget`. Pointers that are not captured (hovers, and presses on
1194+
// platform views or the text editing host) must still be tracked, including
1195+
// outside of the view, which a listener on `_viewTarget` alone would miss.
11471196
//
11481197
// TODO(dkwingsmt): Investigate whether we can configure the behavior for
11491198
// `_viewTarget`. https://github.com/flutter/flutter/issues/157968
@@ -1220,6 +1269,35 @@ class _PointerAdapter extends _BaseAdapter with _WheelEventListenerMixin {
12201269
}
12211270
}, checkModifiers: false);
12221271

1272+
// The browser automatically releases pointer capture on `pointerup` and
1273+
// `pointercancel`, so `releasePointerCapture` never needs to be called
1274+
// explicitly. See
1275+
// https://w3c.github.io/pointerevents/#implicit-release-of-pointer-capture
1276+
//
1277+
// However, capture can also be taken away without a `pointerup` or a
1278+
// `pointercancel`, for example when the page calls `releasePointerCapture`
1279+
// or captures the pointer to another element, or, in some browsers, when
1280+
// the window loses focus. If buttons are still pressed, the press will
1281+
// never be released, so cancel it. The event bubbles, so ignore captures
1282+
// lost by descendants (such as platform view content); only the capture
1283+
// this adapter set on the view root matters.
1284+
_addPointerEventListener(_viewTarget, 'lostpointercapture', (DomPointerEvent event) {
1285+
if (event.target != _viewTarget) {
1286+
return;
1287+
}
1288+
final int device = _getPointerId(event);
1289+
if (_hasSanitizer(device)) {
1290+
final _ButtonSanitizer sanitizer = _getSanitizer(device);
1291+
if (sanitizer.isPressed) {
1292+
final pointerData = <ui.PointerData>[];
1293+
final _SanitizedDetails details = sanitizer.sanitizeCancelEvent();
1294+
_removePointerIfUnhoverable(event);
1295+
_convertEventsToPointerData(data: pointerData, event: event, details: details);
1296+
_callback(event, pointerData);
1297+
}
1298+
}
1299+
}, checkModifiers: false);
1300+
12231301
// Safety net for touches the browser abandons without a `pointerup` or a
12241302
// `pointercancel`. See [_cancelAbandonedTouches].
12251303
addEventListener(_globalTarget, 'touchend', (DomEvent event) {

0 commit comments

Comments
 (0)