OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 the user to scroll the DestkopViewport in fullscreen mode | |
8 * 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 this.eventHook_ = new base.DomEventHook(document.documentElement, 'mousemove', | |
31 this.onMouseMove_.bind(this), false); | |
32 | |
33 this.defineEvents(Object.keys(remoting.BumpScroller.Events)); | |
34 }; | |
35 base.extend(remoting.BumpScroller, base.EventSourceImpl); | |
36 | |
37 /** @enum {string} */ | |
38 remoting.BumpScroller.Events = { | |
39 bumpScrollStarted: 'bumpScrollStarted', | |
kelvinp
2015/02/11 23:57:32
The bumpScrolling browsertest will be changed to u
| |
40 bumpScrollStopped: 'bumpScrollStopped' | |
41 }; | |
42 | |
43 remoting.BumpScroller.prototype.dispose = function() { | |
44 base.dispose(this.eventHook_); | |
45 this.eventHook_ = null; | |
46 }; | |
47 | |
48 /** | |
49 * @param {Event} event The mouse event. | |
50 * @private | |
51 */ | |
52 remoting.BumpScroller.prototype.onMouseMove_ = function(event) { | |
53 if (this.bumpScrollTimer_) { | |
54 window.clearTimeout(this.bumpScrollTimer_); | |
55 this.bumpScrollTimer_ = null; | |
56 } | |
57 | |
58 /** | |
59 * Compute the scroll speed based on how close the mouse is to the edge. | |
60 * @param {number} mousePos The mouse x- or y-coordinate | |
61 * @param {number} size The width or height of the content area. | |
62 * @return {number} The scroll delta, in pixels. | |
63 */ | |
64 var computeDelta = function(mousePos, size) { | |
65 var threshold = 10; | |
66 if (mousePos >= size - threshold) { | |
67 return 1 + 5 * (mousePos - (size - threshold)) / threshold; | |
68 } else if (mousePos <= threshold) { | |
69 return -1 - 5 * (threshold - mousePos) / threshold; | |
70 } | |
71 return 0; | |
72 }; | |
73 | |
74 var clientArea = this.viewport_.getClientArea(); | |
75 var dx = computeDelta(event.x, clientArea.width); | |
76 var dy = computeDelta(event.y, clientArea.height); | |
77 | |
78 if (dx !== 0 || dy !== 0) { | |
79 this.raiseEvent(remoting.BumpScroller.Events.bumpScrollStarted); | |
80 this.repeatScroll_(dx, dy, new Date().getTime()); | |
81 } | |
82 }; | |
83 | |
84 /** | |
85 * Scroll the view, and schedule a timer to do so again unless we've hit | |
86 * the edges of the screen. This timer is cancelled when the mouse moves. | |
87 * @param {number} dx | |
88 * @param {number} dy | |
89 * @param {number} expected The time at which we expect to be called. | |
90 */ | |
91 remoting.BumpScroller.prototype.repeatScroll_ = function(dx, dy, expected) { | |
92 /** @type {number} */ | |
93 var now = new Date().getTime(); | |
94 /** @type {number} */ | |
95 var timeout = 10; | |
96 var lateAdjustment = 1 + (now - expected) / timeout; | |
97 if (this.viewport_.scroll(lateAdjustment * dx, lateAdjustment * dy)) { | |
98 this.raiseEvent(remoting.BumpScroller.Events.bumpScrollStopped); | |
99 } else { | |
100 this.bumpScrollTimer_ = window.setTimeout( | |
101 this.repeatScroll_.bind(this, dx, dy, now + timeout), timeout); | |
102 } | |
103 }; | |
104 | |
105 }()); | |
OLD | NEW |