Skip to content

Instantly share code, notes, and snippets.

@craigvantonder
Last active November 27, 2016 09:48
Show Gist options
  • Save craigvantonder/9617538996b4bac497bd6531e882f832 to your computer and use it in GitHub Desktop.
Save craigvantonder/9617538996b4bac497bd6531e882f832 to your computer and use it in GitHub Desktop.
Electron main process example for tray icon which minimizes / maximizes the app when clicked.
const electron = require('electron');
const app = electron.app;
const Tray = electron.Tray;
const BrowserWindow = electron.BrowserWindow;
// We create a null variables so that the tray and mainWindow
// don’t get removed by garbage collection
let tray = null;
let mainWindow = null;
app.on('window-all-closed', () => {
if (process.platform != 'darwin') {
app.quit();
}
});
app.on('ready', () => {
// http://stackoverflow.com/a/36232831/2110294
tray = new Tray(`${__dirname}/icon-32x32.png`);
// When the tray icon is clicked
tray.on('click', () => {
// http://stackoverflow.com/q/38193739/2110294
if (mainWindow.isVisible()) {
mainWindow.hide()
} else {
mainWindow.show()
}
});
// Add the tool tip
tray.setToolTip('Sample Application');
mainWindow = new BrowserWindow({
width: 800,
height: 600
});
mainWindow.loadURL(`file://${__dirname}/index.html`);
mainWindow.on('closed', () => {
tray = null,
mainWindow = null;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment