mirror of
https://github.com/lqtmcstudio/QZMusic_PC.git
synced 2026-06-20 23:35:06 +08:00
feat: 迁移架构&实现功能
- 持久化音量设置 - 搜索页支持自定义每页显示数量(持久化) - vite-electron-plugin迁移electron-vite
This commit is contained in:
65
src/main/settingsStore.ts
Normal file
65
src/main/settingsStore.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { app } from 'electron';
|
||||
|
||||
export interface AppSettings {
|
||||
// Cache
|
||||
persistCache: boolean;
|
||||
|
||||
// Appearance
|
||||
theme: 'dark' | 'light';
|
||||
accentColor: string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: AppSettings = {
|
||||
persistCache: true,
|
||||
theme: 'dark',
|
||||
accentColor: '#ec4141', // Default red
|
||||
};
|
||||
|
||||
let settingsCache: AppSettings | null = null;
|
||||
|
||||
function getSettingsPath(): string {
|
||||
return path.join(app.getPath('userData'), 'settings.json');
|
||||
}
|
||||
|
||||
export function loadSettings(): AppSettings {
|
||||
if (settingsCache) return settingsCache;
|
||||
|
||||
const settingsPath = getSettingsPath();
|
||||
try {
|
||||
if (fs.existsSync(settingsPath)) {
|
||||
const data = fs.readFileSync(settingsPath, 'utf-8');
|
||||
settingsCache = { ...DEFAULT_SETTINGS, ...JSON.parse(data) };
|
||||
console.log('[Settings] Loaded from disk:', settingsCache);
|
||||
return settingsCache!;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Settings] Failed to load settings:', e);
|
||||
}
|
||||
|
||||
settingsCache = { ...DEFAULT_SETTINGS };
|
||||
return settingsCache;
|
||||
}
|
||||
|
||||
export function saveSettings(settings: Partial<AppSettings>): AppSettings {
|
||||
settingsCache = { ...loadSettings(), ...settings };
|
||||
|
||||
const settingsPath = getSettingsPath();
|
||||
try {
|
||||
fs.writeFileSync(settingsPath, JSON.stringify(settingsCache, null, 2));
|
||||
console.log('[Settings] Saved to disk:', settingsCache);
|
||||
} catch (e) {
|
||||
console.error('[Settings] Failed to save settings:', e);
|
||||
}
|
||||
|
||||
return settingsCache;
|
||||
}
|
||||
|
||||
export function getSetting<K extends keyof AppSettings>(key: K): AppSettings[K] {
|
||||
return loadSettings()[key];
|
||||
}
|
||||
|
||||
export function setSetting<K extends keyof AppSettings>(key: K, value: AppSettings[K]): void {
|
||||
saveSettings({ [key]: value });
|
||||
}
|
||||
Reference in New Issue
Block a user