toolkit/content/viewZoomOverlay.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/viewZoomOverlay.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,115 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/** Document Zoom Management Code
    1.11 + *
    1.12 + * To use this, you'll need to have a getBrowser() function or use the methods
    1.13 + * that accept a browser to be modified.
    1.14 + **/
    1.15 +
    1.16 +var ZoomManager = {
    1.17 +  get _prefBranch() {
    1.18 +    delete this._prefBranch;
    1.19 +    return this._prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
    1.20 +                                        .getService(Components.interfaces.nsIPrefBranch);
    1.21 +  },
    1.22 +
    1.23 +  get MIN() {
    1.24 +    delete this.MIN;
    1.25 +    return this.MIN = this._prefBranch.getIntPref("zoom.minPercent") / 100;
    1.26 +  },
    1.27 +
    1.28 +  get MAX() {
    1.29 +    delete this.MAX;
    1.30 +    return this.MAX = this._prefBranch.getIntPref("zoom.maxPercent") / 100;
    1.31 +  },
    1.32 +
    1.33 +  get useFullZoom() {
    1.34 +    return this._prefBranch.getBoolPref("browser.zoom.full");
    1.35 +  },
    1.36 +
    1.37 +  set useFullZoom(aVal) {
    1.38 +    this._prefBranch.setBoolPref("browser.zoom.full", aVal);
    1.39 +    return aVal;
    1.40 +  },
    1.41 +
    1.42 +  get zoom() {
    1.43 +    return this.getZoomForBrowser(getBrowser());
    1.44 +  },
    1.45 +
    1.46 +  getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) {
    1.47 +    return (this.useFullZoom || aBrowser.isSyntheticDocument)
    1.48 +           ? aBrowser.fullZoom : aBrowser.textZoom;
    1.49 +  },
    1.50 +
    1.51 +  set zoom(aVal) {
    1.52 +    this.setZoomForBrowser(getBrowser(), aVal);
    1.53 +    return aVal;
    1.54 +  },
    1.55 +
    1.56 +  setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) {
    1.57 +    if (aVal < this.MIN || aVal > this.MAX)
    1.58 +      throw Components.results.NS_ERROR_INVALID_ARG;
    1.59 +
    1.60 +    if (this.useFullZoom || aBrowser.isSyntheticDocument) {
    1.61 +      aBrowser.textZoom = 1;
    1.62 +      aBrowser.fullZoom = aVal;
    1.63 +    } else {
    1.64 +      aBrowser.textZoom = aVal;
    1.65 +      aBrowser.fullZoom = 1;
    1.66 +    }
    1.67 +  },
    1.68 +
    1.69 +  get zoomValues() {
    1.70 +    var zoomValues = this._prefBranch.getCharPref("toolkit.zoomManager.zoomValues")
    1.71 +                                     .split(",").map(parseFloat);
    1.72 +    zoomValues.sort(function (a, b) a - b);
    1.73 +
    1.74 +    while (zoomValues[0] < this.MIN)
    1.75 +      zoomValues.shift();
    1.76 +
    1.77 +    while (zoomValues[zoomValues.length - 1] > this.MAX)
    1.78 +      zoomValues.pop();
    1.79 +
    1.80 +    delete this.zoomValues;
    1.81 +    return this.zoomValues = zoomValues;
    1.82 +  },
    1.83 +
    1.84 +  enlarge: function ZoomManager_enlarge() {
    1.85 +    var i = this.zoomValues.indexOf(this.snap(this.zoom)) + 1;
    1.86 +    if (i < this.zoomValues.length)
    1.87 +      this.zoom = this.zoomValues[i];
    1.88 +  },
    1.89 +
    1.90 +  reduce: function ZoomManager_reduce() {
    1.91 +    var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1;
    1.92 +    if (i >= 0)
    1.93 +      this.zoom = this.zoomValues[i];
    1.94 +  },
    1.95 +
    1.96 +  reset: function ZoomManager_reset() {
    1.97 +    this.zoom = 1;
    1.98 +  },
    1.99 +
   1.100 +  toggleZoom: function ZoomManager_toggleZoom() {
   1.101 +    var zoomLevel = this.zoom;
   1.102 +
   1.103 +    this.useFullZoom = !this.useFullZoom;
   1.104 +    this.zoom = zoomLevel;
   1.105 +  },
   1.106 +
   1.107 +  snap: function ZoomManager_snap(aVal) {
   1.108 +    var values = this.zoomValues;
   1.109 +    for (var i = 0; i < values.length; i++) {
   1.110 +      if (values[i] >= aVal) {
   1.111 +        if (i > 0 && aVal - values[i - 1] < values[i] - aVal)
   1.112 +          i--;
   1.113 +        return values[i];
   1.114 +      }
   1.115 +    }
   1.116 +    return values[i - 1];
   1.117 +  }
   1.118 +};

mercurial