summaryrefslogtreecommitdiffstats
path: root/browser/components/tabpreview/tabpreview.mjs
blob: 5256ab22ff3a79d9c020dac2ce375f5e42f9c13c (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
/* 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 { html } from "chrome://global/content/vendor/lit.all.mjs";
import { MozLitElement } from "chrome://global/content/lit-utils.mjs";

var { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);

const TAB_PREVIEW_USE_THUMBNAILS_PREF =
  "browser.tabs.cardPreview.showThumbnails";
const TAB_PREVIEW_DELAY_PREF = "browser.tabs.cardPreview.delayMs";

/**
 * Detailed preview card that displays when hovering a tab
 *
 * @property {MozTabbrowserTab} tab - the tab to preview
 * @fires TabPreview#previewhidden
 * @fires TabPreview#previewshown
 * @fires TabPreview#previewThumbnailUpdated
 */
export default class TabPreview extends MozLitElement {
  static properties = {
    tab: { type: Object },

    _previewIsActive: { type: Boolean, state: true },
    _previewDelayTimeout: { type: Number, state: true },
    _displayTitle: { type: String, state: true },
    _displayURI: { type: String, state: true },
    _displayImg: { type: Object, state: true },
  };

  constructor() {
    super();
    XPCOMUtils.defineLazyPreferenceGetter(
      this,
      "_prefPreviewDelay",
      TAB_PREVIEW_DELAY_PREF,
      1000
    );
    XPCOMUtils.defineLazyPreferenceGetter(
      this,
      "_prefDisplayThumbnail",
      TAB_PREVIEW_USE_THUMBNAILS_PREF,
      false
    );
  }

  // render this inside a <panel>
  createRenderRoot() {
    if (!document.createXULElement) {
      console.error(
        "Unable to create panel: document.createXULElement is not available"
      );
      return super.createRenderRoot();
    }
    this.attachShadow({ mode: "open" });
    this.panel = document.createXULElement("panel");
    this.panel.setAttribute("id", "tabPreviewPanel");
    this.panel.setAttribute("noautofocus", true);
    this.panel.setAttribute("norolluponanchor", true);
    this.panel.setAttribute("consumeoutsideclicks", "never");
    this.panel.setAttribute("level", "parent");
    this.panel.setAttribute("type", "arrow");
    this.shadowRoot.append(this.panel);
    return this.panel;
  }

  get previewCanShow() {
    return this._previewIsActive && this.tab;
  }

  get thumbnailCanShow() {
    return (
      this.previewCanShow &&
      this._prefDisplayThumbnail &&
      !this.tab.selected &&
      this._displayImg
    );
  }

  getPrettyURI(uri) {
    try {
      const url = new URL(uri);
      return `${url.hostname}${url.pathname}`.replace(/\/+$/, "");
    } catch {
      return this.pageURI;
    }
  }

  handleEvent(e) {
    switch (e.type) {
      case "TabSelect": {
        this.requestUpdate();
        break;
      }
      case "wheel": {
        this.hidePreview();
        break;
      }
      case "popuphidden": {
        this.previewHidden();
        break;
      }
    }
  }

  showPreview() {
    this.panel.openPopup(this.tab, {
      position: "bottomleft topleft",
      y: -2,
      isContextMenu: false,
    });
    window.addEventListener("wheel", this, {
      capture: true,
      passive: true,
    });
    window.addEventListener("TabSelect", this);
    this.panel.addEventListener("popuphidden", this);
  }

  hidePreview() {
    this.panel.hidePopup();
    this.updateComplete.then(() => {
      /**
       * @event TabPreview#previewhidden
       * @type {CustomEvent}
       */
      this.dispatchEvent(new CustomEvent("previewhidden"));
    });
  }

  previewHidden() {
    window.removeEventListener("wheel", this, { capture: true, passive: true });
    window.removeEventListener("TabSelect", this);
    this.panel.removeEventListener("popuphidden", this);
  }

  // compute values derived from tab element
  willUpdate(changedProperties) {
    if (!changedProperties.has("tab")) {
      return;
    }
    if (!this.tab) {
      this._displayTitle = "";
      this._displayURI = "";
      this._displayImg = null;
      return;
    }
    this._displayTitle = this.tab.textLabel.textContent;
    this._displayURI = this.getPrettyURI(
      this.tab.linkedBrowser.currentURI.spec
    );
    this._displayImg = null;
    let { tab } = this;
    window.tabPreviews.get(this.tab).then(el => {
      if (this.tab == tab) {
        this._displayImg = el;
      }
    });
  }

  updated(changedProperties) {
    if (changedProperties.has("tab")) {
      // handle preview delay
      if (!this.tab) {
        clearTimeout(this._previewDelayTimeout);
        this._previewIsActive = false;
      } else {
        let lastTabVal = changedProperties.get("tab");
        if (!lastTabVal) {
          // tab was set from an empty state,
          // so wait for the delay duration before showing
          this._previewDelayTimeout = setTimeout(() => {
            this._previewIsActive = true;
          }, this._prefPreviewDelay);
        }
      }
    }
    if (changedProperties.has("_previewIsActive")) {
      if (!this._previewIsActive) {
        this.hidePreview();
      }
    }
    if (
      (changedProperties.has("tab") ||
        changedProperties.has("_previewIsActive")) &&
      this.previewCanShow
    ) {
      this.updateComplete.then(() => {
        if (this.panel.state == "open" || this.panel.state == "showing") {
          this.panel.moveToAnchor(this.tab, "bottomleft topleft", 0, -2);
        } else {
          this.showPreview();
        }

        this.dispatchEvent(
          /**
           * @event TabPreview#previewshown
           * @type {CustomEvent}
           * @property {object} detail
           * @property {MozTabbrowserTab} detail.tab - the tab being previewed
           */
          new CustomEvent("previewshown", {
            detail: { tab: this.tab },
          })
        );
      });
    }
    if (changedProperties.has("_displayImg")) {
      this.updateComplete.then(() => {
        /**
         * fires when the thumbnail for a preview is loaded
         * and added to the document.
         *
         * @event TabPreview#previewThumbnailUpdated
         * @type {CustomEvent}
         */
        this.dispatchEvent(new CustomEvent("previewThumbnailUpdated"));
      });
    }
  }

  render() {
    return html`
      <link
        rel="stylesheet"
        type="text/css"
        href="chrome://browser/content/tabpreview/tabpreview.css"
      />
      <div class="tab-preview-container">
        <div class="tab-preview-text-container">
          <div class="tab-preview-title">${this._displayTitle}</div>
          <div class="tab-preview-uri">${this._displayURI}</div>
        </div>
        ${this.thumbnailCanShow
          ? html`
              <div class="tab-preview-thumbnail-container">
                ${this._displayImg}
              </div>
            `
          : ""}
      </div>
    `;
  }
}
customElements.define("tab-preview", TabPreview);