• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 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 DragAction object, which performs drag on a page
6// using given start and end positions:
7//   1. var action = new __DragAction(callback)
8//   2. action.start(drag_options)
9'use strict';
10
11(function() {
12  function DragGestureOptions(opt_options) {
13    this.element_ = opt_options.element;
14    this.left_start_ratio_ = opt_options.left_start_ratio;
15    this.top_start_ratio_ = opt_options.top_start_ratio;
16    this.left_end_ratio_ = opt_options.left_end_ratio;
17    this.top_end_ratio_ = opt_options.top_end_ratio;
18    this.speed_ = opt_options.speed;
19    this.gesture_source_type_ = opt_options.gesture_source_type;
20  }
21
22  function supportedByBrowser() {
23    return !!(window.chrome &&
24              chrome.gpuBenchmarking &&
25              chrome.gpuBenchmarking.smoothDrag &&
26              chrome.gpuBenchmarking.visualViewportHeight &&
27              chrome.gpuBenchmarking.visualViewportWidth);
28  }
29
30  // This class performs drag action using given start and end positions,
31  // by a single drag gesture.
32  function DragAction(opt_callback) {
33    this.beginMeasuringHook = function() {};
34    this.endMeasuringHook = function() {};
35
36    this.callback_ = opt_callback;
37  }
38
39  DragAction.prototype.start = function(opt_options) {
40    this.options_ = new DragGestureOptions(opt_options);
41    requestAnimationFrame(this.startGesture_.bind(this));
42  };
43
44  DragAction.prototype.startGesture_ = function() {
45    this.beginMeasuringHook();
46
47    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
48    var start_left =
49        rect.left + (rect.width * this.options_.left_start_ratio_);
50    var start_top =
51        rect.top + (rect.height * this.options_.top_start_ratio_);
52    var end_left =
53        rect.left + (rect.width * this.options_.left_end_ratio_);
54    var end_top =
55        rect.top + (rect.height * this.options_.top_end_ratio_);
56    chrome.gpuBenchmarking.smoothDrag(
57        start_left, start_top, end_left, end_top,
58        this.onGestureComplete_.bind(this), this.options_.gesture_source_type_,
59        this.options_.speed_);
60  };
61
62  DragAction.prototype.onGestureComplete_ = function() {
63    this.endMeasuringHook();
64
65    // We're done.
66    if (this.callback_)
67      this.callback_();
68  };
69
70  window.__DragAction = DragAction;
71  window.__DragAction_SupportedByBrowser = supportedByBrowser;
72})();
73