-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
136 lines (120 loc) · 3.72 KB
/
main.js
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
133
134
135
136
const fs = require('fs')
const path = require('node:path')
const { app, BrowserWindow, ipcMain, globalShortcut, dialog } = require('electron')
const prefs = require('./preferences')
const server = require('./server')
const { Folder, File } = require('./files')
let window
function createWindow() {
window = new BrowserWindow({
frame: false,
width: 400,
height: 300,
minWidth: 400,
minHeight: 300,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
window.setMenu(null)
// window.maximize()
// window.webContents.openDevTools()
addWindowHandlers()
addServerHandlers()
addFileHandlers()
window.loadFile('window/index.html')
ipcMain.on('finished-loading', () => {
openProject(prefs.path)
window.webContents.send('server-info', { running: false, port: prefs.port || 3000 })
})
}
function addWindowHandlers() {
ipcMain.on('window-minimize', () => {
window.minimize()
})
ipcMain.on('window-maximize', () => {
if (window.isMaximized()) {
window.unmaximize()
} else {
window.maximize()
}
})
ipcMain.on('window-close', () => {
window.close()
})
}
function addServerHandlers() {
ipcMain.on('server-start', (event, port) => {
prefs.port = port
server.start(port).then((msg) => {
window.webContents.send('server-info', msg)
}).catch(err => {
window.webContents.send('server-info', err)
})
})
ipcMain.on('server-stop', () => {
server.stop().then((msg) => {
window.webContents.send('server-info', msg)
}).catch(err => {
window.webContents.send('server-info', err)
})
})
}
function addFileHandlers() {
ipcMain.on('project-open', async (event, { path, type }) => {
if (!path) {
const picker = await dialog.showOpenDialog({
properties: [type === 'folder' ? 'openDirectory' : 'openFile']
})
if (picker.canceled) return
path = picker.filePaths[0]
}
prefs.path = path
openProject(path, type)
})
}
function openProject(path, type) {
if (!path) return
if (!type) type = fs.statSync(path).isDirectory() ? 'folder' : 'file'
if (type === 'folder') {
global.project = new Folder(path)
} else {
global.project = new File(path)
}
window.webContents.send('project-info', global.project.path)
}
const gotSingleLock = app.requestSingleInstanceLock()
if (!gotSingleLock) {
app.quit()
} else {
// create window when app is ready
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// disable keyboard shortcuts for reloading and zooming
app.on('browser-window-focus', () => {
globalShortcut.register('CommandOrControl+R', () => { })
globalShortcut.register('CommandOrControl+=', () => { })
globalShortcut.register('CommandOrControl+-', () => { })
})
app.on('browser-window-blur', () => {
globalShortcut.unregister('CommandOrControl+R')
globalShortcut.unregister('CommandOrControl+=')
globalShortcut.unregister('CommandOrControl+-')
})
// prevent multiple instances
app.on('second-instance', () => {
if (window) {
if (window.isMinimized()) window.restore();
window.focus();
}
});
// quit when all windows are closed
app.on('window-all-closed', function () {
if (server.running) server.stop()
if (process.platform !== 'darwin') app.quit()
})
}