Skip to content

Commit 870dde2

Browse files
author
Hiram
committed
[bugs] window open multiple
1 parent da6107a commit 870dde2

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/main/core/winManger.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,34 @@ import logger from './logger';
1010

1111
import loadHtml from '../../../resources/html/load.html?asset';
1212

13-
const winPool = {};
13+
let winPool = {};
1414

1515
const createWin = (name, options) => {
1616
const args = Object.assign({}, options);
1717

18-
let win = new BrowserWindow(args);
19-
winPool[name] = win.id;
18+
let win: BrowserWindow;
19+
20+
if (winPool[name] && !getWin(name)?.isDestroyed()) {
21+
win = getWin(name)!;
22+
if (!win.isVisible()) win.show();
23+
win.focus();
24+
setTimeout(() => {
25+
win.reload();
26+
}, 0);
27+
} else {
28+
win = new BrowserWindow(args);
29+
winPool[name] = win.id;
30+
}
2031

2132
return win;
2233
};
2334

2435
const getWin = (name: string) => {
2536
const id = winPool[name];
2637
logger.info(`[winManager][getWin]name:${name} id:${id}`);
27-
return BrowserWindow.fromId(Number(id));
38+
39+
if (id) return BrowserWindow.fromId(Number(id));
40+
else return null;
2841
};
2942

3043
const getAllWin = () => {
@@ -79,6 +92,10 @@ const createMain = () => {
7992
electronLocalshortcut.unregisterAll(mainWindow!);
8093
});
8194

95+
mainWindow.on('closed', () => {
96+
delete winPool['main'];
97+
});
98+
8299
mainWindow.on('ready-to-show', () => {
83100
electronLocalshortcut.register(mainWindow!, ['CommandOrControl+Shift+I', 'F12'], () => {
84101
if (mainWindow!.webContents.isDevToolsOpened()) {
@@ -202,6 +219,7 @@ const createPlay = () => {
202219
});
203220

204221
playWindow.on('closed', () => {
222+
delete winPool['play'];
205223
const mainWindow = getWin('main');
206224
if (!mainWindow || mainWindow.isDestroyed()) {
207225
createMain();
@@ -236,6 +254,9 @@ const createLoad = () => {
236254

237255
loadWindow.loadFile(loadHtml);
238256
loadWindow.on('ready-to-show', () => loadWindow!.show());
257+
loadWindow.on('closed', () => {
258+
delete winPool['load'];
259+
});
239260
};
240261

241262
const getWindowState = (name: string) => {

0 commit comments

Comments
 (0)