import { defineStore } from 'pinia'; import { ref, watch } from 'vue'; export type AudioQuality = 'standard' | 'higher' | 'exhigh' | 'lossless' | 'hires'; export type ThemeName = 'blue' | 'green' | 'rose' | 'violet' | 'orange' | 'cyan' | 'pink'; export type CloseAction = 'ask' | 'minimize' | 'exit'; export const themeLabels: Record = { blue: '天蓝', green: '翠绿', rose: '玫红', violet: '紫罗兰', orange: '橙色', cyan: '青色', pink: '粉色', }; export const themeColors: Record = { blue: '#3b82f6', green: '#22c55e', rose: '#f43f5e', violet: '#8b5cf6', orange: '#f97316', cyan: '#06b6d4', pink: '#ec4899', }; export const qualityLabels: Record = { standard: '标准', higher: '较高', exhigh: '极高 (HQ)', lossless: '无损 (SQ)', hires: 'Hi-Res', }; export const closeActionLabels: Record = { ask: '每次询问', minimize: '最小化到托盘', exit: '直接退出', }; export interface ShortcutBinding { key: string; label: string; } export const defaultShortcuts: Record = { playPause: { key: 'Control+KeyP', label: '播放/暂停' }, prev: { key: 'Control+ArrowLeft', label: '上一首' }, next: { key: 'Control+ArrowRight', label: '下一首' }, volUp: { key: 'Control+ArrowUp', label: '音量增加' }, volDown: { key: 'Control+ArrowDown', label: '音量减小' }, globalPlayPause: { key: 'Control+Alt+KeyP', label: '播放/暂停(全局)' }, globalPrev: { key: 'Control+Alt+ArrowLeft', label: '上一首(全局)' }, globalNext: { key: 'Control+Alt+ArrowRight', label: '下一首(全局)' }, globalVolUp: { key: 'Control+Alt+ArrowUp', label: '音量增加(全局)' }, globalVolDown: { key: 'Control+Alt+ArrowDown', label: '音量减小(全局)' }, }; interface SettingsData { audioQuality: AudioQuality; downloadPath: string; theme: ThemeName; closeAction: CloseAction; shortcuts: Record; outputDevice: string | null; volume: number; } function loadSettings(): SettingsData { try { const raw = localStorage.getItem('app_settings'); if (raw) { const parsed = JSON.parse(raw); const theme = parsed.theme || parsed.accentColor || 'blue'; const validThemes: ThemeName[] = ['blue', 'green', 'rose', 'violet', 'orange', 'cyan', 'pink']; return { audioQuality: parsed.audioQuality || 'standard', downloadPath: parsed.downloadPath || '', theme: validThemes.includes(theme) ? theme : 'blue', closeAction: parsed.closeAction || 'ask', shortcuts: { ...defaultShortcuts, ...(parsed.shortcuts || {}) }, outputDevice: parsed.outputDevice || null, volume: typeof parsed.volume === 'number' ? parsed.volume : 100, }; } } catch {} return { audioQuality: 'standard', downloadPath: '', theme: 'blue', closeAction: 'ask', shortcuts: { ...defaultShortcuts }, outputDevice: null, volume: 100, }; } export const useSettingsStore = defineStore('settings', () => { const saved = loadSettings(); const audioQuality = ref(saved.audioQuality); const downloadPath = ref(saved.downloadPath); const theme = ref(saved.theme); const closeAction = ref(saved.closeAction || 'ask'); const shortcuts = ref>(saved.shortcuts); const outputDevice = ref(saved.outputDevice); const volume = ref(saved.volume); function setAudioQuality(q: AudioQuality) { audioQuality.value = q; } function setDownloadPath(p: string) { downloadPath.value = p; } function setTheme(t: ThemeName) { theme.value = t; } function setCloseAction(a: CloseAction) { closeAction.value = a; } function setShortcut(id: string, key: string) { shortcuts.value = { ...shortcuts.value, [id]: { ...shortcuts.value[id], key } }; } function resetShortcuts() { shortcuts.value = { ...defaultShortcuts }; } function setOutputDevice(device: string | null) { outputDevice.value = device; } function resetAll() { audioQuality.value = 'standard'; downloadPath.value = ''; theme.value = 'blue'; closeAction.value = 'ask'; shortcuts.value = { ...defaultShortcuts }; outputDevice.value = null; volume.value = 100; } watch([audioQuality, downloadPath, theme, closeAction, shortcuts, outputDevice, volume], () => { const data: SettingsData = { audioQuality: audioQuality.value, downloadPath: downloadPath.value, theme: theme.value, closeAction: closeAction.value, shortcuts: shortcuts.value, outputDevice: outputDevice.value, volume: volume.value, }; localStorage.setItem('app_settings', JSON.stringify(data)); }, { deep: true }); return { audioQuality, downloadPath, theme, closeAction, shortcuts, outputDevice, volume, setAudioQuality, setDownloadPath, setTheme, setCloseAction, setOutputDevice, setShortcut, resetShortcuts, resetAll, }; });