OLD | NEW |
(Empty) | |
| 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 /** |
| 6 * @fileoverview |
| 7 * This class allows enables the scrolling of the DestkopViewport in fullscreen |
| 8 * mode by moving the mouse to the edge of the screen. |
| 9 */ |
| 10 |
| 11 /** @suppress {duplicate} */ |
| 12 var remoting = remoting || {}; |
| 13 |
| 14 (function() { |
| 15 |
| 16 'use strict'; |
| 17 |
| 18 /** |
| 19 * @param {remoting.DesktopViewport} viewport |
| 20 * |
| 21 * @constructor |
| 22 * @implements {base.Disposable} |
| 23 * @extends {base.EventSourceImpl} |
| 24 */ |
| 25 remoting.BumpScroller = function(viewport) { |
| 26 /** @private */ |
| 27 this.viewport_ = viewport; |
| 28 /** @private {number?} */ |
| 29 this.bumpScrollTimer_ = null; |
| 30 /** @private */ |
| 31 this.eventHook_ = new base.DomEventHook(document.documentElement, 'mousemove', |
| 32 this.onMouseMove_.bind(this), false); |
| 33 |
| 34 this.defineEvents(base.values(remoting.BumpScroller.Events)); |
| 35 }; |
| 36 base.extend(remoting.BumpScroller, base.EventSourceImpl); |
| 37 |
| 38 /** @enum {string} */ |
| 39 remoting.BumpScroller.Events = { |
| 40 bumpScrollStarted: 'bumpScrollStarted', |
| 41 bumpScrollStopped: 'bumpScrollStopped' |
| 42 }; |
| 43 |
| 44 remoting.BumpScroller.prototype.dispose = function() { |
| 45 base.dispose(this.eventHook_); |
| 46 this.eventHook_ = null; |
| 47 }; |
| 48 |
| 49 /** |
| 50 * @param {Event} event The mouse event. |
| 51 * @private |
| 52 */ |
| 53 remoting.BumpScroller.prototype.onMouseMove_ = function(event) { |
| 54 if (this.bumpScrollTimer_ !== null) { |
| 55 window.clearTimeout(this.bumpScrollTimer_); |
| 56 this.bumpScrollTimer_ = null; |
| 57 } |
| 58 |
| 59 /** |
| 60 * Compute the scroll speed based on how close the mouse is to the edge. |
| 61 * |
| 62 * @param {number} mousePos The mouse x- or y-coordinate |
| 63 * @param {number} size The width or height of the content area. |
| 64 * @return {number} The scroll delta, in pixels. |
| 65 */ |
| 66 var computeDelta = function(mousePos, size) { |
| 67 var threshold = 10; |
| 68 if (mousePos >= size - threshold) { |
| 69 return 1 + 5 * (mousePos - (size - threshold)) / threshold; |
| 70 } else if (mousePos <= threshold) { |
| 71 return -1 - 5 * (threshold - mousePos) / threshold; |
| 72 } |
| 73 return 0; |
| 74 }; |
| 75 |
| 76 var clientArea = this.viewport_.getClientArea(); |
| 77 var dx = computeDelta(event.x, clientArea.width); |
| 78 var dy = computeDelta(event.y, clientArea.height); |
| 79 |
| 80 if (dx !== 0 || dy !== 0) { |
| 81 this.raiseEvent(remoting.BumpScroller.Events.bumpScrollStarted); |
| 82 this.repeatScroll_(dx, dy, new Date().getTime()); |
| 83 } |
| 84 }; |
| 85 |
| 86 /** |
| 87 * Scroll the view, and schedule a timer to do so again unless we've hit |
| 88 * the edges of the screen. This timer is cancelled when the mouse moves. |
| 89 * |
| 90 * @param {number} dx |
| 91 * @param {number} dy |
| 92 * @param {number} expected The time at which we expect to be called. |
| 93 * @private |
| 94 */ |
| 95 remoting.BumpScroller.prototype.repeatScroll_ = function(dx, dy, expected) { |
| 96 /** @type {number} */ |
| 97 var now = new Date().getTime(); |
| 98 var timeout = 10; |
| 99 var lateAdjustment = 1 + (now - expected) / timeout; |
| 100 if (!this.viewport_.scroll(lateAdjustment * dx, lateAdjustment * dy)) { |
| 101 this.raiseEvent(remoting.BumpScroller.Events.bumpScrollStopped); |
| 102 } else { |
| 103 this.bumpScrollTimer_ = window.setTimeout( |
| 104 this.repeatScroll_.bind(this, dx, dy, now + timeout), timeout); |
| 105 } |
| 106 }; |
| 107 |
| 108 }()); |
OLD | NEW |