-
-
Notifications
You must be signed in to change notification settings - Fork 747
/
indicator.ts
149 lines (130 loc) · 3.54 KB
/
indicator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { App, ipcMain, Menu, nativeTheme, Tray } from 'electron';
// const dbus = require('./dbus');
import { IPC } from './ipc-events.const';
import { getSettings } from './get-settings';
import { getWin } from './main-window';
let tray;
let isIndicatorRunning = false;
let DIR: string;
const isGnomeShellExtensionRunning = false;
export const initIndicator = ({
IS_LINUX,
IS_GNOME,
IS_MAC,
showApp,
quitApp,
app,
ICONS_FOLDER,
}: {
IS_LINUX: boolean;
IS_GNOME: boolean;
IS_MAC: boolean;
showApp: () => void;
quitApp: () => void;
app: App;
ICONS_FOLDER: string;
}) => {
DIR = ICONS_FOLDER + 'indicator/';
initAppListeners(app);
initListeners();
const suf = nativeTheme.shouldUseDarkColors
? '-d.png'
: '-l.png';
tray = new Tray(DIR + `stopped${suf}`);
tray.setContextMenu(createContextMenu(showApp, quitApp));
tray.on('click', () => {
showApp();
});
isIndicatorRunning = true;
return tray;
// }
};
function initAppListeners(app) {
if (tray) {
app.on('before-quit', () => {
if (tray) {
tray.destroy();
}
});
}
}
function initListeners() {
ipcMain.on(IPC.SET_PROGRESS_BAR, (ev, {progress}) => {
const suf = nativeTheme.shouldUseDarkColors
? '-d'
: '-l';
if (typeof progress === 'number' && progress > 0 && isFinite(progress)) {
const f = Math.min(Math.round(progress * 15), 15);
const t = DIR + `running-anim${suf}/${f || 0}.png`;
tray.setImage(t);
} else {
const t = DIR + `running${suf}.png`;
tray.setImage(t);
}
});
ipcMain.on(IPC.CURRENT_TASK_UPDATED, (ev, params) => {
const currentTask = params.current;
const mainWin = getWin();
getSettings(mainWin, (settings) => {
const isTrayShowCurrentTask = settings.misc.isTrayShowCurrentTask;
let msg = '';
if (isTrayShowCurrentTask && currentTask) {
msg = createIndicatorStr(currentTask);
}
if (tray) {
// tray handling
if (currentTask && currentTask.title) {
tray.setTitle(msg);
} else {
tray.setTitle('');
const suf = nativeTheme.shouldUseDarkColors
? '-d.png'
: '-l.png';
tray.setImage(DIR + `stopped${suf}`);
}
}
});
});
// ipcMain.on(IPC.POMODORO_UPDATE, (ev, params) => {
// const isOnBreak = params.isOnBreak;
// const currentSessionTime = params.currentSessionTime;
// const currentSessionInitialTime = params.currentSessionInitialTime;
// if (isGnomeShellExtInstalled) {
// dbus.updatePomodoro(isOnBreak, currentSessionTime, currentSessionInitialTime);
// }
// });
}
function createIndicatorStr(task) {
if (task && task.title) {
let title = task.title;
let timeStr = '';
let msg;
if (title.length > 50) {
title = title.substring(0, 47) + '...';
}
// TODO replace with our format helpers once we have ts support
if (task.timeSpent) {
const timeSpentAsMinutes = Math.round(task.timeSpent / 60 / 1000);
timeStr += timeSpentAsMinutes.toString();
}
const timeEstimateAsMin = Math.round(task.timeEstimate / 60 / 1000);
if (task.timeEstimate && timeEstimateAsMin > 0) {
timeStr += '/' + timeEstimateAsMin;
}
msg = title + ' | ' + timeStr + 'm ';
return msg;
}
}
function createContextMenu(showApp, quitApp) {
return Menu.buildFromTemplate([
{
label: 'Show App', click: showApp
},
{
label: 'Quit', click: quitApp
}
]);
}
export const isRunning = () => {
return isIndicatorRunning || isGnomeShellExtensionRunning;
};