blockbench/main.js

238 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-07-16 15:32:59 +08:00
const {app, BrowserWindow, Menu, ipcMain} = require('electron')
2017-10-27 01:00:52 +08:00
const path = require('path')
const url = require('url')
2020-07-16 15:32:59 +08:00
const { autoUpdater } = require('electron-updater');
const fs = require('fs');
2021-05-06 02:19:48 +08:00
const {getColorHexRGB} = require('electron-color-picker')
2017-10-27 01:00:52 +08:00
2019-02-04 04:09:35 +08:00
let orig_win;
let all_wins = [];
2017-10-27 01:00:52 +08:00
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();
}
2019-04-08 00:53:33 +08:00
function createWindow(second_instance) {
2019-03-10 05:06:35 +08:00
if (app.requestSingleInstanceLock && !app.requestSingleInstanceLock()) {
2019-07-22 05:17:36 +08:00
app.quit()
2019-02-04 04:09:35 +08:00
return;
}
let win = new BrowserWindow({
2018-10-18 01:50:25 +08:00
icon:'icon.ico',
show: false,
backgroundColor: '#21252b',
frame: LaunchSettings.get('native_window_frame') === true,
2019-07-18 00:02:07 +08:00
titleBarStyle: 'hidden',
minWidth: 640,
minHeight: 480,
2018-10-18 01:50:25 +08:00
webPreferences: {
webgl: true,
2018-12-29 19:26:02 +08:00
webSecurity: true,
2020-09-15 00:13:05 +08:00
nodeIntegration: true,
2021-05-30 16:48:10 +08:00
contextIsolation: false,
2020-09-15 00:13:05 +08:00
enableRemoteModule: true
2018-10-18 01:50:25 +08:00
}
})
2019-02-04 04:09:35 +08:00
if (!orig_win) orig_win = win;
all_wins.push(win);
2018-10-18 01:50:25 +08:00
var index_path = path.join(__dirname, 'index.html')
2019-12-16 03:04:31 +08:00
if (process.platform === 'darwin') {
2020-07-16 15:32:59 +08:00
let template = [
{
"label": "Blockbench",
"submenu": [
{
"role": "hide"
},
{
"role": "hideothers"
},
{
"role": "unhide"
},
{
"type": "separator"
},
{
2020-07-18 02:27:06 +08:00
"role": "quit"
2020-07-16 15:32:59 +08:00
}
]
},
{
"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"
}
]
}
]
2019-02-04 04:09:35 +08:00
var osxMenu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(osxMenu)
} else {
win.setMenu(null);
}
2019-07-18 00:02:07 +08:00
2018-10-18 01:50:25 +08:00
win.maximize()
2019-07-19 23:31:22 +08:00
win.show()
2019-07-18 00:02:07 +08:00
2018-10-18 01:50:25 +08:00
win.loadURL(url.format({
pathname: index_path,
protocol: 'file:',
slashes: true
}))
win.on('closed', () => {
win = null;
all_wins.splice(all_wins.indexOf(win), 1);
2018-10-18 01:50:25 +08:00
})
2019-04-08 00:53:33 +08:00
if (second_instance === true) {
win.webContents.second_instance = true;
2019-04-08 00:53:33 +08:00
}
2020-09-22 00:51:37 +08:00
return win;
2017-10-27 01:00:52 +08:00
}
2020-09-22 00:51:37 +08:00
app.commandLine.appendSwitch('ignore-gpu-blacklist')
app.commandLine.appendSwitch('enable-accelerated-video')
2019-02-04 04:09:35 +08:00
app.on('second-instance', function (event, argv, cwd) {
process.argv = argv
if (argv[argv.length-1 || 1] && argv[argv.length-1 || 1].substr(0, 2) !== '--') {
orig_win.webContents.send('open-model', argv[argv.length-1 || 1])
} else {
createWindow(true)
}
2019-02-04 04:09:35 +08:00
})
2020-09-22 00:51:37 +08:00
app.on('open-file', function (event, path) {
process.argv[process.argv.length-1 || 1] = path;
if (orig_win) {
orig_win.webContents.send('open-model', path);
2020-09-22 00:51:37 +08:00
}
})
2020-07-16 15:32:59 +08:00
ipcMain.on('change-main-color', (event, arg) => {
all_wins.forEach(win => {
if (win.isDestroyed() || win.webContents == event.sender.webContents) return;
win.webContents.send('set-main-color', arg)
})
})
ipcMain.on('edit-launch-setting', (event, arg) => {
LaunchSettings.set(arg.key, arg.value);
})
ipcMain.on('add-recent-project', (event, path) => {
app.addRecentDocument(path);
})
2021-05-06 02:19:48 +08:00
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;
2021-05-06 02:19:48 +08:00
win.webContents.send('set-main-color', color)
})
}
})
2020-07-16 15:32:59 +08:00
app.on('ready', () => {
createWindow()
2019-01-09 22:54:35 +08:00
2021-03-21 03:56:22 +08:00
ipcMain.on('app-loaded', () => {
2020-07-16 15:32:59 +08:00
2021-03-21 03:56:22 +08:00
if (process.execPath && process.execPath.match(/electron\.\w+$/)) {
2021-03-21 03:56:22 +08:00
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;
}
2021-03-21 03:56:22 +08:00
autoUpdater.on('update-available', (a) => {
console.log('update-available', a)
ipcMain.on('allow-auto-update', () => {
autoUpdater.downloadUpdate()
})
orig_win.webContents.send('update-available');
})
2021-03-21 03:56:22 +08:00
autoUpdater.on('update-downloaded', (a) => {
console.log('update-downloaded', a)
orig_win.webContents.send('update-downloaded', a)
})
autoUpdater.on('error', (a) => {
console.log('update-error', a)
orig_win.webContents.send('update-error', a)
})
autoUpdater.on('download-progress', (a) => {
console.log('update-progress', a)
orig_win.webContents.send('update-progress', a)
})
autoUpdater.checkForUpdates().catch(err => {})
}
})
2020-07-16 15:32:59 +08:00
})
2017-10-27 01:00:52 +08:00
app.on('window-all-closed', () => {
2018-12-29 19:26:02 +08:00
app.quit()
2017-10-27 01:00:52 +08:00
})