k5web/main.js

79 lines
2.3 KiB
JavaScript
Raw Normal View History

const { app, BrowserWindow, dialog } = require('electron/main');
2024-02-04 20:04:55 +00:00
function createWindow() {
2024-02-04 20:04:55 +00:00
const mainWindow = new BrowserWindow({
width: 1280,
height: 760,
autoHideMenuBar: true
});
2024-02-04 20:04:55 +00:00
mainWindow.webContents.session.on('select-serial-port', async (event, portList, webContents, callback) => {
// Add listeners to handle ports being added or removed before the callback for `select-serial-port` is called.
2024-02-04 20:04:55 +00:00
mainWindow.webContents.session.on('serial-port-added', (event, port) => {
console.log('serial-port-added FIRED WITH', port);
2024-02-04 20:04:55 +00:00
// Optionally update portList to add the new port
});
2024-02-04 20:04:55 +00:00
mainWindow.webContents.session.on('serial-port-removed', (event, port) => {
console.log('serial-port-removed FIRED WITH', port);
2024-02-04 20:04:55 +00:00
// Optionally update portList to remove the port
});
event.preventDefault();
2024-02-04 20:04:55 +00:00
if (portList && portList.length > 0) {
// Only keep the last 5 ports
const lastFivePorts = portList.slice(-5);
// Prepare options for dialog box
const options = {
type: 'question',
buttons: lastFivePorts.map(port => port.portName), // Display port names as choices
title: 'Select Serial Port',
message: 'Please select a serial port to use:'
};
const result = await dialog.showMessageBox(mainWindow, options);
if (result.response >= 0) {
callback(lastFivePorts[result.response].portId); // Callback with the selected port ID
} else {
callback(''); // No port selected
}
2024-02-04 20:04:55 +00:00
} else {
callback(''); // No ports available
2024-02-04 20:04:55 +00:00
}
});
2024-02-04 20:04:55 +00:00
mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
if (permission === 'serial' && details.securityOrigin === 'file:///') {
return true;
2024-02-04 20:04:55 +00:00
}
return false;
});
2024-02-04 20:04:55 +00:00
mainWindow.webContents.session.setDevicePermissionHandler((details) => {
if (details.deviceType === 'serial' && details.origin === 'file://') {
return true;
2024-02-04 20:04:55 +00:00
}
return false;
});
2024-02-04 20:04:55 +00:00
mainWindow.loadFile('./dist/index.html');
2024-02-04 20:04:55 +00:00
// mainWindow.webContents.openDevTools();
2024-02-04 20:04:55 +00:00
}
app.whenReady().then(() => {
createWindow();
2024-02-04 20:04:55 +00:00
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
2024-02-04 20:04:55 +00:00
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});