Files
AlgerMusicPlayer/src/main/modules/config.ts

101 lines
2.7 KiB
TypeScript
Raw Normal View History

import { app, ipcMain } from 'electron';
import Store from 'electron-store';
import * as path from 'path';
2025-01-10 22:49:55 +08:00
2026-03-04 20:28:38 +08:00
import { createDefaultShortcuts, type ShortcutsConfig } from '../../shared/shortcuts';
import set from '../set.json';
type SetConfig = {
isProxy: boolean;
proxyConfig: {
enable: boolean;
protocol: string;
host: string;
port: number;
};
enableRealIP: boolean;
realIP: string;
noAnimate: boolean;
animationSpeed: number;
author: string;
authorUrl: string;
musicApiPort: number;
closeAction: 'ask' | 'minimize' | 'close';
musicQuality: string;
fontFamily: string;
fontScope: 'global' | 'lyric';
2025-01-23 11:02:55 +08:00
language: string;
showTopAction: boolean;
enableGpuAcceleration: boolean;
downloadPath: string;
enableDiskCache: boolean;
diskCacheDir: string;
diskCacheMaxSizeMB: number;
diskCacheCleanupPolicy: 'lru' | 'fifo';
};
interface StoreType {
set: SetConfig;
2026-03-04 20:28:38 +08:00
shortcuts: ShortcutsConfig;
}
let store: Store<StoreType>;
/**
*
*/
export function initializeConfig() {
store = new Store<StoreType>({
name: 'config',
defaults: {
set: set as SetConfig,
2026-03-04 20:28:38 +08:00
shortcuts: createDefaultShortcuts()
}
});
store.get('set.downloadPath') || store.set('set.downloadPath', app.getPath('downloads'));
store.get('set.diskCacheDir') ||
store.set('set.diskCacheDir', path.join(app.getPath('userData'), 'cache'));
if (store.get('set.diskCacheMaxSizeMB') === undefined) {
store.set('set.diskCacheMaxSizeMB', 4096);
}
if (!store.get('set.diskCacheCleanupPolicy')) {
store.set('set.diskCacheCleanupPolicy', 'lru');
}
if (store.get('set.enableDiskCache') === undefined) {
store.set('set.enableDiskCache', true);
}
// 定义ipcRenderer监听事件
ipcMain.on('set-store-value', (_, key, value) => {
store.set(key, value);
});
ipcMain.on('get-store-value', (_, key) => {
const value = store.get(key);
_.returnValue = value || '';
});
// GPU加速设置更新处理
// 注意GPU加速设置必须在应用启动时在app.ready之前设置才能生效
ipcMain.on('update-gpu-acceleration', (event, enabled: boolean) => {
try {
console.log('GPU加速设置更新:', enabled);
store.set('set.enableGpuAcceleration', enabled);
// GPU加速设置需要重启应用才能生效
event.sender.send('gpu-acceleration-updated', enabled);
console.log('GPU加速设置已保存重启应用后生效');
} catch (error) {
console.error('GPU加速设置更新失败:', error);
const errorMessage = error instanceof Error ? error.message : String(error);
event.sender.send('gpu-acceleration-update-error', errorMessage);
}
});
return store;
2025-01-10 22:49:55 +08:00
}
export function getStore() {
return store;
}