Open File Dialog From Menu


#1

Hi,

I’m trying to build a simple app on a windows 7 machine with electron and want to use the Menu Item File => Open to show a file open dialog.

But it refuses to show.

My code to register to the event:

app.on('open-file', function(e) {
    e.preventDefault();
    dialog.showOpenDialog({properties: [ 'openFile'], filters: [{ name: 'GPX', extensions: ['gpx'] }]});
});

What am I doing wrong? Can anybody help me?


#2

I came up with a workaround:. The menu was exchanged with a self defined one and there the file dialog is called.

Here is a snippet:

 var showOpen = function() {
	dialog.showOpenDialog({ properties: [ 'openFile'], filters: [{ name: 'GPX', extensions: ['gpx'] }]});
};

 var template = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Open',
        click: function() { showOpen(); }
      }
    ]
  }
]
menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

#3

Spend some times on the doc and can’t really find an answer, I post here since it’s pretty much related.
I got the default menubar, and want to add my own. Thing is I took the code from the doc, and added it right in the body of the page between two .

Obviously this solution is bad, so I decided to create a .js containing the menu creation and stuff, so now I got , which is better but… I have to add this line to EACH window’s html file ?
Can’t we define in the main.js our own default menubar that would apply to each window ?