forked from miao-moe/QZMusic_PC
feat: Implement features and continuously optimize the proxy service
- Settings page UI - Persist settings data - Proxy server fixes & optimizations: - Fixed download latency caused by Range Seek - Improved cache stability - Implemented a dual-threading model to ensure smooth playback - Optimized styles for the homepage TopBar and SideBar - Added theme switching functionality - Added theme color customization feature
This commit is contained in:
65
electron/settingsStore.ts
Normal file
65
electron/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