aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/telemetry/internal/actions/scroll_bounce.js
blob: ec0fc7ff81f4d88902da0c8525b2de4449160524 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

(function() {
  function supportedByBrowser() {
    return !!(window.chrome &&
              chrome.gpuBenchmarking &&
              chrome.gpuBenchmarking.scrollBounce &&
              chrome.gpuBenchmarking.visualViewportHeight &&
              chrome.gpuBenchmarking.visualViewportWidth);
  }

  function ScrollBounceAction(opt_callback) {
    var self = this;

    this.beginMeasuringHook = function() {};
    this.endMeasuringHook = function() {};

    this.callback_ = opt_callback;
  }

  ScrollBounceAction.prototype.start = function(options) {
    this.options_ = options;
    // Assign this.element_ here instead of constructor, because the constructor
    // ensures this method will be called after the document is loaded.
    this.element_ = this.options_.element;
    requestAnimationFrame(this.startGesture_.bind(this));
  };

  ScrollBounceAction.prototype.startGesture_ = function() {
    this.beginMeasuringHook();

    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element);
    var start_left =
        rect.left + rect.width * this.options_.left_start_ratio;
    var start_top =
        rect.top + rect.height * this.options_.top_start_ratio;
    chrome.gpuBenchmarking.scrollBounce(this.options_.direction,
                                        this.options_.distance,
                                        this.options_.overscroll,
                                        this.options_.repeat_count,
                                        this.onGestureComplete_.bind(this),
                                        start_left, start_top,
                                        this.options_.speed);
  };

  ScrollBounceAction.prototype.onGestureComplete_ = function() {
    this.endMeasuringHook();

    // We're done.
    if (this.callback_)
      this.callback_();
  };

  window.__ScrollBounceAction = ScrollBounceAction;
  window.__ScrollBounceAction_SupportedByBrowser = supportedByBrowser;
})();