Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reopen electron application which in tray, from the dock or task-bar icon in Windows?

I have an electron application which uses Vue for the templating part. The application will move to the Tray if we click on the close button. I need to re-Launch the application from the dock/app-icon/task-bar icon which is not happening.

Not able to re-open the application from Tray or we can say its already active.

Bellow is my electron main process code:

'use strict'

import fs from 'fs'
import path from 'path'
import { app, BrowserWindow, shell, ipcMain } from 'electron'
import { autoUpdater } from 'electron-updater'
import request from 'request'

if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:3600`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    minWidth: 800,
    width: 1600,
    height: 900,
    center: true,
    frame: false,
    backgroundColor: '#1A1C1F'
  })

  mainWindow.maximize()
  mainWindow.loadURL(winURL)

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

  mainWindow.webContents.on('new-window', (e, url) => {
    e.preventDefault()
    shell.openExternal(url)
  })

  let cliArgs = process.argv
  ipcMain.on('get-cliArgs', (event, args) => {
    event.sender.send('cliArgs', cliArgs)
  })

  if (process.env.NODE_ENV !== 'production') {
    mainWindow.webContents.openDevTools()
  }
}

var shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
  // Someone tried to run a second instance, we should focus our window.
  if (mainWindow) {
    if (mainWindow.isMinimized()) mainWindow.restore()
    mainWindow.focus()
  }
})

if (shouldQuit) {
  app.quit()
  process.exit()
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  app.quit()
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
like image 823
Abhilash KK Avatar asked Dec 07 '25 05:12

Abhilash KK


1 Answers

Which command are you using for minimizing you window to tray ?

In our application we do the following for minimizing to tray and restoring the window when needed:

for minimizing:

win.minimize();

for creating Tray commands which restoring the window or quitting the app:

  tray = new Tray('<path to app icon>');
  let contextMenu = Menu.buildFromTemplate([
    {
      label: 'showApp', click:  () => {
        win.maximize();
        win.setAlwaysOnTop(true);
      }
    },
    {
      label: 'quit', click:  () => {
        app.isQuiting = true;
        app.quit();
      }
    }
  ]);
  tray.setToolTip('app name');
  tray.setContextMenu(contextMenu);
  tray.on('right-click', () => {
    tray.popUpContextMenu();
  })
  tray.on('click', () => {
    win.maximize();
    win.setAlwaysOnTop(true);
    // restore overlay icon when going back from tray
    setOverlayIcon(currentOverlayIcon);
  });
like image 197
Shai Avatar answered Dec 08 '25 20:12

Shai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!