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
3 // found in the LICENSE file.
5 // This file provides common functionality for synthetic gesture actions.
10 // Make sure functions are injected only once.
11 if (window.__GestureCommon_GetBoundingVisibleRect)
14 // Returns the bounding rectangle wrt to the top-most document.
15 function getBoundingRect(el) {
16 var clientRect = el.getBoundingClientRect();
17 var bound = { left: clientRect.left,
19 width: clientRect.width,
20 height: clientRect.height };
22 var frame = el.ownerDocument.defaultView.frameElement;
24 var frameBound = frame.getBoundingClientRect();
25 // This computation doesn't account for more complex CSS transforms on the
26 // frame (e.g. scaling or rotations).
27 bound.left += frameBound.left;
28 bound.top += frameBound.top;
30 frame = frame.ownerDocument.frameElement;
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();
43 // Zoom-independent window height. See crbug.com/627123 for more details.
44 function getWindowHeight() {
45 return getPageScaleFactor() * chrome.gpuBenchmarking.visualViewportHeight();
48 // Zoom-independent window width. See crbug.com/627123 for more details.
49 function getWindowWidth() {
50 return getPageScaleFactor() * chrome.gpuBenchmarking.visualViewportWidth();
53 function clamp(min, value, max) {
54 return Math.min(Math.max(min, value), max);
57 function getBoundingVisibleRect(el) {
58 // Get the element bounding rect.
59 var rect = getBoundingRect(el);
61 // Get the window dimensions.
62 var windowHeight = getWindowHeight();
63 var windowWidth = getWindowWidth();
65 // Then clip the rect to the screen size.
66 rect.top = clamp(0, rect.top, windowHeight);
67 rect.left = clamp(0, rect.left, windowWidth);
68 rect.height = clamp(0, rect.height, windowHeight - rect.top);
69 rect.width = clamp(0, rect.width, windowWidth - rect.left);
74 window.__GestureCommon_GetBoundingVisibleRect = getBoundingVisibleRect;
75 window.__GestureCommon_GetWindowHeight = getWindowHeight;
76 window.__GestureCommon_GetWindowWidth = getWindowWidth;