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
) {
26 base
.inherits(this, base
.EventSourceImpl
);
29 this.viewport_
= viewport
;
30 /** @private {number?} */
31 this.bumpScrollTimer_
= null;
33 this.eventHook_
= new base
.DomEventHook(document
.documentElement
, 'mousemove',
34 this.onMouseMove_
.bind(this), false);
36 this.defineEvents(base
.values(remoting
.BumpScroller
.Events
));
40 remoting
.BumpScroller
.Events
= {
41 bumpScrollStarted
: 'bumpScrollStarted',
42 bumpScrollStopped
: 'bumpScrollStopped'
45 remoting
.BumpScroller
.prototype.dispose = function() {
46 base
.dispose(this.eventHook_
);
47 this.eventHook_
= null;
51 * @param {Event} event The mouse event.
54 remoting
.BumpScroller
.prototype.onMouseMove_ = function(event
) {
55 if (this.bumpScrollTimer_
!== null) {
56 window
.clearTimeout(this.bumpScrollTimer_
);
57 this.bumpScrollTimer_
= null;
61 * Compute the scroll speed based on how close the mouse is to the edge.
63 * @param {number} mousePos The mouse x- or y-coordinate
64 * @param {number} size The width or height of the content area.
65 * @return {number} The scroll delta, in pixels.
67 var computeDelta = function(mousePos
, size
) {
69 if (mousePos
>= size
- threshold
) {
70 return 1 + 5 * (mousePos
- (size
- threshold
)) / threshold
;
71 } else if (mousePos
<= threshold
) {
72 return -1 - 5 * (threshold
- mousePos
) / threshold
;
77 var clientArea
= this.viewport_
.getClientArea();
78 var dx
= computeDelta(event
.x
, clientArea
.width
);
79 var dy
= computeDelta(event
.y
, clientArea
.height
);
81 if (dx
!== 0 || dy
!== 0) {
82 this.raiseEvent(remoting
.BumpScroller
.Events
.bumpScrollStarted
);
83 this.repeatScroll_(dx
, dy
, new Date().getTime());
88 * Scroll the view, and schedule a timer to do so again unless we've hit
89 * the edges of the screen. This timer is cancelled when the mouse moves.
93 * @param {number} expected The time at which we expect to be called.
96 remoting
.BumpScroller
.prototype.repeatScroll_ = function(dx
, dy
, expected
) {
98 var now
= new Date().getTime();
100 var lateAdjustment
= 1 + (now
- expected
) / timeout
;
101 if (!this.viewport_
.scroll(lateAdjustment
* dx
, lateAdjustment
* dy
)) {
102 this.raiseEvent(remoting
.BumpScroller
.Events
.bumpScrollStopped
);
104 this.bumpScrollTimer_
= window
.setTimeout(
105 this.repeatScroll_
.bind(this, dx
, dy
, now
+ timeout
), timeout
);