Search code examples
javascriptlinuxelectronsystem-traytrayicon

Electron TrayIcon not working properly on linux


I'm trying to make an Electron app that runs on system tray, once the tray icon is clicked, the app window will appears.

I tested it on linux, windows and mac, on windows and mac it works perfectly, when I click the tray icon, the app window appears but on linux, when I click the tray icon a context menu appears (even tough i haven't set it) with the app name and the app window only appears if I click on the app name.

That's how I made the tray

let mainWindow
let tray = null;

function createWindow () {
  mainWindow = new BrowserWindow({
    width: 400,
    height: 500,
    skipTaskbar: true,
    frame: false,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  mainWindow.setMenu(null)

  mainWindow.hide();

  tray = new Tray("./assets/[email protected]");

  tray.on('click', () => {
    mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show()
  })
  mainWindow.on('show', () => {
    tray.setHighlightMode('always')

    const pos = tray.getBounds()

    mainWindow.setPosition(pos.x - 195, pos.y + 30);
  })
  mainWindow.on('hide', () => {
    tray.setHighlightMode('never')
  })

  mainWindow.loadFile('index.html')

  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

I want it to works how it works on windows and mac, when I click the tray icon, the app window appears, someone knows how to do it? Thanks!


Solution

  • just change your tray method from

    tray.setHighlightMode()
    

    to

    tray.setToolTip()
    

    this my code to prevent close win

                win.on('show', () => {
                    //tray.setHighlightMode('always');
                    tray.setToolTip("Server Started");
    
    
                });
                win.on('hide', () => {
                    //tray.popUpContextMenu();
                    tray.setToolTip("Server Started");
                });