layout/ipc/test-ipcbrowser-chrome.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     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/. */
     5 function init() {
     6     enableAsyncScrolling();
     7     messageManager.loadFrameScript(
     8         "chrome://global/content/test-ipcbrowser-content.js", true
     9     );
    10 }
    12 function browser() {
    13     return document.getElementById("content");
    14 }
    16 function frameLoader() {
    17     return browser().QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;
    18 }
    20 function viewManager() {
    21     return frameLoader().QueryInterface(Components.interfaces.nsIContentViewManager);
    22 }
    24 function rootView() {
    25     return viewManager().rootContentView;
    26 }
    28 function enableAsyncScrolling() {
    29     frameLoader().renderMode = Components.interfaces.nsIFrameLoader.RENDER_MODE_ASYNC_SCROLL;
    30 }
    32 // Functions affecting the content window.
    34 function loadURL(url) {
    35     browser().setAttribute('src', url);
    36 }
    38 function scrollContentBy(dx, dy) {
    39     messageManager.broadcastAsyncMessage("scrollBy",
    40                                          { dx: dx, dy: dy });
    42 }
    44 function scrollContentTo(x, y) {
    45     messageManager.broadcastAsyncMessage("scrollTo",
    46                                          { x: x, y: y });
    47 }
    49 function setContentViewport(w, h) {
    50     messageManager.broadcastAsyncMessage("setViewport",
    51                                          { w: w, h: h });
    52 }
    54 function setContentDisplayPort(x, y, w, h) {
    55     messageManager.broadcastAsyncMessage("setDisplayPort",
    56                                          { x: x, y: y, w: w, h: h });
    57 }
    59 function setContentResolution(xres, yres) {
    60     messageManager.broadcastAsyncMessage("setResolution",
    61                                          { xres: xres, yres: yres });
    62 }
    64 // Functions affecting <browser>.
    66 function scrollViewportBy(dx, dy) {
    67     rootView().scrollBy(dx, dy);
    68 }
    70 function scrollViewportTo(x, y) {
    71     rootView().scrollTo(x, y);
    72 }
    74 function setViewportScale(xs, ys) {
    75     rootView().setScale(xs, ys);
    76 }
    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!";
    85     scrolling = true;
    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;
    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;
   102         ddx = Math.min(dx - accumDx, ddx);
   103         ddy = Math.min(dy - accumDy, ddy);
   104         accumDx += ddx;
   105         accumDy += ddy;
   107         rootView().scrollBy(ddx, ddy);
   109         if (!sentScrollBy && 100 <= (now - start)) {
   110             messageManager.broadcastAsyncMessage("scrollBy",
   111                                                  { dx: dx, dy: dy });
   112             sentScrollBy = true;
   113         }
   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);
   120             scrolling = false;
   121         }
   122         else {
   123             mozRequestAnimationFrame(nudgeScroll);
   124         }
   126         prevNow = now;
   127     }
   129     nudgeScroll(start);
   130     mozRequestAnimationFrame(nudgeScroll);
   131 }

mercurial