Skip to content

Commit

Permalink
Add system tray support
Browse files Browse the repository at this point in the history
  • Loading branch information
rand0x0m committed Apr 11, 2019
1 parent 024ae72 commit 65b9ff3
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions main/main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
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');
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;

Expand Down Expand Up @@ -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() {
Expand All @@ -71,14 +87,40 @@ function getChatList () {
}
instagram.getChatList(session).then((chats) => {
mainWindow.webContents.send('chatList', chats)

if (chatListTimeoutObj) {
clearTimeout(chatListTimeoutObj)
}
chatListTimeoutObj = setTimeout(getChatList, pollingInterval);
}).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) {
Expand Down Expand Up @@ -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();
})
Expand Down

0 comments on commit 65b9ff3

Please sign in to comment.