Electron is not killing application process, even with app.quit()

Viewed 722

My electron app has a system tray menu and everything is working as is except when I right click the icon in the system tray to fully exit the application to kill the process. The system doesn't do anything. I am trying to fully kill the process if a user does not want the application to run.

I've tried putting "app.quit()" as mentioned in the title however, the application doesn't do anything

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({ width: 350, height: 550, resizable: true})
  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }

  //position the electron window
  var positioner = new Positioner(win)
  positioner.move('bottomRight')

  //logo for the task bar
  win.setIcon('logo/logo.png');

  win.on('minimize',function(event) {
    event.preventDefault();
    win.hide();
  });

  //clicking 'x' will close the app, but the app will still run in system tray (intended)
  win.on('close', function(event) {
    event.preventDefault();
    win.hide();
  })
}

//function to show/hide app when right clicking on the system tray and selecting "show/hide"
function toggleWindow() {
  if (win.isVisible()) {
    win.webContents.send('setVisible', false);
      setTimeout(function () {
        win.hide();
      }, 300);
    }
  else {
    win.show();
    win.webContents.send('setVisible', true);
  }
}

/**
 * setting bottom right tray menu icon
 * added left/right click events for system tray 
 */

let tray = null
app.on('ready', () => {
  tray = new Tray(path.join(__dirname, '../logo/tray_logo.png'))
  tray.on('click', function handleClicked () {
    toggleWindow(); 
  })
  const contextMenu = Menu.buildFromTemplate([
    { label: 'Show/Hide', type: 'normal', click: function () { toggleWindow(); } },
    { label: 'Exit', type: 'normal', click: function() {  } }  //exit is not working. I've tried app.quit();
  ])
  tray.setToolTip('Activity')
  tray.setContextMenu(contextMenu)
})


// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
0 Answers
Related