feat: 📦 使用窗口管理
parent
d0e7111fcd
commit
5becbbfc87
@ -0,0 +1,5 @@
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
const eventEmitter = new EventEmitter();
|
||||
|
||||
export default eventEmitter;
|
@ -0,0 +1,4 @@
|
||||
export enum WindowName {
|
||||
MAIN_WINDOW = "MAIN_WINDOW",
|
||||
BROWSER_WINDOW = "BROWSER_WINDOW",
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
import { IWindowListItem } from "../../types/main";
|
||||
import { is } from "electron-util";
|
||||
import { ipcMain } from "electron";
|
||||
import logger from "../logger";
|
||||
import { WindowName } from "./variables";
|
||||
|
||||
const windowList = new Map<WindowName, IWindowListItem>();
|
||||
|
||||
windowList.set(WindowName.MAIN_WINDOW, {
|
||||
url: is.development
|
||||
? "http://localhost:3000/main_window/"
|
||||
: "test://123132123123123",
|
||||
options() {
|
||||
return {
|
||||
width: 590,
|
||||
minWidth: 590,
|
||||
height: 600,
|
||||
frame: false,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
enableRemoteModule: true,
|
||||
},
|
||||
};
|
||||
},
|
||||
async callback(window) {
|
||||
if (is.development) window.webContents.openDevTools();
|
||||
},
|
||||
});
|
||||
|
||||
windowList.set(WindowName.BROWSER_WINDOW, {
|
||||
url: is.development
|
||||
? "http://localhost:3000/browser_window/"
|
||||
: "test://123132123123123",
|
||||
options() {
|
||||
return {
|
||||
width: 800,
|
||||
height: 600,
|
||||
show: false,
|
||||
frame: false,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
enableRemoteModule: true,
|
||||
},
|
||||
};
|
||||
},
|
||||
async callback(window) {
|
||||
if (is.development) window.webContents.openDevTools();
|
||||
|
||||
ipcMain.on("openBrowserWindow", () => {
|
||||
window.show();
|
||||
});
|
||||
|
||||
ipcMain.on("closeBrowserWindow", () => {
|
||||
logger.info("closeBrowserWindow");
|
||||
window.hide();
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export default windowList;
|
@ -0,0 +1,41 @@
|
||||
import { BrowserWindow } from "electron";
|
||||
import windowList from "./windowList";
|
||||
import { IWindowListItem, IWindowManager } from "../../types/main";
|
||||
import { WindowName } from "./variables";
|
||||
|
||||
class WindowManager implements IWindowManager {
|
||||
private windowMap: Map<WindowName | string, BrowserWindow> = new Map();
|
||||
private windowIdMap: Map<number, WindowName | string> = new Map();
|
||||
|
||||
async create(name: WindowName) {
|
||||
const windowConfig: IWindowListItem = windowList.get(name)!;
|
||||
const window = new BrowserWindow(windowConfig.options());
|
||||
const id = window.id;
|
||||
this.windowMap.set(name, window);
|
||||
this.windowIdMap.set(window.id, name);
|
||||
await window.loadURL(windowConfig.url);
|
||||
await windowConfig.callback(window, this);
|
||||
window.on("close", () => {
|
||||
this.deleteById(id);
|
||||
});
|
||||
return window;
|
||||
}
|
||||
|
||||
get(name: WindowName) {
|
||||
return this.windowMap.get(name)!;
|
||||
}
|
||||
|
||||
has(name: WindowName) {
|
||||
return this.windowMap.has(name);
|
||||
}
|
||||
|
||||
deleteById = (id: number) => {
|
||||
const name = this.windowIdMap.get(id);
|
||||
if (name) {
|
||||
this.windowMap.delete(name);
|
||||
this.windowIdMap.delete(id);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default new WindowManager();
|
Loading…
Reference in New Issue