-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
132 lines (107 loc) · 3.02 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'core-js/stable'
import 'regenerator-runtime/runtime'
import path from 'path'
import { app, BrowserWindow, Menu, Tray } from 'electron'
import * as url from 'url'
import randomString from './src/utils/randomString'
import Store from 'electron-store'
import createNest from './src/backend/main.backend'
const store = new Store()
const TRAY_TOOL_TIP = 'Electron-React-NestJS Boilerplate'
let mainWindow: BrowserWindow | null = null
let tray: Tray | null = null
const createWindow = async () => {
if(!store.get('address')) store.set('address', '127.0.0.1')
if(!store.get('port')) store.set('port', 8080)
if(!store.get('key')) store.set('key', randomString(88))
const RESOURCES_PATH = app.isPackaged
? path.join(process.resourcesPath, 'assets')
: path.join(__dirname, '../assets')
const getAssetPath = (...paths: string[]): string => {
return path.join(RESOURCES_PATH, ...paths)
}
mainWindow = new BrowserWindow({
show: false,
width: 1024,
height: 600,
frame: false,
backgroundColor: '#ffffff',
icon: getAssetPath('icon.png'),
titleBarStyle: 'hidden' || 'customButtonsOnHover',
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: false,
}
})
mainWindow.setMenuBarVisibility(true)
mainWindow.setResizable(false)
if (process.env.NODE_ENV === 'development') {
mainWindow.loadURL('http://localhost:4000').then()
} else {
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, 'renderer/index.html'),
protocol: 'file:',
slashes: true
})
).then()
}
mainWindow.webContents.on('did-finish-load', () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined')
}
if (process.env.START_MINIMIZED) {
mainWindow.minimize()
} else {
mainWindow.show()
mainWindow.focus()
}
})
mainWindow.on('closed', () => {
mainWindow = null
})
mainWindow.on('minimize', function (event) {
mainWindow?.hide()
tray = createTray(mainWindow)
event.preventDefault()
})
mainWindow.on('restore', function () {
mainWindow?.show()
tray?.destroy()
})
}
function createTray(mainWindow: BrowserWindow) {
const RESOURCES_PATH = app.isPackaged
? path.join(process.resourcesPath, 'assets')
: path.join(__dirname, '../assets')
const getAssetPath = (...paths: string[]): string => {
return path.join(RESOURCES_PATH, ...paths)
}
const nTray = new Tray(getAssetPath('icon.ico'))
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show', click: function () {
mainWindow.show()
}
},
{
label: 'Exit', click: function () {
app.quit()
}
}
])
nTray.on('click', function () {
mainWindow.show()
})
nTray.setToolTip(TRAY_TOOL_TIP)
nTray.setContextMenu(contextMenu)
return nTray
}
app.whenReady().then(async () => {
await createWindow()
await createNest()
})
app.on('activate', async () => {
if (mainWindow === null) await createWindow()
})