From c4e8cdb5e38efb53e169d7ae2d4f49334859bed2 Mon Sep 17 00:00:00 2001 From: EnderDev Date: Mon, 18 Dec 2023 21:49:40 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=9D=20Add=20method=20for=20setting=20c?= =?UTF-8?q?ustomizable=20UI=20state?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BrowserCustomizable.sys.mjs | 48 +++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/components/customizableui/BrowserCustomizable.sys.mjs b/components/customizableui/BrowserCustomizable.sys.mjs index 9dd1bd39dd..2f52d7ec09 100644 --- a/components/customizableui/BrowserCustomizable.sys.mjs +++ b/components/customizableui/BrowserCustomizable.sys.mjs @@ -26,8 +26,9 @@ BrowserCustomizable.prototype = { /** * The current stored customizable state + * @type {object} */ - state: {}, + state: null, /** * The element to render the customizable interface to @@ -45,11 +46,46 @@ BrowserCustomizable.prototype = { return this._root; }, + /** + * Updates the customizable UI state + * @param {object} data + * @param {boolean} [permanent] + */ + async setState(data, permanent = false) { + const newState = await this.internal.parseConfig(data); + + if (!newState) { + throw new Error("Failed to parse customizable state."); + } + + this.state = data; + + if (permanent) { + Services.prefs.setStringPref( + Shared.customizableStatePref, + JSON.stringify(data) + ); + } + + return this.state; + }, + /** * Refetches and validates the customizable state */ async _updateState() { - const newState = await this.internal.parseConfig(); + const serialized = Services.prefs.getStringPref( + Shared.customizableStatePref, + "{}" + ); + + try { + this.state = JSON.parse(serialized); + } catch (e) { + throw new Error("Failed to parse customizable state."); + } + + const newState = await this.internal.parseConfig(this.state); if (!newState) { throw new Error("Failed to parse customizable state."); @@ -110,7 +146,8 @@ BrowserCustomizable.prototype = { */ async _update(boot = false, reset = false) { try { - await this._updateState(); + if (!this.state) this._updateState(); + await this._paint(); } catch (e) { Shared.logger.error("Failure reading customizable state:", e); @@ -123,11 +160,6 @@ BrowserCustomizable.prototype = { e.toString().replace(/^Error: /, "") ); } - - if (boot) { - await this.internal.resetConfig(); - await this._update(false, true); - } } },