Added disable all component handling

This commit is contained in:
Bryan Galdámez 2024-10-17 22:57:55 -06:00
parent d853fa8de4
commit 2f2881814e
3 changed files with 99 additions and 28 deletions

View file

@ -48,6 +48,7 @@ var ZenThemesCommon = {
); );
} }
} }
return this.themes; return this.themes;
}, },
@ -105,4 +106,15 @@ var ZenThemesCommon = {
} }
}; };
}, },
debounce(mainFunction, wait) {
let timerFlag;
return (...args) => {
clearTimeout(timerFlag);
timerFlag = setTimeout(() => {
mainFunction(...args);
}, wait);
};
},
}; };

View file

@ -1,22 +1,29 @@
const kZenStylesheetThemeHeader = ` const kZenStylesheetThemeHeader = '/* Zen Themes - Generated by ZenThemesImporter.';
/* Zen Themes - Generated by ZenThemeImporter. const kZenStylesheetThemeHeaderBody = `* DO NOT EDIT THIS FILE DIRECTLY!
* DO NOT EDIT THIS FILE DIRECTLY! * Your changes will be overwritten.
* Your changes will be overwritten. * Instead, go to the preferences and edit the themes there.
* Instead, go to the preferences and edit the themes there. */
*/
`; `;
const kenStylesheetFooter = ` const kenStylesheetFooter = `
/* End of Zen Themes */ /* End of Zen Themes */
`; `;
const getCurrentDateTime = () =>
new Intl.DateTimeFormat('en-US', {
dateStyle: 'full',
timeStyle: 'full',
}).format(new Date().getTime());
var gZenStylesheetManager = { var gZenStylesheetManager = {
async writeStylesheet(path, themes) { async writeStylesheet(path, themes) {
let content = kZenStylesheetThemeHeader; let content = kZenStylesheetThemeHeader;
content += `\n* FILE GENERATED AT: ${getCurrentDateTime()}\n`;
content += kZenStylesheetThemeHeaderBody;
for (let theme of themes) { for (let theme of themes) {
if (theme.enabled !== undefined && !theme.enabled) { if (theme.enabled !== undefined && !theme.enabled) {
continue; continue;
} }
content += this.getThemeCSS(theme); content += this.getThemeCSS(theme);
} }
@ -29,26 +36,36 @@ var gZenStylesheetManager = {
getThemeCSS(theme) { getThemeCSS(theme) {
let css = '\n'; let css = '\n';
if (theme._readmeURL) {
css += `/* Name: ${theme.name} */\n`; css += `/* Name: ${theme.name} */\n`;
css += `/* Description: ${theme.description} */\n`; css += `/* Description: ${theme.description} */\n`;
css += `/* Author: @${theme.author} */\n`; css += `/* Author: @${theme.author} */\n`;
if (theme._readmeURL) {
css += `/* Readme: ${theme.readme} */\n`; css += `/* Readme: ${theme.readme} */\n`;
} }
css += `@import url("${theme._chromeURL}");\n`; css += `@import url("${theme._chromeURL}");\n`;
return css; return css;
}, },
}; };
var gZenThemeImporter = new (class { var gZenThemesImporter = new (class {
constructor() { constructor() {
console.info('ZenThemeImporter: Initiating Zen theme importer'); console.info('[ZenThemesImporter]: Initializing Zen Themes Importer');
try { try {
window.SessionStore.promiseInitialized.then(async () => { window.SessionStore.promiseInitialized.then(async () => {
this.insertStylesheet(); if (Services.prefs.getBoolPref('zen.themes.disable-all', false)) {
console.log('[ZenThemesImporter]: Disabling all themes.');
return;
}
const themes = await this.getEnabledThemes();
const themesWithPreferences = await Promise.all( const themesWithPreferences = await Promise.all(
Object.values(await ZenThemesCommon.getThemes()).map(async (theme) => { themes.map(async (theme) => {
const preferences = await ZenThemesCommon.getThemePreferences(theme); const preferences = await ZenThemesCommon.getThemePreferences(theme);
return { return {
@ -60,12 +77,16 @@ var gZenThemeImporter = new (class {
); );
this.writeToDom(themesWithPreferences); this.writeToDom(themesWithPreferences);
await this.insertStylesheet();
}); });
console.info('ZenThemeImporter: Zen theme imported'); console.info('[ZenThemesImporter]: Zen Themes imported');
} catch (e) { } catch (e) {
console.error('ZenThemeImporter: Error importing Zen theme: ', e); console.error('[ZenThemesImporter]: Error importing Zen Themes: ', e);
} }
Services.prefs.addObserver('zen.themes.updated-value-observer', this.rebuildThemeStylesheet.bind(this), false); Services.prefs.addObserver('zen.themes.updated-value-observer', this.rebuildThemeStylesheet.bind(this), false);
Services.prefs.addObserver('zen.themes.disable-all', this.handleDisableThemes.bind(this), false);
} }
get sss() { get sss() {
@ -79,9 +100,16 @@ var gZenThemeImporter = new (class {
return PathUtils.join(PathUtils.profileDir, 'chrome', 'zen-themes.css'); return PathUtils.join(PathUtils.profileDir, 'chrome', 'zen-themes.css');
} }
async rebuildThemeStylesheet() { async handleDisableThemes() {
ZenThemesCommon.themes = null; if (Services.prefs.getBoolPref('zen.themes.disable-all', false)) {
await this.updateStylesheet(); console.log('[ZenThemesImporter]: Disabling themes module.');
await this.removeStylesheet();
} else {
console.log('[ZenThemesImporter]: Enabling themes module.');
await this.rebuildThemeStylesheet();
}
} }
get styleSheetURI() { get styleSheetURI() {
@ -99,21 +127,32 @@ var gZenThemeImporter = new (class {
if (await IOUtils.exists(this.styleSheetPath)) { if (await IOUtils.exists(this.styleSheetPath)) {
await this.sss.loadAndRegisterSheet(this.styleSheetURI, this.sss.AGENT_SHEET); await this.sss.loadAndRegisterSheet(this.styleSheetURI, this.sss.AGENT_SHEET);
} }
if (this.sss.sheetRegistered(this.styleSheetURI, this.sss.AGENT_SHEET)) {
console.debug('[ZenThemesImporter]: Sheet successfully registered');
}
} }
async removeStylesheet() { async removeStylesheet() {
await this.sss.unregisterSheet(this.styleSheetURI, this.sss.AGENT_SHEET); await this.sss.unregisterSheet(this.styleSheetURI, this.sss.AGENT_SHEET);
await IOUtils.remove(this.styleSheetPath, { ignoreAbsent: true });
if (!this.sss.sheetRegistered(this.styleSheetURI, this.sss.AGENT_SHEET) && !(await IOUtils.exists(this.styleSheetPath))) {
console.debug('[ZenThemesImporter]: Sheet successfully unregistered');
}
} }
async updateStylesheet() { async rebuildThemeStylesheet() {
if (Services.focus.activeWindow !== window) { if (Services.focus.activeWindow !== window) {
return; return;
} }
console.log('ZenThemeImporter: Updating Zen themes'); ZenThemesCommon.themes = null;
await this.removeStylesheet(); await this.removeStylesheet();
const themes = Object.values(await ZenThemesCommon.getThemes()); const themes = await this.getEnabledThemes();
await this.writeStylesheet(themes); await this.writeStylesheet(themes);
const themesWithPreferences = await Promise.all( const themesWithPreferences = await Promise.all(
@ -134,6 +173,22 @@ var gZenThemeImporter = new (class {
await this.insertStylesheet(); await this.insertStylesheet();
} }
async getEnabledThemes() {
const themeObject = await ZenThemesCommon.getThemes();
const themes = Object.values(themeObject).filter((theme) => theme.enabled === undefined || theme.enabled);
const themeList = themes.map(({ name }) => name).join(', ');
const message =
themeList !== ''
? `[ZenThemesImporter]: Loading enabled Zen themes: ${themeList}.`
: '[ZenThemesImporter]: No enabled Zen themes.';
console.log(message);
return themes;
}
setDefaults(themesWithPreferences) { setDefaults(themesWithPreferences) {
for (const { preferences, enabled } of themesWithPreferences) { for (const { preferences, enabled } of themesWithPreferences) {
if (enabled !== undefined && !enabled) { if (enabled !== undefined && !enabled) {
@ -237,7 +292,7 @@ var gZenThemeImporter = new (class {
} }
} }
async writeStylesheet(themeList) { async writeStylesheet(themeList = []) {
const themes = []; const themes = [];
ZenThemesCommon.themes = null; ZenThemesCommon.themes = null;

View file

@ -104,12 +104,16 @@ export class ZenThemeMarketplaceParent extends JSWindowActorParent {
await this.checkForThemeChanges(); await this.checkForThemeChanges();
} }
getStyleSheetFullContent(style) { getStyleSheetFullContent(style = '') {
return ` let stylesheet = '@-moz-document url-prefix("chrome:") {';
@-moz-document url-prefix("chrome:") {
${style} for (const line of style.split('\n')) {
stylesheet += ` ${line}`;
} }
`;
stylesheet += '}';
return stylesheet;
} }
async downloadUrlToFile(url, path, isStyleSheet = false) { async downloadUrlToFile(url, path, isStyleSheet = false) {