Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system tray support #886

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions main/main.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
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');
const utils = require('./utils');

// fixes electron's timeout inconsistency
// not doing this on windows because the fix doesn't work for windows.
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();
}


const RATE_LIMIT_DELAY = 60000;
let pollingInterval = 10000;

Expand Down Expand Up @@ -45,7 +48,19 @@ function createWindow () {
}))
})

mainWindow.on('closed', () => mainWindow = null)
mainWindow.on('close', function (event) {
if (!app.isQuiting) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just use this clause instead, or not?

event.preventDefault();
mainWindow.hide();
} else {
mainWidnow = null;
}
})

mainWindow.on('minimize', function (event) {
event.preventDefault();
mainWindow.hide();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this functionality should probably only apply to a windows machine. Also, what is the difference between "minimize" and "hide"? 🤔

})
}

function createCheckpointWindow() {
Expand All @@ -71,14 +86,43 @@ 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() {
let tray = new Tray(utils.getTrayImagePath());

const contextMenu = Menu.buildFromTemplate([
{ label: 'Toggle Show', click: function(menuItem, BrowserWindow, event) {
if (mainWindow.isVisible()) {
mainWindow.hide();
} else {
mainWindow.show();
}
} },
{ label: 'Light Icon', type : 'checkbox', checked : utils.isTrayIconLight(), click: function(item) {
if (item.checked) {
utils.setTrayImageLight();
} else {
utils.setTrayImageDark();
}
tray.setImage(utils.getTrayImagePath());
} },
{ label: 'Quit', click: function() {
app.isQuiting = true;
tray.destroy();
app.quit();
} }
]);

tray.setContextMenu(contextMenu);
}

let chatTimeoutObj;
let messagesThread;
function getChat (evt, id) {
Expand Down Expand Up @@ -118,11 +162,12 @@ app.setAppUserModelId('com.ifedapoolarewaju.desktop.igdm')

app.on('ready', () => {
createWindow();
createTray();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this functionality should probably only apply to a windows machine

// 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
31 changes: 30 additions & 1 deletion main/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const Client = require('instagram-private-api').V1;
const app = require('electron').app;
const path = require('path');
const config = require('electron-json-config');

const buildAndGetStoragePath = () => {
const storagePath = path.join(app.getPath('userData'), 'session-cookie')
Expand Down Expand Up @@ -65,7 +66,35 @@ const getDevice = (username) => {
return device;
}

//on windows it is recommended to use ICO icons to get best visual effects
let trayImage = 'icon.ico';
if (process.platform != 'win32') {
trayImage = 'background.png';
}

const isTrayIconLight = () => {
return config.get('trayImageTheme') == 'light';
}

const setTrayImageLight = () => {
config.set('trayImageTheme', 'light');
}

const setTrayImageDark = () => {
config.set('trayImageTheme', 'dark');
}

const getTrayImagePath = () => {
let trayImageTheme = config.get('trayImageTheme');
if (trayImageTheme == 'light') {
return path.join(__dirname, '/../browser/img/' + 'l_' + trayImage);
}
return path.join(__dirname, '/../browser/img/' + trayImage);
}

module.exports = {
canUseFileStorage, guessUsername,
getCookieStorage, clearCookieFiles, getDevice
getCookieStorage, clearCookieFiles, getDevice,
setTrayImageLight, setTrayImageDark, getTrayImagePath,
isTrayIconLight
}
32 changes: 7 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
"description": "Desktop application for Instagram DMs",
"main": "main/main.js",
"scripts": {
"start": "gulp build && electron .",
"pack": "gulp build && build --dir",
"dist": "gulp build && build -mwl --x64"
"pack": "electron-builder --dir",
"dist": "electron-builder"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain the reason for this change? Why are we now running the command this way?

},
"repository": {
"type": "git",
Expand All @@ -29,7 +28,8 @@
"fb": "^2.0.0",
"instagram-private-api": "github:ifedapoolarewaju/instagram-private-api#a66fd1c1a15297877a66ac10f41de70432bd11f4",
"node-notifier": "^5.1.2",
"nojs": "^0.1.1"
"nojs": "^0.1.1",
"electron-json-config": "^1.5.3"
},
"devDependencies": {
"electron": "2.0.12",
Expand All @@ -39,7 +39,8 @@
"gulp": "^4.0.0",
"gulp-htmlmin": "^4.0.0",
"gulp-pug": "^3.3.0",
"jstransformer-markdown-it": "^2.0.0"
"jstransformer-markdown-it": "^2.0.0",
"electron-json-config": "^1.5.3"
},
"build": {
"publish": [
Expand All @@ -51,28 +52,9 @@
],
"appId": "com.ifedapoolarewaju.desktop.igdm",
"copyright": "ifedapo Olarewaju 2018",
"mac": {
"target": [
"dmg",
"zip"
],
"publish": "github",
"category": "public.app-category.social-networking"
},
"win": {
"target": [
"nsis",
"portable"
],
"publish": "github",
"publisherName": "ifedapoolarewaju"
},
"linux": {
"target": [
"snap",
"AppImage",
"zip",
"deb"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the reason for removing the build config?

"AppImage"
],
"publish": "github",
"maintainer": "Ifedapo Olarewaju <[email protected]>"
Expand Down