tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

zoom-keys.js (2267B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const ZOOM_PREF = "devtools.toolbox.zoomValue";
      8 const MIN_ZOOM = 0.5;
      9 const MAX_ZOOM = 2;
     10 
     11 const { LocalizationHelper } = require("resource://devtools/shared/l10n.js");
     12 const L10N = new LocalizationHelper(
     13  "devtools/client/locales/toolbox.properties"
     14 );
     15 
     16 /**
     17 * Register generic keys to control zoom level of the given document.
     18 * Used by both the toolboxes and the browser console.
     19 *
     20 * @param {DOMWindow}
     21 *        The window on which we should listent to key strokes and modify the zoom factor.
     22 * @param {KeyShortcuts}
     23 *        KeyShortcuts instance where the zoom keys should be added.
     24 */
     25 exports.register = function (window, shortcuts) {
     26  const bc = BrowsingContext.getFromWindow(window);
     27  let zoomValue = parseFloat(Services.prefs.getCharPref(ZOOM_PREF));
     28  const zoomIn = function (event) {
     29    setZoom(zoomValue + 0.1);
     30    event.preventDefault();
     31  };
     32 
     33  const zoomOut = function (event) {
     34    setZoom(zoomValue - 0.1);
     35    event.preventDefault();
     36  };
     37 
     38  const zoomReset = function (event) {
     39    setZoom(1);
     40    event.preventDefault();
     41  };
     42 
     43  const setZoom = function (newValue) {
     44    // cap zoom value
     45    zoomValue = Math.max(newValue, MIN_ZOOM);
     46    zoomValue = Math.min(zoomValue, MAX_ZOOM);
     47    // Prevent the floating-point error. (e.g. 1.1 + 0.1 = 1.2000000000000002)
     48    zoomValue = Math.round(zoomValue * 10) / 10;
     49 
     50    bc.fullZoom = zoomValue;
     51 
     52    Services.prefs.setCharPref(ZOOM_PREF, zoomValue);
     53  };
     54 
     55  // Set zoom to whatever the last setting was.
     56  setZoom(zoomValue);
     57 
     58  shortcuts.on(L10N.getStr("toolbox.zoomIn.key"), zoomIn);
     59  const zoomIn2 = L10N.getStr("toolbox.zoomIn2.key");
     60  if (zoomIn2) {
     61    shortcuts.on(zoomIn2, zoomIn);
     62  }
     63 
     64  shortcuts.on(L10N.getStr("toolbox.zoomOut.key"), zoomOut);
     65  const zoomOut2 = L10N.getStr("toolbox.zoomOut2.key");
     66  if (zoomOut2) {
     67    shortcuts.on(zoomOut2, zoomOut);
     68  }
     69 
     70  shortcuts.on(L10N.getStr("toolbox.zoomReset.key"), zoomReset);
     71  const zoomReset2 = L10N.getStr("toolbox.zoomReset2.key");
     72  if (zoomReset2) {
     73    shortcuts.on(zoomReset2, zoomReset);
     74  }
     75 };