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.
4
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';
10
11(function() {
12
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  }
28
29  function supportedByBrowser() {
30    return !!(window.chrome &&
31              chrome.gpuBenchmarking &&
32              chrome.gpuBenchmarking.pinchBy);
33  }
34
35  // This class zooms into or out of a page, given a number of pixels for
36  // the synthetic pinch gesture to cover.
37  function PinchAction(opt_callback) {
38    var self = this;
39
40    this.beginMeasuringHook = function() {}
41    this.endMeasuringHook = function() {}
42
43    this.callback_ = opt_callback;
44  };
45
46  PinchAction.prototype.start = function(opt_options) {
47    this.options_ = new PinchGestureOptions(opt_options);
48
49    requestAnimationFrame(this.startPass_.bind(this));
50  };
51
52  PinchAction.prototype.startPass_ = function() {
53    this.beginMeasuringHook();
54
55    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
56    var anchor_left =
57        rect.left + rect.width * this.options_.left_anchor_ratio_;
58    var anchor_top =
59        rect.top + rect.height * this.options_.top_anchor_ratio_;
60    chrome.gpuBenchmarking.pinchBy(this.options_.scale_factor_,
61                                   anchor_left, anchor_top,
62                                   this.onGestureComplete_.bind(this),
63                                   this.options_.speed_);
64  };
65
66  PinchAction.prototype.onGestureComplete_ = function() {
67    this.endMeasuringHook();
68
69    if (this.callback_)
70      this.callback_();
71  };
72
73  window.__PinchAction = PinchAction;
74  window.__PinchAction_SupportedByBrowser = supportedByBrowser;
75})();
76