Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / telemetry / telemetry / internal / actions / pinch.js
blob4abb66e6431620ec2f4db92cefed3eb091bc2dee
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 the PinchAction object, which zooms into or out of a
6 // page by a given scale factor:
7 //   1. var action = new __PinchAction(callback)
8 //   2. action.start(pinch_options)
9 'use strict';
11 (function() {
13   function PinchGestureOptions(opt_options) {
14     if (opt_options) {
15       this.element_ = opt_options.element;
16       this.left_anchor_ratio_ = opt_options.left_anchor_ratio;
17       this.top_anchor_ratio_ = opt_options.top_anchor_ratio;
18       this.scale_factor_ = opt_options.scale_factor;
19       this.speed_ = opt_options.speed;
20     } else {
21       this.element_ = document.body;
22       this.left_anchor_ratio_ = 0.5;
23       this.top_anchor_ratio_ = 0.5;
24       this.scale_factor_ = 2.0;
25       this.speed_ = 800;
26     }
27   }
29   function supportedByBrowser() {
30     return !!(window.chrome &&
31               chrome.gpuBenchmarking &&
32               chrome.gpuBenchmarking.pinchBy &&
33               chrome.gpuBenchmarking.visualViewportHeight &&
34               chrome.gpuBenchmarking.visualViewportWidth);
35   }
37   // This class zooms into or out of a page, given a number of pixels for
38   // the synthetic pinch gesture to cover.
39   function PinchAction(opt_callback) {
40     var self = this;
42     this.beginMeasuringHook = function() {};
43     this.endMeasuringHook = function() {};
45     this.callback_ = opt_callback;
46   };
48   PinchAction.prototype.start = function(opt_options) {
49     this.options_ = new PinchGestureOptions(opt_options);
51     requestAnimationFrame(this.startPass_.bind(this));
52   };
54   PinchAction.prototype.startPass_ = function() {
55     this.beginMeasuringHook();
57     var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
58     var anchor_left =
59         rect.left + rect.width * this.options_.left_anchor_ratio_;
60     var anchor_top =
61         rect.top + rect.height * this.options_.top_anchor_ratio_;
62     chrome.gpuBenchmarking.pinchBy(this.options_.scale_factor_,
63                                    anchor_left, anchor_top,
64                                    this.onGestureComplete_.bind(this),
65                                    this.options_.speed_);
66   };
68   PinchAction.prototype.onGestureComplete_ = function() {
69     this.endMeasuringHook();
71     if (this.callback_)
72       this.callback_();
73   };
75   window.__PinchAction = PinchAction;
76   window.__PinchAction_SupportedByBrowser = supportedByBrowser;
77 })();