diff --git a/main/main.js b/main/main.js index bdb4794..ae69d53 100644 --- a/main/main.js +++ b/main/main.js @@ -1,6 +1,7 @@ const electron = require('electron'); const app = electron.app; const Menu = electron.Menu; +const Tray = electron.Tray; const menuTemplate = require('./menutemplate'); const BrowserWindow = electron.BrowserWindow; const path = require('path'); @@ -8,12 +9,17 @@ const url = require('url'); const instagram = require('./instagram'); const autoUpdater = require('./autoupdater'); -// fixes electron's timeout inconsistency -// not doing this on windows because the fix doesn't work for windows. +// on windows it is recommended to use ICO icons to get best visual effects +let trayImage = 'icon.ico'; + if (process.platform != 'win32') { + // fixes electron's timeout inconsistency + // not doing this on windows because the fix doesn't work for windows. require('./timeout-shim').fix(); + trayImage = 'background.png'; } + const RATE_LIMIT_DELAY = 60000; let pollingInterval = 10000; @@ -45,7 +51,17 @@ function createWindow () { })) }) - mainWindow.on('closed', () => mainWindow = null) + mainWindow.on('close', function (event) { + if (!app.isQuiting) { + event.preventDefault(); + mainWindow.hide(); + } + }) + + mainWindow.on('minimize', function (event) { + event.preventDefault(); + mainWindow.hide(); + }) } function createCheckpointWindow() { @@ -71,7 +87,7 @@ function getChatList () { } instagram.getChatList(session).then((chats) => { mainWindow.webContents.send('chatList', chats) - + if (chatListTimeoutObj) { clearTimeout(chatListTimeoutObj) } @@ -79,6 +95,32 @@ function getChatList () { }).catch(() => setTimeout(getChatList, RATE_LIMIT_DELAY)) } +function createTray() { + tray = new Tray('//'); //TODO replace insert path to image + tray.on('click', (event, bounds, position) => { + mainWindow.show(); + }); + const contextMenu = Menu.buildFromTemplate([ + { label: 'Show App', click: function() { + mainWindow.show(); + } }, + { label: 'Light Icon', type : 'checkbox', checked : false, click: function(item) { + if (item.checked) { + trayImage = 'l_' + trayImage; + } else { + trayImage = trayImage.substring(2); + } + tray.setImage('//'); //TODO insert path to image + } }, + { label: 'Quit', click: function() { + app.isQuiting = true; + app.quit(); + } } + ]); + + tray.setContextMenu(contextMenu); +} + let chatTimeoutObj; let messagesThread; function getChat (evt, id) { @@ -118,11 +160,12 @@ app.setAppUserModelId('com.ifedapoolarewaju.desktop.igdm') app.on('ready', () => { createWindow(); + createTray(); // only set the menu template when in production mode/ // this also leaves the dev console enabled when in dev mode. if (!process.defaultApp) { const menu = Menu.buildFromTemplate(menuTemplate); - Menu.setApplicationMenu(menu); + Menu.setApplicationMenu(menu); } autoUpdater.init(); })