|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:ui/ui.dart' as ui show Offset; |
| 6 | + |
| 7 | +import '../dom.dart'; |
| 8 | +import '../semantics.dart' show EngineSemanticsOwner; |
| 9 | + |
| 10 | +/// Returns an [ui.Offset] of the position of [event], relative to the position of [actualTarget]. |
| 11 | +/// |
| 12 | +/// The offset is *not* multiplied by DPR or anything else, it's the closest |
| 13 | +/// to what the DOM would return if we had currentTarget readily available. |
| 14 | +/// |
| 15 | +/// This needs an `actualTarget`, because the `event.currentTarget` (which is what |
| 16 | +/// this would really need to use) gets lost when the `event` comes from a "coalesced" |
| 17 | +/// event. |
| 18 | +/// |
| 19 | +/// It also takes into account semantics being enabled to fix the case where |
| 20 | +/// offsetX, offsetY == 0 (TalkBack events). |
| 21 | +// |
| 22 | +// TODO(dit): Make this understand 3D transforms in the platform view case, https://github.com/flutter/flutter/issues/117091 |
| 23 | +ui.Offset computeEventOffsetToTarget(DomMouseEvent event, DomElement actualTarget) { |
| 24 | + // On top of a platform view |
| 25 | + if (event.target != actualTarget) { |
| 26 | + return _computeOffsetOnPlatformView(event, actualTarget); |
| 27 | + } |
| 28 | + // On a TalkBack event |
| 29 | + if (EngineSemanticsOwner.instance.semanticsEnabled && event.offsetX == 0 && event.offsetY == 0) { |
| 30 | + return _computeOffsetForTalkbackEvent(event, actualTarget); |
| 31 | + } |
| 32 | + // Return the offsetX/Y in the normal case. |
| 33 | + return ui.Offset(event.offsetX, event.offsetY); |
| 34 | +} |
| 35 | + |
| 36 | +/// Computes the event offset when hovering over a platformView. |
| 37 | +/// |
| 38 | +/// This still uses offsetX/Y, but adds the offset from the top/left corner of the |
| 39 | +/// platform view to the glass pane (`actualTarget`). |
| 40 | +/// |
| 41 | +/// ×--FlutterView(actualTarget)--------------+ |
| 42 | +/// |\ | |
| 43 | +/// | x1,y1 | |
| 44 | +/// | | |
| 45 | +/// | | |
| 46 | +/// | ×-PlatformView(target)---------+ | |
| 47 | +/// | |\ | | |
| 48 | +/// | | x2,y2 | | |
| 49 | +/// | | | | |
| 50 | +/// | | × (event) | | |
| 51 | +/// | | \ | | |
| 52 | +/// | | offsetX, offsetY | | |
| 53 | +/// | | (Relative to PlatformView) | | |
| 54 | +/// | +------------------------------+ | |
| 55 | +/// +-----------------------------------------+ |
| 56 | +/// |
| 57 | +/// Offset between PlatformView and FlutterView (xP, yP) = (x2 - x1, y2 - y1) |
| 58 | +/// |
| 59 | +/// Event offset relative to FlutterView = (offsetX + xP, offsetY + yP) |
| 60 | +ui.Offset _computeOffsetOnPlatformView(DomMouseEvent event, DomElement actualTarget) { |
| 61 | + final DomElement target = event.target! as DomElement; |
| 62 | + final DomRect targetRect = target.getBoundingClientRect(); |
| 63 | + final DomRect actualTargetRect = actualTarget.getBoundingClientRect(); |
| 64 | + final double offsetTop = targetRect.y - actualTargetRect.y; |
| 65 | + final double offsetLeft = targetRect.x - actualTargetRect.x; |
| 66 | + return ui.Offset(event.offsetX + offsetLeft, event.offsetY + offsetTop); |
| 67 | +} |
| 68 | + |
| 69 | +/// Computes the event offset when TalkBack is firing the event. |
| 70 | +/// |
| 71 | +/// In this case, we need to use the clientX/Y position of the event (which are |
| 72 | +/// relative to the absolute top-left corner of the page, including scroll), then |
| 73 | +/// deduct the offsetLeft/Top from every offsetParent of the `actualTarget`. |
| 74 | +/// |
| 75 | +/// ×-Page----║-------------------------------+ |
| 76 | +/// | ║ | |
| 77 | +/// | ×-------║--------offsetParent(s)-----+ | |
| 78 | +/// | |\ | | |
| 79 | +/// | | offsetLeft, offsetTop | | |
| 80 | +/// | | | | |
| 81 | +/// | | | | |
| 82 | +/// | | ×-----║-------------actualTarget-+ | | |
| 83 | +/// | | | | | | |
| 84 | +/// ═════ × ─ (scrollLeft, scrollTop)═ ═ ═ |
| 85 | +/// | | | | | | |
| 86 | +/// | | | × | | | |
| 87 | +/// | | | \ | | | |
| 88 | +/// | | | clientX, clientY | | | |
| 89 | +/// | | | (Relative to Page + Scroll) | | | |
| 90 | +/// | | +-----║--------------------------+ | | |
| 91 | +/// | +-------║----------------------------+ | |
| 92 | +/// +---------║-------------------------------+ |
| 93 | +/// |
| 94 | +/// Computing the offset of the event relative to the actualTarget requires to |
| 95 | +/// compute the clientX, clientY of the actualTarget. To do that, we iterate |
| 96 | +/// up the offsetParent elements of actualTarget adding their offset and scroll |
| 97 | +/// positions. Finally, we deduct that from clientX, clientY of the event. |
| 98 | +
|
| 99 | +ui.Offset _computeOffsetForTalkbackEvent(DomMouseEvent event, DomElement actualTarget) { |
| 100 | + assert(EngineSemanticsOwner.instance.semanticsEnabled); |
| 101 | + // Use clientX/clientY as the position of the event (this is relative to |
| 102 | + // the top left of the page, including scroll) |
| 103 | + double offsetX = event.clientX; |
| 104 | + double offsetY = event.clientY; |
| 105 | + // Compute the scroll offset of actualTarget |
| 106 | + DomHTMLElement parent = actualTarget as DomHTMLElement; |
| 107 | + while(parent.offsetParent != null){ |
| 108 | + offsetX -= parent.offsetLeft - parent.scrollLeft; |
| 109 | + offsetY -= parent.offsetTop - parent.scrollTop; |
| 110 | + parent = parent.offsetParent!; |
| 111 | + } |
| 112 | + return ui.Offset(offsetX, offsetY); |
| 113 | +} |
0 commit comments