Bug 1669129 - [devtools] Enable devtools.overflow.debugging.enabled. r=jdescottes
[gecko.git] / toolkit / content / viewZoomOverlay.js
blob877651a7b15751e0957abb6bcd91c5a8d6c5bca4
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
8  *
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.
11  **/
13 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
15 var ZoomManager = {
16   get MIN() {
17     delete this.MIN;
18     return (this.MIN = Services.prefs.getIntPref("zoom.minPercent") / 100);
19   },
21   get MAX() {
22     delete this.MAX;
23     return (this.MAX = Services.prefs.getIntPref("zoom.maxPercent") / 100);
24   },
26   get useFullZoom() {
27     return Services.prefs.getBoolPref("browser.zoom.full");
28   },
30   set useFullZoom(aVal) {
31     Services.prefs.setBoolPref("browser.zoom.full", aVal);
32     return aVal;
33   },
35   get zoom() {
36     return this.getZoomForBrowser(gBrowser);
37   },
39   useFullZoomForBrowser: function ZoomManager_useFullZoomForBrowser(aBrowser) {
40     return this.useFullZoom || aBrowser.isSyntheticDocument;
41   },
43   getFullZoomForBrowser: function ZoomManager_getFullZoomForBrowser(aBrowser) {
44     if (!this.useFullZoomForBrowser(aBrowser)) {
45       return 1.0;
46     }
47     return this.getZoomForBrowser(aBrowser);
48   },
50   getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) {
51     let zoom = this.useFullZoomForBrowser(aBrowser)
52       ? aBrowser.fullZoom
53       : aBrowser.textZoom;
54     // Round to remove any floating-point error.
55     return Number(zoom ? zoom.toFixed(2) : 1);
56   },
58   set zoom(aVal) {
59     this.setZoomForBrowser(gBrowser, aVal);
60     return aVal;
61   },
63   setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) {
64     if (aVal < this.MIN || aVal > this.MAX) {
65       throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
66     }
68     if (this.useFullZoomForBrowser(aBrowser)) {
69       aBrowser.textZoom = 1;
70       aBrowser.fullZoom = aVal;
71     } else {
72       aBrowser.textZoom = aVal;
73       aBrowser.fullZoom = 1;
74     }
75   },
77   get zoomValues() {
78     var zoomValues = Services.prefs
79       .getCharPref("toolkit.zoomManager.zoomValues")
80       .split(",")
81       .map(parseFloat);
82     zoomValues.sort((a, b) => a - b);
84     while (zoomValues[0] < this.MIN) {
85       zoomValues.shift();
86     }
88     while (zoomValues[zoomValues.length - 1] > this.MAX) {
89       zoomValues.pop();
90     }
92     delete this.zoomValues;
93     return (this.zoomValues = zoomValues);
94   },
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];
100     }
101   },
103   reduce: function ZoomManager_reduce() {
104     var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1;
105     if (i >= 0) {
106       this.zoom = this.zoomValues[i];
107     }
108   },
110   reset: function ZoomManager_reset() {
111     this.zoom = 1;
112   },
114   toggleZoom: function ZoomManager_toggleZoom() {
115     var zoomLevel = this.zoom;
117     this.useFullZoom = !this.useFullZoom;
118     this.zoom = zoomLevel;
119   },
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) {
126           i--;
127         }
128         return values[i];
129       }
130     }
131     return values[i - 1];
132   },