From 333e326bd2550992ac772357de48438eaa251812 Mon Sep 17 00:00:00 2001 From: renatn Date: Mon, 2 Nov 2015 18:54:29 +0300 Subject: [PATCH] Fixed Minimize to tray #7 --- electron/main.js | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/electron/main.js b/electron/main.js index a15c55c1..8eb3a72d 100644 --- a/electron/main.js +++ b/electron/main.js @@ -6,11 +6,16 @@ var app = require('app'), BrowserWindow = require('browser-window'), path = require('path'), - fs = require('fs'); + fs = require('fs'), + Tray = require('tray'), + Menu = require('menu'); var mainWindow = null, openFile = process.argv.filter(function(arg) { return /\.kdbx$/i.test(arg); })[0], - ready = false; + ready = false, + appIcon = null, + quiting = false, + firstTray = true; app.on('window-all-closed', function() { app.quit(); }); app.on('ready', function() { @@ -33,13 +38,49 @@ app.on('ready', function() { notifyOpenFile(); }); mainWindow.on('closed', function() { mainWindow = null; }); + + mainWindow.on('close', function(e) { + if (!quiting) { + e.preventDefault(); + mainWindow.minimize(); + mainWindow.setSkipTaskbar(true) + if (firstTray) { + appIcon.displayBalloon({title: 'KeeWeb', content: 'Application minimized to tray!'}); + firstTray = false; + } + } + }); + + appIcon = new Tray(path.join(__dirname, 'icon.png')); + var contextMenu = Menu.buildFromTemplate([ + { + label: 'Open KeeWeb', + click: restoreMainWindow + }, + { + label: 'Quit KeeWeb', + click: function() { + quiting = true; + mainWindow.close(); + } + } + ]); + appIcon.on('clicked', restoreMainWindow) + appIcon.setContextMenu(contextMenu); + appIcon.setToolTip('KeeWeb Desktop'); }); + app.on('open-file', function(e, path) { e.preventDefault(); openFile = path; notifyOpenFile(); }); +function restoreMainWindow() { + mainWindow.restore(); + mainWindow.setSkipTaskbar(false); +} + function notifyOpenFile() { if (ready && openFile && mainWindow) { openFile = openFile.replace(/"/g, '\\"').replace(/\\/g, '\\\\');