Add electron api context
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { app, BrowserWindow } from "electron";
|
||||
import { app, BrowserWindow, ipcMain } from "electron";
|
||||
import { Service } from "@Service/Service";
|
||||
import { join as pathJoin } from "path";
|
||||
const ENV = process.env ?? {};
|
||||
|
||||
class ElectronApp {
|
||||
@@ -14,6 +15,11 @@ class ElectronApp {
|
||||
|
||||
public async runService() {
|
||||
|
||||
if (ENV.LIVING_TOGETHER_SERVICE) {
|
||||
this.serviceUrl = ENV.LIVING_TOGETHER_SERVICE;
|
||||
return;
|
||||
}
|
||||
|
||||
let defaultPort: number | undefined = parseInt(ENV.LIVING_TOGETHER_DEFAULT_PORT ?? "");
|
||||
if (isNaN(defaultPort)) defaultPort = undefined;
|
||||
|
||||
@@ -22,7 +28,7 @@ class ElectronApp {
|
||||
);
|
||||
}
|
||||
|
||||
public mainWindows?: BrowserWindow;
|
||||
public simulatorWindow?: BrowserWindow;
|
||||
|
||||
public async runMainThread() {
|
||||
|
||||
@@ -30,14 +36,36 @@ class ElectronApp {
|
||||
|
||||
await this.runService();
|
||||
|
||||
this.mainWindows = new BrowserWindow({
|
||||
let preload = pathJoin(__dirname, "./SimulatorWindow.js");
|
||||
|
||||
// if (ENV.LIVING_TOGETHER_BASE_PATH) {
|
||||
// preload = pathJoin(__dirname, ENV.LIVING_TOGETHER_BASE_PATH, "./SimulatorWindow.js");
|
||||
// }
|
||||
|
||||
this.simulatorWindow = new BrowserWindow({
|
||||
width: 800,
|
||||
height: 600,
|
||||
titleBarStyle: 'hidden',
|
||||
frame: false,
|
||||
minWidth: 460,
|
||||
minHeight: 300,
|
||||
webPreferences: { preload }
|
||||
});
|
||||
|
||||
this.mainWindows.loadURL(this.serviceUrl);
|
||||
this.simulatorWindow.loadURL(this.serviceUrl);
|
||||
|
||||
this.handelSimulatorWindowBehavior();
|
||||
|
||||
app.on('window-all-closed', function () {
|
||||
if (process.platform !== 'darwin') app.quit()
|
||||
})
|
||||
}
|
||||
|
||||
private handelSimulatorWindowBehavior() {
|
||||
|
||||
ipcMain.on("close", () => {
|
||||
this.simulatorWindow?.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
interface ISimulatorAPI {
|
||||
|
||||
/**
|
||||
* 关闭窗口
|
||||
*/
|
||||
close: () => void;
|
||||
|
||||
/**
|
||||
* 最大化窗口
|
||||
*/
|
||||
maximize: () => void;
|
||||
|
||||
/**
|
||||
* 取消最大化
|
||||
*/
|
||||
unMaximize: () => void;
|
||||
|
||||
/**
|
||||
* 是否处于最大化状态
|
||||
*/
|
||||
isMaximized: () => boolean;
|
||||
}
|
||||
|
||||
export { ISimulatorAPI }
|
||||
@@ -0,0 +1,19 @@
|
||||
import { contextBridge, ipcRenderer } from "electron";
|
||||
import { ISimulatorAPI } from "@Electron/SimulatorAPI"
|
||||
|
||||
const API: ISimulatorAPI = {
|
||||
|
||||
close() {
|
||||
ipcRenderer.send("close");
|
||||
},
|
||||
|
||||
maximize(){},
|
||||
|
||||
unMaximize(){},
|
||||
|
||||
isMaximized(){
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
contextBridge.exposeInMainWorld("API", API);
|
||||
Reference in New Issue
Block a user