-
-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy pathindex.ts
More file actions
119 lines (98 loc) · 3.33 KB
/
Copy pathindex.ts
File metadata and controls
119 lines (98 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { TitleTheme } from '/@/shared/types/types';
import { app, dialog, ipcMain, nativeTheme, OpenDialogOptions, safeStorage } from 'electron';
import Store from 'electron-store';
import path from 'path';
const isWindows = process.platform === 'win32';
const isMacOS = process.platform === 'darwin';
const getFrame = () => {
if (isWindows) {
return 'windows';
}
if (isMacOS) {
return 'macOS';
}
return 'linux';
};
const isDevelopment = process.env.NODE_ENV === 'development';
const defaultUserDataPath = app.getPath('userData');
const storePath = isDevelopment
? path.normalize(`${defaultUserDataPath}-dev`)
: path.normalize(defaultUserDataPath);
export const store = new Store<any>({
beforeEachMigration: (_store, context) => {
console.log(`settings migrate from ${context.fromVersion} → ${context.toVersion}`);
},
cwd: storePath,
defaults: {
disable_auto_updates: false,
enableNeteaseTranslation: false,
global_media_hotkeys: true,
lyrics: ['NetEase', 'lrclib.net'],
mediaSession: isMacOS,
playbackType: 'web',
window_enable_tray: true,
window_exit_to_tray: false,
window_minimize_to_tray: false,
window_start_minimized: false,
window_window_bar_style: getFrame(),
},
migrations: {
'>=0.21.2': (store) => {
store.set('window_bar_style', 'linux');
},
'>=1.0.0': (store) => {
store.clear();
},
},
});
ipcMain.handle('settings-get', (_event, data: { property: string }) => {
return store.get(`${data.property}`);
});
ipcMain.on('settings-set', (__event, data: { property: string; value: any }) => {
if (data.value === undefined) {
store.delete(data.property);
} else {
store.set(data.property, data.value);
}
});
ipcMain.handle('password-get', (_event, server: string): null | string => {
if (safeStorage.isEncryptionAvailable()) {
const servers = store.get('server') as Record<string, string> | undefined;
if (!servers) {
return null;
}
const encrypted = servers[server];
if (!encrypted) return null;
const decrypted = safeStorage.decryptString(Buffer.from(encrypted, 'hex'));
return decrypted;
}
return null;
});
ipcMain.on('password-remove', (_event, server: string) => {
const passwords = store.get('server', {}) as Record<string, string>;
if (server in passwords) {
delete passwords[server];
}
store.set({ server: passwords });
});
ipcMain.handle('password-set', (_event, password: string, server: string) => {
if (safeStorage.isEncryptionAvailable()) {
const encrypted = safeStorage.encryptString(password);
const passwords = store.get('server', {}) as Record<string, string>;
passwords[server] = encrypted.toString('hex');
store.set({ server: passwords });
return true;
}
return false;
});
ipcMain.on('theme-set', (_event, theme: TitleTheme) => {
store.set('theme', theme);
nativeTheme.themeSource = theme;
});
ipcMain.handle('open-file-selector', async (_event, options: OpenDialogOptions) => {
const result = await dialog.showOpenDialog({
...options,
properties: ['openFile'],
});
return result.filePaths[0] || null;
});