blockbench/main.js

160 lines
3.0 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');
2017-10-27 01:00:52 +08:00
2019-02-04 04:09:35 +08:00
let orig_win;
2017-10-27 01:00:52 +08:00
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,
nodeIntegration: true
2018-10-18 01:50:25 +08:00
}
})
2019-02-04 04:09:35 +08:00
if (!orig_win) orig_win = 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
})
2019-04-08 00:53:33 +08:00
if (second_instance === true) {
win.webContents.second_instance = true
}
2017-10-27 01:00:52 +08:00
}
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
})
2018-10-18 01:50:25 +08:00
app.commandLine.appendSwitch('ignore-gpu-blacklist')
2020-07-16 15:32:59 +08:00
app.commandLine.appendSwitch('enable-accelerated-video')
app.on('ready', () => {
createWindow()
2019-01-09 22:54:35 +08:00
2020-07-16 15:32:59 +08:00
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');
})
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()
})
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
})