Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / toolkit / content / viewZoomOverlay.js
blob36142d3d51b1608581146608f06d75780e9196ce
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 ZoomManager = {
14   set useFullZoom(aVal) {
15     Services.prefs.setBoolPref("browser.zoom.full", aVal);
16   },
18   get zoom() {
19     return this.getZoomForBrowser(gBrowser);
20   },
22   useFullZoomForBrowser: function ZoomManager_useFullZoomForBrowser(aBrowser) {
23     return this.useFullZoom || aBrowser.isSyntheticDocument;
24   },
26   getFullZoomForBrowser: function ZoomManager_getFullZoomForBrowser(aBrowser) {
27     if (!this.useFullZoomForBrowser(aBrowser)) {
28       return 1.0;
29     }
30     return this.getZoomForBrowser(aBrowser);
31   },
33   getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) {
34     let zoom = this.useFullZoomForBrowser(aBrowser)
35       ? aBrowser.fullZoom
36       : aBrowser.textZoom;
37     // Round to remove any floating-point error.
38     return Number(zoom ? zoom.toFixed(2) : 1);
39   },
41   set zoom(aVal) {
42     this.setZoomForBrowser(gBrowser, aVal);
43   },
45   setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) {
46     if (aVal < this.MIN || aVal > this.MAX) {
47       throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
48     }
50     if (this.useFullZoomForBrowser(aBrowser)) {
51       aBrowser.textZoom = 1;
52       aBrowser.fullZoom = aVal;
53     } else {
54       aBrowser.textZoom = aVal;
55       aBrowser.fullZoom = 1;
56     }
57   },
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];
63     }
64   },
66   reduce: function ZoomManager_reduce() {
67     var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1;
68     if (i >= 0) {
69       this.zoom = this.zoomValues[i];
70     }
71   },
73   reset: function ZoomManager_reset() {
74     this.zoom = 1;
75   },
77   toggleZoom: function ZoomManager_toggleZoom() {
78     var zoomLevel = this.zoom;
80     this.useFullZoom = !this.useFullZoom;
81     this.zoom = zoomLevel;
82   },
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) {
89           i--;
90         }
91         return values[i];
92       }
93     }
94     return values[i - 1];
95   },
98 XPCOMUtils.defineLazyPreferenceGetter(
99   ZoomManager,
100   "MIN",
101   "zoom.minPercent",
102   30,
103   null,
104   v => v / 100
106 XPCOMUtils.defineLazyPreferenceGetter(
107   ZoomManager,
108   "MAX",
109   "zoom.maxPercent",
110   500,
111   null,
112   v => v / 100
115 XPCOMUtils.defineLazyPreferenceGetter(
116   ZoomManager,
117   "zoomValues",
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",
120   null,
121   zoomValues => {
122     zoomValues = zoomValues.split(",").map(parseFloat);
123     zoomValues.sort((a, b) => a - b);
125     while (zoomValues[0] < this.MIN) {
126       zoomValues.shift();
127     }
129     while (zoomValues[zoomValues.length - 1] > this.MAX) {
130       zoomValues.pop();
131     }
133     return zoomValues;
134   }
137 XPCOMUtils.defineLazyPreferenceGetter(
138   ZoomManager,
139   "useFullZoom",
140   "browser.zoom.full"