Refactor ZenGradientGenerator class to use constructor instead of init method

This commit is contained in:
mr. M 2024-10-26 22:19:50 +02:00
parent 9e7b399089
commit 89d29e664d
No known key found for this signature in database
GPG key ID: CBD57A2AEDBDA1FB
2 changed files with 31 additions and 14 deletions

View file

@ -10,7 +10,8 @@
numberOfDots = 0; numberOfDots = 0;
init() { constructor() {
super();
if (!Services.prefs.getBoolPref('zen.theme.gradient', true) || !ZenWorkspaces.shouldHaveWorkspaces) { if (!Services.prefs.getBoolPref('zen.theme.gradient', true) || !ZenWorkspaces.shouldHaveWorkspaces) {
return; return;
} }
@ -18,13 +19,15 @@
ChromeUtils.defineLazyGetter(this, 'panel', () => document.getElementById('PanelUI-zen-gradient-generator')); ChromeUtils.defineLazyGetter(this, 'panel', () => document.getElementById('PanelUI-zen-gradient-generator'));
ChromeUtils.defineLazyGetter(this, 'toolbox', () => document.getElementById('TabsToolbar')); ChromeUtils.defineLazyGetter(this, 'toolbox', () => document.getElementById('TabsToolbar'));
ChromeUtils.defineLazyGetter(this, 'customColorInput', () => document.getElementById('PanelUI-zen-gradient-generator-custom-input')); ChromeUtils.defineLazyGetter(this, 'customColorInput', () => document.getElementById('PanelUI-zen-gradient-generator-custom-input'));
ChromeUtils.defineLazyGetter(this, 'customColorList', () => document.getElementById('PanelUI-zen-gradient-generator-custom-list')); ChromeUtils.defineLazyGetter(this, 'customColorList', () => document.getElementById('PanelUI-zen-gradient-generator-custom-list'));
this.initRotation(); this.initRotation();
this.initCanvas(); this.initCanvas();
ZenWorkspaces.addChangeListeners(this.onWorkspaceChange.bind(this)); ZenWorkspaces.addChangeListeners(this.onWorkspaceChange.bind(this));
window.matchMedia('(prefers-color-scheme: dark)').addListener(this.onDarkModeChange.bind(this)); window.matchMedia('(prefers-color-scheme: dark)').addListener(this.onDarkModeChange.bind(this));
this._hasInitialized = true;
this.onDarkModeChange(null, true);
} }
get isDarkMode() { get isDarkMode() {
@ -364,14 +367,20 @@
async onWorkspaceChange(workspace, skipUpdate = false, theme = null) { async onWorkspaceChange(workspace, skipUpdate = false, theme = null) {
const uuid = workspace.uuid; const uuid = workspace.uuid;
// Use theme from workspace object or passed theme // Use theme from workspace object or passed theme
const workspaceTheme = theme || workspace.theme; let workspaceTheme = theme || workspace.theme;
await this.foreachWindowAsActive(async (browser) => { await this.foreachWindowAsActive(async (browser) => {
// Do not rebuild if the workspace is not the same as the current one if (!browser.gZenThemePicker._hasInitialized) {
const windowWorkspace = await browser.ZenWorkspaces.getActiveWorkspace();
if (windowWorkspace.uuid !== uuid) {
return; return;
} }
// Do not rebuild if the workspace is not the same as the current one
const windowWorkspace = await browser.ZenWorkspaces.getActiveWorkspace();
if (windowWorkspace.uuid !== uuid && theme !== null) {
return;
}
// get the theme from the window
workspaceTheme = theme || windowWorkspace.theme;
const appWrapper = browser.document.getElementById('zen-main-app-wrapper'); const appWrapper = browser.document.getElementById('zen-main-app-wrapper');
if (!skipUpdate) { if (!skipUpdate) {
@ -383,11 +392,11 @@
browser.window.requestAnimationFrame(() => { browser.window.requestAnimationFrame(() => {
setTimeout(() => { setTimeout(() => {
appWrapper.removeAttribute('animating'); appWrapper.removeAttribute('animating');
}, 600); }, 500);
}); });
} }
browser.gZenThemePicker.customColorList.innerHTML = ''; browser.gZenThemePicker.resetCustomColorList();
if (!workspaceTheme || workspaceTheme.type !== 'gradient') { if (!workspaceTheme || workspaceTheme.type !== 'gradient') {
browser.document.body.style.removeProperty('--zen-main-browser-background'); browser.document.body.style.removeProperty('--zen-main-browser-background');
browser.gZenThemePicker.updateNoise(0); browser.gZenThemePicker.updateNoise(0);
@ -425,6 +434,10 @@
}); });
} }
resetCustomColorList() {
this.customColorList.innerHTML = '';
}
removeCustomColor(event) { removeCustomColor(event) {
const target = event.target.closest('.zen-theme-picker-custom-list-item'); const target = event.target.closest('.zen-theme-picker-custom-list-item');
const color = target.getAttribute('data-color'); const color = target.getAttribute('data-color');
@ -480,5 +493,5 @@
} }
} }
window.gZenThemePicker = new ZenThemePicker(); window.ZenThemePicker = ZenThemePicker;
} }

View file

@ -151,8 +151,8 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
this._initializeWorkspaceCreationIcons(); this._initializeWorkspaceCreationIcons();
this._initializeWorkspaceTabContextMenus(); this._initializeWorkspaceTabContextMenus();
window.addEventListener('TabBrowserInserted', this.onTabBrowserInserted.bind(this)); window.addEventListener('TabBrowserInserted', this.onTabBrowserInserted.bind(this));
await SessionStore.promiseInitialized;
let workspaces = await this._workspaces(); let workspaces = await this._workspaces();
gZenThemePicker.init();
if (workspaces.workspaces.length === 0) { if (workspaces.workspaces.length === 0) {
await this.createAndSaveWorkspace('Default Workspace', true); await this.createAndSaveWorkspace('Default Workspace', true);
} else { } else {
@ -165,9 +165,13 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
activeWorkspace = workspaces.workspaces[0]; activeWorkspace = workspaces.workspaces[0];
this.activeWorkspace = activeWorkspace.uuid; this.activeWorkspace = activeWorkspace.uuid;
} }
await SessionStore.promiseInitialized;
await this.changeWorkspace(activeWorkspace, true); await this.changeWorkspace(activeWorkspace, true);
} }
try {
window.gZenThemePicker = new ZenThemePicker();
} catch (e) {
console.error('ZenWorkspaces: Error initializing theme picker', e);
}
} }
} }
@ -915,12 +919,12 @@ var ZenWorkspaces = new (class extends ZenMultiWindowFeature {
document.getElementById('tabbrowser-tabs')._positionPinnedTabs(); document.getElementById('tabbrowser-tabs')._positionPinnedTabs();
await this._propagateWorkspaceData({clearCache: false}); await this._propagateWorkspaceData({clearCache: onInit});
this._inChangingWorkspace = false;
for (let listener of this._changeListeners ?? []) { for (let listener of this._changeListeners ?? []) {
listener(window); listener(window);
} }
this._inChangingWorkspace = false;
} }
async _updateWorkspacesChangeContextMenu() { async _updateWorkspacesChangeContextMenu() {