From 929171dcba75b51be464e12906801c875ab7647a Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Wed, 22 Dec 2021 19:17:07 +0100 Subject: Initial commit Project skeleton based on https://github.com/cawa-93/vite-electron-builder but we use react instead of vue and yarn instead of npm. --- packages/main/src/index.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 packages/main/src/index.ts (limited to 'packages/main/src') diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts new file mode 100644 index 0000000..ef954e1 --- /dev/null +++ b/packages/main/src/index.ts @@ -0,0 +1,95 @@ +import { app, BrowserWindow } from 'electron'; +import { join } from 'path'; +import { URL } from 'url'; + +const isSingleInstance = app.requestSingleInstanceLock(); +const isDevelopment = import.meta.env.MODE === 'development'; + +if (!isSingleInstance) { + app.quit(); + process.exit(0); +} + +app.enableSandbox(); + +if (isDevelopment) { + app.whenReady().then(async () => { + const { + default: installExtension, + MOBX_DEVTOOLS, + REACT_DEVELOPER_TOOLS, + } = await import('electron-devtools-installer'); + installExtension( + [ + MOBX_DEVTOOLS, + REACT_DEVELOPER_TOOLS, + ], + { + forceDownload: false, + loadExtensionOptions: { + allowFileAccess: true, + }, + }, + ); + }).catch((err) => { + console.error('Failed to install devtools extension', err); + }); +} + +let mainWindow: BrowserWindow | null = null; + +function createWindow(): Promise { + mainWindow = new BrowserWindow({ + show: false, + webPreferences: { + nativeWindowOpen: true, + webviewTag: false, + sandbox: true, + preload: join(__dirname, '../../preload/dist/index.cjs'), + } + }); + + // See https://github.com/MarshallOfSound/electron-devtools-installer/issues/195#issuecomment-998872878 + if (isDevelopment) { + const { webContents } = mainWindow; + webContents.once('dom-ready', () => { + webContents.once('devtools-opened', () => { + mainWindow?.focus(); + }); + webContents.openDevTools(); + }); + } + + mainWindow.on('ready-to-show', () => { + mainWindow?.show(); + }); + + const pageUrl = (isDevelopment && import.meta.env.VITE_DEV_SERVER_URL !== undefined) + ? import.meta.env.VITE_DEV_SERVER_URL + : new URL('../renderer/dist/index.html', `file://${__dirname}`).toString(); + + return mainWindow.loadURL(pageUrl); +} + +app.on('second-instance', () => { + if (mainWindow !== null) { + if (!mainWindow.isVisible()) { + mainWindow.show(); + } + if (mainWindow.isMinimized()) { + mainWindow.restore(); + } + mainWindow.focus(); + } +}); + +app.on('window-all-closed', () => { + if (process.platform !== 'darwin') { + app.quit(); + } +}); + +app.whenReady().then(createWindow).catch((err) => { + console.error('Failed to create window', err); + process.exit(1); +}); -- cgit v1.2.3-54-g00ecf