Bind windows api

This commit is contained in:
2022-04-16 13:54:56 +08:00
parent 4f175a5505
commit 8b29654fdf
5 changed files with 124 additions and 25 deletions
+24 -1
View File
@@ -63,9 +63,32 @@ class ElectronApp {
private handelSimulatorWindowBehavior() {
ipcMain.on("close", () => {
ipcMain.on("windows.close", () => {
this.simulatorWindow?.close();
});
ipcMain.on("windows.maximize", () => {
this.simulatorWindow?.maximize();
});
ipcMain.on("windows.unMaximize", () => {
this.simulatorWindow?.unmaximize();
});
ipcMain.on("windows.isMaximized", (event) => {
event.returnValue = this.simulatorWindow?.isMaximized();
});
ipcMain.on("windows.minimize", (event) => {
this.simulatorWindow?.minimize();
});
const sendWindowsChangeMessage = () => {
this.simulatorWindow?.webContents.send("windows.windowsSizeStateChange");
}
this.simulatorWindow?.on("maximize", sendWindowsChangeMessage);
this.simulatorWindow?.on("unmaximize", sendWindowsChangeMessage);
}
}
+12 -2
View File
@@ -1,5 +1,10 @@
import { Emitter } from "@Model/Emitter";
interface ISimulatorAPI {
type IApiEmitterEvent = {
windowsSizeStateChange: void;
}
interface ISimulatorAPI extends Emitter<IApiEmitterEvent> {
/**
* 关闭窗口
@@ -20,6 +25,11 @@ interface ISimulatorAPI {
* 是否处于最大化状态
*/
isMaximized: () => boolean;
/**
* 是否处于最大化状态
*/
minimize: () => void;
}
export { ISimulatorAPI }
export { ISimulatorAPI, IApiEmitterEvent }
+55 -10
View File
@@ -1,19 +1,64 @@
import { contextBridge, ipcRenderer } from "electron";
import { ISimulatorAPI } from "@Electron/SimulatorAPI"
const API: ISimulatorAPI = {
const emitterMap: Array<[key: string, value: Function[]]> = [];
const queryEmitter = (key: string) => {
let res: (typeof emitterMap)[0] | undefined;
emitterMap.forEach((item) => {
if (item[0] === key) res = item;
});
close() {
ipcRenderer.send("close");
},
if (res) {
if (Array.isArray(res[1])) return res[1];
res[1] = [];
return res[1];
}
maximize(){},
unMaximize(){},
isMaximized(){
return false
else {
res = [key, []];
emitterMap.push(res);
return res[1];
}
}
const API: ISimulatorAPI = {
close() {
ipcRenderer.send("windows.close");
},
maximize() {
ipcRenderer.send("windows.maximize");
},
unMaximize() {
ipcRenderer.send("windows.unMaximize");
},
isMaximized() {
return ipcRenderer.sendSync("windows.isMaximized");
},
minimize() {
ipcRenderer.send("windows.minimize");
},
all: new Map() as any,
resetAll: () => emitterMap.splice(0),
reset: (type) => queryEmitter(type).splice(0),
on: (type, handler) => queryEmitter(type).push(handler),
off: (type, handler) => {
const handlers = queryEmitter(type);
handlers.splice(handlers.indexOf(handler!) >>> 0, 1);
},
emit: ((type: string, evt: any) => {
queryEmitter(type).slice().map((handler: any) => { handler(evt) });
}) as any,
}
ipcRenderer.on("windows.windowsSizeStateChange", () => {
API.emit("windowsSizeStateChange");
});
contextBridge.exposeInMainWorld("API", API);