From 1463c0e3f0e87b4702ae01920e4abedd310bca99 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Tue, 24 Sep 2024 16:24:56 +0900 Subject: [PATCH] Migrate deprecated setting values (#954) --- browser_tests/menu.spec.ts | 19 +++++++++++++++++++ src/scripts/ui/settings.ts | 20 +++++++++++++++++++- src/stores/coreSettings.ts | 8 +++++++- src/types/settingTypes.ts | 2 ++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/browser_tests/menu.spec.ts b/browser_tests/menu.spec.ts index 6020d8ae..eab80425 100644 --- a/browser_tests/menu.spec.ts +++ b/browser_tests/menu.spec.ts @@ -454,6 +454,25 @@ test.describe('Menu', () => { }) }) + // Only test 'Top' to reduce test time. + // ['Bottom', 'Top'] + ;['Top'].forEach(async (position) => { + test(`Can migrate deprecated menu positions (${position})`, async ({ + comfyPage + }) => { + await comfyPage.setSetting('Comfy.UseNewMenu', position) + expect(await comfyPage.getSetting('Comfy.UseNewMenu')).toBe('Floating') + }) + + test(`Can migrate deprecated menu positions on initial load (${position})`, async ({ + comfyPage + }) => { + await comfyPage.setSetting('Comfy.UseNewMenu', position) + await comfyPage.setup() + expect(await comfyPage.getSetting('Comfy.UseNewMenu')).toBe('Floating') + }) + }) + test('Can change canvas zoom speed setting', async ({ comfyPage }) => { const [defaultSpeed, maxSpeed] = [1.1, 2.5] expect(await comfyPage.getSetting('Comfy.Graph.ZoomSpeed')).toBe( diff --git a/src/scripts/ui/settings.ts b/src/scripts/ui/settings.ts index ef8a1411..b4f6ad0e 100644 --- a/src/scripts/ui/settings.ts +++ b/src/scripts/ui/settings.ts @@ -58,6 +58,17 @@ export class ComfySettingsDialog extends ComfyDialog { return Object.values(this.settingsLookup) } + private tryMigrateDeprecatedValue(id: string, value: any) { + if (this.app.vueAppReady) { + const settingStore = useSettingStore() + const setting = settingStore.settings[id] + if (setting?.migrateDeprecatedValue) { + return setting.migrateDeprecatedValue(value) + } + } + return value + } + #dispatchChange(id: string, value: T, oldValue?: T) { // Keep the settingStore updated. Not using `store.set` as it would trigger // setSettingValue again. @@ -86,7 +97,12 @@ export class ComfySettingsDialog extends ComfyDialog { // Trigger onChange for any settings added before load for (const id in this.settingsLookup) { - const value = this.settingsValues[this.getId(id)] + const compatId = this.getId(id) + this.settingsValues[compatId] = this.tryMigrateDeprecatedValue( + id, + this.settingsValues[compatId] + ) + const value = this.settingsValues[compatId] this.settingsLookup[id].onChange?.(value) this.#dispatchChange(id, value) } @@ -123,6 +139,8 @@ export class ComfySettingsDialog extends ComfyDialog { id: K, value: Settings[K] ) { + value = this.tryMigrateDeprecatedValue(id, value) + const json = JSON.stringify(value) localStorage['Comfy.Settings.' + id] = json // backwards compatibility for extensions keep setting in storage diff --git a/src/stores/coreSettings.ts b/src/stores/coreSettings.ts index da0fa30d..f786b60e 100644 --- a/src/stores/coreSettings.ts +++ b/src/stores/coreSettings.ts @@ -379,7 +379,13 @@ export const CORE_SETTINGS: SettingParams[] = [ name: 'Use new menu and workflow management.', experimental: true, type: 'combo', - options: ['Disabled', 'Floating'] + options: ['Disabled', 'Floating'], + migrateDeprecatedValue: (value: string) => { + if (['Top', 'Bottom'].includes(value)) { + return 'Floating' + } + return value + } }, { id: 'Comfy.Workflow.WorkflowTabsPosition', diff --git a/src/types/settingTypes.ts b/src/types/settingTypes.ts index 34ab85dd..dcf6d95a 100644 --- a/src/types/settingTypes.ts +++ b/src/types/settingTypes.ts @@ -45,4 +45,6 @@ export interface SettingParams { category?: string[] experimental?: boolean deprecated?: boolean + // Deprecated values are mapped to new values. + migrateDeprecatedValue?: (value: any) => any }