From dd4d519bbfd3dbce55a17709ba1e950a84b10b59 Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Sun, 30 Apr 2017 20:07:37 +0200 Subject: [PATCH] refactor all tray / indicator stuff into it's own file --- electron/indicator.js | 141 ++++++++++++++++++++++++++++++++++++++++++ electron/main.js | 111 ++++----------------------------- 2 files changed, 153 insertions(+), 99 deletions(-) create mode 100644 electron/indicator.js diff --git a/electron/indicator.js b/electron/indicator.js new file mode 100644 index 0000000000..28eea88c43 --- /dev/null +++ b/electron/indicator.js @@ -0,0 +1,141 @@ +const electron = require('electron'); +const dbus = require('./dbus'); +const fs = require('fs'); +const moment = require('moment'); + +let tray; + +function init(params) { + const IS_LINUX = params.ICONS_FOLDER; + const IS_GNOME = params.ICONS_FOLDER; + const mainWin = params.mainWin; + const showApp = params.showApp; + const quitApp = params.quitApp; + const IS_MAC = params.IS_MAC; + const app = params.app; + const ICONS_FOLDER = params.ICONS_FOLDER; + const isGnomeShellExtensionInstalled = isGnomeShellInstalled(IS_LINUX, IS_GNOME); + + initAppListeners(app); + initListeners(isGnomeShellExtensionInstalled); + + // if we have the gnome shell extension installed set up bus + if (isGnomeShellExtensionInstalled) { + dbus.init({ + mainWin, + quitApp, + showApp, + }); + dbus.setMainWindow(mainWin); + return; + } + + // otherwise create a regular tray icon + else { + // switch tray icon based on + let trayIcoFile; + if (IS_MAC) { + trayIcoFile = 'tray-ico-dark.png' + } else { + trayIcoFile = 'tray-ico.png' + } + + tray = new electron.Tray(ICONS_FOLDER + trayIcoFile); + + tray.setContextMenu(createContextMenu(showApp, quitApp)); + + tray.on('click', () => { + showApp(); + }); + } + + return tray; + +} + +function isGnomeShellInstalled(IS_LINUX, IS_GNOME) { + // check if shell extension is installed + let isGnomeShellExtInstalled = false; + if (IS_LINUX && IS_GNOME) { + const HOME_DIR = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']; + if (fs.existsSync(HOME_DIR + '/.local/share/gnome-shell/extensions/indicator@johannes.super-productivity.com')) { + isGnomeShellExtInstalled = true; + } + } + return isGnomeShellExtInstalled; +} + +function initAppListeners(app) { + if (tray) { + app.on('before-quit', () => { + if (tray) { + tray.destroy(); + } + }); + } +} + +function initListeners(isGnomeShellExtInstalled) { + electron.ipcMain.on('CHANGED_CURRENT_TASK', (ev, params) => { + const currentTask = params.current; + const lastCurrentTask = params.lastCurrent; + + if (currentTask && currentTask.title) { + const msg = createIndicatorStr(currentTask); + + if (tray) { + tray.setTitle(msg); + } + if (isGnomeShellExtInstalled) { + dbus.setTask(currentTask.id, msg); + } + } else if (isGnomeShellExtInstalled && !currentTask && lastCurrentTask && !lastCurrentTask.isDone) { + const msg = createIndicatorStr(lastCurrentTask); + dbus.setTask('PAUSED', msg); + } else { + if (isGnomeShellExtInstalled) { + dbus.setTask('NONE', 'NONE'); + } + } + }); +} + +function createIndicatorStr(task) { + if (task && task.title) { + let title = task.title; + let timeStr = ''; + let msg; + + if (title.length > 50) { + title = title.substring(0, 47) + '...'; + } + + if (task.timeSpent && task.timeSpent._data) { + task.timeSpent = moment.duration(task.timeSpent._data); + timeStr += parseInt(task.timeSpent.asMinutes()).toString(); + } + task.timeEstimate = task.timeEstimate && moment.duration(task.timeEstimate._data); + const timeEstimateAsMin = moment.duration(task.timeEstimate).asMinutes(); + if (task.timeEstimate && timeEstimateAsMin > 0) { + timeStr += '/' + timeEstimateAsMin; + } + + msg = title + ' | ' + timeStr + 'm '; + return msg; + } +} + +function createContextMenu(showApp, quitApp) { + return electron.Menu.buildFromTemplate([ + { + label: 'Show App', click: showApp + }, + { + label: 'Quit', click: quitApp + } + ]); +} + +module.exports = { + init, +}; \ No newline at end of file diff --git a/electron/main.js b/electron/main.js index 18508b18a7..b3e374c7da 100644 --- a/electron/main.js +++ b/electron/main.js @@ -2,7 +2,6 @@ const electron = require('electron'); const powerSaveBlocker = require('electron').powerSaveBlocker; -const moment = require('moment'); const notifier = require('node-notifier'); const open = require('open'); const fs = require('fs'); @@ -10,16 +9,15 @@ const CONFIG = require('./CONFIG'); const ICONS_FOLDER = __dirname + '/assets/icons/'; const IS_MAC = process.platform === 'darwin'; const IS_LINUX = process.platform === 'linux'; -const IS_DEV = process.env.NODE_ENV === 'DEV'; - const DESKTOP_ENV = process.env.DESKTOP_SESSION; const IS_GNOME = (DESKTOP_ENV === 'gnome'); +const IS_DEV = process.env.NODE_ENV === 'DEV'; + +const indicator = require('./indicator'); -const dbus = require('./dbus'); const idle = require('./idle'); const jira = require('./jira'); const gitLog = require('./git-log'); -const pyGtkIndicator = require('./py-gtk-indicator'); powerSaveBlocker.start('prevent-app-suspension'); @@ -37,15 +35,6 @@ let mainWin; let lastIdleTime; let darwinForceQuit = false; -// check if shell extension is installed -let isGnomeShellExtInstalled = false; -if (IS_LINUX && IS_GNOME) { - const HOME_DIR = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME']; - if (fs.existsSync(HOME_DIR + '/.local/share/gnome-shell/extensions/indicator@johannes.super-productivity.com')) { - isGnomeShellExtInstalled = true; - } -} - function createWindow() { let frontendDir; @@ -166,40 +155,16 @@ if (shouldQuitBecauseAppIsAnotherInstance) { // Some APIs can only be used after this event occurs. app.on('ready', createWindow); -let tray = null; app.on('ready', () => { - let trayIcoFile; - - if (isGnomeShellExtInstalled) { - dbus.init({ - mainWin, - quitApp, - showApp, - }); - dbus.setMainWindow(mainWin); - return; - } - - if (IS_MAC) { - trayIcoFile = 'tray-ico-dark.png' - } else { - trayIcoFile = 'tray-ico.png' - } - - tray = new electron.Tray(ICONS_FOLDER + trayIcoFile); - let contextMenu = electron.Menu.buildFromTemplate([ - { - label: 'Show App', click: showApp - }, - { - label: 'Quit', click: quitApp - } - ]); - // mac os only - tray.setContextMenu(contextMenu); - - tray.on('click', () => { - showApp(); + indicator.init({ + app, + mainWin, + showApp, + quitApp, + IS_MAC, + IS_LINUX, + IS_GNOME, + ICONS_FOLDER, }); }); @@ -219,10 +184,6 @@ app.on('ready', () => { }); app.on('before-quit', () => { - if (tray) { - tray.destroy(); - } - // handle darwin if (IS_MAC) { darwinForceQuit = true; @@ -271,54 +232,6 @@ electron.ipcMain.on('NOTIFY', (ev, notification) => { notifier.notify(notification); }); -function createIndicatorStr(task) { - if (task && task.title) { - let title = task.title; - let timeStr = ''; - let msg; - - if (title.length > 50) { - title = title.substring(0, 47) + '...'; - } - - if (task.timeSpent && task.timeSpent._data) { - task.timeSpent = moment.duration(task.timeSpent._data); - timeStr += parseInt(task.timeSpent.asMinutes()).toString(); - } - task.timeEstimate = task.timeEstimate && moment.duration(task.timeEstimate._data); - const timeEstimateAsMin = moment.duration(task.timeEstimate).asMinutes(); - if (task.timeEstimate && timeEstimateAsMin > 0) { - timeStr += '/' + timeEstimateAsMin; - } - - msg = title + ' | ' + timeStr + 'm '; - return msg; - } -} -electron.ipcMain.on('CHANGED_CURRENT_TASK', (ev, params) => { - const currentTask = params.current; - const lastCurrentTask = params.lastCurrent; - - if (currentTask && currentTask.title) { - const msg = createIndicatorStr(currentTask); - - if (tray) { - tray.setTitle(msg); - } - - if (isGnomeShellExtInstalled) { - dbus.setTask(currentTask.id, msg); - } - } else if (isGnomeShellExtInstalled && !currentTask && lastCurrentTask && !lastCurrentTask.isDone) { - const msg = createIndicatorStr(lastCurrentTask); - dbus.setTask('PAUSED', msg); - } else { - if (isGnomeShellExtInstalled) { - dbus.setTask('NONE', 'NONE'); - } - } -}); - function showIdleDialog(idleTimeInMs) { // first show, then send again mainWin.webContents.send('WAS_IDLE', ({