1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /** Document Zoom Management Code
9 * To use this, you'll need to have a global gBrowser variable
10 * or use the methods that accept a browser to be modified.
13 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
18 return (this.MIN = Services.prefs.getIntPref("zoom.minPercent") / 100);
23 return (this.MAX = Services.prefs.getIntPref("zoom.maxPercent") / 100);
27 return Services.prefs.getBoolPref("browser.zoom.full");
30 set useFullZoom(aVal) {
31 Services.prefs.setBoolPref("browser.zoom.full", aVal);
36 return this.getZoomForBrowser(gBrowser);
39 useFullZoomForBrowser: function ZoomManager_useFullZoomForBrowser(aBrowser) {
40 return this.useFullZoom || aBrowser.isSyntheticDocument;
43 getFullZoomForBrowser: function ZoomManager_getFullZoomForBrowser(aBrowser) {
44 if (!this.useFullZoomForBrowser(aBrowser)) {
47 return this.getZoomForBrowser(aBrowser);
50 getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) {
51 let zoom = this.useFullZoomForBrowser(aBrowser)
54 // Round to remove any floating-point error.
55 return Number(zoom ? zoom.toFixed(2) : 1);
59 this.setZoomForBrowser(gBrowser, aVal);
63 setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) {
64 if (aVal < this.MIN || aVal > this.MAX) {
65 throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
68 if (this.useFullZoomForBrowser(aBrowser)) {
69 aBrowser.textZoom = 1;
70 aBrowser.fullZoom = aVal;
72 aBrowser.textZoom = aVal;
73 aBrowser.fullZoom = 1;
78 var zoomValues = Services.prefs
79 .getCharPref("toolkit.zoomManager.zoomValues")
82 zoomValues.sort((a, b) => a - b);
84 while (zoomValues[0] < this.MIN) {
88 while (zoomValues[zoomValues.length - 1] > this.MAX) {
92 delete this.zoomValues;
93 return (this.zoomValues = zoomValues);
96 enlarge: function ZoomManager_enlarge() {
97 var i = this.zoomValues.indexOf(this.snap(this.zoom)) + 1;
98 if (i < this.zoomValues.length) {
99 this.zoom = this.zoomValues[i];
103 reduce: function ZoomManager_reduce() {
104 var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1;
106 this.zoom = this.zoomValues[i];
110 reset: function ZoomManager_reset() {
114 toggleZoom: function ZoomManager_toggleZoom() {
115 var zoomLevel = this.zoom;
117 this.useFullZoom = !this.useFullZoom;
118 this.zoom = zoomLevel;
121 snap: function ZoomManager_snap(aVal) {
122 var values = this.zoomValues;
123 for (var i = 0; i < values.length; i++) {
124 if (values[i] >= aVal) {
125 if (i > 0 && aVal - values[i - 1] < values[i] - aVal) {
131 return values[i - 1];