Skip to content

Commit 5fd87e7

Browse files
committed
fix windows11 port
1 parent 40a1b93 commit 5fd87e7

File tree

1 file changed

+9
-101
lines changed

1 file changed

+9
-101
lines changed

src/main/index.ts

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ export default class DeskreenApp {
117117
}
118118
});
119119

120-
// ensure window creation happens even if app.whenReady() has already fired
121-
const initializeApp = async (): Promise<void> => {
120+
app.whenReady().then(async () => {
122121
app.setAppUserModelId('com.deskreen-ce.app');
123122
app.setActivationPolicy('regular');
124123

@@ -128,17 +127,7 @@ export default class DeskreenApp {
128127
await this.createWindow();
129128

130129
void this.checkForLatestVersionAndNotify();
131-
};
132-
133-
if (app.isReady()) {
134-
// app is already ready, initialize immediately
135-
void initializeApp();
136-
} else {
137-
// app is not ready yet, wait for it
138-
app.whenReady().then(initializeApp).catch((error) => {
139-
console.error('Failed to initialize app:', error);
140-
});
141-
}
130+
});
142131

143132
app.on('browser-window-created', (_, window) => {
144133
optimizer.watchWindowShortcuts(window);
@@ -204,11 +193,8 @@ export default class DeskreenApp {
204193
await installExtensions();
205194
}
206195

207-
// on windows, show window immediately to prevent it from staying hidden
208-
const shouldShowImmediately = process.platform === 'win32';
209-
210196
this.mainWindow = new BrowserWindow({
211-
show: shouldShowImmediately,
197+
show: false,
212198
width: 940,
213199
height: 640,
214200
minHeight: 460,
@@ -225,85 +211,20 @@ export default class DeskreenApp {
225211
},
226212
});
227213

228-
// track if window has been properly shown and focused
229-
let windowShown = shouldShowImmediately;
230-
// fallback method: timeout to ensure window shows even if events don't fire
231-
const showTimeout = setTimeout(() => {
232-
console.warn('Window show timeout reached, forcing window to show');
233-
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
234-
if (process.env.START_MINIMIZED === 'true') {
235-
this.mainWindow.minimize();
236-
} else {
237-
this.mainWindow.show();
238-
this.mainWindow.focus();
239-
// on windows, ensure window is brought to front
240-
if (process.platform === 'win32') {
241-
this.mainWindow.setAlwaysOnTop(true);
242-
this.mainWindow.setAlwaysOnTop(false);
243-
}
244-
}
245-
windowShown = true;
246-
}
247-
}, process.platform === 'win32' ? 2000 : 3000);
248-
249-
const showWindow = (): void => {
250-
if (!this.mainWindow || windowShown) {
251-
return;
214+
// @TODO: Use 'ready-to-show' event
215+
// https://github.com/electron/electron/blob/master/docs/api/browser-window.md#using-ready-to-show-event
216+
this.mainWindow.on('ready-to-show', () => {
217+
if (!this.mainWindow) {
218+
throw new Error('"mainWindow" is not defined');
252219
}
253-
windowShown = true;
254-
clearTimeout(showTimeout);
255220
if (process.env.START_MINIMIZED === 'true') {
256221
this.mainWindow.minimize();
257222
} else {
258223
this.mainWindow.show();
259224
this.mainWindow.focus();
260-
// on windows, ensure window is brought to front
261-
if (process.platform === 'win32') {
262-
this.mainWindow.setAlwaysOnTop(true);
263-
this.mainWindow.setAlwaysOnTop(false);
264-
}
265-
}
266-
};
267-
268-
// primary method: ready-to-show event
269-
this.mainWindow.on('ready-to-show', () => {
270-
if (shouldShowImmediately) {
271-
// window is already shown, just ensure it's focused
272-
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
273-
this.mainWindow.focus();
274-
if (process.platform === 'win32') {
275-
this.mainWindow.setAlwaysOnTop(true);
276-
this.mainWindow.setAlwaysOnTop(false);
277-
}
278-
}
279-
} else {
280-
// window was hidden, show it now
281-
showWindow();
282225
}
283226
});
284227

285-
// fallback method: did-finish-load event
286-
this.mainWindow.webContents.on('did-finish-load', () => {
287-
// delay slightly to ensure renderer is ready
288-
setTimeout(() => {
289-
showWindow();
290-
}, 100);
291-
});
292-
293-
// handle renderer crashes
294-
this.mainWindow.webContents.on('render-process-gone', (_, details) => {
295-
console.error('Renderer process crashed:', details);
296-
// try to reload the window
297-
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
298-
this.mainWindow.reload();
299-
}
300-
});
301-
302-
// handle unresponsive renderer
303-
this.mainWindow.on('unresponsive', () => {
304-
console.warn('Window became unresponsive');
305-
});
306-
307228
this.mainWindow.webContents.setWindowOpenHandler((details) => {
308229
shell.openExternal(details.url);
309230
return { action: 'deny' };
@@ -378,25 +299,12 @@ export default class DeskreenApp {
378299
}
379300
this.mainWindow.focus();
380301
this.mainWindow.show();
381-
} else {
382-
// window was never created or was closed, create it now
383-
if (app.isReady()) {
384-
void this.createWindow();
385-
} else {
386-
app.whenReady().then(() => {
387-
void this.createWindow();
388-
});
389-
}
390302
}
391303
});
392304

393305
const cliLocalIp = this.parseCliLocalIp();
394306
initGlobals(join(__dirname, '..'), cliLocalIp);
395-
396-
// start signaling server with error handling to prevent unhandled promise rejections
397-
void signalingServer.start().catch((error) => {
398-
console.error('Failed to start signaling server:', error);
399-
});
307+
signalingServer.start();
400308

401309
this.initElectronAppObject();
402310
}

0 commit comments

Comments
 (0)