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.
7 * This class allows enables the scrolling of the DestkopViewport in fullscreen
8 * mode by moving the mouse to the edge of the screen.
11 /** @suppress {duplicate} */
12 var remoting = remoting || {};
19 * @param {remoting.DesktopViewport} viewport
22 * @implements {base.Disposable}
23 * @extends {base.EventSourceImpl}
25 remoting.BumpScroller = function(viewport) {
27 this.viewport_ = viewport;
28 /** @private {number?} */
29 this.bumpScrollTimer_ = null;
31 this.eventHook_ = new base.DomEventHook(document.documentElement, 'mousemove',
32 this.onMouseMove_.bind(this), false);
34 this.defineEvents(base.values(remoting.BumpScroller.Events));
36 base.extend(remoting.BumpScroller, base.EventSourceImpl);
39 remoting.BumpScroller.Events = {
40 bumpScrollStarted: 'bumpScrollStarted',
41 bumpScrollStopped: 'bumpScrollStopped'
44 remoting.BumpScroller.prototype.dispose = function() {
45 base.dispose(this.eventHook_);
46 this.eventHook_ = null;
50 * @param {Event} event The mouse event.
53 remoting.BumpScroller.prototype.onMouseMove_ = function(event) {
54 if (this.bumpScrollTimer_ !== null) {
55 window.clearTimeout(this.bumpScrollTimer_);
56 this.bumpScrollTimer_ = null;
60 * Compute the scroll speed based on how close the mouse is to the edge.
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.
66 var computeDelta = function(mousePos, size) {
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;
76 var clientArea = this.viewport_.getClientArea();
77 var dx = computeDelta(event.x, clientArea.width);
78 var dy = computeDelta(event.y, clientArea.height);
80 if (dx !== 0 || dy !== 0) {
81 this.raiseEvent(remoting.BumpScroller.Events.bumpScrollStarted);
82 this.repeatScroll_(dx, dy, new Date().getTime());
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.
92 * @param {number} expected The time at which we expect to be called.
95 remoting.BumpScroller.prototype.repeatScroll_ = function(dx, dy, expected) {
97 var now = new Date().getTime();
99 var lateAdjustment = 1 + (now - expected) / timeout;
100 if (!this.viewport_.scroll(lateAdjustment * dx, lateAdjustment * dy)) {
101 this.raiseEvent(remoting.BumpScroller.Events.bumpScrollStopped);
103 this.bumpScrollTimer_ = window.setTimeout(
104 this.repeatScroll_.bind(this, dx, dy, now + timeout), timeout);