ditsikts ditsikts - 3 months ago 41
JSON Question

Electron, new BrowserWindow load values

I try load and pass from a json file the x,y position and width, height. Here is my code

const electron = require('electron')
const {app, BrowserWindow} = electron
var fs = require('fs');
var loadsttngs = JSON.parse(fs.readFileSync('settings.json', 'utf8'));
console.log(loadsttngs.width);
let win

function createWindow () {
win = new BrowserWindow({
x: loadsttngs.x,
y: loadsttngs.y,
width: loadsttngs.width,
height: loadsttngs.height,
frame: false})

win.setMenu(null)
win.loadURL(`file://${__dirname}/index.html`)

win.webContents.openDevTools()

// Emitted when the window is closed.
win.on('closed', () => {
//var bounds = win.getBounds();
win = null
})
}


the
console.log(loadsttngs.width);
return the right value, so there is no problem with reading the file but in the
new BrowserWindow
does not be used(like using some defaults values). If I write the values directly to
new BrowserWindow
works fine.

settings.json
{
"x" : "50",
"y" : "50",
"width" : "1200",
"height" : "200",
"maximized": "false"
}

Answer

Your x and y values are strings not numbers.

Should be:

"x":50

Maximized is also a string not a Boolean.