Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
appears in tray
  • Loading branch information
saraford committed Jul 27, 2016
1 parent a48e578 commit fd80ad8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -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 changes: 69 additions & 9 deletions 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()
}
})
}
Binary file added umbrellaTemplate.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fd80ad8

Please sign in to comment.