From 8deb83c4d1e7093a1ae6a484686636e7e51512c1 Mon Sep 17 00:00:00 2001 From: ran9om Date: Fri, 12 Apr 2019 01:06:04 +0300 Subject: [PATCH 1/3] Add system tray support --- main/main.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) 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(); }) From bdb86c301328121a5c678d97d823eb2f299f481c Mon Sep 17 00:00:00 2001 From: ran9om Date: Fri, 12 Apr 2019 14:14:51 +0300 Subject: [PATCH 2/3] Moved image and preferences handling functions to utils.js --- main/main.js | 28 +++++++++++++++------------- main/utils.js | 26 +++++++++++++++++++++++++- package.json | 32 +++++++------------------------- 3 files changed, 47 insertions(+), 39 deletions(-) diff --git a/main/main.js b/main/main.js index ae69d53..12393ce 100644 --- a/main/main.js +++ b/main/main.js @@ -8,15 +8,12 @@ const path = require('path'); const url = require('url'); const instagram = require('./instagram'); const autoUpdater = require('./autoupdater'); - -// on windows it is recommended to use ICO icons to get best visual effects -let trayImage = 'icon.ico'; +const utils = require('./utils'); 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'; } @@ -55,6 +52,8 @@ function createWindow () { if (!app.isQuiting) { event.preventDefault(); mainWindow.hide(); + } else { + mainWidnow = null; } }) @@ -96,24 +95,27 @@ function getChatList () { } function createTray() { - tray = new Tray('//'); //TODO replace insert path to image - tray.on('click', (event, bounds, position) => { - mainWindow.show(); - }); + let tray = new Tray(utils.getTrayImagePath()); + const contextMenu = Menu.buildFromTemplate([ - { label: 'Show App', click: function() { - mainWindow.show(); + { label: 'Toggle Show', click: function(menuItem, BrowserWindow, event) { + if (mainWindow.isVisible()) { + mainWindow.hide(); + } else { + mainWindow.show(); + } } }, { label: 'Light Icon', type : 'checkbox', checked : false, click: function(item) { if (item.checked) { - trayImage = 'l_' + trayImage; + utils.setTrayImageLight(); } else { - trayImage = trayImage.substring(2); + utils.setTrayImageDark(); } - tray.setImage('//'); //TODO insert path to image + tray.setImage(utils.getTrayImagePath()); } }, { label: 'Quit', click: function() { app.isQuiting = true; + tray.destroy(); app.quit(); } } ]); diff --git a/main/utils.js b/main/utils.js index f78822f..c64e4bb 100644 --- a/main/utils.js +++ b/main/utils.js @@ -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') @@ -65,7 +66,30 @@ const getDevice = (username) => { return device; } +//on windows it is recommended to use ICO icons to get best visual effects +let trayImage = 'image_name'; //TODO +if (process.platform != 'win32') { + trayImage = 'image_name'; //TODO +} + +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, 'path_to_image' + 'l_' + trayImage); //TODO + } + return path.join(__dirname, 'path_to_image' + trayImage); //TODO +} + module.exports = { canUseFileStorage, guessUsername, - getCookieStorage, clearCookieFiles, getDevice + getCookieStorage, clearCookieFiles, getDevice, + setTrayImageLight, setTrayImageDark, getTrayImagePath } diff --git a/package.json b/package.json index 869efca..be4c388 100644 --- a/package.json +++ b/package.json @@ -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" }, "repository": { "type": "git", @@ -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", @@ -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": [ @@ -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" + "AppImage" ], "publish": "github", "maintainer": "Ifedapo Olarewaju " From 567f6acfd9830a8677a70297b22e399c6b59e80c Mon Sep 17 00:00:00 2001 From: ran9om Date: Fri, 12 Apr 2019 15:33:56 +0300 Subject: [PATCH 3/3] Fixed Light Icon checkbox --- main/main.js | 2 +- main/utils.js | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/main/main.js b/main/main.js index 12393ce..e433e51 100644 --- a/main/main.js +++ b/main/main.js @@ -105,7 +105,7 @@ function createTray() { mainWindow.show(); } } }, - { label: 'Light Icon', type : 'checkbox', checked : false, click: function(item) { + { label: 'Light Icon', type : 'checkbox', checked : utils.isTrayIconLight(), click: function(item) { if (item.checked) { utils.setTrayImageLight(); } else { diff --git a/main/utils.js b/main/utils.js index c64e4bb..6e4d960 100644 --- a/main/utils.js +++ b/main/utils.js @@ -67,9 +67,13 @@ const getDevice = (username) => { } //on windows it is recommended to use ICO icons to get best visual effects -let trayImage = 'image_name'; //TODO +let trayImage = 'icon.ico'; if (process.platform != 'win32') { - trayImage = 'image_name'; //TODO + trayImage = 'background.png'; +} + +const isTrayIconLight = () => { + return config.get('trayImageTheme') == 'light'; } const setTrayImageLight = () => { @@ -82,14 +86,15 @@ const setTrayImageDark = () => { const getTrayImagePath = () => { let trayImageTheme = config.get('trayImageTheme'); - if (trayImageTheme === 'light') { - return path.join(__dirname, 'path_to_image' + 'l_' + trayImage); //TODO + if (trayImageTheme == 'light') { + return path.join(__dirname, '/../browser/img/' + 'l_' + trayImage); } - return path.join(__dirname, 'path_to_image' + trayImage); //TODO + return path.join(__dirname, '/../browser/img/' + trayImage); } module.exports = { canUseFileStorage, guessUsername, getCookieStorage, clearCookieFiles, getDevice, - setTrayImageLight, setTrayImageDark, getTrayImagePath + setTrayImageLight, setTrayImageDark, getTrayImagePath, + isTrayIconLight }