-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (159 loc) · 5.12 KB
/
main.js
File metadata and controls
195 lines (159 loc) · 5.12 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
//to run from node: npm run start
//to generate exe: npm run package
const {app, Tray, Menu, BrowserWindow} = require('electron');
const ipc = require('electron').ipcMain;
const moment = require('moment-timezone');
const path = require('path');
const url = require('url');
const config = require('./config');
const {utils} = require('./utils');
const isMacOs = process.platform === 'darwin';
const trayIcon = isMacOs ? 'tray-icon-Template.png' : 'tray-icon-invert.ico';
const appIcon = isMacOs ? 'app-icon.icns' : 'app-icon.ico';
const trayIconPath = path.join(__dirname, `icons/${trayIcon}`);
const appIconPath = path.join(__dirname, `icons/${appIcon}`);
const dockIconPath = path.join(__dirname, 'icons/app-icon.png');
let tray = null;
let settingsWin = null;
let planningWin = null;
let menuVisible = false;
/*
TODO:
Settings dialog- make selection for new timezone a typeahead
Create MSI installer with https://stackoverflow.com/questions/36398955/electron-create-msi-installer-using-electron-builder
*/
function updateContextMenu() {
//don't update the menu item while it is visible. try again in 5 seconds.
if(menuVisible)
return;
const localTz = moment.tz.guess(true);
const ts = moment.tz(localTz);
const template = [];
//start with i=-1, which indicates local time zone which is always drawn at the top with a separator.
for(let i = -1; i < config.timezones.length; i++) {
const tz = (i < 0 ? {code: localTz, label: 'Local Time'} : config.timezones[i]);
const formatted = utils.formatTimestamp(ts, tz, config);
const diffDay = formatDayDiff(formatted.dayDiff);
if(isMacOs)
template.push({label: `${formatted.tzTime}${diffDay} - ${formatted.label}`});
else
template.push({label: formatted.label, sublabel: `${formatted.tzTime}${diffDay}`});
if(i < 0)
template.push({type: 'separator'});
}
template.push({type: 'separator'});
template.push({label: 'Planning...', click: showPlanning});
template.push({label: 'Settings...', click: showSettings});
template.push({label: 'Quit', click: () => app.exit(0)});
let menu = Menu.buildFromTemplate(template);
menu.on('menu-will-show', function() { menuVisible = true; });
menu.on('menu-will-close', function() { menuVisible = false; updateContextMenu(); });
tray.setContextMenu(menu);
}
function formatDayDiff(diff) {
if(diff === 0)
return '';
if(diff === -1)
return ' (yesterday)';
if(diff === 1)
return ' (tomorrow)';
const sign = diff < 0 ? '-' : '+';
const num = Math.abs(diff);
return ` (${sign}${num} days)`;
}
function initSettingsWindow() {
//initialize settings window but don't display yet.
settingsWin = new BrowserWindow({
title: 'Everytime Settings',
width: 700,
height: 600,
minWidth: 600,
minHeight: 400,
show: false,
icon: appIconPath,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
settingsWin.setMenu(null);
settingsWin.loadURL(url.format({
pathname: path.join(__dirname, 'settings.html'),
protocol: 'file:',
slashes: true,
}));
settingsWin.on('close', function(e) {
//on close, just hide the window instead of closing
e.preventDefault();
settingsWin.hide();
});
}
function showSettings() {
settingsWin.webContents.send('send-config', config.serialize());
settingsWin.show();
}
function initPlanningWindow() {
//initialize settings window but don't display yet.
planningWin = new BrowserWindow({
title: 'Everytime Planning',
width: 700,
height: 600,
minWidth: 600,
minHeight: 400,
show: false,
icon: appIconPath,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
planningWin.setMenu(null);
planningWin.loadURL(url.format({
pathname: path.join(__dirname, 'planning.html'),
protocol: 'file:',
slashes: true,
}));
planningWin.on('close', function(e) {
//on close, just hide the window instead of closing
e.preventDefault();
planningWin.hide();
});
}
function showPlanning() {
if(planningWin.isVisible()) {
planningWin.focus();
}
else{
planningWin.webContents.send('send-config', config.serialize());
planningWin.webContents.send('before-show');
planningWin.show();
}
}
app.on('ready', function() {
tray = new Tray(trayIconPath);
if(isMacOs) {
app.dock.setIcon(dockIconPath);
app.dock.hide();
}
config.loadConfig();
initSettingsWindow();
initPlanningWindow();
ipc.on('config-updated', function(e, _config) {
config.autoLaunch = _config.autoLaunch;
config.timeFormat = _config.timeFormat;
config.offsetDisplay = _config.offsetDisplay;
config.timezones = _config.timezones;
config.saveConfig();
updateContextMenu();
planningWin.webContents.send('send-config', config.serialize());
});
tray.on('double-click', showPlanning);
ipc.on('debug-message', (e, m) => console.log(m));
//function runs at the start of every minute to update menu
let updateTimer = function() {
updateContextMenu();
setTimeout(updateTimer, 60000 - (Date.now() % 60000));
};
updateTimer();
});