|
1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
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/. */ |
|
6 |
|
7 /** Document Zoom Management Code |
|
8 * |
|
9 * To use this, you'll need to have a getBrowser() function or use the methods |
|
10 * that accept a browser to be modified. |
|
11 **/ |
|
12 |
|
13 var ZoomManager = { |
|
14 get _prefBranch() { |
|
15 delete this._prefBranch; |
|
16 return this._prefBranch = Components.classes["@mozilla.org/preferences-service;1"] |
|
17 .getService(Components.interfaces.nsIPrefBranch); |
|
18 }, |
|
19 |
|
20 get MIN() { |
|
21 delete this.MIN; |
|
22 return this.MIN = this._prefBranch.getIntPref("zoom.minPercent") / 100; |
|
23 }, |
|
24 |
|
25 get MAX() { |
|
26 delete this.MAX; |
|
27 return this.MAX = this._prefBranch.getIntPref("zoom.maxPercent") / 100; |
|
28 }, |
|
29 |
|
30 get useFullZoom() { |
|
31 return this._prefBranch.getBoolPref("browser.zoom.full"); |
|
32 }, |
|
33 |
|
34 set useFullZoom(aVal) { |
|
35 this._prefBranch.setBoolPref("browser.zoom.full", aVal); |
|
36 return aVal; |
|
37 }, |
|
38 |
|
39 get zoom() { |
|
40 return this.getZoomForBrowser(getBrowser()); |
|
41 }, |
|
42 |
|
43 getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) { |
|
44 return (this.useFullZoom || aBrowser.isSyntheticDocument) |
|
45 ? aBrowser.fullZoom : aBrowser.textZoom; |
|
46 }, |
|
47 |
|
48 set zoom(aVal) { |
|
49 this.setZoomForBrowser(getBrowser(), aVal); |
|
50 return aVal; |
|
51 }, |
|
52 |
|
53 setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) { |
|
54 if (aVal < this.MIN || aVal > this.MAX) |
|
55 throw Components.results.NS_ERROR_INVALID_ARG; |
|
56 |
|
57 if (this.useFullZoom || aBrowser.isSyntheticDocument) { |
|
58 aBrowser.textZoom = 1; |
|
59 aBrowser.fullZoom = aVal; |
|
60 } else { |
|
61 aBrowser.textZoom = aVal; |
|
62 aBrowser.fullZoom = 1; |
|
63 } |
|
64 }, |
|
65 |
|
66 get zoomValues() { |
|
67 var zoomValues = this._prefBranch.getCharPref("toolkit.zoomManager.zoomValues") |
|
68 .split(",").map(parseFloat); |
|
69 zoomValues.sort(function (a, b) a - b); |
|
70 |
|
71 while (zoomValues[0] < this.MIN) |
|
72 zoomValues.shift(); |
|
73 |
|
74 while (zoomValues[zoomValues.length - 1] > this.MAX) |
|
75 zoomValues.pop(); |
|
76 |
|
77 delete this.zoomValues; |
|
78 return this.zoomValues = zoomValues; |
|
79 }, |
|
80 |
|
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]; |
|
85 }, |
|
86 |
|
87 reduce: function ZoomManager_reduce() { |
|
88 var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1; |
|
89 if (i >= 0) |
|
90 this.zoom = this.zoomValues[i]; |
|
91 }, |
|
92 |
|
93 reset: function ZoomManager_reset() { |
|
94 this.zoom = 1; |
|
95 }, |
|
96 |
|
97 toggleZoom: function ZoomManager_toggleZoom() { |
|
98 var zoomLevel = this.zoom; |
|
99 |
|
100 this.useFullZoom = !this.useFullZoom; |
|
101 this.zoom = zoomLevel; |
|
102 }, |
|
103 |
|
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) |
|
109 i--; |
|
110 return values[i]; |
|
111 } |
|
112 } |
|
113 return values[i - 1]; |
|
114 } |
|
115 }; |