-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (65 loc) · 1.72 KB
/
main.js
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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const DataStore = require('./renderer/MusicDataStore');
// 封装一个窗口类
const myStore = new DataStore({
name: 'Music Data'
});
class AppWindow extends BrowserWindow {
constructor(config, flieLocation) {
const basicConfig = {
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
};
// const finalConfig = Object.assign(basicConfig,config);
const finalConfig = { ...basicConfig, ...config };
super(finalConfig);
this.loadFile(flieLocation);
this.once('ready-to-show', () => {
this.show();
});
}
}
app.on('ready', () => {
console.log('ready');
const mainWindow = new AppWindow({}, './renderer/index.html');
console.log('page did finish load');
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.send('getTracks', myStore.getTracks());
});
// 监听ipc
ipcMain.on('deleteTracks', (event, id) => {
const updateTracks = myStore.deleteTracks(id).getTracks();
mainWindow.send('getTracks', updateTracks);
});
ipcMain.on('addTracks', (event, tracks) => {
console.log(tracks);
const updateTracks = myStore.addTracks(tracks).getTracks();
mainWindow.send('getTracks', updateTracks);
});
ipcMain.on('openMusicFile', (event) => {
dialog
.showOpenDialog({
properties: [ 'openFile', 'multiSelections' ],
filters: [ { name: 'Music', extensions: [ 'mp3' ] } ]
})
.then((res) => {
if (!res.canceled) {
console.log(res.filePaths);
event.sender.send('has-choosed-file', res.filePaths);
}
});
});
ipcMain.on('addMusic', () => {
const addMusicWindow = new AppWindow(
{
width: 500,
height: 400,
parent: mainWindow
},
'./renderer/add.html'
);
});
});