Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Menu Items #937

Closed
iamdriz opened this issue Dec 18, 2014 · 4 comments
Closed

Update Menu Items #937

iamdriz opened this issue Dec 18, 2014 · 4 comments

Comments

@iamdriz
Copy link

iamdriz commented Dec 18, 2014

How can I update a Menu Item depending on an event?

For example I have two items for entering and leaving fullscreen:

{
    label: 'View',
    submenu: [
    {
        label: 'Enter Fullscreen',
        accelerator: 'Control+Command+F',
        click: function() {
            BrowserWindow.getFocusedWindow().setFullScreen(true);
        }
    },
    {
        label: 'Exit Fullscreen',
        accelerator: 'Control+Command+F',
        click: function() {
            BrowserWindow.getFocusedWindow().setFullScreen(false);
        }
    }
}

But I want to hide and show them depending if I'm fullscreen or not.

My idea was to create a menu for each state and then change the whole menu like:

mainWindow.on('enter-full-screen', function() {

    Menu.setApplicationMenu(menu);  
});

mainWindow.on('leave-full-screen', function() {

    Menu.setApplicationMenu(menu);
});

But this seems really bad and I'm not sure if you can change the Application Menu afterwards...

Plus I want to be able to enable and disable certain menu items depending on other events and context... is their a way to handle this?

@zcbenz
Copy link
Member

zcbenz commented Dec 18, 2014

You can not bind the same accelerator to two menu items, for your case a toggleFullScreen menu should be better.

To set enable/disable a menu item, you can simply set MenuItem's attributes:

var template = [
  {
    label: 'Atom',
    submenu: [
      {
        label: 'Quit',
        accelerator: 'Command+Q',
        click: function() { app.quit(); }
      },
    ]
  },
  {
    label: 'View',
    submenu: [
      {
        label: 'Item',
      }
    ]
  },
];

menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

menu.items[1].submenu.items[0].enabled = false;

You can also change the visible and checked attributes dynamically too.

But you can not add/remove menu items dynamically (#846) or bind menu item's state to a variable automatically (#528) for now.

@zcbenz zcbenz closed this as completed Dec 18, 2014
@samueleaton
Copy link
Contributor

Can someone add the "menu" label to this issue. It would have helped me find it earlier. Thanks

@fcoury
Copy link

fcoury commented Dec 29, 2015

How can one change a menu item label?

@niftylettuce
Copy link

See #4097 I can't change type since it's readonly.

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

No branches or pull requests

6 participants