Skip to content

Menu delegates and data-driven menus #528

Closed as not planned
Closed as not planned

Description

@quasado

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.

Activity

  1. quasado commented on Jul 29, 2014

    @quasado
    Author

    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!!');
    }

    menu.delegate.menuWillShow();
    

    How am I supposed to do the same within the client/render context?

  2. zcbenz commented on Jul 29, 2014

    @zcbenz
    Contributor

    Currently there is no support for overriding the menu delegate on user's side, but we may support it in future.

  3. changed the title [-]MenuItem delegate and remote[/-] [+]Override menu delegate[/+] on Jul 29, 2014
  4. quasado commented on Jul 29, 2014

    @quasado
    Author

    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!!

  5. hhsnopek commented on Feb 27, 2015

    @hhsnopek

    @zcbenz Any updates on this? - I know that nw.js has this capability; any chance that we could implement it similar to theirs?

  6. dekarguy commented on Oct 13, 2015

    @dekarguy

    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.

  7. JayBizzle commented on Oct 14, 2015

    @JayBizzle

    Yep, also needing this functionality!

  8. petrvecera commented on Nov 13, 2015

    @petrvecera

    We would really love to have this functionality in our product!

  9. reiberdatschi23 commented on Nov 30, 2015

    @reiberdatschi23

    Any update on this yet since I've started this topic over a year ago ;) ?

  10. chanpory commented on Dec 7, 2015

    @chanpory

    I'd love to see this functionality as well. Perhaps an early holiday present for us?

  11. kamesh-a commented on Dec 23, 2015

    @kamesh-a

    +1

  12. jamesmorgan commented on Jan 7, 2016

    @jamesmorgan

    +1 This would be a great feature!

  13. mwhearty commented on Jan 14, 2016

    @mwhearty

    +1 Could really use dynamic menu functionality in our app.

  14. 9 remaining items

  15. ncortines commented on Oct 19, 2016

    @ncortines

    +1

  16. MarshallOfSound commented on Oct 19, 2016

    @MarshallOfSound
    Member

    @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 👍

  17. nLamprok commented on Dec 16, 2016

    @nLamprok

    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 application
    
    ipcMain.on('rebuild-menu', (event, arg) => {
      let resetMenu = 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.

  18. eriedl-kiban commented on Apr 19, 2017

    @eriedl-kiban

    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.

  19. zhenchaoli commented on Feb 8, 2018

    @zhenchaoli

    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.

  20. deleted a comment from marvinschopf on Mar 30, 2018
  21. Anrock commented on Sep 4, 2018

    @Anrock
    Contributor

    Isn't this implemented via #11754 ?

  22. caseywebdev commented on Sep 5, 2018

    @caseywebdev

    Isn't this implemented via #11754 ?

    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.

  23. self-assigned this
    on Sep 23, 2018
  24. removed their assignment
    on Sep 30, 2018
  25. nickensoul commented on Oct 28, 2019

    @nickensoul

    @fantasticsid

    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)

  26. felixrieseberg commented on Jan 17, 2025

    @felixrieseberg
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions