|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function init() { |
|
6 enableAsyncScrolling(); |
|
7 messageManager.loadFrameScript( |
|
8 "chrome://global/content/test-ipcbrowser-content.js", true |
|
9 ); |
|
10 } |
|
11 |
|
12 function browser() { |
|
13 return document.getElementById("content"); |
|
14 } |
|
15 |
|
16 function frameLoader() { |
|
17 return browser().QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader; |
|
18 } |
|
19 |
|
20 function viewManager() { |
|
21 return frameLoader().QueryInterface(Components.interfaces.nsIContentViewManager); |
|
22 } |
|
23 |
|
24 function rootView() { |
|
25 return viewManager().rootContentView; |
|
26 } |
|
27 |
|
28 function enableAsyncScrolling() { |
|
29 frameLoader().renderMode = Components.interfaces.nsIFrameLoader.RENDER_MODE_ASYNC_SCROLL; |
|
30 } |
|
31 |
|
32 // Functions affecting the content window. |
|
33 |
|
34 function loadURL(url) { |
|
35 browser().setAttribute('src', url); |
|
36 } |
|
37 |
|
38 function scrollContentBy(dx, dy) { |
|
39 messageManager.broadcastAsyncMessage("scrollBy", |
|
40 { dx: dx, dy: dy }); |
|
41 |
|
42 } |
|
43 |
|
44 function scrollContentTo(x, y) { |
|
45 messageManager.broadcastAsyncMessage("scrollTo", |
|
46 { x: x, y: y }); |
|
47 } |
|
48 |
|
49 function setContentViewport(w, h) { |
|
50 messageManager.broadcastAsyncMessage("setViewport", |
|
51 { w: w, h: h }); |
|
52 } |
|
53 |
|
54 function setContentDisplayPort(x, y, w, h) { |
|
55 messageManager.broadcastAsyncMessage("setDisplayPort", |
|
56 { x: x, y: y, w: w, h: h }); |
|
57 } |
|
58 |
|
59 function setContentResolution(xres, yres) { |
|
60 messageManager.broadcastAsyncMessage("setResolution", |
|
61 { xres: xres, yres: yres }); |
|
62 } |
|
63 |
|
64 // Functions affecting <browser>. |
|
65 |
|
66 function scrollViewportBy(dx, dy) { |
|
67 rootView().scrollBy(dx, dy); |
|
68 } |
|
69 |
|
70 function scrollViewportTo(x, y) { |
|
71 rootView().scrollTo(x, y); |
|
72 } |
|
73 |
|
74 function setViewportScale(xs, ys) { |
|
75 rootView().setScale(xs, ys); |
|
76 } |
|
77 |
|
78 var kDelayMs = 100; |
|
79 var kDurationMs = 250; |
|
80 var scrolling = false; |
|
81 function startAnimatedScrollBy(dx, dy) { |
|
82 if (scrolling) |
|
83 throw "don't interrupt me!"; |
|
84 |
|
85 scrolling = true; |
|
86 |
|
87 var start = mozAnimationStartTime; |
|
88 var end = start + kDurationMs; |
|
89 // |- k| so that we do something in first invocation of nudge() |
|
90 var prevNow = start - 20; |
|
91 var accumDx = 0, accumDy = 0; |
|
92 |
|
93 var sentScrollBy = false; |
|
94 function nudgeScroll(now) { |
|
95 if (!scrolling) { |
|
96 // we've been canceled |
|
97 return; |
|
98 } |
|
99 var ddx = dx * (now - prevNow) / kDurationMs; |
|
100 var ddy = dy * (now - prevNow) / kDurationMs; |
|
101 |
|
102 ddx = Math.min(dx - accumDx, ddx); |
|
103 ddy = Math.min(dy - accumDy, ddy); |
|
104 accumDx += ddx; |
|
105 accumDy += ddy; |
|
106 |
|
107 rootView().scrollBy(ddx, ddy); |
|
108 |
|
109 if (!sentScrollBy && 100 <= (now - start)) { |
|
110 messageManager.broadcastAsyncMessage("scrollBy", |
|
111 { dx: dx, dy: dy }); |
|
112 sentScrollBy = true; |
|
113 } |
|
114 |
|
115 if (now >= end || (accumDx >= dx && accumDy >= dy)) { |
|
116 var fixupDx = Math.max(dx - accumDx, 0); |
|
117 var fixupDy = Math.max(dy - accumDy, 0); |
|
118 rootView().scrollBy(fixupDx, fixupDy); |
|
119 |
|
120 scrolling = false; |
|
121 } |
|
122 else { |
|
123 mozRequestAnimationFrame(nudgeScroll); |
|
124 } |
|
125 |
|
126 prevNow = now; |
|
127 } |
|
128 |
|
129 nudgeScroll(start); |
|
130 mozRequestAnimationFrame(nudgeScroll); |
|
131 } |