2007-06-19 18:58:14 +00:00
|
|
|
/*
|
|
|
|
#ifdef 0
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Content Preferences (cpref).
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Myk Melez <myk@mozilla.org>
|
2007-10-25 23:02:20 +00:00
|
|
|
* Dão Gottwald <dao@mozilla.com>
|
2009-01-23 09:12:51 +00:00
|
|
|
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
|
2007-06-19 18:58:14 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK *****
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
|
2007-10-25 23:02:20 +00:00
|
|
|
// One of the possible values for the mousewheel.* preferences.
|
|
|
|
// From nsEventStateManager.cpp.
|
2008-02-13 11:00:45 +00:00
|
|
|
const MOUSE_SCROLL_ZOOM = 3;
|
2007-06-19 18:58:14 +00:00
|
|
|
|
|
|
|
/**
|
2007-10-25 23:02:20 +00:00
|
|
|
* Controls the "full zoom" setting and its site-specific preferences.
|
2007-06-19 18:58:14 +00:00
|
|
|
*/
|
2007-10-25 23:02:20 +00:00
|
|
|
var FullZoom = {
|
2007-06-19 18:58:14 +00:00
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Name & Values
|
|
|
|
|
|
|
|
// The name of the setting. Identifies the setting in the prefs database.
|
2007-10-25 23:02:20 +00:00
|
|
|
name: "browser.content.full-zoom",
|
2007-06-19 18:58:14 +00:00
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
// The global value (if any) for the setting. Lazily loaded from the service
|
|
|
|
// when first requested, then updated by the pref change listener as it changes.
|
2007-06-19 18:58:14 +00:00
|
|
|
// If there is no global value, then this should be undefined.
|
2007-11-26 08:35:22 +00:00
|
|
|
get globalValue FullZoom_get_globalValue() {
|
|
|
|
var globalValue = this._cps.getPref(null, this.name);
|
|
|
|
if (typeof globalValue != "undefined")
|
|
|
|
globalValue = this._ensureValid(globalValue);
|
|
|
|
delete this.globalValue;
|
|
|
|
return this.globalValue = globalValue;
|
|
|
|
},
|
2007-06-19 18:58:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Convenience Getters
|
|
|
|
|
|
|
|
// Content Pref Service
|
2007-11-26 08:35:22 +00:00
|
|
|
get _cps FullZoom_get__cps() {
|
2007-10-25 23:02:20 +00:00
|
|
|
delete this._cps;
|
|
|
|
return this._cps = Cc["@mozilla.org/content-pref/service;1"].
|
|
|
|
getService(Ci.nsIContentPrefService);
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
2008-02-26 23:05:53 +00:00
|
|
|
get _prefBranch FullZoom_get__prefBranch() {
|
|
|
|
delete this._prefBranch;
|
|
|
|
return this._prefBranch = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefBranch2);
|
|
|
|
},
|
|
|
|
|
|
|
|
// browser.zoom.siteSpecific preference cache
|
2009-01-23 09:12:51 +00:00
|
|
|
_siteSpecificPref: undefined,
|
2008-02-26 23:05:53 +00:00
|
|
|
|
2009-03-01 11:22:44 +00:00
|
|
|
// browser.zoom.updateBackgroundTabs preference cache
|
|
|
|
updateBackgroundTabs: undefined,
|
|
|
|
|
2009-01-23 09:12:51 +00:00
|
|
|
// whether we are in private browsing mode
|
|
|
|
_inPrivateBrowsing: false,
|
|
|
|
|
|
|
|
get siteSpecific FullZoom_get_siteSpecific() {
|
|
|
|
return !this._inPrivateBrowsing && this._siteSpecificPref;
|
|
|
|
},
|
2007-06-19 18:58:14 +00:00
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// nsISupports
|
|
|
|
|
|
|
|
// We can't use the Ci shortcut here because it isn't defined yet.
|
|
|
|
interfaces: [Components.interfaces.nsIDOMEventListener,
|
2008-02-26 23:05:53 +00:00
|
|
|
Components.interfaces.nsIObserver,
|
2007-06-19 18:58:14 +00:00
|
|
|
Components.interfaces.nsIContentPrefObserver,
|
2008-02-26 23:05:53 +00:00
|
|
|
Components.interfaces.nsISupportsWeakReference,
|
2007-06-19 18:58:14 +00:00
|
|
|
Components.interfaces.nsISupports],
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
QueryInterface: function FullZoom_QueryInterface(aIID) {
|
2007-10-25 23:02:20 +00:00
|
|
|
if (!this.interfaces.some(function (v) aIID.equals(v)))
|
2007-06-19 18:58:14 +00:00
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Initialization & Destruction
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
init: function FullZoom_init() {
|
2007-06-19 18:58:14 +00:00
|
|
|
// Listen for scrollwheel events so we can save scrollwheel-based changes.
|
|
|
|
window.addEventListener("DOMMouseScroll", this, false);
|
|
|
|
|
|
|
|
// Register ourselves with the service so we know when our pref changes.
|
|
|
|
this._cps.addObserver(this.name, this);
|
2008-02-26 23:05:53 +00:00
|
|
|
|
2009-01-23 09:12:51 +00:00
|
|
|
// We disable site-specific preferences in Private Browsing mode, because the
|
|
|
|
// content preferences module is disabled
|
|
|
|
let os = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
os.addObserver(this, "private-browsing", true);
|
|
|
|
|
|
|
|
// Retrieve the initial status of the Private Browsing mode.
|
|
|
|
this._inPrivateBrowsing = Cc["@mozilla.org/privatebrowsing;1"].
|
|
|
|
getService(Ci.nsIPrivateBrowsingService).
|
|
|
|
privateBrowsingEnabled;
|
|
|
|
|
|
|
|
this._siteSpecificPref =
|
2008-02-26 23:05:53 +00:00
|
|
|
this._prefBranch.getBoolPref("browser.zoom.siteSpecific");
|
2009-03-01 11:22:44 +00:00
|
|
|
this.updateBackgroundTabs =
|
|
|
|
this._prefBranch.getBoolPref("browser.zoom.updateBackgroundTabs");
|
|
|
|
// Listen for changes to the browser.zoom branch so we can enable/disable
|
|
|
|
// updating background tabs and per-site saving and restoring of zoom levels.
|
|
|
|
this._prefBranch.addObserver("browser.zoom.", this, true);
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
destroy: function FullZoom_destroy() {
|
2009-01-23 09:12:51 +00:00
|
|
|
let os = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
os.removeObserver(this, "private-browsing");
|
2009-03-01 11:22:44 +00:00
|
|
|
this._prefBranch.removeObserver("browser.zoom.", this);
|
2007-06-19 18:58:14 +00:00
|
|
|
this._cps.removeObserver(this.name, this);
|
|
|
|
window.removeEventListener("DOMMouseScroll", this, false);
|
2007-11-26 08:35:22 +00:00
|
|
|
delete this._cps;
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Event Handlers
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
handleEvent: function FullZoom_handleEvent(event) {
|
2007-10-25 23:02:20 +00:00
|
|
|
switch (event.type) {
|
|
|
|
case "DOMMouseScroll":
|
|
|
|
this._handleMouseScrolled(event);
|
|
|
|
break;
|
|
|
|
}
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
_handleMouseScrolled: function FullZoom__handleMouseScrolled(event) {
|
2007-06-19 18:58:14 +00:00
|
|
|
// Construct the "mousewheel action" pref key corresponding to this event.
|
|
|
|
// Based on nsEventStateManager::GetBasePrefKeyForMouseWheel.
|
|
|
|
var pref = "mousewheel";
|
2008-08-13 03:08:59 +00:00
|
|
|
if (event.axis == event.HORIZONTAL_AXIS)
|
2007-06-19 18:58:14 +00:00
|
|
|
pref += ".horizscroll";
|
|
|
|
|
|
|
|
if (event.shiftKey)
|
|
|
|
pref += ".withshiftkey";
|
|
|
|
else if (event.ctrlKey)
|
|
|
|
pref += ".withcontrolkey";
|
|
|
|
else if (event.altKey)
|
|
|
|
pref += ".withaltkey";
|
|
|
|
else if (event.metaKey)
|
|
|
|
pref += ".withmetakey";
|
|
|
|
else
|
|
|
|
pref += ".withnokey";
|
|
|
|
|
|
|
|
pref += ".action";
|
|
|
|
|
2007-10-25 23:02:20 +00:00
|
|
|
// Don't do anything if this isn't a "zoom" scroll event.
|
|
|
|
var isZoomEvent = false;
|
|
|
|
try {
|
2008-02-13 11:00:45 +00:00
|
|
|
isZoomEvent = (gPrefService.getIntPref(pref) == MOUSE_SCROLL_ZOOM);
|
2007-10-25 23:02:20 +00:00
|
|
|
} catch (e) {}
|
|
|
|
if (!isZoomEvent)
|
2007-06-19 18:58:14 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// XXX Lazily cache all the possible action prefs so we don't have to get
|
|
|
|
// them anew from the pref service for every scroll event? We'd have to
|
|
|
|
// make sure to observe them so we can update the cache when they change.
|
|
|
|
|
|
|
|
// We have to call _applySettingToPref in a timeout because we handle
|
|
|
|
// the event before the event state manager has a chance to apply the zoom
|
|
|
|
// during nsEventStateManager::PostHandleEvent.
|
2007-10-25 23:02:20 +00:00
|
|
|
window.setTimeout(function (self) { self._applySettingToPref() }, 0, this);
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
2008-02-26 23:05:53 +00:00
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
observe: function (aSubject, aTopic, aData) {
|
|
|
|
switch(aTopic) {
|
|
|
|
case "nsPref:changed":
|
|
|
|
switch(aData) {
|
|
|
|
case "browser.zoom.siteSpecific":
|
2009-01-23 09:12:51 +00:00
|
|
|
this._siteSpecificPref =
|
2008-02-26 23:05:53 +00:00
|
|
|
this._prefBranch.getBoolPref("browser.zoom.siteSpecific");
|
|
|
|
break;
|
2009-03-01 11:22:44 +00:00
|
|
|
case "browser.zoom.updateBackgroundTabs":
|
|
|
|
this.updateBackgroundTabs =
|
|
|
|
this._prefBranch.getBoolPref("browser.zoom.updateBackgroundTabs");
|
|
|
|
break;
|
2008-02-26 23:05:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-01-23 09:12:51 +00:00
|
|
|
case "private-browsing":
|
|
|
|
switch (aData) {
|
|
|
|
case "enter":
|
|
|
|
this._inPrivateBrowsing = true;
|
|
|
|
break;
|
|
|
|
case "exit":
|
|
|
|
this._inPrivateBrowsing = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2008-02-26 23:05:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-06-19 18:58:14 +00:00
|
|
|
// nsIContentPrefObserver
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
onContentPrefSet: function FullZoom_onContentPrefSet(aGroup, aName, aValue) {
|
2007-06-19 18:58:14 +00:00
|
|
|
if (aGroup == this._cps.grouper.group(gBrowser.currentURI))
|
|
|
|
this._applyPrefToSetting(aValue);
|
|
|
|
else if (aGroup == null) {
|
|
|
|
this.globalValue = this._ensureValid(aValue);
|
|
|
|
|
|
|
|
// If the current page doesn't have a site-specific preference,
|
|
|
|
// then its zoom should be set to the new global preference now that
|
|
|
|
// the global preference has changed.
|
|
|
|
if (!this._cps.hasPref(gBrowser.currentURI, this.name))
|
|
|
|
this._applyPrefToSetting();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
onContentPrefRemoved: function FullZoom_onContentPrefRemoved(aGroup, aName) {
|
2007-06-19 18:58:14 +00:00
|
|
|
if (aGroup == this._cps.grouper.group(gBrowser.currentURI))
|
|
|
|
this._applyPrefToSetting();
|
|
|
|
else if (aGroup == null) {
|
|
|
|
this.globalValue = undefined;
|
|
|
|
|
|
|
|
// If the current page doesn't have a site-specific preference,
|
|
|
|
// then its zoom should be set to the default preference now that
|
|
|
|
// the global preference has changed.
|
|
|
|
if (!this._cps.hasPref(gBrowser.currentURI, this.name))
|
|
|
|
this._applyPrefToSetting();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
// location change observer
|
2007-06-19 18:58:14 +00:00
|
|
|
|
2009-01-12 20:15:27 +00:00
|
|
|
onLocationChange: function FullZoom_onLocationChange(aURI, aBrowser) {
|
2007-11-26 08:35:22 +00:00
|
|
|
if (!aURI)
|
|
|
|
return;
|
2009-01-12 20:15:27 +00:00
|
|
|
this._applyPrefToSetting(this._cps.getPref(aURI, this.name), aBrowser);
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
2008-02-13 11:00:45 +00:00
|
|
|
// update state of zoom type menu item
|
|
|
|
|
|
|
|
updateMenu: function FullZoom_updateMenu() {
|
|
|
|
var menuItem = document.getElementById("toggle_zoom");
|
|
|
|
|
|
|
|
menuItem.setAttribute("checked", !ZoomManager.useFullZoom);
|
|
|
|
},
|
2007-06-19 18:58:14 +00:00
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Setting & Pref Manipulation
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
reduce: function FullZoom_reduce() {
|
2007-10-25 23:02:20 +00:00
|
|
|
ZoomManager.reduce();
|
2007-06-19 18:58:14 +00:00
|
|
|
this._applySettingToPref();
|
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
enlarge: function FullZoom_enlarge() {
|
2007-10-25 23:02:20 +00:00
|
|
|
ZoomManager.enlarge();
|
2007-06-19 18:58:14 +00:00
|
|
|
this._applySettingToPref();
|
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
reset: function FullZoom_reset() {
|
2007-06-19 18:58:14 +00:00
|
|
|
if (typeof this.globalValue != "undefined")
|
2008-02-13 11:00:45 +00:00
|
|
|
ZoomManager.zoom = this.globalValue;
|
2007-06-19 18:58:14 +00:00
|
|
|
else
|
2007-10-25 23:02:20 +00:00
|
|
|
ZoomManager.reset();
|
2007-06-19 18:58:14 +00:00
|
|
|
|
|
|
|
this._removePref();
|
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
setSettingValue: function FullZoom_setSettingValue() {
|
2007-11-09 10:19:12 +00:00
|
|
|
var value = this._cps.getPref(gBrowser.currentURI, this.name);
|
|
|
|
this._applyPrefToSetting(value);
|
|
|
|
},
|
|
|
|
|
2007-06-19 18:58:14 +00:00
|
|
|
/**
|
2007-10-25 23:02:20 +00:00
|
|
|
* Set the zoom level for the current tab.
|
2007-06-19 18:58:14 +00:00
|
|
|
*
|
2007-11-26 08:35:22 +00:00
|
|
|
* Per nsPresContext::setFullZoom, we can set the zoom to its current value
|
|
|
|
* without significant impact on performance, as the setting is only applied
|
|
|
|
* if it differs from the current setting. In fact getting the zoom and then
|
|
|
|
* checking ourselves if it differs costs more.
|
|
|
|
*
|
|
|
|
* And perhaps we should always set the zoom even if it was more expensive,
|
|
|
|
* since DocumentViewerImpl::SetTextZoom claims that child documents can have
|
|
|
|
* a different text zoom (although it would be unusual), and it implies that
|
|
|
|
* those child text zooms should get updated when the parent zoom gets set,
|
|
|
|
* and perhaps the same is true for full zoom
|
|
|
|
* (although DocumentViewerImpl::SetFullZoom doesn't mention it).
|
2007-06-19 18:58:14 +00:00
|
|
|
*
|
|
|
|
* So when we apply new zoom values to the browser, we simply set the zoom.
|
|
|
|
* We don't check first to see if the new value is the same as the current
|
|
|
|
* one.
|
|
|
|
**/
|
2009-01-12 20:15:27 +00:00
|
|
|
_applyPrefToSetting: function FullZoom__applyPrefToSetting(aValue, aBrowser) {
|
2008-12-08 12:47:42 +00:00
|
|
|
if (!this.siteSpecific || gInPrintPreviewMode ||
|
|
|
|
content.document instanceof Ci.nsIImageDocument)
|
2007-11-09 10:19:12 +00:00
|
|
|
return;
|
|
|
|
|
2007-06-19 18:58:14 +00:00
|
|
|
try {
|
|
|
|
if (typeof aValue != "undefined")
|
2009-01-12 20:15:27 +00:00
|
|
|
ZoomManager.setZoomForBrowser(aBrowser || gBrowser, this._ensureValid(aValue));
|
2007-06-19 18:58:14 +00:00
|
|
|
else if (typeof this.globalValue != "undefined")
|
2009-01-12 20:15:27 +00:00
|
|
|
ZoomManager.setZoomForBrowser(aBrowser || gBrowser, this.globalValue);
|
2007-06-19 18:58:14 +00:00
|
|
|
else
|
2009-01-12 20:15:27 +00:00
|
|
|
ZoomManager.setZoomForBrowser(aBrowser || gBrowser, 1);
|
2007-06-19 18:58:14 +00:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
_applySettingToPref: function FullZoom__applySettingToPref() {
|
2008-12-08 12:47:42 +00:00
|
|
|
if (!this.siteSpecific || gInPrintPreviewMode ||
|
|
|
|
content.document instanceof Ci.nsIImageDocument)
|
2007-11-09 10:19:12 +00:00
|
|
|
return;
|
|
|
|
|
2008-02-13 11:00:45 +00:00
|
|
|
var zoomLevel = ZoomManager.zoom;
|
|
|
|
this._cps.setPref(gBrowser.currentURI, this.name, zoomLevel);
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
_removePref: function FullZoom__removePref() {
|
2008-12-08 12:47:42 +00:00
|
|
|
if (!(content.document instanceof Ci.nsIImageDocument))
|
|
|
|
this._cps.removePref(gBrowser.currentURI, this.name);
|
2007-06-19 18:58:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
//**************************************************************************//
|
|
|
|
// Utilities
|
|
|
|
|
2007-11-26 08:35:22 +00:00
|
|
|
_ensureValid: function FullZoom__ensureValid(aValue) {
|
2007-06-19 18:58:14 +00:00
|
|
|
if (isNaN(aValue))
|
2007-10-25 23:02:20 +00:00
|
|
|
return 1;
|
2007-06-19 18:58:14 +00:00
|
|
|
|
2007-10-25 23:02:20 +00:00
|
|
|
if (aValue < ZoomManager.MIN)
|
|
|
|
return ZoomManager.MIN;
|
2007-06-19 18:58:14 +00:00
|
|
|
|
2007-10-25 23:02:20 +00:00
|
|
|
if (aValue > ZoomManager.MAX)
|
|
|
|
return ZoomManager.MAX;
|
2007-06-19 18:58:14 +00:00
|
|
|
|
|
|
|
return aValue;
|
|
|
|
}
|
|
|
|
};
|