mirror of
https://github.com/silenty4ng/k5web
synced 2025-04-02 05:35:10 +00:00
parent
fae888ba45
commit
58828b26dc
1 changed files with 44 additions and 30 deletions
74
main.js
74
main.js
|
@ -1,64 +1,78 @@
|
||||||
const { app, BrowserWindow } = require('electron/main')
|
const { app, BrowserWindow, dialog } = require('electron/main');
|
||||||
|
|
||||||
function createWindow () {
|
function createWindow() {
|
||||||
const mainWindow = new BrowserWindow({
|
const mainWindow = new BrowserWindow({
|
||||||
width: 1280,
|
width: 1280,
|
||||||
height: 760,
|
height: 760,
|
||||||
autoHideMenuBar: true
|
autoHideMenuBar: true
|
||||||
})
|
});
|
||||||
|
|
||||||
mainWindow.webContents.session.on('select-serial-port', (event, portList, webContents, callback) => {
|
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`
|
// Add listeners to handle ports being added or removed before the callback for `select-serial-port` is called.
|
||||||
// is called.
|
|
||||||
mainWindow.webContents.session.on('serial-port-added', (event, port) => {
|
mainWindow.webContents.session.on('serial-port-added', (event, port) => {
|
||||||
console.log('serial-port-added FIRED WITH', port)
|
console.log('serial-port-added FIRED WITH', port);
|
||||||
// Optionally update portList to add the new port
|
// Optionally update portList to add the new port
|
||||||
})
|
});
|
||||||
|
|
||||||
mainWindow.webContents.session.on('serial-port-removed', (event, port) => {
|
mainWindow.webContents.session.on('serial-port-removed', (event, port) => {
|
||||||
console.log('serial-port-removed FIRED WITH', port)
|
console.log('serial-port-removed FIRED WITH', port);
|
||||||
// Optionally update portList to remove the port
|
// Optionally update portList to remove the port
|
||||||
})
|
});
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
event.preventDefault()
|
|
||||||
if (portList && portList.length > 0) {
|
if (portList && portList.length > 0) {
|
||||||
console.log(portList[portList.length - 1])
|
// Only keep the last 5 ports
|
||||||
callback(portList[portList.length - 1].portId)
|
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
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// eslint-disable-next-line n/no-callback-literal
|
callback(''); // No ports available
|
||||||
callback('') // Could not find any matching devices
|
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
|
mainWindow.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
|
||||||
if (permission === 'serial' && details.securityOrigin === 'file:///') {
|
if (permission === 'serial' && details.securityOrigin === 'file:///') {
|
||||||
return true
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false;
|
||||||
})
|
});
|
||||||
|
|
||||||
mainWindow.webContents.session.setDevicePermissionHandler((details) => {
|
mainWindow.webContents.session.setDevicePermissionHandler((details) => {
|
||||||
if (details.deviceType === 'serial' && details.origin === 'file://') {
|
if (details.deviceType === 'serial' && details.origin === 'file://') {
|
||||||
return true
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false;
|
||||||
})
|
});
|
||||||
|
|
||||||
mainWindow.loadFile('./dist/index.html')
|
mainWindow.loadFile('./dist/index.html');
|
||||||
|
|
||||||
// mainWindow.webContents.openDevTools()
|
// mainWindow.webContents.openDevTools();
|
||||||
}
|
}
|
||||||
|
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
createWindow()
|
createWindow();
|
||||||
|
|
||||||
app.on('activate', function () {
|
app.on('activate', function () {
|
||||||
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
app.on('window-all-closed', function () {
|
app.on('window-all-closed', function () {
|
||||||
if (process.platform !== 'darwin') app.quit()
|
if (process.platform !== 'darwin') app.quit();
|
||||||
})
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue