summaryrefslogtreecommitdiffstats
path: root/toolkit/components/prompts/content/adjustableTitle.js
blob: 16442142a8e06bad57e1b940a2a0b3931c88093e (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
/* 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";

let { PromptUtils } = ChromeUtils.import(
  "resource://gre/modules/SharedPromptUtils.jsm"
);

const AdjustableTitle = {
  _cssSnippet: `
    #titleContainer {
      /* This gets display: flex by virtue of being a row in a subdialog, from
        * commonDialog.css . */
      flex-shrink: 0;

      flex-direction: row;
      align-items: baseline;

      margin-inline: 4px;
      /* Ensure we don't exceed the bounds of the dialog: */
      max-width: calc(100vw - 32px);

      --icon-size: 16px;
    }

    #titleContainer[noicon] > .titleIcon {
      display: none;
    }

    .titleIcon {
      width: var(--icon-size);
      height: var(--icon-size);
      padding-inline-end: 4px;
      flex-shrink: 0;

      background-image: var(--icon-url, url("chrome://global/skin/icons/defaultFavicon.svg"));
      background-size: 16px 16px;
      background-origin: content-box;
      background-repeat: no-repeat;
      background-color: var(--in-content-page-background);
      -moz-context-properties: fill;
      fill: currentColor;
    }

    #titleCropper:not([nomaskfade]) {
      display: inline-flex;
    }

    #titleCropper {
      overflow: hidden;

      justify-content: right;
      mask-repeat: no-repeat;
      /* go from left to right with the mask: */
      --mask-dir: right;
    }

    #titleContainer:not([noicon]) > #titleCropper {
      /* Align the icon and text: */
      translate: 0 calc(-1px - max(.6 * var(--icon-size) - .6em, 0px));
    }

    #titleCropper[rtlorigin] {
      justify-content: left;
      /* go from right to left with the mask: */
      --mask-dir: left;
    }


    #titleCropper:not([nomaskfade]) #titleText {
      display: inline-flex;
      white-space: nowrap;
    }

    #titleText {
      font-weight: 600;
      flex: 1 0 auto; /* Grow but do not shrink. */
      unicode-bidi: plaintext; /* Ensure we align RTL text correctly. */
      text-align: match-parent;
    }

    #titleCropper[overflown] {
      mask-image: linear-gradient(to var(--mask-dir), transparent, black 100px);
    }

    /* hide the old title */
    #infoTitle {
      display: none;
    }
   `,

  _insertMarkup() {
    let iconEl = document.createElement("span");
    iconEl.className = "titleIcon";
    this._titleCropEl = document.createElement("span");
    this._titleCropEl.id = "titleCropper";
    this._titleEl = document.createElement("span");
    this._titleEl.id = "titleText";
    this._containerEl = document.createElement("div");
    this._containerEl.id = "titleContainer";
    this._containerEl.className = "dialogRow titleContainer";
    this._titleCropEl.append(this._titleEl);
    this._containerEl.append(iconEl, this._titleCropEl);
    let targetID = document.documentElement.getAttribute("headerparent");
    document.getElementById(targetID).prepend(this._containerEl);
    let styleEl = document.createElement("style");
    styleEl.textContent = this._cssSnippet;
    document.documentElement.prepend(styleEl);
  },

  _overflowHandler() {
    requestAnimationFrame(async () => {
      let isOverflown;
      try {
        isOverflown = await window.promiseDocumentFlushed(() => {
          return (
            this._titleCropEl.getBoundingClientRect().width <
            this._titleEl.getBoundingClientRect().width
          );
        });
      } catch (ex) {
        // In automated tests, this can fail with a DOM exception if
        // the window has closed by the time layout tries to call us.
        // In this case, just bail, and only log any other errors:
        if (
          !DOMException.isInstance(ex) ||
          ex.name != "NoModificationAllowedError"
        ) {
          Cu.reportError(ex);
        }
        return;
      }
      this._titleCropEl.toggleAttribute("overflown", isOverflown);
      if (isOverflown) {
        this._titleEl.setAttribute("title", this._titleEl.textContent);
      } else {
        this._titleEl.removeAttribute("title");
      }
    });
  },

  _updateTitle(title) {
    title = JSON.parse(title);
    if (title.raw) {
      this._titleEl.textContent = title.raw;
      let { DIRECTION_RTL } = window.windowUtils;
      this._titleCropEl.toggleAttribute(
        "rtlorigin",
        window.windowUtils.getDirectionFromText(title.raw) == DIRECTION_RTL
      );
    } else {
      document.l10n.setAttributes(this._titleEl, title.l10nId);
    }

    if (!document.documentElement.hasAttribute("neediconheader")) {
      this._containerEl.setAttribute("noicon", "true");
    } else if (title.shouldUseMaskFade) {
      this._overflowHandler();
    } else {
      this._titleCropEl.toggleAttribute("nomaskfade", true);
    }
  },

  init() {
    // Only run this if we're embedded and proton modals are enabled.
    if (!window.docShell.chromeEventHandler) {
      return;
    }

    this._insertMarkup();
    let title = document.documentElement.getAttribute("headertitle");
    if (title) {
      this._updateTitle(title);
    }
    this._mutObs = new MutationObserver(() => {
      this._updateTitle(document.documentElement.getAttribute("headertitle"));
    });
    this._mutObs.observe(document.documentElement, {
      attributes: true,
      attributeFilter: ["headertitle"],
    });
  },
};

document.addEventListener(
  "DOMContentLoaded",
  () => {
    AdjustableTitle.init();
  },
  { once: true }
);