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');
|
2020-12-06 05:31:45 +08:00
|
|
|
const fs = require('fs');
|
2017-10-27 01:00:52 +08:00
|
|
|
|
2019-02-04 04:09:35 +08:00
|
|
|
let orig_win;
|
2020-09-01 03:40:37 +08:00
|
|
|
let all_wins = [];
|
2017-10-27 01:00:52 +08:00
|
|
|
|
2020-12-06 05:31:45 +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',
|
2019-07-18 00:02:07 +08:00
|
|
|
frame: false,
|
|
|
|
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,
|
|
|
|
enableRemoteModule: true
|
2018-10-18 01:50:25 +08:00
|
|
|
}
|
|
|
|
})
|
2019-02-04 04:09:35 +08:00
|
|
|
if (!orig_win) orig_win = win;
|
2020-09-01 03:40:37 +08:00
|
|
|
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', () => {
|
2020-09-01 03:40:37 +08:00
|
|
|
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) {
|
2020-09-01 03:40:37 +08:00
|
|
|
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
|
2019-04-08 00:53:33 +08:00
|
|
|
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) {
|
|
|
|
createWindow(true)
|
|
|
|
}
|
|
|
|
})
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2020-09-01 03:40:37 +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)
|
|
|
|
})
|
|
|
|
})
|
2020-12-06 05:31:45 +08:00
|
|
|
ipcMain.on('edit-launch-setting', (event, arg) => {
|
|
|
|
LaunchSettings.set(arg.key, arg.value);
|
|
|
|
})
|
2020-07-16 15:32:59 +08:00
|
|
|
|
|
|
|
app.on('ready', () => {
|
|
|
|
|
|
|
|
createWindow()
|
2019-01-09 22:54:35 +08:00
|
|
|
|
2020-09-16 19:32:40 +08:00
|
|
|
if (process.execPath && process.execPath.match(/electron\.\w+$/)) {
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2020-09-16 19:32:40 +08:00
|
|
|
console.log('[Blockbench] Launching in development mode')
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
autoUpdater.autoInstallOnAppQuit = true;
|
|
|
|
autoUpdater.autoDownload = false;
|
|
|
|
|
|
|
|
autoUpdater.on('update-available', (a) => {
|
|
|
|
console.log('update-available', a)
|
|
|
|
ipcMain.on('allow-auto-update', () => {
|
|
|
|
autoUpdater.downloadUpdate()
|
|
|
|
})
|
|
|
|
orig_win.webContents.send('update-available');
|
2020-07-16 15:32:59 +08:00
|
|
|
})
|
2020-09-16 19:32:40 +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
|
|
|
})
|