adding a context menu listener works on dev env but not on production (package)
Closed this issue · 4 comments
Hi,
I added a context menu listener at electron.js, like so:
//electron.js
const electron = require('electron');
const Menu = electron.Menu;
mainWindow.webContents.on('context-menu', () => {
let template = [
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', selector: 'cut:', role: 'cut' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', selector: 'copy:', role: 'copy' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', selector: 'paste:', role: 'paste' },
];
let menu = Menu.buildFromTemplate(template);
menu.popup(mainWindow);
});
This works fine when I run ember electron
but when I run ember electron:package
the menu stops popping up when I right click.
Playing around, I tried adding a similar code in index.html as a tag script and that actually works on dev an prod (package)
//index.html
<script>
const {remote} = requireNode('electron');
const {Menu} = remote;
let template = [
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', selector: 'cut:', role: 'cut' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', selector: 'copy:', role: 'copy' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', selector: 'paste:', role: 'paste' },
];
let menu = Menu.buildFromTemplate(template);
window.addEventListener('contextmenu', (e) => {
e.preventDefault();
menu.popup(remote.getCurrentWindow());
}, false);
</script>
But adding it to index.html looks like an ugly hack. Any idea what is the best way to get this working?
This is most likely a problem with my understanding of electron remote and webContent processes
https://github.com/electron/electron/blob/master/docs/api/remote.md
https://github.com/electron/electron/blob/master/docs/api/web-contents.md#webcontentsexecutejavascriptcode-usergesture
I'm node/electron noob, so I'd really appreciate some help with this.
The source code is at https://github.com/holandes22/exercism-gui
This is likely not an error with ember-electron - do you see any output in the console? If not, can you confirm that the context-menu
event listener fires?
I'd also ensure that you're adding the event listener when webContents is ready.
No error in the console whatsoever, not in the browser console nor at stdout (I run it from the command line to make sure I don't miss any output)
I added a console.log within the callback for context-menu
, indeed is not being triggered on production (during dev, no problem, it works fine).new-window
I added listeners for did-finish-load
and dom-ready
(in webContents) and those work fine both in dev and prod. Also have listeners for will-navigate
and new-window
, again working OK
You mentioned ensuring webContents is ready, but not sure how to do that to be honest.
It baffles me that is stop working with the package version.
BTW, something similar happens with key shortcuts. I used an npm package called electron-debug which adds a shortcut to reload (F5) which works on dev but not on prod.
BTW, I tried with a fresh ember-cli project (2.6.1) and it also occurs.
I added this code to electron.js
let handleContextMenu = (e, _) => {
console.log('context-menu fired');
};
mainWindow.webContents.on('context-menu', handleContextMenu);
@felixrieseberg I'm Ok with closing this as the problem is probably on my side and not on the addon, but If you have any clue of why this happens I'll be very glad to hear your comments.
Quick re-cap: context-menu
event listener is triggered in dev mode but not in package when setting it in the main process (electron.js)