blend/blend-settings/main.js

199 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-02-11 06:03:29 -06:00
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const path = require('path')
const pty = require("node-pty");
2023-06-24 00:13:36 -05:00
var mainWindow, terminalWindow, packageWindow, ptyProcess
2023-02-11 06:03:29 -06:00
2023-04-10 23:54:31 -05:00
app.commandLine.appendSwitch('enable-transparent-visuals');
app.disableHardwareAcceleration();
2023-06-24 00:13:36 -05:00
require('@electron/remote/main').initialize()
2023-02-11 06:03:29 -06:00
function createWindow() {
mainWindow = new BrowserWindow({
minWidth: 1000,
minHeight: 600,
maxWidth: 1000,
maxHeight: 600,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
contextIsolation: false,
enableRemoteModule: true,
sandbox: false,
preload: path.join(__dirname, 'src/preload.js')
},
autoHideMenuBar: true
})
2023-05-02 13:24:00 -05:00
// mainWindow.setMenu(null)
2023-02-11 06:03:29 -06:00
mainWindow.loadFile('src/index.html')
}
function createTerminalWindow() {
2023-06-24 00:13:36 -05:00
let t_window_settings = {
2023-02-11 06:03:29 -06:00
minWidth: 800,
minHeight: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
sandbox: false
},
autoHideMenuBar: true,
show: false
2023-06-24 00:13:36 -05:00
}
if (process.argv.length > 2) {
if (process.argv[2] == 'package') {
t_window_settings['frame'] = false
}
}
terminalWindow = new BrowserWindow(t_window_settings)
2023-02-11 06:03:29 -06:00
terminalWindow.loadFile('src/pages/terminal.html')
// terminalWindow.setMenu(null)
2023-02-11 06:03:29 -06:00
}
2023-06-24 00:13:36 -05:00
function createPackageWindow() {
packageWindow = new BrowserWindow({
minWidth: 450,
minHeight: 450,
width: 450,
height: 450,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
sandbox: false
},
autoHideMenuBar: true
})
packageWindow.loadFile('src/package-installer.html')
require("@electron/remote/main").enable(packageWindow.webContents)
// packageWindow.setMenu(null)
}
2023-02-11 06:03:29 -06:00
function loadTerminalWindow(title, cmd) {
if (terminalWindow.isDestroyed()) {
createTerminalWindow()
}
if (terminalWindow.isVisible() && (title.startsWith('Container: ') && terminalWindow.getTitle().startsWith('Creating container: '))) {
dialog.showErrorBox('Error opening container window', 'You cannot open a container window while creating a container.')
return
}
if (terminalWindow.isVisible() && title.startsWith('Container: ')) {
dialog.showErrorBox('Error opening container window', 'You must close any other container windows before opening a new one.')
return
}
if (terminalWindow.isVisible() && (title.startsWith('Creating container: ') && terminalWindow.getTitle().startsWith('Creating container: '))) {
dialog.showErrorBox('Error creating container', 'You cannot create two containers simultaneously.')
return
}
if (terminalWindow.isVisible() && title.startsWith('Creating container: ')) {
dialog.showErrorBox('Error creating container', 'You must close the container window before creating a container.')
return
}
terminalWindow.show()
ipcMain.removeAllListeners('terminal.reset')
2023-05-02 13:24:00 -05:00
ipcMain.removeAllListeners('terminal.resize')
2023-02-11 06:03:29 -06:00
ipcMain.removeAllListeners('terminal.keystroke')
ipcMain.removeAllListeners('terminal.incomingData')
ipcMain.removeAllListeners('title')
terminalWindow.removeAllListeners()
terminalWindow.on('close', e => {
if (terminalWindow.getTitle().startsWith('Creating container: ')) {
dialog.showMessageBox(terminalWindow, {
'type': 'question',
'title': 'Are you sure?',
'message': "A container is being created. Closing this window will prevent you from seeing its creation status. Are you sure?",
'buttons': [
'Yes',
'No'
]
}).then((result) => {
if (result.response === 0) {
terminalWindow.hide()
ptyProcess.destroy()
}
})
2023-06-24 00:13:36 -05:00
} else if (terminalWindow.getTitle().startsWith('Package installation')) {
e.preventDefault()
2023-02-11 06:03:29 -06:00
} else {
terminalWindow.hide()
ptyProcess.destroy()
}
e.preventDefault()
})
terminalWindow.webContents.send("terminal.reset")
terminalWindow.webContents.send("title", title);
ptyProcess = pty.spawn('/bin/bash', ['-c', cmd], {
name: "xterm-color",
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
ptyProcess.on('data', data => {
if (!terminalWindow.isDestroyed()) {
terminalWindow.webContents.send("terminal.incomingData", data)
}
});
ptyProcess.on('exit', () => {
if (!terminalWindow.isDestroyed()) {
terminalWindow.webContents.send("terminal.reset")
terminalWindow.hide()
if (title.startsWith('Creating container: ')) {
mainWindow.webContents.send("container-created")
2023-06-24 00:13:36 -05:00
} else if (title.startsWith('Package installation')) {
packageWindow.webContents.send("installation-complete")
2023-02-11 06:03:29 -06:00
}
}
})
ipcMain.on("terminal.keystroke", (event, key) => {
ptyProcess.write(key)
});
ipcMain.on("terminal.resize", (event, size) => {
ptyProcess.resize(size[0], size[1])
});
}
app.whenReady().then(() => {
app.allowRendererProcessReuse = false
2023-06-24 00:13:36 -05:00
if (process.argv.length > 2) {
if (process.argv[2] == 'package') {
createPackageWindow()
}
} else {
createWindow()
2023-06-24 00:13:36 -05:00
}
2023-02-11 06:03:29 -06:00
createTerminalWindow()
ipcMain.on('create-term', (event, data) => {
loadTerminalWindow(data['title'], data['cmd'])
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})