Need advice on menu click for function call


#1

Hi there I am struggling with a function call from electron menu.

I did some research but nothing of what i found helped me to solve my problem.

What I want to do is to call a function defined in main.js from a menu entry click.
i.e:

main.js (I did cut what I think is relevant part of it)

let win

function createWindow (position) {
var width = 350;
position.x -= width/2;
win = new BrowserWindow({
show: false,
center: false,
resizable: false,
alwaysOnTop: true,
fullscreenable: false,
frame:false,
backgroundColor: ‘#cccccc’,
width: width,
height: 145,
x: position.x,
y: position.y,
webPreferences: {
experimentalFeatures: true
}
})

win.loadURL(url.format({
pathname: path.join(__dirname, ‘index.html’),
protocol: ‘file:’,
slashes: true
}))

win.once(‘ready-to-show’, () => {
win.show()
})

win.webContents.openDevTools()

win.on(‘closed’, () => {
win = null
})

require(’./app.menu’)
}

function test(){
console.log(“test”);
}

app.menu.js:

const {BrowserWindow, Menu, MenuItem} = require(‘electron’)
const electron = require(‘electron’)
const app = electron.app

const template = [
{
label: ‘Edit’,
submenu: [
{
label: ‘edit DB’,
click(){
test();
}
},
{
label: ‘open config Folder’,
click: function(){
electron.shell.openItem(app.getPath(“userData”));
}
},
{
role: ‘cut’
},
{
role: ‘copy’
},
{
role: ‘paste’
},
{
role: ‘delete’
}
]
},
{
role: ‘window’,
submenu: [
{
role: ‘minimize’
},
{
role: ‘close’
}
]
}/,
{
role: ‘help’,
submenu: [
{
label: ‘Learn More’,
click () { require(‘electron’).shell.openExternal(‘http://electron.atom.io’) }
}
]
}
/
]

const menu = Menu.buildFromTemplate(template)

Menu.setApplicationMenu(menu)

So how can I achieve this? I cant understand were menu is located in process hierarchy - I tried to figure it out by moving the initialisation and logging ‘this’ from inside the click.

how do i get reference to main.js and or main ‘win’.

The goal is to create a new window from inside main.js and fill it with data.

Thanks in advance.


#2

When sharing code, you should either post a link to an external site or make sure to wrap your code with markdown tags so that it’s easy to read.