aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/index.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-22 19:17:07 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-22 19:37:20 +0100
commit929171dcba75b51be464e12906801c875ab7647a (patch)
treeae59d77dd1a8dda3ec8118f8f810a783c9103655 /packages/main/src/index.ts
downloadsophie-929171dcba75b51be464e12906801c875ab7647a.tar.gz
sophie-929171dcba75b51be464e12906801c875ab7647a.tar.zst
sophie-929171dcba75b51be464e12906801c875ab7647a.zip
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.
Diffstat (limited to 'packages/main/src/index.ts')
-rw-r--r--packages/main/src/index.ts95
1 files changed, 95 insertions, 0 deletions
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 @@
1import { app, BrowserWindow } from 'electron';
2import { join } from 'path';
3import { URL } from 'url';
4
5const isSingleInstance = app.requestSingleInstanceLock();
6const isDevelopment = import.meta.env.MODE === 'development';
7
8if (!isSingleInstance) {
9 app.quit();
10 process.exit(0);
11}
12
13app.enableSandbox();
14
15if (isDevelopment) {
16 app.whenReady().then(async () => {
17 const {
18 default: installExtension,
19 MOBX_DEVTOOLS,
20 REACT_DEVELOPER_TOOLS,
21 } = await import('electron-devtools-installer');
22 installExtension(
23 [
24 MOBX_DEVTOOLS,
25 REACT_DEVELOPER_TOOLS,
26 ],
27 {
28 forceDownload: false,
29 loadExtensionOptions: {
30 allowFileAccess: true,
31 },
32 },
33 );
34 }).catch((err) => {
35 console.error('Failed to install devtools extension', err);
36 });
37}
38
39let mainWindow: BrowserWindow | null = null;
40
41function createWindow(): Promise<void> {
42 mainWindow = new BrowserWindow({
43 show: false,
44 webPreferences: {
45 nativeWindowOpen: true,
46 webviewTag: false,
47 sandbox: true,
48 preload: join(__dirname, '../../preload/dist/index.cjs'),
49 }
50 });
51
52 // See https://github.com/MarshallOfSound/electron-devtools-installer/issues/195#issuecomment-998872878
53 if (isDevelopment) {
54 const { webContents } = mainWindow;
55 webContents.once('dom-ready', () => {
56 webContents.once('devtools-opened', () => {
57 mainWindow?.focus();
58 });
59 webContents.openDevTools();
60 });
61 }
62
63 mainWindow.on('ready-to-show', () => {
64 mainWindow?.show();
65 });
66
67 const pageUrl = (isDevelopment && import.meta.env.VITE_DEV_SERVER_URL !== undefined)
68 ? import.meta.env.VITE_DEV_SERVER_URL
69 : new URL('../renderer/dist/index.html', `file://${__dirname}`).toString();
70
71 return mainWindow.loadURL(pageUrl);
72}
73
74app.on('second-instance', () => {
75 if (mainWindow !== null) {
76 if (!mainWindow.isVisible()) {
77 mainWindow.show();
78 }
79 if (mainWindow.isMinimized()) {
80 mainWindow.restore();
81 }
82 mainWindow.focus();
83 }
84});
85
86app.on('window-all-closed', () => {
87 if (process.platform !== 'darwin') {
88 app.quit();
89 }
90});
91
92app.whenReady().then(createWindow).catch((err) => {
93 console.error('Failed to create window', err);
94 process.exit(1);
95});