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  }
27
28  // This class performs drag action using given start and end positions,
29  // by a single drag gesture.
30  function DragAction(opt_callback) {
31    this.beginMeasuringHook = function() {}
32    this.endMeasuringHook = function() {}
33
34    this.callback_ = opt_callback;
35  }
36
37  DragAction.prototype.start = function(opt_options) {
38    this.options_ = new DragGestureOptions(opt_options);
39    requestAnimationFrame(this.startGesture_.bind(this));
40  };
41
42  DragAction.prototype.startGesture_ = function() {
43    this.beginMeasuringHook();
44
45    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
46    var start_left =
47        rect.left + (rect.width * this.options_.left_start_ratio_);
48    var start_top =
49        rect.top + (rect.height * this.options_.top_start_ratio_);
50    var end_left =
51        rect.left + (rect.width * this.options_.left_end_ratio_);
52    var end_top =
53        rect.top + (rect.height * this.options_.top_end_ratio_);
54    chrome.gpuBenchmarking.smoothDrag(
55        start_left, start_top, end_left, end_top,
56        this.onGestureComplete_.bind(this), this.options_.gesture_source_type_,
57        this.options_.speed_);
58  };
59
60  DragAction.prototype.onGestureComplete_ = function() {
61    this.endMeasuringHook();
62
63    // We're done.
64    if (this.callback_)
65      this.callback_();
66  };
67
68  window.__DragAction = DragAction;
69  window.__DragAction_SupportedByBrowser = supportedByBrowser;
70})();
71