mirror of
https://github.com/johannesjo/super-productivity.git
synced 2026-07-30 11:10:04 +00:00
refactor all tray / indicator stuff into it's own file
This commit is contained in:
parent
7ff30fd071
commit
dd4d519bbf
2 changed files with 153 additions and 99 deletions
141
electron/indicator.js
Normal file
141
electron/indicator.js
Normal file
|
|
@ -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,
|
||||
};
|
||||
111
electron/main.js
111
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', ({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue