Permalink
Browse files

appears in tray

  • Loading branch information...
saraford committed Jul 27, 2016
1 parent a48e578 commit fd80ad897fcae85409452aab9ad9c0db1df87340
Showing with 70 additions and 10 deletions.
  1. +1 −1 index.html
  2. +69 −9 main.js
  3. BIN umbrellaTemplate.png
@@ -38,4 +38,4 @@
<h1 id="zen-phrase">Hello World!</h1>
<br>
<button id="zen-button">Get Zen!</button>
<a href="url" id="close-window">close this window</a>
<a href="url" id="close-window">close this app!</a>
78 main.js
@@ -1,19 +1,79 @@
'use strict';

const {app, BrowserWindow, ipcMain} = require('electron');
const {app, BrowserWindow, ipcMain, Tray} = require('electron');

var mainWindow = null;
var tray = undefined;
var mainWindow = undefined;

app.on('ready', function() {

mainWindow = new BrowserWindow({
height: 300,
width: 400
});

mainWindow.loadURL('file://' + __dirname + '/index.html');
createTray();
createWindow();
});

ipcMain.on('close-app', function () {
app.quit();
});

const createTray = () => {
tray = new Tray(__dirname + '/umbrellaTemplate.png');
tray.on('click', function (event) {
toggleWindow();
});
}

const toggleWindow = () => {
if (mainWindow.isVisible()) {
mainWindow.hide()
} else {
showWindow()
}
}

const showWindow = () => {
const position = getWindowPosition()
mainWindow.setPosition(position.x, position.y, false)
mainWindow.show()
mainWindow.focus()
}

const getWindowPosition = () => {
const windowBounds = mainWindow.getBounds()
const trayBounds = tray.getBounds()

// Center window horizontally below the tray icon
const x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2))

// Position window 4 pixels vertically below the tray icon
const y = Math.round(trayBounds.y + trayBounds.height + 4)

return {x: x, y: y}
}

const createWindow = () => {

mainWindow = new BrowserWindow({
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: true,
height: 400,
width: 350,
webPreferences: {
// Prevents renderer process code from not running when window is
// hidden
backgroundThrottling: false
}
})

mainWindow.loadURL('file://' + __dirname + '/index.html');
//mainWindow.webContents.openDevTools();


// Hide the window when it loses focus
mainWindow.on('blur', () => {
if (!mainWindow.webContents.isDevToolsOpened()) {
mainWindow.hide()
}
})
}
BIN +265 Bytes umbrellaTemplate.png
Binary file not shown.

0 comments on commit fd80ad8

Please sign in to comment.