简体   繁体   中英

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/icon@2x.png");

  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!

This is an open issue with the electron project, there is nothing wrong with your code.

https://github.com/electron/electron/issues/14941

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM