summaryrefslogtreecommitdiffstats
path: root/comm/mail/modules/UIDensity.jsm
blob: be94ed1408db6e35f925e623a8eafc924dad42bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* 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/. */

const EXPORTED_SYMBOLS = ["UIDensity"];

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);

var registeredWindows = new Set();

function updateWindow(win) {
  switch (UIDensity.prefValue) {
    case UIDensity.MODE_COMPACT:
      win.document.documentElement.setAttribute("uidensity", "compact");
      break;
    case UIDensity.MODE_TOUCH:
      win.document.documentElement.setAttribute("uidensity", "touch");
      break;
    default:
      win.document.documentElement.removeAttribute("uidensity");
      break;
  }

  if (win.TabsInTitlebar !== undefined) {
    win.TabsInTitlebar.update();
  }

  win.dispatchEvent(
    new win.CustomEvent("uidensitychange", { detail: UIDensity.prefValue })
  );
}

function updateAllWindows() {
  for (let win of registeredWindows) {
    updateWindow(win);
  }
}

var UIDensity = {
  MODE_COMPACT: 0,
  MODE_NORMAL: 1,
  MODE_TOUCH: 2,

  prefName: "mail.uidensity",

  /**
   * Set the UI density.
   *
   * @param {integer} mode - One of the MODE constants.
   */
  setMode(mode) {
    Services.prefs.setIntPref(this.prefName, mode);
  },

  /**
   * Register a window to be updated if the mode ever changes. The current
   * value is applied to the window. Deregistration is automatic.
   *
   * @param {Window} win
   */
  registerWindow(win) {
    registeredWindows.add(win);
    win.addEventListener("unload", () => registeredWindows.delete(win));
    updateWindow(win);
  },
};

XPCOMUtils.defineLazyPreferenceGetter(
  UIDensity,
  "prefValue",
  UIDensity.prefName,
  null,
  updateAllWindows
);