-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathmain.js
More file actions
139 lines (114 loc) · 4.03 KB
/
Copy pathmain.js
File metadata and controls
139 lines (114 loc) · 4.03 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
const { app, BrowserWindow, ipcMain, Menu } = require('electron');
const path = require('path');
const { createServer } = require('./server/server');
const appConfig = require('./config/app.config');
const adsConfigLoader = require('./config/ads-config-loader');
const isDev = () => process.env.NODE_ENV === 'development';
// Fix GPU error by disabling GPU acceleration
app.disableHardwareAcceleration();
// Single instance lock
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', () => {
// Someone tried to run a second instance, we should focus our window
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
let mainWindow;
let serverInstance;
async function createWindow() {
try {
// Remove default menu
Menu.setApplicationMenu(null);
console.log('Creating server...');
const serverResult = await createServer(appConfig.server.port);
serverInstance = serverResult.server;
actualServerPort = serverResult.port;
console.log(`Server created on port ${actualServerPort}`);
console.log('Creating window...');
mainWindow = new BrowserWindow({
width: appConfig.window.width,
height: appConfig.window.height,
frame: true,
resizable: appConfig.window.resizable,
title: 'TeleMirror',
devTools: isDev(),
devToolsKeyCombination: isDev(),
icon: appConfig.paths.icon,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
// Set window title explicitly after creation
mainWindow.setTitle('TeleMirror');
// Force fullscreen after window is created and loaded
// mainWindow.webContents.once('did-finish-load', () => {
// // Use simple fullscreen instead of true fullscreen
// mainWindow.setSimpleFullScreen(true);
// });
console.log('Loading HTML file...');
mainWindow.loadFile(appConfig.paths.mainHtml);
// Set title again after HTML is loaded
mainWindow.webContents.once('did-finish-load', () => {
mainWindow.setTitle('TeleMirror');
});
console.log('Window loaded successfully');
} catch (error) {
console.error('Error in createWindow:', error);
}
}
// IPC handlers
ipcMain.on('minimize-window', () => {
if (mainWindow) {
mainWindow.minimize();
}
});
ipcMain.on('close-window', () => {
if (mainWindow) {
mainWindow.close();
}
});
ipcMain.handle('get-app-config', () => {
return appConfig.app;
});
ipcMain.handle('get-ads-config', async () => {
return await adsConfigLoader.loadAdsConfig();
});
// Store actual port globally for access in IPC handlers
let actualServerPort = appConfig.server.port;
ipcMain.handle('get-server-port', () => {
return actualServerPort;
});
// Cleanup function to close server
function cleanup() {
if (serverInstance) {
console.log('Closing server...');
serverInstance.close(() => {
console.log('Server closed successfully');
});
}
}
// Handle app quit
app.on('before-quit', () => {
cleanup();
});
// Handle window close
app.on('window-all-closed', () => {
cleanup();
if (process.platform !== 'darwin') {
app.quit();
}
});
app.whenReady().then(createWindow);
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
}