-
Notifications
You must be signed in to change notification settings - Fork 296
/
main.js
311 lines (285 loc) · 7.96 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const {app, BrowserWindow, Menu, ipcMain, shell} = require('electron')
const path = require('path')
const url = require('url')
const { autoUpdater } = require('electron-updater');
const fs = require('fs');
const {getColorHexRGB} = require('electron-color-picker')
require('@electron/remote/main').initialize()
let orig_win;
let all_wins = [];
let load_project_data;
(() => {
// Allow advanced users to specify a custom userData directory.
// Useful for portable installations, and for setting up development environments.
const index = process.argv.findIndex(arg => arg === '--userData');
if (index !== -1) {
if (!process.argv.at(index + 1)) {
console.error('No path specified after --userData')
process.exit(1)
}
app.setPath('userData', process.argv[index + 1]);
}
})()
const LaunchSettings = {
path: path.join(app.getPath('userData'), 'launch_settings.json'),
settings: {},
get(key) {
return this.settings[key]
},
set(key, value) {
this.settings[key] = value;
let content = JSON.stringify(this.settings, null, '\t');
fs.writeFileSync(this.path, content);
},
load() {
try {
if (fs.existsSync(this.path)) {
let content = fs.readFileSync(this.path, 'utf-8');
this.settings = JSON.parse(content);
}
} catch (error) {}
return this;
}
}.load();
if (LaunchSettings.get('hardware_acceleration') == false) {
app.disableHardwareAcceleration();
}
function createWindow(second_instance, options = {}) {
if (app.requestSingleInstanceLock && !app.requestSingleInstanceLock()) {
app.quit()
return;
}
let win_options = {
icon: 'icon.ico',
show: false,
backgroundColor: '#21252b',
frame: LaunchSettings.get('native_window_frame') === true,
titleBarStyle: 'hidden',
minWidth: 640,
minHeight: 480,
width: 1080,
height: 720,
webPreferences: {
webgl: true,
webSecurity: true,
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
};
if (options.position) {
win_options.x = options.position[0] - 300;
win_options.y = Math.max(options.position[1] - 100, 0);
}
let win = new BrowserWindow(win_options)
if (!orig_win) orig_win = win;
all_wins.push(win);
require('@electron/remote/main').enable(win.webContents)
var index_path = path.join(__dirname, 'index.html')
if (process.platform === 'darwin') {
let template = [
{
"label": "Blockbench",
"submenu": [
{
"role": "hide"
},
{
"role": "hideothers"
},
{
"role": "unhide"
},
{
"type": "separator"
},
{
"role": "quit"
}
]
},
{
"label": "Edit",
"submenu": [
{
"role": "cut"
},
{
"role": "copy"
},
{
"role": "paste"
},
{
"role": "selectall"
}
]
},
{
"label": "Window",
"role": "window",
"submenu": [
{
"label": "Toggle Full Screen",
"accelerator": "Ctrl+Command+F"
},
{
"role": "minimize"
},
{
"role": "close"
},
{
"type": "separator"
},
{
"role": "front"
}
]
}
]
var osxMenu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(osxMenu)
} else {
win.setMenu(null);
}
if (options.maximize !== false) win.maximize()
win.show()
win.loadURL(url.format({
pathname: index_path,
protocol: 'file:',
slashes: true
}))
win.on('closed', () => {
win = null;
all_wins.splice(all_wins.indexOf(win), 1);
})
if (second_instance === true) {
win.webContents.second_instance = true;
}
return win;
}
app.commandLine.appendSwitch('ignore-gpu-blacklist')
app.commandLine.appendSwitch('ignore-gpu-blocklist')
app.commandLine.appendSwitch('enable-accelerated-video')
app.on('second-instance', function (event, argv, cwd) {
process.argv = argv;
let win = all_wins.find(win => !win.isDestroyed());
if (win && argv[argv.length-1 || 1] && argv[argv.length-1 || 1].substr(0, 2) !== '--') {
win.webContents.send('open-model', argv[argv.length-1 || 1]);
win.focus();
} else {
createWindow(true);
}
})
app.on('open-file', function (event, path) {
process.argv[process.argv.length-1 || 1] = path;
let win = all_wins.find(win => !win.isDestroyed());
if (win) {
win.webContents.send('open-model', path);
}
})
ipcMain.on('edit-launch-setting', (event, arg) => {
LaunchSettings.set(arg.key, arg.value);
})
ipcMain.on('add-recent-project', (event, path) => {
app.addRecentDocument(path);
})
ipcMain.on('dragging-tab', (event, value) => {
all_wins.forEach(win => {
if (win.isDestroyed() || win.id == event.sender.id) return;
win.webContents.send('accept-detached-tab', JSON.parse(value));
})
})
ipcMain.on('new-window', (event, data, position) => {
if (typeof data == 'string') load_project_data = JSON.parse(data);
if (position) {
position = JSON.parse(position)
let place_in_window = all_wins.find(win => {
if (win.isDestroyed() || win.webContents == event.sender || win.isMinimized()) return false;
let pos = win.getPosition();
let size = win.getSize();
return (position.offset[0] >= pos[0] && position.offset[0] <= pos[0] + size[0]
&& position.offset[1] >= pos[1] && position.offset[1] <= pos[1] + size[1]);
})
if (place_in_window) {
place_in_window.send('load-tab', load_project_data);
place_in_window.focus();
load_project_data = null;
} else {
createWindow(true, {
maximize: false,
position: position.offset
});
}
} else {
createWindow(true);
}
})
ipcMain.on('close-detached-project', async (event, window_id, uuid) => {
let window = all_wins.find(win => win.id == window_id);
if (window) window.send('close-detached-project', uuid);
})
ipcMain.on('request-color-picker', async (event, arg) => {
const color = await getColorHexRGB().catch((error) => {
console.warn('[Error] Failed to pick color', error)
return ''
})
if (color) {
all_wins.forEach(win => {
if (win.isDestroyed() || (!arg.sync && win.webContents.getProcessId() != event.sender.getProcessId())) return;
win.webContents.send('set-main-color', color)
})
}
})
ipcMain.on('show-item-in-folder', async (event, path) => {
shell.showItemInFolder(path);
})
app.on('ready', () => {
createWindow()
let app_was_loaded = false;
ipcMain.on('app-loaded', () => {
if (load_project_data) {
all_wins[all_wins.length-1].send('load-tab', load_project_data);
load_project_data = null;
}
if (app_was_loaded) {
console.log('[Blockbench] App reloaded or new window opened')
return;
}
app_was_loaded = true;
if (process.execPath && process.execPath.match(/node_modules[\\\/]electron/)) {
console.log('[Blockbench] App launched in development mode')
} else {
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.autoDownload = false;
if (LaunchSettings.get('update_to_prereleases') === true) {
autoUpdater.allowPrerelease = true;
//autoUpdater.channel = 'beta';
}
autoUpdater.on('update-available', (a) => {
console.log('update-available', a)
ipcMain.on('allow-auto-update', () => {
autoUpdater.downloadUpdate()
})
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-available', a);
})
autoUpdater.on('update-downloaded', (a) => {
console.log('update-downloaded', a)
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-downloaded', a)
})
autoUpdater.on('error', (a) => {
console.log('update-error', a)
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-error', a)
})
autoUpdater.on('download-progress', (a) => {
console.log('update-progress', a)
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-progress', a)
})
autoUpdater.checkForUpdates().catch(err => {})
}
})
})
app.on('window-all-closed', () => {
app.quit()
})