Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / telemetry / telemetry / internal / actions / gesture_common.js
blobec76ebaedb258ba8a7a2db3eec455dd3baee7273
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.
6 'use strict';
8 (function() {
10   // Make sure functions are injected only once.
11   if (window.__GestureCommon_GetBoundingVisibleRect)
12     return;
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,
18                   top: clientRect.top,
19                   width: clientRect.width,
20                   height: clientRect.height };
22     var frame = el.ownerDocument.defaultView.frameElement;
23     while (frame) {
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;
31     }
32     return bound;
33   }
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   }
43   // Zoom-independent window height. See crbug.com/627123 for more details.
44   function getWindowHeight() {
45     return getPageScaleFactor() * chrome.gpuBenchmarking.visualViewportHeight();
46   }
48   // Zoom-independent window width. See crbug.com/627123 for more details.
49   function getWindowWidth() {
50     return getPageScaleFactor() * chrome.gpuBenchmarking.visualViewportWidth();
51   }
53   function clamp(min, value, max) {
54     return Math.min(Math.max(min, value), max);
55   }
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);
71     return rect;
72   }
74   window.__GestureCommon_GetBoundingVisibleRect = getBoundingVisibleRect;
75   window.__GestureCommon_GetWindowHeight = getWindowHeight;
76   window.__GestureCommon_GetWindowWidth = getWindowWidth;
77 })();