-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
148 lines (125 loc) · 4.32 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const { app, BrowserWindow, Tray, Menu, ipcMain } = require('electron');
const { setupTitlebar, attachTitlebarToWindow } = require('custom-electron-titlebar/main');
const path = require('path');
const fs = require('fs');
const fsp = require('fs').promises;
const webserver = require('./webserver');
// ugly hack to remove menu generated by custom-electron-titlebar
const menu = new Menu();
Menu.setApplicationMenu(menu);
setupTitlebar();
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 350,
titleBarStyle: 'hidden',
resizable: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
sandbox: false,
}
});
// Load the GUI HTML file
mainWindow.loadFile(path.join(__dirname, 'frontend', 'gui.html'));
// Create the Tray instance
const iconPath = path.join(__dirname, 'build', 'icons', 'icon.png');
let tray = new Tray(iconPath);
const contextMenu = Menu.buildFromTemplate([
{
label: 'Open', click: function () {
mainWindow.show()
}
},
{
label: 'Quit', click: function () {
app.quit()
}
}
])
tray.setToolTip('Your App Name')
tray.setContextMenu(contextMenu)
// Minimize mainWindow to the system tray
mainWindow.on('minimize', function (event) {
event.preventDefault()
mainWindow.hide()
})
// Show the mainWindow when the tray icon is clicked
tray.on('click', function () {
mainWindow.show()
})
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.on('close', (event) => {
event.preventDefault();
mainWindow.webContents.send('close-save-data');
});
ipcMain.on('close-save-data-response', () => {
mainWindow.destroy(); // Now close the window
});
});
// Nullify mainWindow when the window is closed.
mainWindow.on('closed', () => {
mainWindow = null;
});
attachTitlebarToWindow(mainWindow);
}
const userDataPath = app.getPath('userData');
async function readLastPlayed() {
const filePath = path.join(userDataPath, 'lastPlayed.json');
try {
if (!fs.existsSync(filePath)) {
// If the file doesn't exist, create it with default data
const defaultData = {}; // Empty object as default data
await fsp.writeFile(filePath, JSON.stringify(defaultData), 'utf8');
}
const data = await fsp.readFile(filePath, 'utf8');
if (data && data.trim() && data.trim() !== '{}') {
const lastPlayed = JSON.parse(data);
mainWindow.webContents.send('load-data', lastPlayed);
} else {
console.log('lastPlayed.json is empty, no data sent.');
}
} catch (err) {
console.log('Error reading lastPlayed.json:', err);
}
}
function saveLastPlayed(data) {
const filePath = path.join(userDataPath, 'lastPlayed.json');
fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf8', (err) => {
if (err) {
console.log('Error saving lastPlayed.json:', err);
} else {
console.log('Data saved successfully!', filePath);
}
});
}
// When app is ready, open the window
app.whenReady().then(() => {
createWindow();
fs.readFile(path.join(userDataPath, 'lastPlayed.json'), 'utf8', (err, data) => {
if (err) {
console.log('Error reading lastPlayed.json:', err);
return;
}
readLastPlayed();
});
});
// Quit when all windows are closed, except on macOS
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
});
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
});
ipcMain.on('start-server', (event, data) => {
webserver.startServer(data);
mainWindow.webContents.send('server-status', true); // Notify renderer the server has started
});
ipcMain.on('stop-server', () => {
webserver.stopServer();
mainWindow.webContents.send('server-status', false); // Notify renderer the server has stopped
mainWindow.webContents.send('save-data');
});
ipcMain.on('save-data-response', (event, data) => {
saveLastPlayed(data);
});