summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/components/MeatballMenu.js
blob: 74d8592723e5b3f03fdef54dd5eeb33980ab978e (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* 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 SPLITCONSOLE_ENABLED_PREF = "devtools.toolbox.splitconsole.enabled";

const {
  PureComponent,
  createFactory,
} = require("resource://devtools/client/shared/vendor/react.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const { hr } = dom;

loader.lazyGetter(this, "MenuItem", function () {
  return createFactory(
    require("resource://devtools/client/shared/components/menu/MenuItem.js")
  );
});
loader.lazyGetter(this, "MenuList", function () {
  return createFactory(
    require("resource://devtools/client/shared/components/menu/MenuList.js")
  );
});

loader.lazyRequireGetter(
  this,
  "openDocLink",
  "resource://devtools/client/shared/link.js",
  true
);
loader.lazyRequireGetter(
  this,
  "assert",
  "resource://devtools/shared/DevToolsUtils.js",
  true
);

const openDevToolsDocsLink = () => {
  openDocLink("https://firefox-source-docs.mozilla.org/devtools-user/");
};

const openCommunityLink = () => {
  openDocLink(
    "https://discourse.mozilla.org/c/devtools?utm_source=devtools&utm_medium=tabbar-menu"
  );
};

class MeatballMenu extends PureComponent {
  static get propTypes() {
    return {
      // The id of the currently selected tool, e.g. "inspector"
      currentToolId: PropTypes.string,

      // List of possible docking options.
      hostTypes: PropTypes.arrayOf(
        PropTypes.shape({
          position: PropTypes.string.isRequired,
          switchHost: PropTypes.func.isRequired,
        })
      ),

      // Current docking type. Typically one of the position values in
      // |hostTypes| but this is not always the case (e.g. for "browsertoolbox").
      currentHostType: PropTypes.string,

      // Is the split console currently visible?
      isSplitConsoleActive: PropTypes.bool,

      // Are we disabling the behavior where pop-ups are automatically closed
      // when clicking outside them?
      //
      // This is a tri-state value that may be true/false or undefined where
      // undefined means that the option is not relevant in this context
      // (i.e. we're not in a browser toolbox).
      disableAutohide: PropTypes.bool,

      // Apply a pseudo-locale to the Firefox UI. This is only available in the browser
      // toolbox. This value can be undefined, "accented", "bidi", "none".
      pseudoLocale: PropTypes.string,

      // Function to turn the options panel on / off.
      toggleOptions: PropTypes.func.isRequired,

      // Function to turn the split console on / off.
      toggleSplitConsole: PropTypes.func,

      // Function to turn the disable pop-up autohide behavior on / off.
      toggleNoAutohide: PropTypes.func,

      // Manage the pseudo-localization for the Firefox UI.
      // https://firefox-source-docs.mozilla.org/l10n/fluent/tutorial.html#manually-testing-ui-with-pseudolocalization
      disablePseudoLocale: PropTypes.func,
      enableAccentedPseudoLocale: PropTypes.func,
      enableBidiPseudoLocale: PropTypes.func,

      // Bug 1709191 - The help shortcut key is localized without Fluent, and still needs
      // to be migrated. This is the only remaining use of the legacy L10N object.
      // Everything else should prefer the Fluent API.
      L10N: PropTypes.object.isRequired,

      // Callback function that will be invoked any time the component contents
      // update in such a way that its bounding box might change.
      onResize: PropTypes.func,
    };
  }

  componentDidUpdate(prevProps) {
    if (!this.props.onResize) {
      return;
    }

    // We are only expecting the following kinds of dynamic changes when a popup
    // is showing:
    //
    // - The "Disable pop-up autohide" menu item being added after the Browser
    //   Toolbox is connected.
    // - The pseudo locale options being added after the Browser Toolbox is connected.
    // - The split console label changing between "Show Split Console" and "Hide
    //   Split Console".
    // - The "Show/Hide Split Console" entry being added removed or removed.
    //
    // The latter two cases are only likely to be noticed when "Disable pop-up
    // autohide" is active, but for completeness we handle them here.
    const didChange =
      typeof this.props.disableAutohide !== typeof prevProps.disableAutohide ||
      this.props.pseudoLocale !== prevProps.pseudoLocale ||
      this.props.currentToolId !== prevProps.currentToolId ||
      this.props.isSplitConsoleActive !== prevProps.isSplitConsoleActive;

    if (didChange) {
      this.props.onResize();
    }
  }

  render() {
    const items = [];

    // Dock options
    for (const hostType of this.props.hostTypes) {
      // This is more verbose than it needs to be but lets us easily search for
      // l10n entities.
      let l10nID;
      switch (hostType.position) {
        case "window":
          l10nID = "toolbox-meatball-menu-dock-separate-window-label";
          break;

        case "bottom":
          l10nID = "toolbox-meatball-menu-dock-bottom-label";
          break;

        case "left":
          l10nID = "toolbox-meatball-menu-dock-left-label";
          break;

        case "right":
          l10nID = "toolbox-meatball-menu-dock-right-label";
          break;

        default:
          assert(false, `Unexpected hostType.position: ${hostType.position}`);
          break;
      }

      items.push(
        MenuItem({
          id: `toolbox-meatball-menu-dock-${hostType.position}`,
          key: `dock-${hostType.position}`,
          l10nID,
          onClick: hostType.switchHost,
          checked: hostType.position === this.props.currentHostType,
          className: "iconic",
        })
      );
    }

    if (items.length) {
      items.push(hr({ key: "dock-separator" }));
    }

    // Split console
    if (this.props.currentToolId !== "webconsole") {
      const isSplitConsoleEnabled = Services.prefs.getBoolPref(
        SPLITCONSOLE_ENABLED_PREF,
        true
      );

      if (isSplitConsoleEnabled) {
        const l10nID = this.props.isSplitConsoleActive
          ? "toolbox-meatball-menu-hideconsole-label"
          : "toolbox-meatball-menu-splitconsole-label";

        items.push(
          MenuItem({
            id: "toolbox-meatball-menu-splitconsole",
            key: "splitconsole",
            l10nID,
            accelerator: "Esc",
            onClick: this.props.toggleSplitConsole,
            className: "iconic",
          })
        );
      }
    }

    // Settings
    items.push(
      MenuItem({
        id: "toolbox-meatball-menu-settings",
        key: "settings",
        l10nID: "toolbox-meatball-menu-settings-label",
        // Bug 1709191 - The help key is localized without Fluent, and still needs to
        // be migrated.
        accelerator: this.props.L10N.getStr("toolbox.help.key"),
        onClick: this.props.toggleOptions,
        className: "iconic",
      })
    );

    if (
      typeof this.props.disableAutohide !== "undefined" ||
      typeof this.props.pseudoLocale !== "undefined"
    ) {
      items.push(hr({ key: "docs-separator-1" }));
    }

    // Disable pop-up autohide
    //
    // If |disableAutohide| is undefined, it means this feature is not available
    // in this context.
    if (typeof this.props.disableAutohide !== "undefined") {
      items.push(
        MenuItem({
          id: "toolbox-meatball-menu-noautohide",
          key: "noautohide",
          l10nID: "toolbox-meatball-menu-noautohide-label",
          type: "checkbox",
          checked: this.props.disableAutohide,
          onClick: this.props.toggleNoAutohide,
          className: "iconic",
        })
      );
    }

    // Pseudo-locales.
    if (typeof this.props.pseudoLocale !== "undefined") {
      const {
        pseudoLocale,
        enableAccentedPseudoLocale,
        enableBidiPseudoLocale,
        disablePseudoLocale,
      } = this.props;
      items.push(
        MenuItem({
          id: "toolbox-meatball-menu-pseudo-locale-accented",
          key: "pseudo-locale-accented",
          l10nID: "toolbox-meatball-menu-pseudo-locale-accented",
          type: "checkbox",
          checked: pseudoLocale === "accented",
          onClick:
            pseudoLocale === "accented"
              ? disablePseudoLocale
              : enableAccentedPseudoLocale,
          className: "iconic",
        }),
        MenuItem({
          id: "toolbox-meatball-menu-pseudo-locale-bidi",
          key: "pseudo-locale-bidi",
          l10nID: "toolbox-meatball-menu-pseudo-locale-bidi",
          type: "checkbox",
          checked: pseudoLocale === "bidi",
          onClick:
            pseudoLocale === "bidi"
              ? disablePseudoLocale
              : enableBidiPseudoLocale,
          className: "iconic",
        })
      );
    }

    items.push(hr({ key: "docs-separator-2" }));

    // Getting started
    items.push(
      MenuItem({
        id: "toolbox-meatball-menu-documentation",
        key: "documentation",
        l10nID: "toolbox-meatball-menu-documentation-label",
        onClick: openDevToolsDocsLink,
      })
    );

    // Give feedback
    items.push(
      MenuItem({
        id: "toolbox-meatball-menu-community",
        key: "community",
        l10nID: "toolbox-meatball-menu-community-label",
        onClick: openCommunityLink,
      })
    );

    return MenuList({ id: "toolbox-meatball-menu" }, items);
  }
}

module.exports = MeatballMenu;