Skip to content

Commit 65b9ff3

Browse files
committed
Add system tray support
1 parent 024ae72 commit 65b9ff3

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

main/main.js

+48-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
const electron = require('electron');
22
const app = electron.app;
33
const Menu = electron.Menu;
4+
const Tray = electron.Tray;
45
const menuTemplate = require('./menutemplate');
56
const BrowserWindow = electron.BrowserWindow;
67
const path = require('path');
78
const url = require('url');
89
const instagram = require('./instagram');
910
const autoUpdater = require('./autoupdater');
1011

11-
// fixes electron's timeout inconsistency
12-
// not doing this on windows because the fix doesn't work for windows.
12+
// on windows it is recommended to use ICO icons to get best visual effects
13+
let trayImage = 'icon.ico';
14+
1315
if (process.platform != 'win32') {
16+
// fixes electron's timeout inconsistency
17+
// not doing this on windows because the fix doesn't work for windows.
1418
require('./timeout-shim').fix();
19+
trayImage = 'background.png';
1520
}
1621

22+
1723
const RATE_LIMIT_DELAY = 60000;
1824
let pollingInterval = 10000;
1925

@@ -45,7 +51,17 @@ function createWindow () {
4551
}))
4652
})
4753

48-
mainWindow.on('closed', () => mainWindow = null)
54+
mainWindow.on('close', function (event) {
55+
if (!app.isQuiting) {
56+
event.preventDefault();
57+
mainWindow.hide();
58+
}
59+
})
60+
61+
mainWindow.on('minimize', function (event) {
62+
event.preventDefault();
63+
mainWindow.hide();
64+
})
4965
}
5066

5167
function createCheckpointWindow() {
@@ -71,14 +87,40 @@ function getChatList () {
7187
}
7288
instagram.getChatList(session).then((chats) => {
7389
mainWindow.webContents.send('chatList', chats)
74-
90+
7591
if (chatListTimeoutObj) {
7692
clearTimeout(chatListTimeoutObj)
7793
}
7894
chatListTimeoutObj = setTimeout(getChatList, pollingInterval);
7995
}).catch(() => setTimeout(getChatList, RATE_LIMIT_DELAY))
8096
}
8197

98+
function createTray() {
99+
tray = new Tray('//'); //TODO replace insert path to image
100+
tray.on('click', (event, bounds, position) => {
101+
mainWindow.show();
102+
});
103+
const contextMenu = Menu.buildFromTemplate([
104+
{ label: 'Show App', click: function() {
105+
mainWindow.show();
106+
} },
107+
{ label: 'Light Icon', type : 'checkbox', checked : false, click: function(item) {
108+
if (item.checked) {
109+
trayImage = 'l_' + trayImage;
110+
} else {
111+
trayImage = trayImage.substring(2);
112+
}
113+
tray.setImage('//'); //TODO insert path to image
114+
} },
115+
{ label: 'Quit', click: function() {
116+
app.isQuiting = true;
117+
app.quit();
118+
} }
119+
]);
120+
121+
tray.setContextMenu(contextMenu);
122+
}
123+
82124
let chatTimeoutObj;
83125
let messagesThread;
84126
function getChat (evt, id) {
@@ -118,11 +160,12 @@ app.setAppUserModelId('com.ifedapoolarewaju.desktop.igdm')
118160

119161
app.on('ready', () => {
120162
createWindow();
163+
createTray();
121164
// only set the menu template when in production mode/
122165
// this also leaves the dev console enabled when in dev mode.
123166
if (!process.defaultApp) {
124167
const menu = Menu.buildFromTemplate(menuTemplate);
125-
Menu.setApplicationMenu(menu);
168+
Menu.setApplicationMenu(menu);
126169
}
127170
autoUpdater.init();
128171
})

0 commit comments

Comments
 (0)