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 getBrowser() function or use the methods
10 * that accept a browser to be modified.
15 delete this._prefBranch;
16 return this._prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
17 .getService(Components.interfaces.nsIPrefBranch);
22 return this.MIN = this._prefBranch.getIntPref("zoom.minPercent") / 100;
27 return this.MAX = this._prefBranch.getIntPref("zoom.maxPercent") / 100;
31 return this._prefBranch.getBoolPref("browser.zoom.full");
34 set useFullZoom(aVal) {
35 this._prefBranch.setBoolPref("browser.zoom.full", aVal);
40 return this.getZoomForBrowser(getBrowser());
43 getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) {
44 return (this.useFullZoom || aBrowser.isSyntheticDocument)
45 ? aBrowser.fullZoom : aBrowser.textZoom;
49 this.setZoomForBrowser(getBrowser(), aVal);
53 setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) {
54 if (aVal < this.MIN || aVal > this.MAX)
55 throw Components.results.NS_ERROR_INVALID_ARG;
57 if (this.useFullZoom || aBrowser.isSyntheticDocument) {
58 aBrowser.textZoom = 1;
59 aBrowser.fullZoom = aVal;
61 aBrowser.textZoom = aVal;
62 aBrowser.fullZoom = 1;
67 var zoomValues = this._prefBranch.getCharPref("toolkit.zoomManager.zoomValues")
68 .split(",").map(parseFloat);
69 zoomValues.sort(function (a, b) a - b);
71 while (zoomValues[0] < this.MIN)
74 while (zoomValues[zoomValues.length - 1] > this.MAX)
77 delete this.zoomValues;
78 return this.zoomValues = zoomValues;
81 enlarge: function ZoomManager_enlarge() {
82 var i = this.zoomValues.indexOf(this.snap(this.zoom)) + 1;
83 if (i < this.zoomValues.length)
84 this.zoom = this.zoomValues[i];
87 reduce: function ZoomManager_reduce() {
88 var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1;
90 this.zoom = this.zoomValues[i];
93 reset: function ZoomManager_reset() {
97 toggleZoom: function ZoomManager_toggleZoom() {
98 var zoomLevel = this.zoom;
100 this.useFullZoom = !this.useFullZoom;
101 this.zoom = zoomLevel;
104 snap: function ZoomManager_snap(aVal) {
105 var values = this.zoomValues;
106 for (var i = 0; i < values.length; i++) {
107 if (values[i] >= aVal) {
108 if (i > 0 && aVal - values[i - 1] < values[i] - aVal)
113 return values[i - 1];