Home | History | Annotate | Download | only in actions
      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 'use strict';
      6 
      7 (function() {
      8   function SwipeGestureOptions(opt_options) {
      9     if (opt_options) {
     10       this.element_ = opt_options.element;
     11       this.left_start_ratio_ = opt_options.left_start_ratio;
     12       this.top_start_ratio_ = opt_options.top_start_ratio;
     13       this.direction_ = opt_options.direction;
     14       this.distance_ = opt_options.distance;
     15       this.speed_ = opt_options.speed;
     16     } else {
     17       this.element_ = document.body;
     18       this.left_start_ratio_ = 0.5;
     19       this.top_start_ratio_ = 0.5;
     20       this.direction_ = 'left';
     21       this.distance_ = 0;
     22       this.speed_ = 800;
     23     }
     24   }
     25 
     26   function supportedByBrowser() {
     27     return !!(window.chrome &&
     28               chrome.gpuBenchmarking &&
     29               chrome.gpuBenchmarking.swipe &&
     30               chrome.gpuBenchmarking.visualViewportHeight &&
     31               chrome.gpuBenchmarking.visualViewportWidth);
     32   }
     33 
     34   // This class swipes a page for a specified distance.
     35   function SwipeAction(opt_callback) {
     36     var self = this;
     37 
     38     this.beginMeasuringHook = function() {};
     39     this.endMeasuringHook = function() {};
     40 
     41     this.callback_ = opt_callback;
     42   }
     43 
     44   SwipeAction.prototype.start = function(opt_options) {
     45     this.options_ = new SwipeGestureOptions(opt_options);
     46     // Assign this.element_ here instead of constructor, because the constructor
     47     // ensures this method will be called after the document is loaded.
     48     this.element_ = this.options_.element_;
     49     requestAnimationFrame(this.startGesture_.bind(this));
     50   };
     51 
     52   SwipeAction.prototype.startGesture_ = function() {
     53     this.beginMeasuringHook();
     54 
     55     var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
     56     var start_left =
     57         rect.left + rect.width * this.options_.left_start_ratio_;
     58     var start_top =
     59         rect.top + rect.height * this.options_.top_start_ratio_;
     60     chrome.gpuBenchmarking.swipe(this.options_.direction_,
     61                                  this.options_.distance_,
     62                                  this.onGestureComplete_.bind(this),
     63                                  start_left, start_top,
     64                                  this.options_.speed_);
     65   };
     66 
     67   SwipeAction.prototype.onGestureComplete_ = function() {
     68     this.endMeasuringHook();
     69 
     70     // We're done.
     71     if (this.callback_)
     72       this.callback_();
     73   };
     74 
     75   window.__SwipeAction = SwipeAction;
     76   window.__SwipeAction_SupportedByBrowser = supportedByBrowser;
     77 })();
     78