diff --git a/apps/desktop/src/components/editor/EditorSettingsDialog.vue b/apps/desktop/src/components/editor/EditorSettingsDialog.vue index c2a67cecbc..a115e46571 100644 --- a/apps/desktop/src/components/editor/EditorSettingsDialog.vue +++ b/apps/desktop/src/components/editor/EditorSettingsDialog.vue @@ -106,6 +106,7 @@ import { type CsvNullMode, type CustomThemeColors, type CustomTheme, + DEFAULT_CUSTOM_THEMES, type McpConnectionPolicy, type McpGlobalPolicy, normalizeMcpGlobalPolicy, @@ -649,7 +650,7 @@ const backgroundImageFileMissing = ref(false); // Set when the draft clears a configured image; the stored copy is only // deleted once the change is actually applied. let pendingBackgroundImageCleanup: string | null = null; -const editCustomThemes = ref([...settingsStore.editorSettings.customThemes]); +const editCustomThemes = ref([...(settingsStore.editorSettings.customThemes.length > 0 ? settingsStore.editorSettings.customThemes : DEFAULT_CUSTOM_THEMES)]); const editActiveCustomThemeId = ref(settingsStore.editorSettings.activeCustomThemeId); const editDataGridTypeColorSchemes = ref(cloneDataGridTypeColorSchemes(settingsStore.editorSettings.dataGridTypeColorSchemes)); const editActiveDataGridTypeColorSchemeId = ref(settingsStore.editorSettings.activeDataGridTypeColorSchemeId); @@ -1715,8 +1716,8 @@ function syncEditorSettingsDraftFromStore() { editTheme.value = settingsStore.editorSettings.theme; editBackgroundImage.value = cloneBackgroundImageDraft(settingsStore.editorSettings.backgroundImage); pendingBackgroundImageCleanup = null; - editCustomThemes.value = [...settingsStore.editorSettings.customThemes]; - editActiveCustomThemeId.value = settingsStore.editorSettings.activeCustomThemeId; + editCustomThemes.value = [...(settingsStore.editorSettings.customThemes.length > 0 ? settingsStore.editorSettings.customThemes : DEFAULT_CUSTOM_THEMES)]; + editActiveCustomThemeId.value = settingsStore.editorSettings.activeCustomThemeId || editCustomThemes.value[0]?.id || "default"; editExecuteMode.value = settingsStore.editorSettings.executeMode; editDefaultTransactionMode.value = settingsStore.editorSettings.defaultTransactionMode; editKeepExplicitTransactionInAutoCommit.value = settingsStore.editorSettings.keepExplicitTransactionInAutoCommit; @@ -2891,7 +2892,11 @@ function resetAiTypography() { const themeSelectValue = computed(() => { if (editTheme.value === "custom") { - return `custom:${editActiveCustomThemeId.value}`; + if (editCustomThemes.value.length > 0) { + const activeMatch = editCustomThemes.value.find((t) => t.id === editActiveCustomThemeId.value); + return activeMatch ? `custom:${activeMatch.id}` : `custom:${editCustomThemes.value[0].id}`; + } + return "custom"; } return editTheme.value; }); @@ -2903,12 +2908,21 @@ const themeSelectOptions = computed(() => [ dark: theme.dark, isCustom: false, })), - ...editCustomThemes.value.map((theme) => ({ - value: `custom:${theme.id}`, - label: theme.name, - dark: true, - isCustom: true, - })), + ...(editCustomThemes.value.length === 0 + ? [ + { + value: "custom", + label: t("settings.customTheme") || "Custom Theme", + dark: true, + isCustom: true, + }, + ] + : editCustomThemes.value.map((theme) => ({ + value: `custom:${theme.id}`, + label: theme.name, + dark: true, + isCustom: true, + }))), ]); function onThemeChange(v: any) { @@ -2916,14 +2930,20 @@ function onThemeChange(v: any) { if (v.startsWith("custom:")) { editTheme.value = "custom"; editActiveCustomThemeId.value = v.slice(7); + } else if (v === "custom") { + editTheme.value = "custom"; + if (editCustomThemes.value.length === 0) { + editCustomThemes.value = JSON.parse(JSON.stringify(DEFAULT_CUSTOM_THEMES)); + } + editActiveCustomThemeId.value = editCustomThemes.value[0]?.id ?? "default"; } else { editTheme.value = v as typeof DEFAULT_EDITOR_SETTINGS.theme; } } function handleThemeSave(updatedThemes: CustomTheme[], activeId: string) { - editCustomThemes.value = updatedThemes; - editActiveCustomThemeId.value = activeId; + editCustomThemes.value = updatedThemes.length > 0 ? updatedThemes : JSON.parse(JSON.stringify(DEFAULT_CUSTOM_THEMES)); + editActiveCustomThemeId.value = activeId || editCustomThemes.value[0]?.id || "default"; editTheme.value = "custom"; showThemeCustomizer.value = false; } diff --git a/apps/desktop/src/components/editor/ThemeCustomizerDialog.vue b/apps/desktop/src/components/editor/ThemeCustomizerDialog.vue index 90b270f7ea..6fd2eb603e 100644 --- a/apps/desktop/src/components/editor/ThemeCustomizerDialog.vue +++ b/apps/desktop/src/components/editor/ThemeCustomizerDialog.vue @@ -5,7 +5,7 @@ import { Button } from "@/components/ui/button"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Input } from "@/components/ui/input"; import type { CustomTheme, CustomThemeColors } from "@/stores/settingsStore"; -import { DEFAULT_CUSTOM_THEME_COLORS, DEFAULT_CUSTOM_THEME_DDL_COLORS } from "@/stores/settingsStore"; +import { DEFAULT_CUSTOM_THEMES, DEFAULT_CUSTOM_THEME_COLORS, DEFAULT_CUSTOM_THEME_DDL_COLORS } from "@/stores/settingsStore"; import { Plus, Trash2, Copy, Pencil, ChevronDown, Palette } from "@lucide/vue"; import { useToast } from "@/composables/useToast"; import { useI18n } from "vue-i18n"; @@ -32,12 +32,22 @@ const jsonText = ref(""); const renamingId = ref(null); const renamingName = ref(""); +function cloneCustomThemes(themes: CustomTheme[]): CustomTheme[] { + return themes.map((theme) => ({ + id: theme.id, + name: theme.name, + colors: { ...theme.colors }, + ddlColors: { ...theme.ddlColors }, + })); +} + watch( () => props.open, (isOpen) => { if (isOpen) { - localThemes.value = JSON.parse(JSON.stringify(props.themes)); - activeEditId.value = props.activeThemeId; + localThemes.value = props.themes && props.themes.length > 0 ? cloneCustomThemes(props.themes) : cloneCustomThemes(DEFAULT_CUSTOM_THEMES); + const hasActive = localThemes.value.some((t) => t.id === props.activeThemeId); + activeEditId.value = hasActive ? props.activeThemeId : (localThemes.value[0]?.id ?? "default"); syncJson(); } }, @@ -352,11 +362,9 @@ function handleJsonChange() { } function handleSave() { - emit( - "save", - localThemes.value.map((t) => ({ ...t })), - activeEditId.value, - ); + const themesToSave = localThemes.value.length > 0 ? localThemes.value : cloneCustomThemes(DEFAULT_CUSTOM_THEMES); + const activeId = themesToSave.some((t) => t.id === activeEditId.value) ? activeEditId.value : (themesToSave[0]?.id ?? "default"); + emit("save", cloneCustomThemes(themesToSave), activeId); emit("update:open", false); } diff --git a/apps/desktop/src/components/editor/__tests__/EditorSettingsDialog.customThemesFallback.spec.ts b/apps/desktop/src/components/editor/__tests__/EditorSettingsDialog.customThemesFallback.spec.ts new file mode 100644 index 0000000000..d8ccd1af25 --- /dev/null +++ b/apps/desktop/src/components/editor/__tests__/EditorSettingsDialog.customThemesFallback.spec.ts @@ -0,0 +1,76 @@ +// @vitest-environment happy-dom + +import { createApp, defineComponent, h, nextTick, ref } from "vue"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { createPinia, setActivePinia } from "pinia"; +import ThemeCustomizerDialog from "../ThemeCustomizerDialog.vue"; +import dialogSource from "../EditorSettingsDialog.vue?raw"; + +vi.mock("vue-i18n", async () => { + const { ref } = await import("vue"); + const locale = ref("en"); + const t = (key: string) => key; + return { + createI18n: () => ({ global: { t, locale }, install: () => undefined }), + useI18n: () => ({ t, locale }), + }; +}); + +describe("ThemeCustomizerDialog with empty themes (#10994)", () => { + let app: ReturnType | null = null; + let container: HTMLDivElement | null = null; + + afterEach(() => { + if (app && container) { + app.unmount(); + container.remove(); + } + app = null; + container = null; + }); + + it("seeds DEFAULT_CUSTOM_THEMES when opened with empty themes", async () => { + setActivePinia(createPinia()); + container = document.createElement("div"); + document.body.appendChild(container); + + const onSave = vi.fn(); + const isOpen = ref(false); + + const Host = defineComponent({ + setup() { + return () => + h(ThemeCustomizerDialog, { + open: isOpen.value, + themes: [], + activeThemeId: "", + "onUpdate:open": (val: boolean) => { + isOpen.value = val; + }, + onSave, + }); + }, + }); + + app = createApp(Host); + app.mount(container); + + // Open the dialog + isOpen.value = true; + await nextTick(); + + // Verify dialog content is rendered into document.body via Teleport and does not crash + expect(document.body.innerHTML).toContain("Custom"); + }); +}); + +describe("EditorSettingsDialog theme select fallback (#10994)", () => { + it("includes a custom theme option even if editCustomThemes is empty", () => { + // Ensures themeSelectOptions checks editCustomThemes.value.length === 0 + // and supplies a fallback custom theme entry so the entry point never disappears + expect(dialogSource).toContain("editCustomThemes.value.length === 0"); + expect(dialogSource).toContain('value: "custom"'); + expect(dialogSource).toContain('} else if (v === "custom")'); + expect(dialogSource).toContain("editCustomThemes.value = JSON.parse(JSON.stringify(DEFAULT_CUSTOM_THEMES))"); + }); +}); diff --git a/apps/desktop/src/stores/__tests__/settingsStore.spec.ts b/apps/desktop/src/stores/__tests__/settingsStore.spec.ts index 350e267d75..05c6c2238d 100644 --- a/apps/desktop/src/stores/__tests__/settingsStore.spec.ts +++ b/apps/desktop/src/stores/__tests__/settingsStore.spec.ts @@ -4,6 +4,7 @@ import { isProxy } from "vue"; import { AI_PROVIDER_PARTNER_PRESETS, AI_PROVIDER_PRESETS, + DEFAULT_CUSTOM_THEMES, DEFAULT_EDITOR_SETTINGS, EXECUTE_MODE_CURRENT_DEFAULT_VERSION, SIDEBAR_BROWSE_OBJECTS_MIGRATION_VERSION, @@ -130,6 +131,37 @@ describe("normalizeEditorSettings", () => { }); }); + it("normalizes customThemes when empty or absent by seeding DEFAULT_CUSTOM_THEMES", () => { + // When customThemes is an empty array + const normalizedEmpty = normalizeEditorSettings({ customThemes: [] }); + expect(normalizedEmpty.customThemes).toEqual(DEFAULT_CUSTOM_THEMES); + expect(normalizedEmpty.activeCustomThemeId).toBe("default"); + + // When customThemes is absent + const normalizedAbsent = normalizeEditorSettings({}); + expect(normalizedAbsent.customThemes).toEqual(DEFAULT_CUSTOM_THEMES); + expect(normalizedAbsent.activeCustomThemeId).toBe("default"); + + // When customThemes has invalid activeCustomThemeId, fall back to first theme id + const customList = [{ id: "my-theme", name: "My Theme", colors: DEFAULT_CUSTOM_THEMES[0].colors, ddlColors: DEFAULT_CUSTOM_THEMES[0].ddlColors }]; + const normalizedInvalidActive = normalizeEditorSettings({ + customThemes: customList, + activeCustomThemeId: "nonexistent", + }); + expect(normalizedInvalidActive.customThemes).toHaveLength(1); + expect(normalizedInvalidActive.activeCustomThemeId).toBe("my-theme"); + + // When legacy customThemeColors is provided and customThemes is empty + const legacyColors = { ...DEFAULT_CUSTOM_THEMES[0].colors, keyword: "#ff0000" }; + const normalizedLegacy = normalizeEditorSettings({ + customThemes: [], + customThemeColors: legacyColors, + }); + expect(normalizedLegacy.customThemes).toHaveLength(1); + expect(normalizedLegacy.customThemes[0].id).toBe("migrated"); + expect(normalizedLegacy.customThemes[0].colors.keyword).toBe("#ff0000"); + }); + it("defaults and bounds the persisted text filter panel height", () => { expect(normalizeEditorSettings({}).dataGridTextFilterPanelHeight).toBe(168); expect(normalizeEditorSettings({ dataGridTextFilterPanelHeight: 236.4 }).dataGridTextFilterPanelHeight).toBe(236); @@ -1186,6 +1218,15 @@ describe("settingsStore persisted settings initialization", () => { expect(saveEditorSettings).toHaveBeenCalledWith(expect.objectContaining({ fontSize: 17, theme: "xcode-dark", appLayout: "separated" })); }); + it("seeds DEFAULT_CUSTOM_THEMES when updateEditorSettings receives empty customThemes", async () => { + const { useSettingsStore } = await import("@/stores/settingsStore"); + const store = useSettingsStore(); + + store.updateEditorSettings({ customThemes: [] }); + expect(store.editorSettings.customThemes).toEqual(DEFAULT_CUSTOM_THEMES); + expect(store.editorSettings.activeCustomThemeId).toBe("default"); + }); + it("migrates the legacy filter-editor preference in incremental settings updates", async () => { const loadEditorSettings = vi.fn().mockResolvedValue({}); const saveEditorSettings = vi.fn().mockResolvedValue(undefined); diff --git a/apps/desktop/src/stores/settingsStore.ts b/apps/desktop/src/stores/settingsStore.ts index 3104fd5ab0..cca81cd96b 100644 --- a/apps/desktop/src/stores/settingsStore.ts +++ b/apps/desktop/src/stores/settingsStore.ts @@ -1704,6 +1704,36 @@ export function normalizeEditorSettings(settings: Partial, exist const autoUpdateJdbc = typeof settings.autoUpdateJdbc === "boolean" ? settings.autoUpdateJdbc : legacyUpdateOptOut ? false : DEFAULT_EDITOR_SETTINGS.autoUpdateJdbc; const autoUpdateMcp = typeof settings.autoUpdateMcp === "boolean" ? settings.autoUpdateMcp : legacyUpdateOptOut ? false : DEFAULT_EDITOR_SETTINGS.autoUpdateMcp; const autoUpdatePlugins = typeof settings.autoUpdatePlugins === "boolean" ? settings.autoUpdatePlugins : legacyUpdateOptOut ? false : DEFAULT_EDITOR_SETTINGS.autoUpdatePlugins; + const customThemes = (() => { + if (Array.isArray(settings.customThemes) && settings.customThemes.length > 0) { + return settings.customThemes.map((theme) => { + const renamed = theme.name === "默认" ? { ...theme, name: "Custom" } : { ...theme }; + return { + ...renamed, + colors: { ...DEFAULT_CUSTOM_THEME_COLORS, ...renamed.colors }, + ddlColors: { + ...DEFAULT_CUSTOM_THEME_DDL_COLORS, + ...(renamed as any).ddlColors, + }, + }; + }); + } + if (settings.customThemeColors && Object.keys(settings.customThemeColors).length > 0) { + return [ + { + id: "migrated", + name: "Migrated", + colors: { + ...DEFAULT_CUSTOM_THEME_COLORS, + ...settings.customThemeColors, + }, + ddlColors: { ...DEFAULT_CUSTOM_THEME_DDL_COLORS }, + }, + ]; + } + return [...DEFAULT_CUSTOM_THEMES]; + })(); + const activeCustomThemeId = settings.activeCustomThemeId && customThemes.some((t) => t.id === settings.activeCustomThemeId) ? settings.activeCustomThemeId : (customThemes[0]?.id ?? "default"); return { fontFamily: normalizeFontFamily(settings.fontFamily, DEFAULT_EDITOR_SETTINGS.fontFamily), fontSize: settings.fontSize ?? DEFAULT_EDITOR_SETTINGS.fontSize, @@ -1716,35 +1746,8 @@ export function normalizeEditorSettings(settings: Partial, exist ...DEFAULT_CUSTOM_THEME_COLORS, ...settings.customThemeColors, }, - customThemes: (() => { - if (Array.isArray(settings.customThemes) && settings.customThemes.length > 0) { - return settings.customThemes.map((theme) => { - const renamed = theme.name === "默认" ? { ...theme, name: "Custom" } : { ...theme }; - return { - ...renamed, - colors: { ...DEFAULT_CUSTOM_THEME_COLORS, ...renamed.colors }, - ddlColors: { - ...DEFAULT_CUSTOM_THEME_DDL_COLORS, - ...(renamed as any).ddlColors, - }, - }; - }); - } - return settings.customThemeColors - ? [ - { - id: "migrated", - name: "Migrated", - colors: { - ...DEFAULT_CUSTOM_THEME_COLORS, - ...settings.customThemeColors, - }, - ddlColors: { ...DEFAULT_CUSTOM_THEME_DDL_COLORS }, - }, - ] - : []; - })(), - activeCustomThemeId: settings.activeCustomThemeId ?? "default", + customThemes, + activeCustomThemeId, executeMode: hasCurrentExecuteModeDefault && (settings.executeMode === "all" || settings.executeMode === "current") ? settings.executeMode : DEFAULT_EDITOR_SETTINGS.executeMode, executeModeDefaultVersion, executeAllOnBlankLine: settings.executeAllOnBlankLine === true, @@ -2578,7 +2581,7 @@ export const useSettingsStore = defineStore("settings", () => { }; } if (partial.customThemes !== undefined) { - editorSettings.value.customThemes = Array.isArray(partial.customThemes) ? partial.customThemes : editorSettings.value.customThemes; + editorSettings.value.customThemes = Array.isArray(partial.customThemes) && partial.customThemes.length > 0 ? partial.customThemes : [...DEFAULT_CUSTOM_THEMES]; } if (partial.activeCustomThemeId !== undefined) { editorSettings.value.activeCustomThemeId = partial.activeCustomThemeId; @@ -2589,6 +2592,7 @@ export const useSettingsStore = defineStore("settings", () => { const activeTheme = themes.find((t) => t.id === activeId) || themes[0]; if (activeTheme) { editorSettings.value.customThemeColors = { ...activeTheme.colors }; + editorSettings.value.activeCustomThemeId = activeTheme.id; } } if (partial.executeMode !== undefined) editorSettings.value.executeMode = partial.executeMode;