blockbench/main.js

301 lines
7.6 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')
require('@electron/remote/main').initialize()
2017-10-27 01:00:52 +08:00
2019-02-04 04:09:35 +08:00
let orig_win;
let all_wins = [];
2022-10-29 04:56:39 +08:00
let load_project_data;
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();
}
2022-10-29 04:56:39 +08:00
function createWindow(second_instance, options = {}) {
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;
}
2022-10-29 04:56:39 +08:00
let win_options = {
icon: 'icon.ico',
2018-10-18 01:50:25 +08:00
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,
2022-10-29 04:56:39 +08:00
width: 1080,
height: 720,
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
}
2022-10-29 04:56:39 +08:00
};
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)
2019-02-04 04:09:35 +08:00
if (!orig_win) orig_win = win;
all_wins.push(win);
require('@electron/remote/main').enable(win.webContents)
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
2022-10-29 04:56:39 +08:00
if (options.maximize !== false) 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) {
2021-10-23 22:22:00 +08:00
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);
}
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;
2021-10-23 22:22:00 +08:00
let win = all_wins.find(win => !win.isDestroyed());
if (win) {
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);
})
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));
})
})
2022-10-29 04:56:39 +08:00
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();
2022-10-29 04:56:39 +08:00
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);
2021-11-12 04:23:27 +08:00
})
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
let app_was_loaded = false;
2021-03-21 03:56:22 +08:00
ipcMain.on('app-loaded', () => {
2020-07-16 15:32:59 +08:00
2022-10-29 04:56:39 +08:00
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;
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;
//autoUpdater.channel = 'beta';
}
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()
})
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-available', a);
})
2021-03-21 03:56:22 +08:00
autoUpdater.on('update-downloaded', (a) => {
console.log('update-downloaded', a)
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-downloaded', a)
2021-03-21 03:56:22 +08:00
})
autoUpdater.on('error', (a) => {
console.log('update-error', a)
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-error', a)
2021-03-21 03:56:22 +08:00
})
autoUpdater.on('download-progress', (a) => {
console.log('update-progress', a)
if (!orig_win.isDestroyed()) orig_win.webContents.send('update-progress', a)
2021-03-21 03:56:22 +08:00
})
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
})