0

I have created an electron app with a tray Icon. When I right click the tray icon I show a menu with 4 options:

  • Minimize
  • Maximize
  • Restart
  • Exit

Here is the code that creates the tray Icon:

    trayIcon = new Tray('icons/foo.png');
    const trayMenuTemplate = [{
        label: 'Maximize',
        click:(_,window)=>{
            window.maximize();
        }
    }, {
        label: 'Minimize',
        click:(_,window)=>{
            window.minimize();
        }
    }, {
        label: 'Restart'
    }, {
        type: 'separator'
    }, {
        label: 'Quit',
        role: 'quit'
    }];

However I have a problem.When I click Minimize and then I click Maximize I get an error saying Cannnot read property maximize of null Any ideas?Thanks

Manos Kounelakis
  • 2,848
  • 5
  • 31
  • 55

2 Answers2

2

You can always check if it's minimized and restore it as a workaround. I don't think this is such a big deal.

To check and restore it you can use this:

if (window.isMinimized()) {
    window.restore();
}

The whole thing would be like this:

{
    label: 'Maximize',
    click:(_,window)=>{
        if (window.isMinimized()) {
            window.restore();
        }
        window.maximize();
    }
}
Joshua
  • 5,032
  • 2
  • 29
  • 45
1

The tray is not bound to any BrowserWindow, thus window is null. You can just use your mainWindow reference if you created the tray menu in your Main-process or remote.getCurrentWindow() if you are in the Renderer.

RoyalBingBong
  • 1,106
  • 7
  • 15