Bug 1875768 - Call the appropriate postfork handler on MacOS r=glandium
[gecko.git] / toolkit / modules / LayoutUtils.sys.mjs
blobe6b0623421af199b36fbae4118a09a7d04d6c871
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 = {
7   /**
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>).
10    */
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);
17   },
19   /**
20    * Similar to getElementBoundingScreenRect using window and rect,
21    * returns screen coordinates in screen units.
22    */
23   rectToScreenRect(win, rect) {
24     const { x, y, width, height } = this._rectToClientRect(win, rect);
25     return win.ownerGlobal.windowUtils.toScreenRect(x, y, width, height);
26   },
28   /**
29    * Convert rect into the top level widget coordinates in LayoutDevicePixel
30    * units.
31    */
32   rectToTopLevelWidgetRect(win, rect) {
33     const { x, y, width, height } = this._rectToClientRect(win, rect);
34     return win.ownerGlobal.windowUtils.toTopLevelWidgetRect(
35       x,
36       y,
37       width,
38       height
39     );
40   },
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;
52     while (parentFrame) {
53       win = parentFrame.ownerGlobal;
54       let cstyle = win.getComputedStyle(parentFrame);
56       let framerect = parentFrame.getBoundingClientRect();
57       let xDelta =
58         framerect.left +
59         parseFloat(cstyle.borderLeftWidth) +
60         parseFloat(cstyle.paddingLeft);
61       let yDelta =
62         framerect.top +
63         parseFloat(cstyle.borderTopWidth) +
64         parseFloat(cstyle.paddingTop);
66       x += xDelta * win.devicePixelRatio;
67       y += yDelta * win.devicePixelRatio;
69       parentFrame = win.browsingContext?.embedderElement;
70     }
72     return {
73       x: x / winDpr,
74       y: y / winDpr,
75       width: rect.width,
76       height: rect.height,
77     };
78   },