Manos Kounelakis
Manos Kounelakis

Reputation: 3181

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:

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

Upvotes: 0

Views: 905

Answers (2)

RoyalBingBong
RoyalBingBong

Reputation: 1129

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.

Upvotes: 1

Joshua
Joshua

Reputation: 5332

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

Upvotes: 2

Related Questions