You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Maybe I missunderstood something but trying to overwrite some delegates in a newly created Menu will not call my custom function:
menu.delegate.menuWillShow = function () {
console.log('MY MENU DELEGATE');
}
menu.delegate.menuWillShow(); // doesn't call my function??
Note that the Menu is created using remote call within a BrowserWindow.
Reacted by Sindre Sorhus, Antonio Stoilkov, Jakub Žitný, Raj Nigam, Dara Hak, Sascha Ißbrücker, Blixt, Kenny Williams, alexcpendleton, ClumsyLee and 13 more
Has any progress been made on the front of exposing the delegates? I'm trying to build a data driven menu system for an application where the enabled state of menu items are driven by a function and just ran into this issue.
Reacted by Raj Nigam, Lipis, Samuel Attard, Sindre Sorhus, Tom Chapman, Dara Hak, Benjamin Weiss, Kevin Murphy, Afshin Moazami, Garrett Criss and 3 more
@ncortines@kennyjwilli Please don't comment "+1". GitHub has reactions now, simply use the "thumbs up" reaction instead of commenting which adds nothing to the conversation and spams everyone's inboxes 👍
Reacted by Raj Nigam, Jens Habegger, Dara Hak, Benjamin Weiss, Jem, Vadim Macagon, Kevin Murphy, Tev, ProPuke, Steve Cosman and 7 more
Not sure if it's the best solution, but I managed to do it the "dirty" way.
Basically, I sent a message to ipcMain from ipcRenderer to "rebuild" the menu using the initial template. So whenever I add things to the menus and want to go back to the starting template I simply send a message to ipcMain.
It's not ideal, but at least it's something (since .clear() didn't really work):
From renderer:
ipcRenderer.sendSync('rebuild-menu');
At main process:
// cachedTemplate is the initial template used when bootstrapping the applicationipcMain.on('rebuild-menu',(event,arg)=>{letresetMenu=Menu.buildFromTemplate(cachedTemplate);Menu.setApplicationMenu(resetMenu);event.returnValue='Rebuilt electron menu.';});
NOTE that if you don't provide a returnValue, the application halts.
I know it's not ideal, but since there's no official and documented way of removing things from Menu, I had to basically reset the whole thing on demand.
I'm trying to implement a "recent items" feature and I'm having difficulties adding/removing MenuItems at run-time. I came across this comment here #608 (comment), which led me here. I just wanted to confirm that there is at the moment no way to dynamically add and remove MenuItems or if I'm just doing something wrong. Specifically, I'm trying to add and remove MenuItems from a submenu collection of another MenuItem.
However, menuItem.submenu.append(new MenuItem{}) seems to add the items, but they only become visible after a state change of the parent, i.e. enabling then disabling it.
If you are using setApplicationMenu to rebuild and rerender the whole menu every time you want to modify a menu item, it might be worthwhile to look into some memory leak issues. In our application, we use the trick to work around the limitation (can't dynamically change menu label etc). But it seems just allocating a MenuItem (while rebuilding menu) in JS causes memory leaks.
Menu.clear is mentioned above as an attempt to clean up, but in practice I found that you need to call Menu.destroy as well to further reclaim memory, and I'm not sure it's the proper way to do it / if it claims all memory, as both clear and destroy are not exposed in API doc.
I can probably look into it a bit more when I get some time.
Reacted by Jens Habegger, ChesuCR, Charles Kerr, David Rodrigue, Sebastian Schmidt, zer09, Tony Anziano, Nikita Saltykov and Kot
Menu.clear is mentioned above as an attempt to clean up, but in practice I found that you need to call Menu.destroy as well to further reclaim memory, and I'm not sure it's the proper way to do it / if it claims all memory, as both clear and destroy are not exposed in API doc.
Thanks, it works, except using destroy method leads main process to crash in case of fast multiple updates of menu (doing it in a way where we destroy and generate a new template again).
For now, trying to find a solution beside dummy throttle with value about 500ms or more.
You could reproduce this via creating a function that destroys menu, then generates template and sets to app; then, call it multiple times straight away. Launching app with that code will crash app. (macOS 10.15, Electron 5.0.3)
Hey everyone! Thank you all so much for giving Electron a try and thinking about ways to make it better. As you can probably tell from how this issue has been open for more than ten years, so far it has never been important enough for someone to close it directly or to otherwise make the resources available for it to happen.
If you're reading this and are thinking to yourself "Alright, I'll do it!" - good for you! For smaller changes, you can usually open a well-tested and documented PR directly on this repository. For larger changes, I'd recommend that you start with a "request for comments". This will ensure that you don't spend a lot of time on a larger change without getting feedback about your approach first. Alternatively, you could always hire a consultant, either from the maintainer community our outside, to perform the work that's important to you.
In short, I'm closing this issue because we're currently not planning this work, but if you care deeply about this issue, we'd welcome your contribution.
Activity
quasado commentedon Jul 29, 2014
I can confirm that running the code within browser context in main.js works as expected and calls my function correctly:
menu.delegate.menuWillShow = function () {
console.log('MENU_WILL_SHOW!!');
}
How am I supposed to do the same within the client/render context?
zcbenz commentedon Jul 29, 2014
Currently there is no support for overriding the menu delegate on user's side, but we may support it in future.
[-]MenuItem delegate and remote[/-][+]Override menu delegate[/+]quasado commentedon Jul 29, 2014
I understand that however running the code in browser context works quite fine so we could use this as a hack for now. But doing the same in browser context doesn't so is there any way for this?
Note: We're using Atom-Shell for a really complex app (http://www.gravit.io) and this is the last thing hindering us in releasing our desktop version so I'd be happy to find a hack at least .. thanks!!
hhsnopek commentedon Feb 27, 2015
@zcbenz Any updates on this? - I know that nw.js has this capability; any chance that we could implement it similar to theirs?
dekarguy commentedon Oct 13, 2015
Has any progress been made on the front of exposing the delegates? I'm trying to build a data driven menu system for an application where the enabled state of menu items are driven by a function and just ran into this issue.
JayBizzle commentedon Oct 14, 2015
Yep, also needing this functionality!
petrvecera commentedon Nov 13, 2015
We would really love to have this functionality in our product!
reiberdatschi23 commentedon Nov 30, 2015
Any update on this yet since I've started this topic over a year ago ;) ?
chanpory commentedon Dec 7, 2015
I'd love to see this functionality as well. Perhaps an early holiday present for us?
kamesh-a commentedon Dec 23, 2015
+1
jamesmorgan commentedon Jan 7, 2016
+1 This would be a great feature!
mwhearty commentedon Jan 14, 2016
+1 Could really use dynamic menu functionality in our app.
9 remaining items
ncortines commentedon Oct 19, 2016
+1
MarshallOfSound commentedon Oct 19, 2016
@ncortines @kennyjwilli Please don't comment "+1". GitHub has reactions now, simply use the "thumbs up" reaction instead of commenting which adds nothing to the conversation and spams everyone's inboxes 👍
nLamprok commentedon Dec 16, 2016
Not sure if it's the best solution, but I managed to do it the "dirty" way.
Basically, I sent a message to
ipcMainfromipcRendererto "rebuild" the menu using the initial template. So whenever I add things to the menus and want to go back to the starting template I simply send a message toipcMain.It's not ideal, but at least it's something (since .clear() didn't really work):
From renderer:
At main process:
NOTE that if you don't provide a
returnValue, the application halts.I know it's not ideal, but since there's no official and documented way of removing things from Menu, I had to basically reset the whole thing on demand.
eriedl-kiban commentedon Apr 19, 2017
I'm trying to implement a "recent items" feature and I'm having difficulties adding/removing MenuItems at run-time. I came across this comment here #608 (comment), which led me here. I just wanted to confirm that there is at the moment no way to dynamically add and remove MenuItems or if I'm just doing something wrong. Specifically, I'm trying to add and remove MenuItems from a submenu collection of another MenuItem.
However,
menuItem.submenu.append(new MenuItem{})seems to add the items, but they only become visible after a state change of the parent, i.e. enabling then disabling it.Thank you.
zhenchaoli commentedon Feb 8, 2018
If you are using
setApplicationMenuto rebuild and rerender the whole menu every time you want to modify a menu item, it might be worthwhile to look into some memory leak issues. In our application, we use the trick to work around the limitation (can't dynamically change menu label etc). But it seems just allocating a MenuItem (while rebuilding menu) in JS causes memory leaks.Menu.clearis mentioned above as an attempt to clean up, but in practice I found that you need to callMenu.destroyas well to further reclaim memory, and I'm not sure it's the proper way to do it / if it claims all memory, as bothclearanddestroyare not exposed in API doc.I can probably look into it a bit more when I get some time.
Anrock commentedon Sep 4, 2018
Isn't this implemented via #11754 ?
caseywebdev commentedon Sep 5, 2018
No, the PR you linked implements events for the system menu but does not improve the state of dynamic menu updates, which is what this issue is about.
nickensoul commentedon Oct 28, 2019
@fantasticsid
Thanks, it works, except using
destroymethod leads main process to crash in case of fast multiple updates of menu (doing it in a way where we destroy and generate a new template again).For now, trying to find a solution beside dummy throttle with value about 500ms or more.
You could reproduce this via creating a function that destroys menu, then generates template and sets to app; then, call it multiple times straight away. Launching app with that code will crash app. (macOS 10.15, Electron 5.0.3)
felixrieseberg commentedon Jan 17, 2025
Hey everyone! Thank you all so much for giving Electron a try and thinking about ways to make it better. As you can probably tell from how this issue has been open for more than ten years, so far it has never been important enough for someone to close it directly or to otherwise make the resources available for it to happen.
If you're wondering what you can do to get this into Electron, great news - Electron is truly open source! We, the maintainers, welcome outside contributions - especially from individuals and companies interested in joining us in maintaining this platform.
If you're reading this and are thinking to yourself "Alright, I'll do it!" - good for you! For smaller changes, you can usually open a well-tested and documented PR directly on this repository. For larger changes, I'd recommend that you start with a "request for comments". This will ensure that you don't spend a lot of time on a larger change without getting feedback about your approach first. Alternatively, you could always hire a consultant, either from the maintainer community our outside, to perform the work that's important to you.
In short, I'm closing this issue because we're currently not planning this work, but if you care deeply about this issue, we'd welcome your contribution.