Electron: Tray is not a constructor

Created on 22 Jun 2016  路  1Comment  路  Source: electron/electron

var {Menu, Tray} = require('electron')
var path = require('path');
var ipc = require('electron').ipcRenderer;


var soundButtons = document.querySelectorAll('.button-sound');
for(var i=0;i<soundButtons.length;i++){
    var soundButton = soundButtons[i];
    var soundName = soundButton.attributes['data-sound'].value;

    prepareButton(soundButton,soundName);
}

function prepareButton(soundButton,soundName){
    soundButton.querySelector('span').style.backgroundImage = 'url("img/icons/'+soundName+'.png")';
    var audio = new Audio(__dirname+'/wav/'+soundName+'.wav');
    soundButton.addEventListener('click', function () {
        audio.currentTime = 0;
        audio.play();
    });
}

//menu
var trayIcon = null;
if(process.platform === 'darwin'){
    trayIcon = new Tray(path.join(__dirname,'/img/tray-iconTemplate.png'));
}else{
    trayIcon = new Tray(path.join(__dirname,'/img/tray-icon-alt.png'));
}

var trayMenuTemplate = [
    {
        label:'sound machine',
        enable:false
    },
    {
        label:'Settings',
        click:function(){
            ipc.send('open-setting-window');
        }
    },
    {
        label:'Quit',
        click:function(){
            ipc.send('close-main-window');
        }
    }
];
var trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
trayIcon.setContextMenu(trayMenu);



var closeButton = document.querySelector('.close');
closeButton.addEventListener('click', function(){
    ipc.send('close-main-window');
});

ipc.on('global-shortcut',function(event,arg){
    console.log(arg);
    var event = new MouseEvent('click');
    soundButtons[arg].dispatchEvent(event);
});

var settingButton = document.querySelector('.settings');
settingButton.addEventListener('click', function(){
    ipc.send('open-setting-window');
});

the chrome devtools gives me "Uncaught TypeError: Tray is not a constructor". What's wrong with my code?

Most helpful comment

It looks like this code runs in the renderer process since it is accessing the document.

This lines needs to be updated then:

var {Menu, Tray} = require('electron')

should instead be:

var {Menu, Tray} = require('electron').remote

Closing this out.

>All comments

It looks like this code runs in the renderer process since it is accessing the document.

This lines needs to be updated then:

var {Menu, Tray} = require('electron')

should instead be:

var {Menu, Tray} = require('electron').remote

Closing this out.

Was this page helpful?
0 / 5 - 0 ratings