summaryrefslogtreecommitdiffstats
path: root/devtools/client/responsive/utils/notification.js
blob: a261fe4252d5a4fd333a5540745e6eef3c221bc1 (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
/* 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,
  "gDevTools",
  "resource://devtools/client/framework/devtools.js",
  true
);

/**
 * Displays a notification either at the browser or toolbox level, depending on whether
 * a toolbox is currently open for this tab.
 *
 * @param window
 *        The main browser chrome window.
 * @param tab
 *        The browser tab.
 * @param options
 *        Other options associated with opening.  Currently includes:
 *        - `toolbox`: Whether initiated via toolbox button
 *        - `msg`: String to show in the notification
 *        - `priority`: Priority level for the notification, which affects the icon and
 *                      overall appearance.
 */
async function showNotification(
  window,
  tab,
  { toolboxButton, msg, priority } = {}
) {
  // Default to using the browser's per-tab notification box
  let nbox = window.gBrowser.getNotificationBox(tab.linkedBrowser);

  // If opening was initiated by a toolbox button, check for an open
  // toolbox for the tab.  If one exists, use the toolbox's notification box so that the
  // message is placed closer to the action taken by the user.
  if (toolboxButton) {
    const toolbox = gDevTools.getToolboxForTab(tab);
    if (toolbox) {
      nbox = toolbox.notificationBox;
    }
  }

  const value = "devtools-responsive";
  if (nbox.getNotificationWithValue(value)) {
    // Notification already displayed
    return;
  }

  if (!priority) {
    priority = nbox.PRIORITY_INFO_MEDIUM;
  }

  nbox.appendNotification(value, { label: msg, priority });
}

exports.showNotification = showNotification;