1 /* -*- mode: js; indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 export var LayoutUtils = {
8 * For a given DOM element, returns its position in screen coordinates of CSS units
9 * (<https://developer.mozilla.org/en-US/docs/Web/CSS/CSSOM_View/Coordinate_systems#screen>).
11 getElementBoundingScreenRect(aElement) {
12 let rect = aElement.getBoundingClientRect();
13 let win = aElement.ownerGlobal;
15 const { x, y, width, height } = this._rectToClientRect(win, rect);
16 return win.windowUtils.toScreenRectInCSSUnits(x, y, width, height);
20 * Similar to getElementBoundingScreenRect using window and rect,
21 * returns screen coordinates in screen units.
23 rectToScreenRect(win, rect) {
24 const { x, y, width, height } = this._rectToClientRect(win, rect);
25 return win.ownerGlobal.windowUtils.toScreenRect(x, y, width, height);
29 * Convert rect into the top level widget coordinates in LayoutDevicePixel
32 rectToTopLevelWidgetRect(win, rect) {
33 const { x, y, width, height } = this._rectToClientRect(win, rect);
34 return win.ownerGlobal.windowUtils.toTopLevelWidgetRect(
42 _rectToClientRect(win, rect) {
43 // We need to compensate the position for ancestor iframes in the same
44 // process that might shift things over. Those might have different CSS
45 // pixel scales, so we compute the position in device pixels and then go
46 // back to css pixels at the end.
47 let winDpr = win.devicePixelRatio;
48 let x = rect.left * winDpr;
49 let y = rect.top * winDpr;
51 let parentFrame = win.browsingContext?.embedderElement;
53 win = parentFrame.ownerGlobal;
54 let cstyle = win.getComputedStyle(parentFrame);
56 let framerect = parentFrame.getBoundingClientRect();
59 parseFloat(cstyle.borderLeftWidth) +
60 parseFloat(cstyle.paddingLeft);
63 parseFloat(cstyle.borderTopWidth) +
64 parseFloat(cstyle.paddingTop);
66 x += xDelta * win.devicePixelRatio;
67 y += yDelta * win.devicePixelRatio;
69 parentFrame = win.browsingContext?.embedderElement;