Dawn/editor/electron/main.ts
2023-06-25 20:58:47 -07:00

65 lines
1.6 KiB
TypeScript

import { app, BrowserWindow, ipcMain } from 'electron';
import * as path from 'path';
import installExtension, { REACT_DEVELOPER_TOOLS } from "electron-devtools-installer";
import { API_HANDLERS } from './api/handlers';
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
// contextIsolation: false,
preload: path.join(__dirname, 'preload.js')
}
})
if (app.isPackaged) {
// 'build/index.html'
win.loadURL(`file://${__dirname}/../index.html`);
} else {
win.loadURL('http://localhost:3000/index.html');
win.webContents.openDevTools();
// Hot Reloading on 'node_modules/.bin/electronPath'
require('electron-reload')(__dirname, {
electron: path.join(__dirname,
'..',
'..',
'node_modules',
'.bin',
'electron' + (process.platform === "win32" ? ".cmd" : "")),
forceHardReset: true,
hardResetMethod: 'exit'
});
}
}
app.whenReady().then(() => {
// DevTools
installExtension(REACT_DEVELOPER_TOOLS)
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err));
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Add API Handlers
Object.entries(API_HANDLERS).forEach(entry => {
ipcMain.handle(entry[0], (event:any, ...args:any) => {
return entry[1](...args);
});
})
});