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