fune/devtools/client/shared/zoom-keys.js
Emilio Cobos Álvarez d36c96ab4c Bug 1612068 - Move zoom from the content viewer to the browsing context. r=nika
We need it to live in BrowsingContext instead of WindowContext, because
we need to preserve the zoom level across same-origin navigation.

It'd be nice if it only lived in the top BC, but that's not possible at
the moment because a lot of tests rely on zooming only iframes. Some of
them can be adjusted for scaling the top instead, but not sure it's
worth it's worth fixing them and moving the zoom to be top-only, as it'd
be a bunch of effort, and the complexity and overhead of propagating the
zoom is not so big.

The print-preview-specific code in nsContentViewer is from before we did
the document cloning setup, and it seems useless. I've tested print
preview scaling before and after my patch and both behave the same.

The rest is just various test changes to use the SpecialPowers APIs or
BrowsingContext as needed instead of directly poking at the content
viewer.

I named the pres context hook RecomputeBrowsingContextDependentData, as
more stuff should move there like overrideDPPX and other media emulation
shenanigans.

I also have some ideas to simplify or even remove ZoomChild and such,
but that's followup work.

Differential Revision: https://phabricator.services.mozilla.com/D71969
2020-04-22 19:32:52 +00:00

77 lines
2.2 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const Services = require("Services");
const ZOOM_PREF = "devtools.toolbox.zoomValue";
const MIN_ZOOM = 0.5;
const MAX_ZOOM = 2;
const { LocalizationHelper } = require("devtools/shared/l10n");
const L10N = new LocalizationHelper(
"devtools/client/locales/toolbox.properties"
);
/**
* Register generic keys to control zoom level of the given document.
* Used by both the toolboxes and the browser console.
*
* @param {DOMWindow}
* The window on which we should listent to key strokes and modify the zoom factor.
* @param {KeyShortcuts}
* KeyShortcuts instance where the zoom keys should be added.
*/
exports.register = function(window, shortcuts) {
const bc = BrowsingContext.getFromWindow(window);
let zoomValue = parseFloat(Services.prefs.getCharPref(ZOOM_PREF));
const zoomIn = function(event) {
setZoom(zoomValue + 0.1);
event.preventDefault();
};
const zoomOut = function(event) {
setZoom(zoomValue - 0.1);
event.preventDefault();
};
const zoomReset = function(event) {
setZoom(1);
event.preventDefault();
};
const setZoom = function(newValue) {
// cap zoom value
zoomValue = Math.max(newValue, MIN_ZOOM);
zoomValue = Math.min(zoomValue, MAX_ZOOM);
// Prevent the floating-point error. (e.g. 1.1 + 0.1 = 1.2000000000000002)
zoomValue = Math.round(zoomValue * 10) / 10;
bc.fullZoom = zoomValue;
Services.prefs.setCharPref(ZOOM_PREF, zoomValue);
};
// Set zoom to whatever the last setting was.
setZoom(zoomValue);
shortcuts.on(L10N.getStr("toolbox.zoomIn.key"), zoomIn);
const zoomIn2 = L10N.getStr("toolbox.zoomIn2.key");
if (zoomIn2) {
shortcuts.on(zoomIn2, zoomIn);
}
shortcuts.on(L10N.getStr("toolbox.zoomOut.key"), zoomOut);
const zoomOut2 = L10N.getStr("toolbox.zoomOut2.key");
if (zoomOut2) {
shortcuts.on(zoomOut2, zoomOut);
}
shortcuts.on(L10N.getStr("toolbox.zoomReset.key"), zoomReset);
const zoomReset2 = L10N.getStr("toolbox.zoomReset2.key");
if (zoomReset2) {
shortcuts.on(zoomReset2, zoomReset);
}
};