-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cjs
More file actions
142 lines (118 loc) · 3.22 KB
/
main.cjs
File metadata and controls
142 lines (118 loc) · 3.22 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { app, BrowserWindow, Tray, Menu } = require('electron');
const { spawn } = require('child_process');
const path = require('path');
let mainWindow;
let tray;
let serverProcess;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
icon: path.join(__dirname, 'public/icons/main.png'),
webPreferences: {
nodeIntegration: false,
contextIsolation: true
},
autoHideMenuBar: true
});
mainWindow.loadURL('http://localhost:3000');
// Minimiser dans la barre d'état au lieu de fermer
mainWindow.on('close', (event) => {
if (!app.isQuitting) {
event.preventDefault();
mainWindow.hide();
}
return false;
});
}
function createTray() {
const iconPath = path.join(__dirname, 'public/icons/main.png');
tray = new Tray(iconPath);
const contextMenu = Menu.buildFromTemplate([
{
label: 'Ouvrir Stream Deck',
click: () => {
mainWindow.show();
}
},
{
label: 'Quitter',
click: () => {
app.isQuitting = true;
app.quit();
}
}
]);
tray.setToolTip('Stream Deck');
tray.setContextMenu(contextMenu);
tray.on('click', () => {
mainWindow.show();
});
}
function startServer() {
return new Promise((resolve, reject) => {
const serverPath = app.isPackaged
? path.join(process.resourcesPath, 'app.asar', 'server.js')
: path.join(__dirname, 'server.js');
// Utiliser 'node' directement du PATH système
const nodeCommand = app.isPackaged
? process.execPath.replace('electron.exe', 'node.exe')
: 'node';
serverProcess = spawn(nodeCommand, [serverPath], {
cwd: app.isPackaged ? process.resourcesPath : __dirname,
stdio: 'pipe',
shell: true // Permet de trouver node dans le PATH
});
serverProcess.stdout.on('data', (data) => {
console.log(`[Server] ${data}`);
if (data.toString().includes('en écoute')) {
resolve();
}
});
serverProcess.stderr.on('data', (data) => {
console.error(`[Server Error] ${data}`);
});
serverProcess.on('error', (error) => {
console.error('Erreur lors du démarrage du serveur:', error);
reject(error);
});
// Timeout de sécurité
setTimeout(() => resolve(), 3000);
});
}
app.whenReady().then(async () => {
console.log('🚀 Démarrage de Stream Deck...');
// Désactiver l'accélération GPU pour éviter les erreurs de cache
app.disableHardwareAcceleration();
// Démarrer le serveur Node.js
await startServer();
console.log('✅ Serveur démarré');
// Créer la fenêtre et la barre d'état
createWindow();
createTray();
console.log('✅ Interface prête');
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', (event) => {
event.preventDefault();
});
app.on('before-quit', () => {
app.isQuitting = true;
});
app.on('quit', () => {
if (serverProcess) {
console.log('🛑 Arrêt du serveur...');
serverProcess.kill();
}
});
// Configurer l'auto-start au démarrage
app.setLoginItemSettings({
openAtLogin: true,
openAsHidden: false,
path: process.execPath,
args: []
});