summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/content/extension-action-button.mjs
blob: cc833aae62915a26271e41ea0b75123aa36888a9 (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
/* 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/. */

import { UnifiedToolbarButton } from "./unified-toolbar-button.mjs";

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  ExtensionParent: "resource://gre/modules/ExtensionParent.sys.mjs",
});
let browserActionFor = extensionId => {
  const extension =
    lazy.ExtensionParent.GlobalManager.getExtension(extensionId);
  if (!extension) {
    return null;
  }
  return lazy.ExtensionParent.apiManager.global.browserActionFor(extension);
};

const BADGE_BACKGROUND_COLOR = "--toolbar-button-badge-bg-color";

/**
 * Attributes:
 * - extension: ID of the extension this button is for.
 * - open: true if the popup is currently open. Gets redirected to aria-pressed.
 */
class ExtensionActionButton extends UnifiedToolbarButton {
  static get observedAttributes() {
    return super.observedAttributes.concat("open");
  }

  /**
   * ext-browserAction instance for this button.
   *
   * @type {?ToolbarButtonAPI}
   */
  #action = null;

  connectedCallback() {
    if (this.hasConnected) {
      super.connectedCallback();
      if (this.#action?.extension?.hasPermission("menus")) {
        document.addEventListener("popupshowing", this.#action);
      }
      return;
    }
    super.connectedCallback();
    this.#action = browserActionFor(this.getAttribute("extension"));
    if (!this.#action) {
      return;
    }
    const contextData = this.#action.getContextData(
      this.#action.getTargetFromWindow(window)
    );
    this.applyTabData(contextData);
    if (this.#action.extension.hasPermission("menus")) {
      document.addEventListener("popupshowing", this.#action);
      if (this.#action.defaults.type == "menu") {
        let menupopup = document.createXULElement("menupopup");
        menupopup.dataset.actionMenu = this.#action.manifestName;
        menupopup.dataset.extensionId = this.#action.extension.id;
        menupopup.addEventListener("popuphiding", event => {
          if (event.target.state === "open") {
            return;
          }
          this.removeAttribute("aria-pressed");
        });
        this.appendChild(menupopup);
      }
    }
  }

  disconnectedCallback() {
    if (this.#action?.extension?.hasPermission("menus")) {
      document.removeEventListener("popupshowing", this.#action);
    }
  }

  attributeChangedCallback(attribute) {
    super.attributeChangedCallback(attribute);
    if (attribute === "open") {
      if (this.getAttribute("open") === "true") {
        this.setAttribute("aria-pressed", "true");
      } else {
        this.removeAttribute("aria-pressed");
      }
    }
  }

  /**
   * Apply the data for the current tab to the extension button. Updates title,
   * label, icon, badge, disabled and popup.
   *
   * @param {object} tabData - Properties for the button in the current tab. See
   *   ExtensionToolbarButtons.jsm for more details.
   */
  applyTabData(tabData) {
    if (!this.#action) {
      this.#action = browserActionFor(this.getAttribute("extension"));
    }
    this.title = tabData.title || this.#action.extension.name;
    this.setAttribute("label", tabData.label || this.title);
    this.classList.toggle("prefer-icon-only", tabData.label == "");
    this.badge = tabData.badgeText;
    this.disabled = !tabData.enabled;
    const { style } = this.#action.iconData.get(tabData.icon);
    for (const [propName, value] of style) {
      this.style.setProperty(propName, value);
    }
    if (tabData.badgeText && tabData.badgeBackgroundColor) {
      const bgColor = tabData.badgeBackgroundColor;
      this.style.setProperty(
        BADGE_BACKGROUND_COLOR,
        `rgba(${bgColor[0]}, ${bgColor[1]}, ${bgColor[2]}, ${bgColor[3] / 255})`
      );
    } else {
      this.style.removeProperty(BADGE_BACKGROUND_COLOR);
    }
    this.toggleAttribute("popup", tabData.popup || tabData.type == "menu");
    if (!tabData.popup) {
      this.removeAttribute("aria-pressed");
    }
  }

  handleClick = event => {
    // If there is a menupopup associated with this button, open it, instead of
    // executing the click action.
    const menupopup = this.querySelector("menupopup");
    if (menupopup) {
      event.preventDefault();
      event.stopPropagation();
      menupopup.openPopup(this, {
        position: "after_start",
        triggerEvent: event,
      });
      this.setAttribute("aria-pressed", "true");
      return;
    }
    this.#action?.handleEvent(event);
  };

  handlePopupShowing(event) {
    this.#action.handleEvent(event);
  }
}
customElements.define("extension-action-button", ExtensionActionButton, {
  extends: "button",
});