OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file provides common functionality for synthetic gesture actions. | 5 // This file provides common functionality for synthetic gesture actions. |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 (function() { | 8 (function() { |
9 | 9 |
10 // Make sure functions are injected only once. | 10 // Make sure functions are injected only once. |
11 if (window.__GestureCommon_GetBoundingVisibleRect) | 11 if (window.__GestureCommon_GetBoundingVisibleRect) |
12 return; | 12 return; |
13 | 13 |
14 // Returns the bounding rectangle wrt to the top-most document. | 14 // Returns the bounding rectangle wrt to the layout viewport. |
15 function getBoundingRect(el) { | 15 function getBoundingRect(el) { |
16 var clientRect = el.getBoundingClientRect(); | 16 var clientRect = el.getBoundingClientRect(); |
17 var bound = { left: clientRect.left, | 17 var bound = { left: clientRect.left, |
18 top: clientRect.top, | 18 top: clientRect.top, |
19 width: clientRect.width, | 19 width: clientRect.width, |
20 height: clientRect.height }; | 20 height: clientRect.height }; |
21 | 21 |
22 var frame = el.ownerDocument.defaultView.frameElement; | 22 var frame = el.ownerDocument.defaultView.frameElement; |
23 while (frame) { | 23 while (frame) { |
24 var frameBound = frame.getBoundingClientRect(); | 24 var frameBound = frame.getBoundingClientRect(); |
25 // This computation doesn't account for more complex CSS transforms on the | 25 // This computation doesn't account for more complex CSS transforms on the |
26 // frame (e.g. scaling or rotations). | 26 // frame (e.g. scaling or rotations). |
27 bound.left += frameBound.left; | 27 bound.left += frameBound.left; |
28 bound.top += frameBound.top; | 28 bound.top += frameBound.top; |
29 | 29 |
30 frame = frame.ownerDocument.frameElement; | 30 frame = frame.ownerDocument.frameElement; |
31 } | 31 } |
32 return bound; | 32 return bound; |
33 } | 33 } |
34 | 34 |
35 // TODO(ulan): Remove this function once | |
36 // chrome.gpuBenchmarking.pageScaleFactor is available in reference builds. | |
37 function getPageScaleFactor() { | |
38 if (chrome.gpuBenchmarking.pageScaleFactor) | |
39 return chrome.gpuBenchmarking.pageScaleFactor(); | |
40 return 1; | |
41 } | |
42 | |
43 // Zoom-independent window height. See crbug.com/627123 for more details. | 35 // Zoom-independent window height. See crbug.com/627123 for more details. |
44 function getWindowHeight() { | 36 function getWindowHeight() { |
45 return getPageScaleFactor() * chrome.gpuBenchmarking.visualViewportHeight(); | 37 return chrome.gpuBenchmarking.pageScaleFactor() * |
38 chrome.gpuBenchmarking.visualViewportHeight(); | |
46 } | 39 } |
47 | 40 |
48 // Zoom-independent window width. See crbug.com/627123 for more details. | 41 // Zoom-independent window width. See crbug.com/627123 for more details. |
49 function getWindowWidth() { | 42 function getWindowWidth() { |
50 return getPageScaleFactor() * chrome.gpuBenchmarking.visualViewportWidth(); | 43 return chrome.gpuBenchmarking.pageScaleFactor() * |
44 chrome.gpuBenchmarking.visualViewportWidth(); | |
51 } | 45 } |
52 | 46 |
53 function clamp(min, value, max) { | 47 function clamp(min, value, max) { |
54 return Math.min(Math.max(min, value), max); | 48 return Math.min(Math.max(min, value), max); |
55 } | 49 } |
56 | 50 |
51 // Returns the bounding rect in the visual viewport's coordinates. | |
57 function getBoundingVisibleRect(el) { | 52 function getBoundingVisibleRect(el) { |
58 // Get the element bounding rect. | 53 // Get the element bounding rect. |
59 var rect = getBoundingRect(el); | 54 var rect = getBoundingRect(el); |
60 | 55 |
56 // TODO(bokan): Remove this once gpuBenchmarking is changed to take all | |
57 // coordinates in viewport space. crbug.com/610021. | |
58 if ('gesturesExpectedInViewportCoordinates' in chrome.gpuBenchmarking) { | |
ericrk
2017/09/15 16:41:04
So, to make sure I understand, this code is the ne
bokan
2017/09/17 05:48:51
That's right. Once that CL lands the code guarded
| |
59 // Apply the visual viewport transform (i.e. pinch-zoom) to the bounding | |
60 // rect. | |
61 const scale = chrome.gpuBenchmarking.pageScaleFactor(); | |
62 const visualViewportX = chrome.gpuBenchmarking.visualViewportX(); | |
63 const visualViewportY = chrome.gpuBenchmarking.visualViewportY(); | |
64 rect.top = (rect.top - visualViewportY) * scale; | |
65 rect.left = (rect.left - visualViewportX) * scale; | |
66 rect.width *= scale; | |
ericrk
2017/09/15 16:41:04
So, I think your code is correct, but I want to ma
bokan
2017/09/17 05:48:51
Your understanding is correct. These values are in
| |
67 rect.height *= scale; | |
68 } | |
69 | |
61 // Get the window dimensions. | 70 // Get the window dimensions. |
62 var windowHeight = getWindowHeight(); | 71 const windowHeight = getWindowHeight(); |
63 var windowWidth = getWindowWidth(); | 72 const windowWidth = getWindowWidth(); |
64 | 73 |
65 // Then clip the rect to the screen size. | 74 // Then clip the rect to the screen size. |
66 rect.top = clamp(0, rect.top, windowHeight); | 75 rect.top = clamp(0, rect.top, windowHeight); |
67 rect.left = clamp(0, rect.left, windowWidth); | 76 rect.left = clamp(0, rect.left, windowWidth); |
68 rect.height = clamp(0, rect.height, windowHeight - rect.top); | 77 rect.height = clamp(0, rect.height, windowHeight - rect.top); |
69 rect.width = clamp(0, rect.width, windowWidth - rect.left); | 78 rect.width = clamp(0, rect.width, windowWidth - rect.left); |
70 | 79 |
71 return rect; | 80 return rect; |
72 } | 81 } |
73 | 82 |
74 window.__GestureCommon_GetBoundingVisibleRect = getBoundingVisibleRect; | 83 window.__GestureCommon_GetBoundingVisibleRect = getBoundingVisibleRect; |
75 window.__GestureCommon_GetWindowHeight = getWindowHeight; | 84 window.__GestureCommon_GetWindowHeight = getWindowHeight; |
76 window.__GestureCommon_GetWindowWidth = getWindowWidth; | 85 window.__GestureCommon_GetWindowWidth = getWindowWidth; |
77 })(); | 86 })(); |
OLD | NEW |