summaryrefslogtreecommitdiffstats
path: root/devtools/client/devtools-experimental-prefs.js
blob: dfa006841015e49fa42b5e118ebdd536b8c0aa32 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* 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";

loader.lazyRequireGetter(
  this,
  "HTMLTooltip",
  "resource://devtools/client/shared/widgets/tooltip/HTMLTooltip.js",
  true
);

const PREFERENCES = [
  [
    "fission.autostart",
    "Enable fission in Firefox. When navigating between two domains, you " +
      "will switch between two distinct processes. And if an iframe is " +
      "hosted from another domain, it will run in another process",
  ],
  [
    "devtools.every-frame-target.enabled",
    "When enabled, targets will be created for all iframes, no matter if " +
      "they are remote or not, independently of Fission being enabled or not",
  ],
  [
    "fission.bfcacheInParent",
    "Enable bfcache navigation in parent process (requires Fission and involve " +
      "more top level target switching",
  ],
];

/**
 * Temporary module to show a Tooltip with the currently enabled preferences
 * relevant for DevTools ongoing architectural work (e.g. Fission, EFT, …).
 *
 * This module should be deleted once all experimental prefs are preffed on in Nightly.
 */
function showTooltip(toolbox) {
  if (!toolbox._experimentalPrefsTooltip) {
    toolbox._experimentalPrefsTooltip = new HTMLTooltip(toolbox.doc, {
      type: "doorhanger",
      useXulWrapper: true,
    });
    toolbox.once("destroy", () => toolbox._experimentalPrefsTooltip.destroy());
  }

  // Terrible hack to allow to toggle using the command button.
  if (toolbox._experimentalPrefsTooltip.preventShow) {
    return;
  }

  updateTooltipContent(toolbox);

  const commandId = "command-button-experimental-prefs";
  toolbox._experimentalPrefsTooltip.show(toolbox.doc.getElementById(commandId));

  // Follows a hack to be able to close the tooltip when clicking on the
  // command button. Otherwise it will flicker and reopen.
  toolbox._experimentalPrefsTooltip.preventShow = true;
  toolbox._experimentalPrefsTooltip.once("hidden", () => {
    toolbox.win.setTimeout(
      () => (toolbox._experimentalPrefsTooltip.preventShow = false),
      250
    );
  });
}
exports.showTooltip = showTooltip;
function updateTooltipContent(toolbox) {
  const container = toolbox.doc.createElement("div");

  /*
   *  This is the grid we want to have:
   *  +--------------------------------------------+---------------+
   *  | Header text                                | Reset button  |
   *  +------+-----------------------------+-------+---------------+
   *  | Icon | Preference name             | Value | Toggle button |
   *  +------+-----------------------------+-------+---------------+
   *  | Icon | Preference name             | Value | Toggle button |
   *  +------+-----------------------------+-------+---------------+
   */

  Object.assign(container.style, {
    display: "grid",
    gridTemplateColumns:
      "max-content minmax(300px, auto) max-content max-content",
    gridColumnGap: "8px",
    gridTemplateRows: `repeat(${PREFERENCES.length + 1}, auto)`,
    gridRowGap: "8px",
    padding: "12px",
    fontSize: "11px",
  });

  container.classList.add("theme-body");

  const headerContainer = toolbox.doc.createElement("header");
  /**
   * The grid layout of the header container is as follows:
   *
   *  +-------------------------+--------------+
   *  | Header text             | Reset button |
   *  +-------------------------+--------------+
   */

  Object.assign(headerContainer.style, {
    display: "grid",
    gridTemplateColumns: "subgrid",
    gridColumn: "1 / -1",
  });

  const header = toolbox.doc.createElement("h1");

  Object.assign(header.style, {
    gridColumn: "1 / -2",
    fontSize: "11px",
    margin: "0",
    padding: "0",
  });

  header.textContent = "DevTools Experimental preferences";

  const resetButton = toolbox.doc.createElement("button");
  resetButton.addEventListener("click", () => {
    for (const [name] of PREFERENCES) {
      Services.prefs.clearUserPref(name);
    }
    updateTooltipContent(toolbox);
  });
  resetButton.textContent = "reset all";

  headerContainer.append(header, resetButton);

  const prefList = toolbox.doc.createElement("ul");
  Object.assign(prefList.style, {
    display: "grid",
    gridTemplateColumns: "subgrid",
    gridTemplateRows: "subgrid",
    // Subgrid should span all grid columns
    gridColumn: "1 / -1",
    gridRow: "2 / -1",
    listStyle: "none",
    margin: "0",
    padding: "0",
  });

  for (const [name, desc] of PREFERENCES) {
    const prefEl = createPreferenceListItem(toolbox, name, desc);
    prefList.appendChild(prefEl);
  }

  container.append(headerContainer, prefList);

  toolbox._experimentalPrefsTooltip.panel.innerHTML = "";
  // There is a hardcoded 320px max width for doorhanger tooltips,
  // see Bug 1654020.
  toolbox._experimentalPrefsTooltip.panel.style.maxWidth = "unset";
  toolbox._experimentalPrefsTooltip.panel.appendChild(container);
}

function createPreferenceListItem(toolbox, name, desc) {
  const isPrefEnabled = Services.prefs.getBoolPref(name, false);

  const prefEl = toolbox.doc.createElement("li");

  /**
   * The grid layout of a preference line is as follows:
   *
   *  +------+-----------------------------+-------+---------------+
   *  | Icon | Preference name             | Value | Toggle button |
   *  +------+-----------------------------+-------+---------------+
   */

  Object.assign(prefEl.style, {
    margin: "0",
    lineHeight: "12px",
    display: "grid",
    alignItems: "center",
    gridTemplateColumns: "subgrid",
    gridColumn: "1 / -1",
  });

  prefEl.classList.toggle("theme-comment", !isPrefEnabled);

  // Icon
  const prefInfo = toolbox.doc.createElement("div");
  prefInfo.title = desc;

  Object.assign(prefInfo.style, {
    width: "12px",
    height: "12px",
  });

  prefInfo.classList.add("experimental-pref-icon");

  // Preference name
  const prefTitle = toolbox.doc.createElement("span");

  Object.assign(prefTitle.style, {
    userSelect: "text",
    fontWeight: isPrefEnabled ? "bold" : "normal",
  });

  prefTitle.textContent = name;

  // Value
  const prefValue = toolbox.doc.createElement("span");
  prefValue.textContent = isPrefEnabled;

  // Toggle Button
  const toggleButton = toolbox.doc.createElement("button");
  toggleButton.addEventListener("click", () => {
    Services.prefs.setBoolPref(name, !isPrefEnabled);
    updateTooltipContent(toolbox);
  });
  toggleButton.textContent = "toggle";

  prefEl.append(prefInfo, prefTitle, prefValue, toggleButton);
  return prefEl;
}

function isAnyPreferenceEnabled() {
  for (const [name] of PREFERENCES) {
    const isPrefEnabled = Services.prefs.getBoolPref(name, false);
    if (isPrefEnabled) {
      return true;
    }
  }
  return false;
}
exports.isAnyPreferenceEnabled = isAnyPreferenceEnabled;