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.
14 set useFullZoom(aVal) {
15 Services.prefs.setBoolPref("browser.zoom.full", aVal);
19 return this.getZoomForBrowser(gBrowser);
22 useFullZoomForBrowser: function ZoomManager_useFullZoomForBrowser(aBrowser) {
23 return this.useFullZoom || aBrowser.isSyntheticDocument;
26 getFullZoomForBrowser: function ZoomManager_getFullZoomForBrowser(aBrowser) {
27 if (!this.useFullZoomForBrowser(aBrowser)) {
30 return this.getZoomForBrowser(aBrowser);
33 getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) {
34 let zoom = this.useFullZoomForBrowser(aBrowser)
37 // Round to remove any floating-point error.
38 return Number(zoom ? zoom.toFixed(2) : 1);
42 this.setZoomForBrowser(gBrowser, aVal);
45 setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) {
46 if (aVal < this.MIN || aVal > this.MAX) {
47 throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
50 if (this.useFullZoomForBrowser(aBrowser)) {
51 aBrowser.textZoom = 1;
52 aBrowser.fullZoom = aVal;
54 aBrowser.textZoom = aVal;
55 aBrowser.fullZoom = 1;
59 enlarge: function ZoomManager_enlarge() {
60 var i = this.zoomValues.indexOf(this.snap(this.zoom)) + 1;
61 if (i < this.zoomValues.length) {
62 this.zoom = this.zoomValues[i];
66 reduce: function ZoomManager_reduce() {
67 var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1;
69 this.zoom = this.zoomValues[i];
73 reset: function ZoomManager_reset() {
77 toggleZoom: function ZoomManager_toggleZoom() {
78 var zoomLevel = this.zoom;
80 this.useFullZoom = !this.useFullZoom;
81 this.zoom = zoomLevel;
84 snap: function ZoomManager_snap(aVal) {
85 var values = this.zoomValues;
86 for (var i = 0; i < values.length; i++) {
87 if (values[i] >= aVal) {
88 if (i > 0 && aVal - values[i - 1] < values[i] - aVal) {
98 XPCOMUtils.defineLazyPreferenceGetter(
106 XPCOMUtils.defineLazyPreferenceGetter(
115 XPCOMUtils.defineLazyPreferenceGetter(
118 "toolkit.zoomManager.zoomValues",
119 ".3,.5,.67,.8,.9,1,1.1,1.2,1.33,1.5,1.7,2,2.4,3,4,5",
122 zoomValues = zoomValues.split(",").map(parseFloat);
123 zoomValues.sort((a, b) => a - b);
125 while (zoomValues[0] < this.MIN) {
129 while (zoomValues[zoomValues.length - 1] > this.MAX) {
137 XPCOMUtils.defineLazyPreferenceGetter(