aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/telemetry/internal/actions/drag.js
blob: 8c5613c955c249a1c6df1805b56db74f80828df7 (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
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2015 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.

// This file provides the DragAction object, which performs drag on a page
// using given start and end positions:
//   1. var action = new __DragAction(callback)
//   2. action.start(drag_options)
'use strict';

(function() {
  function DragGestureOptions(opt_options) {
    this.element_ = opt_options.element;
    this.left_start_ratio_ = opt_options.left_start_ratio;
    this.top_start_ratio_ = opt_options.top_start_ratio;
    this.left_end_ratio_ = opt_options.left_end_ratio;
    this.top_end_ratio_ = opt_options.top_end_ratio;
    this.speed_ = opt_options.speed;
    this.gesture_source_type_ = opt_options.gesture_source_type;
  }

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

  // This class performs drag action using given start and end positions,
  // by a single drag gesture.
  function DragAction(opt_callback) {
    this.beginMeasuringHook = function() {};
    this.endMeasuringHook = function() {};

    this.callback_ = opt_callback;
  }

  DragAction.prototype.start = function(opt_options) {
    this.options_ = new DragGestureOptions(opt_options);
    requestAnimationFrame(this.startGesture_.bind(this));
  };

  DragAction.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_);
    var end_left =
        rect.left + (rect.width * this.options_.left_end_ratio_);
    var end_top =
        rect.top + (rect.height * this.options_.top_end_ratio_);
    chrome.gpuBenchmarking.smoothDrag(
        start_left, start_top, end_left, end_top,
        this.onGestureComplete_.bind(this), this.options_.gesture_source_type_,
        this.options_.speed_);
  };

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

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

  window.__DragAction = DragAction;
  window.__DragAction_SupportedByBrowser = supportedByBrowser;
})();