|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +class BrowserDebugHologram extends HTMLElement { |
| 6 | + /** |
| 7 | + * Creates a new debug hologram |
| 8 | + * @param {object} options |
| 9 | + * @param {string} options.id |
| 10 | + * @param {string} [options.prefId] |
| 11 | + * @param {Function} render |
| 12 | + */ |
| 13 | + static create(options, render) { |
| 14 | + const el = /** @type {BrowserDebugHologram} */ ( |
| 15 | + html("browser-debug-hologram") |
| 16 | + ); |
| 17 | + |
| 18 | + el.setAttribute("debugid", options.id); |
| 19 | + |
| 20 | + if (options.prefId) { |
| 21 | + el.setAttribute("prefid", options.prefId); |
| 22 | + } |
| 23 | + |
| 24 | + el.doRender = render; |
| 25 | + |
| 26 | + return el; |
| 27 | + } |
| 28 | + |
| 29 | + constructor() { |
| 30 | + super(); |
| 31 | + |
| 32 | + this.attachShadow({ mode: "open" }); |
| 33 | + } |
| 34 | + |
| 35 | + /** @type {number} */ |
| 36 | + debugUpdateInt = null; |
| 37 | + |
| 38 | + /** @type {Function} */ |
| 39 | + doRender = () => {}; |
| 40 | + |
| 41 | + get prefId() { |
| 42 | + return this.getAttribute("prefid"); |
| 43 | + } |
| 44 | + |
| 45 | + _createStyle() { |
| 46 | + const style = html("style"); |
| 47 | + style.textContent = `:host { |
| 48 | + display: flex; |
| 49 | + flex-direction: column; |
| 50 | + background-color: black; |
| 51 | + color: white; |
| 52 | + font-weight: 600; |
| 53 | + font-size: 10px; |
| 54 | + font-family: monospace; |
| 55 | + white-space: nowrap; |
| 56 | + } |
| 57 | +
|
| 58 | + :host([hidden]) { |
| 59 | + display: none !important; |
| 60 | + } |
| 61 | +
|
| 62 | + ::slotted(*) { |
| 63 | + display: flex; |
| 64 | + gap: 4px; |
| 65 | + } |
| 66 | + `; |
| 67 | + |
| 68 | + return style; |
| 69 | + } |
| 70 | + |
| 71 | + _render() { |
| 72 | + const isVisible = this.prefId |
| 73 | + ? Services.prefs.getBoolPref(this.prefId, false) |
| 74 | + : false; |
| 75 | + |
| 76 | + console.log(isVisible, this.debugUpdateInt); |
| 77 | + |
| 78 | + if (isVisible && this.debugUpdateInt == null) { |
| 79 | + this.hidden = false; |
| 80 | + |
| 81 | + this.debugUpdateInt = setInterval(() => this.doRender(this), 1); |
| 82 | + } else { |
| 83 | + clearInterval(this.debugUpdateInt); |
| 84 | + this.debugUpdateInt = null; |
| 85 | + |
| 86 | + this.hidden = true; |
| 87 | + this.replaceChildren(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + connectedCallback() { |
| 92 | + this.shadowRoot.appendChild(this._createStyle()); |
| 93 | + this.shadowRoot.appendChild(html("slot")); |
| 94 | + |
| 95 | + this._render(); |
| 96 | + |
| 97 | + if (this.prefId) { |
| 98 | + Services.prefs.addObserver(this.prefId, this._render.bind(this)); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +customElements.define("browser-debug-hologram", BrowserDebugHologram); |
0 commit comments