From dd7100d1d5bbbca292cdab7f7dceccbf24e625f3 Mon Sep 17 00:00:00 2001 From: salman Date: Sat, 13 Jun 2026 16:37:16 +0200 Subject: [PATCH] Enable auto cfg loading and running from arguments. --- TrimSPelec.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++-- main.js | 28 ++++++++++++++------------ myplots.js | 9 +++++---- package.json | 2 +- 4 files changed, 74 insertions(+), 20 deletions(-) diff --git a/TrimSPelec.js b/TrimSPelec.js index b10d17b..c524aa5 100644 --- a/TrimSPelec.js +++ b/TrimSPelec.js @@ -120,13 +120,51 @@ function fallbackBrowseForFolder() { picker.click(); } +function getConfigValue(content, key) { + const lines = content.toString().split('\n'); + for (const line of lines) { + const trimmed = line.trim(); + if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('[')) { + continue; + } + const separator = trimmed.indexOf('='); + if (separator === -1) { + continue; + } + const param = trimmed.slice(0, separator).trim(); + if (param === key) { + return trimmed.slice(separator + 1).trim(); + } + } + return null; +} + +function resolveConfigWorkPath(filename, content) { + const configuredWorkPath = getConfigValue(content, 'workPath'); + if (!configuredWorkPath) { + return null; + } + if (configuredWorkPath.startsWith('./')) { + return path.resolve(path.dirname(filename), configuredWorkPath); + } + if (path.isAbsolute(configuredWorkPath)) { + return configuredWorkPath; + } + return null; +} + function loadConfigFile(filename, autoRun = false) { - console.log('Open file ' + filename); - fs.readFile(filename.toString(), function read(err, data) { + const configPath = Array.isArray(filename) ? filename[0] : filename.toString(); + console.log('Open file ' + configPath); + fs.readFile(configPath, function read(err, data) { if (err) { throw err; } setValues(data); + const resolvedWorkPath = resolveConfigWorkPath(configPath, data); + if (resolvedWorkPath) { + setWorkFolder([resolvedWorkPath]); + } if (autoRun) { setTimeout(() => startSim(), 0); } @@ -215,3 +253,16 @@ function firstLoad() { fallbackBrowseForFolder(); }); } + +window.addEventListener('load', async function() { + try { + const startupOptions = await ipcRenderer.invoke('getStartupOptions'); + if (startupOptions && startupOptions.configPath) { + setTimeout(() => { + loadConfigFile(startupOptions.configPath, !!startupOptions.autoRun); + }, 0); + } + } catch (err) { + console.log('Failed to read startup options:', err); + } +}); diff --git a/main.js b/main.js index 0415fe0..91766e5 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,6 @@ const path = require('path'); -const { app, BrowserWindow, Menu, dialog, ipcMain, fs } = require('electron'); +const fs = require('fs'); +const { app, BrowserWindow, Menu, dialog, ipcMain } = require('electron'); let currentWorkPath = process.cwd(); const startupOptions = parseStartupOptions(process.argv); @@ -12,11 +13,18 @@ function parseStartupOptions(argv) { autoRun: false, }; - for (const arg of argv.slice(1)) { + for (const arg of argv.slice(2)) { if (arg === '--run') { options.autoRun = true; } else if (!arg.startsWith('--') && options.configPath == null) { - options.configPath = path.resolve(arg); + const candidate = path.resolve(arg); + if ( + path.extname(candidate).toLowerCase() === '.cfg' && + fs.existsSync(candidate) && + fs.statSync(candidate).isFile() + ) { + options.configPath = candidate; + } } } @@ -85,16 +93,6 @@ function createWindow () { } }) - win.webContents.on('did-finish-load', () => { - if (startupOptions.configPath) { - win.webContents.send('openFileAndMaybeRun', { - filename: startupOptions.configPath, - autoRun: startupOptions.autoRun, - }); - } - }); - - let template = [ { label: 'File', @@ -344,6 +342,10 @@ ipcMain.on('updateWorkPath', (event, folder) => { } }); +ipcMain.handle('getStartupOptions', async () => { + return startupOptions; +}); + //ipcMain.on('browseFolder-send', (event, args) => { // dialog.showOpenDialog(null, args).then(filePaths => { // event.sender.dend('browseFolder', filePaths); diff --git a/myplots.js b/myplots.js index a0781dd..e881029 100644 --- a/myplots.js +++ b/myplots.js @@ -102,12 +102,13 @@ function resizePl(flag){ width: newx, height: newy }; + const canRelayout = (plotDiv) => plotDiv && plotDiv.data && plotDiv.layout; if (flag == null) { - Plotly.relayout(plotDiv1,update); - Plotly.relayout(plotDiv2,update); + if (canRelayout(plotDiv1)) Plotly.relayout(plotDiv1,update); + if (canRelayout(plotDiv2)) Plotly.relayout(plotDiv2,update); } else if (flag == 1) { - Plotly.relayout(plotDiv1,update); + if (canRelayout(plotDiv1)) Plotly.relayout(plotDiv1,update); } else if ( flag == 2) { - Plotly.relayout(plotDiv2,update); + if (canRelayout(plotDiv2)) Plotly.relayout(plotDiv2,update); } } diff --git a/package.json b/package.json index bfa4b63..260d502 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build:fortran": "make -C fortran trimspNL-static && cp fortran/trimspNL-static resources/bin/trimspNL", "build:fortran:dynamic": "make -C fortran trimspNL && cp fortran/trimspNL resources/bin/trimspNL", "build:fortran:static": "npm run build:fortran", - "start": "electron-forge start", + "start": "electron-forge start --", "prepackage": "npm run build:fortran", "package": "electron-forge package", "premake": "npm run build:fortran",