Search code examples
electrontrayicontray

window.maximize() fails when window is minimized electron


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


Solution

  • 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();
        }
    }