Skip to content

Instantly share code, notes, and snippets.

@navio
Created May 10, 2016 02:45
Show Gist options
  • Save navio/23dffbd5ec2464979534857266df2ca7 to your computer and use it in GitHub Desktop.
Save navio/23dffbd5ec2464979534857266df2ca7 to your computer and use it in GitHub Desktop.
Electron Tray Icon
'use strict';
const electron = require('electron');
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const Menu = electron.Menu;
const Tray = electron.Tray;
const Positioner = require('electron-positioner');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X 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();
}
});
app.dock.hide();
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
let appIcon = new Tray(__dirname + '/icon.png');
const contextMenu = Menu.buildFromTemplate([
{ label: 'Companion', accelerator: 'Command+C' },
{
label: 'View',
submenu: [
{
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
click: function() { BrowserWindow.getFocusedWindow().toggleDevTools(); }
},
]
},
{
type: 'separator'
},
{ label: 'Quit', accelerator: 'Command+Q', selector: 'terminate:' }
]);
const appMenu = Menu.buildFromTemplate([{
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{
label: 'Reload',
accelerator: 'Command+R',
click: function() { BrowserWindow.getFocusedWindow().webContents.reloadIgnoringCache(); }
},
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" },
{ label: 'Quit', accelerator: 'Command+Q', selector: 'terminate:' }
]}
]);
app.setApplicationMenu(appMenu);
appIcon.setToolTip('This is my application.');
appIcon.on('click', showWindow);
mainWindow =
new BrowserWindow({ width: 400, height: 600, show: true, frame: false}).on('blur', hideWindow);
mainWindow.loadURL('file://' + __dirname + '/index.html');
positioner = new Positioner(mainWindow);
function showWindow(e, bounds){
if (e.ctrlKey) appIcon.popUpContextMenu(contextMenu);
mainWindow.show();
}
function hideWindow(e, bounds){
mainWindow.hide();
}
// Open the DevTools.
//mainWindow.webContents.openDevTools();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment