michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** Document Zoom Management Code michael@0: * michael@0: * To use this, you'll need to have a getBrowser() function or use the methods michael@0: * that accept a browser to be modified. michael@0: **/ michael@0: michael@0: var ZoomManager = { michael@0: get _prefBranch() { michael@0: delete this._prefBranch; michael@0: return this._prefBranch = Components.classes["@mozilla.org/preferences-service;1"] michael@0: .getService(Components.interfaces.nsIPrefBranch); michael@0: }, michael@0: michael@0: get MIN() { michael@0: delete this.MIN; michael@0: return this.MIN = this._prefBranch.getIntPref("zoom.minPercent") / 100; michael@0: }, michael@0: michael@0: get MAX() { michael@0: delete this.MAX; michael@0: return this.MAX = this._prefBranch.getIntPref("zoom.maxPercent") / 100; michael@0: }, michael@0: michael@0: get useFullZoom() { michael@0: return this._prefBranch.getBoolPref("browser.zoom.full"); michael@0: }, michael@0: michael@0: set useFullZoom(aVal) { michael@0: this._prefBranch.setBoolPref("browser.zoom.full", aVal); michael@0: return aVal; michael@0: }, michael@0: michael@0: get zoom() { michael@0: return this.getZoomForBrowser(getBrowser()); michael@0: }, michael@0: michael@0: getZoomForBrowser: function ZoomManager_getZoomForBrowser(aBrowser) { michael@0: return (this.useFullZoom || aBrowser.isSyntheticDocument) michael@0: ? aBrowser.fullZoom : aBrowser.textZoom; michael@0: }, michael@0: michael@0: set zoom(aVal) { michael@0: this.setZoomForBrowser(getBrowser(), aVal); michael@0: return aVal; michael@0: }, michael@0: michael@0: setZoomForBrowser: function ZoomManager_setZoomForBrowser(aBrowser, aVal) { michael@0: if (aVal < this.MIN || aVal > this.MAX) michael@0: throw Components.results.NS_ERROR_INVALID_ARG; michael@0: michael@0: if (this.useFullZoom || aBrowser.isSyntheticDocument) { michael@0: aBrowser.textZoom = 1; michael@0: aBrowser.fullZoom = aVal; michael@0: } else { michael@0: aBrowser.textZoom = aVal; michael@0: aBrowser.fullZoom = 1; michael@0: } michael@0: }, michael@0: michael@0: get zoomValues() { michael@0: var zoomValues = this._prefBranch.getCharPref("toolkit.zoomManager.zoomValues") michael@0: .split(",").map(parseFloat); michael@0: zoomValues.sort(function (a, b) a - b); michael@0: michael@0: while (zoomValues[0] < this.MIN) michael@0: zoomValues.shift(); michael@0: michael@0: while (zoomValues[zoomValues.length - 1] > this.MAX) michael@0: zoomValues.pop(); michael@0: michael@0: delete this.zoomValues; michael@0: return this.zoomValues = zoomValues; michael@0: }, michael@0: michael@0: enlarge: function ZoomManager_enlarge() { michael@0: var i = this.zoomValues.indexOf(this.snap(this.zoom)) + 1; michael@0: if (i < this.zoomValues.length) michael@0: this.zoom = this.zoomValues[i]; michael@0: }, michael@0: michael@0: reduce: function ZoomManager_reduce() { michael@0: var i = this.zoomValues.indexOf(this.snap(this.zoom)) - 1; michael@0: if (i >= 0) michael@0: this.zoom = this.zoomValues[i]; michael@0: }, michael@0: michael@0: reset: function ZoomManager_reset() { michael@0: this.zoom = 1; michael@0: }, michael@0: michael@0: toggleZoom: function ZoomManager_toggleZoom() { michael@0: var zoomLevel = this.zoom; michael@0: michael@0: this.useFullZoom = !this.useFullZoom; michael@0: this.zoom = zoomLevel; michael@0: }, michael@0: michael@0: snap: function ZoomManager_snap(aVal) { michael@0: var values = this.zoomValues; michael@0: for (var i = 0; i < values.length; i++) { michael@0: if (values[i] >= aVal) { michael@0: if (i > 0 && aVal - values[i - 1] < values[i] - aVal) michael@0: i--; michael@0: return values[i]; michael@0: } michael@0: } michael@0: return values[i - 1]; michael@0: } michael@0: };