mirror of
https://github.com/zen-browser/desktop.git
synced 2025-07-10 20:35:28 +02:00
refactor(zen-settings): moved common utils to ZenThemesCommon
This commit is contained in:
parent
b5323b00ad
commit
ed82f7d459
2 changed files with 18 additions and 119 deletions
|
@ -26,6 +26,7 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window.addEventListener("DOMContentLoaded", async () => {
|
window.addEventListener("DOMContentLoaded", async () => {
|
||||||
// Theme importer always needs to be loaded first
|
// Theme importer always needs to be loaded first
|
||||||
|
Services.scriptloader.loadSubScript("chrome://browser/content/zen-components/ZenThemesCommon.mjs");
|
||||||
Services.scriptloader.loadSubScript("chrome://browser/content/zen-components/ZenThemesImporter.mjs");
|
Services.scriptloader.loadSubScript("chrome://browser/content/zen-components/ZenThemesImporter.mjs");
|
||||||
Services.scriptloader.loadSubScript("chrome://browser/content/ZenUIManager.mjs");
|
Services.scriptloader.loadSubScript("chrome://browser/content/ZenUIManager.mjs");
|
||||||
Services.scriptloader.loadSubScript("chrome://browser/content/zen-components/ZenSidebarManager.mjs");
|
Services.scriptloader.loadSubScript("chrome://browser/content/zen-components/ZenSidebarManager.mjs");
|
||||||
|
|
|
@ -2,25 +2,6 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
const kZenColors = [
|
|
||||||
'#aac7ff',
|
|
||||||
'#74d7cb',
|
|
||||||
'#a0d490',
|
|
||||||
'#dec663',
|
|
||||||
'#ffb787',
|
|
||||||
'#dec1b1',
|
|
||||||
'#ffb1c0',
|
|
||||||
'#ddbfc3',
|
|
||||||
'#f6b0ea',
|
|
||||||
'#d4bbff',
|
|
||||||
];
|
|
||||||
|
|
||||||
const kZenOSToSmallName = {
|
|
||||||
WINNT: 'windows',
|
|
||||||
Darwin: 'macos',
|
|
||||||
Linux: 'linux',
|
|
||||||
};
|
|
||||||
|
|
||||||
var gZenMarketplaceManager = {
|
var gZenMarketplaceManager = {
|
||||||
init() {
|
init() {
|
||||||
const checkForUpdates = document.getElementById('zenThemeMarketplaceCheckForUpdates');
|
const checkForUpdates = document.getElementById('zenThemeMarketplaceCheckForUpdates');
|
||||||
|
@ -79,125 +60,42 @@ var gZenMarketplaceManager = {
|
||||||
return document.getElementById('zenThemeMarketplaceList');
|
return document.getElementById('zenThemeMarketplaceList');
|
||||||
},
|
},
|
||||||
|
|
||||||
get themesDataFile() {
|
|
||||||
return PathUtils.join(PathUtils.profileDir, 'zen-themes.json');
|
|
||||||
},
|
|
||||||
|
|
||||||
get themesRootPath() {
|
|
||||||
return PathUtils.join(PathUtils.profileDir, 'chrome', 'zen-themes');
|
|
||||||
},
|
|
||||||
|
|
||||||
async removeTheme(themeId) {
|
async removeTheme(themeId) {
|
||||||
console.info('[ZenThemeMarketplaceParent:settings]: Removing theme ', themePath);
|
const themePath = ZenThemesCommon.getThemeFolder(themeId);
|
||||||
|
|
||||||
|
console.info(`[ZenThemeMarketplaceParent:settings]: Removing theme ${themePath}`);
|
||||||
|
|
||||||
const themePath = PathUtils.join(this.themesRootPath, themeId);
|
|
||||||
await IOUtils.remove(themePath, { recursive: true, ignoreAbsent: true });
|
await IOUtils.remove(themePath, { recursive: true, ignoreAbsent: true });
|
||||||
|
|
||||||
const themes = await this._getThemes();
|
const themes = await ZenThemesCommon.getThemes();
|
||||||
delete themes[themeId];
|
delete themes[themeId];
|
||||||
await IOUtils.writeJSON(this.themesDataFile, themes);
|
await IOUtils.writeJSON(ZenThemesCommon.themesDataFile, themes);
|
||||||
|
|
||||||
this.triggerThemeUpdate();
|
this.triggerThemeUpdate();
|
||||||
},
|
},
|
||||||
|
|
||||||
async disableTheme(themeId) {
|
async disableTheme(themeId) {
|
||||||
const themes = await this._getThemes();
|
const themes = await ZenThemesCommon.getThemes();
|
||||||
const theme = themes[themeId];
|
const theme = themes[themeId];
|
||||||
|
|
||||||
theme.enabled = false;
|
theme.enabled = false;
|
||||||
|
|
||||||
await IOUtils.writeJSON(this.themesDataFile, themes);
|
await IOUtils.writeJSON(ZenThemesCommon.themesDataFile, themes);
|
||||||
this._doNotRebuildThemesList = true;
|
this._doNotRebuildThemesList = true;
|
||||||
this.triggerThemeUpdate();
|
this.triggerThemeUpdate();
|
||||||
},
|
},
|
||||||
|
|
||||||
async enableTheme(themeId) {
|
async enableTheme(themeId) {
|
||||||
const themes = await this._getThemes();
|
const themes = await ZenThemesCommon.getThemes();
|
||||||
const theme = themes[themeId];
|
const theme = themes[themeId];
|
||||||
|
|
||||||
theme.enabled = true;
|
theme.enabled = true;
|
||||||
|
|
||||||
await IOUtils.writeJSON(this.themesDataFile, themes);
|
await IOUtils.writeJSON(ZenThemesCommon.themesDataFile, themes);
|
||||||
this._doNotRebuildThemesList = true;
|
this._doNotRebuildThemesList = true;
|
||||||
this.triggerThemeUpdate();
|
this.triggerThemeUpdate();
|
||||||
},
|
},
|
||||||
|
|
||||||
async _getThemes() {
|
|
||||||
if (!this._themes) {
|
|
||||||
if (!(await IOUtils.exists(this.themesDataFile))) {
|
|
||||||
await IOUtils.writeJSON(this.themesDataFile, {});
|
|
||||||
}
|
|
||||||
this._themes = await IOUtils.readJSON(this.themesDataFile);
|
|
||||||
}
|
|
||||||
return this._themes;
|
|
||||||
},
|
|
||||||
|
|
||||||
get currentOperatingSystem() {
|
|
||||||
let os = Services.appinfo.OS;
|
|
||||||
return kZenOSToSmallName[os];
|
|
||||||
},
|
|
||||||
|
|
||||||
async _getThemePreferences(theme) {
|
|
||||||
const themePath = PathUtils.join(this.themesRootPath, theme.id, 'preferences.json');
|
|
||||||
if (!(await IOUtils.exists(themePath)) || !theme.preferences) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const preferences = await IOUtils.readJSON(themePath);
|
|
||||||
|
|
||||||
// compat mode for old preferences, all of them can only be checkboxes
|
|
||||||
if (typeof preferences === 'object' && !Array.isArray(preferences)) {
|
|
||||||
console.warn(
|
|
||||||
`[ZenThemeMarketplaceManager]: Warning, ${theme.name} uses legacy preferences, please migrate them to the new preferences style, as legacy preferences might be removed at a future release. More information at: https://docs.zen-browser.app/themes-store/themes-marketplace-preferences`
|
|
||||||
);
|
|
||||||
const newThemePreferences = [];
|
|
||||||
|
|
||||||
for (let [entry, label] of Object.entries(preferences)) {
|
|
||||||
const [_, negation = '', os = '', property] = /(!?)(?:(macos|windows|linux):)?([A-z0-9-_.]+)/g.exec(entry);
|
|
||||||
const isNegation = negation === '!';
|
|
||||||
|
|
||||||
if (
|
|
||||||
(isNegation && os === this.currentOperatingSystem) ||
|
|
||||||
(os !== '' && os !== this.currentOperatingSystem && !isNegation)
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
newThemePreferences.push({
|
|
||||||
property,
|
|
||||||
label,
|
|
||||||
type: 'checkbox',
|
|
||||||
disabledOn: os !== '' ? [os] : [],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return newThemePreferences;
|
|
||||||
}
|
|
||||||
|
|
||||||
return preferences.filter(({ disabledOn = [] }) => !disabledOn.includes(this.currentOperatingSystem));
|
|
||||||
},
|
|
||||||
|
|
||||||
_getBrowser() {
|
|
||||||
if (!this.__browser) {
|
|
||||||
this.__browser = Services.wm.getMostRecentWindow('navigator:browser');
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.__browser;
|
|
||||||
},
|
|
||||||
|
|
||||||
__throttle(mainFunction, delay) {
|
|
||||||
let timerFlag = null;
|
|
||||||
|
|
||||||
return (...args) => {
|
|
||||||
if (timerFlag === null) {
|
|
||||||
mainFunction(...args);
|
|
||||||
timerFlag = setTimeout(() => {
|
|
||||||
timerFlag = null;
|
|
||||||
}, delay);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
async _buildThemesList() {
|
async _buildThemesList() {
|
||||||
if (!this.themesList) return;
|
if (!this.themesList) return;
|
||||||
if (this._doNotRebuildThemesList) {
|
if (this._doNotRebuildThemesList) {
|
||||||
|
@ -205,11 +103,11 @@ var gZenMarketplaceManager = {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('ZenThemeMarketplaceParent(settings): Building themes list');
|
console.log('[ZenThemeMarketplaceParent:settings]: Building themes list');
|
||||||
|
|
||||||
let themes = await this._getThemes();
|
let themes = await ZenThemesCommon.getThemes();
|
||||||
|
|
||||||
const browser = this._getBrowser();
|
const browser = ZenThemesCommon.getBrowser();
|
||||||
|
|
||||||
const themeList = document.createElement('div');
|
const themeList = document.createElement('div');
|
||||||
|
|
||||||
|
@ -318,7 +216,7 @@ var gZenMarketplaceManager = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const preferences = await this._getThemePreferences(theme);
|
const preferences = await ZenThemesCommon.getThemePreferences(theme);
|
||||||
|
|
||||||
if (preferences.length > 0) {
|
if (preferences.length > 0) {
|
||||||
const preferencesWrapper = document.createXULElement('vbox');
|
const preferencesWrapper = document.createXULElement('vbox');
|
||||||
|
@ -457,7 +355,7 @@ var gZenMarketplaceManager = {
|
||||||
|
|
||||||
input.addEventListener(
|
input.addEventListener(
|
||||||
'input',
|
'input',
|
||||||
this.__throttle((event) => {
|
ZenThemesCommon.throttle((event) => {
|
||||||
const value = event.target.value;
|
const value = event.target.value;
|
||||||
|
|
||||||
Services.prefs.setStringPref(property, value);
|
Services.prefs.setStringPref(property, value);
|
||||||
|
@ -608,7 +506,7 @@ var gZenLooksAndFeel = {
|
||||||
_initializeColorPicker(accentColor) {
|
_initializeColorPicker(accentColor) {
|
||||||
let elem = document.getElementById('zenLooksAndFeelColorOptions');
|
let elem = document.getElementById('zenLooksAndFeelColorOptions');
|
||||||
elem.innerHTML = '';
|
elem.innerHTML = '';
|
||||||
for (let color of kZenColors) {
|
for (let color of ZenThemesCommon.kZenColors) {
|
||||||
let colorElemParen = document.createElement('div');
|
let colorElemParen = document.createElement('div');
|
||||||
let colorElem = document.createElement('div');
|
let colorElem = document.createElement('div');
|
||||||
colorElemParen.classList.add('zenLooksAndFeelColorOptionParen');
|
colorElemParen.classList.add('zenLooksAndFeelColorOptionParen');
|
||||||
|
@ -631,7 +529,7 @@ var gZenLooksAndFeel = {
|
||||||
},
|
},
|
||||||
|
|
||||||
_getInitialAccentColor() {
|
_getInitialAccentColor() {
|
||||||
return Services.prefs.getStringPref('zen.theme.accent-color', kZenColors[0]);
|
return Services.prefs.getStringPref('zen.theme.accent-color', ZenThemesCommon.kZenColors[0]);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue