forked from thamara/time-to-leave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esm-main.js
190 lines (169 loc) · 4.97 KB
/
esm-main.js
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
/*eslint-disable no-useless-escape*/
'use strict';
import { app, ipcMain } from 'electron';
const { createWindow, createMenu, getMainWindow, triggerStartupDialogs } = require('./js/main-window');
const { createNotification } = require('./js/notification');
const { openWaiverManagerWindow } = require('./js/windows.js');
const { setupI18n, getCurrentTranslation, setLanguageChangedCallback } = require('./src/configs/i18next.config.js');
const { handleSquirrelEvent } = require('./js/squirrel.js');
const { showAlert, showDialogSync } = require('./js/window-aux.cjs');
import { appConfig } from './js/app-config.cjs';
import { setupCalendarStore } from './main/calendar-aux.js';
if (appConfig.win32)
{
if (handleSquirrelEvent(app))
{
// squirrel event handled and app will exit in 1000ms, so don't do anything else
app.quit();
}
}
ipcMain.on('SET_WAIVER_DAY', (event, waiverDay) =>
{
global.waiverDay = waiverDay;
const mainWindow = getMainWindow();
openWaiverManagerWindow(mainWindow);
});
ipcMain.handle('GET_WAIVER_DAY', () =>
{
return global.waiverDay;
});
ipcMain.handle('USER_DATA_PATH', () =>
{
return new Promise((resolve) =>
{
resolve(app.getPath('userData'));
});
});
ipcMain.on('SHOW_ALERT', (event, alertMessage) =>
{
showAlert(alertMessage);
});
ipcMain.handle('SHOW_DIALOG', (event, dialogOptions) =>
{
return showDialogSync(dialogOptions);
});
let launchDate = new Date();
// Logic for recommending user to punch in when they've been idle for too long
let recommendPunchIn = false;
setTimeout(() => { recommendPunchIn = true; }, 30 * 60 * 1000);
process.on('uncaughtException', function(err)
{
if (!err.message.includes('net::ERR_NETWORK_CHANGED'))
{
console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
process.exit(1);
}
});
function checkIdleAndNotify()
{
if (recommendPunchIn)
{
recommendPunchIn = false;
createNotification(getCurrentTranslation('$Notification.punch-reminder')).show();
}
}
function refreshOnDayChange()
{
const mainWindow = getMainWindow();
if (mainWindow === null)
{
return;
}
const today = new Date();
if (today > launchDate)
{
const oldDate = launchDate.getDate();
const oldMonth = launchDate.getMonth();
const oldYear = launchDate.getFullYear();
launchDate = today;
// Reload only the calendar itself to avoid a flash
mainWindow.webContents.send('REFRESH_ON_DAY_CHANGE', oldDate, oldMonth, oldYear);
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
// Check first to see if the app is aleady running,
// fail out gracefully if so.
if (!app.requestSingleInstanceLock())
{
app.exit(0);
}
else
{
app.on('second-instance', () =>
{
// Someone tried to run a second instance, we should focus our window.
const mainWindow = getMainWindow();
if (mainWindow)
{
if (mainWindow.isMinimized())
{
mainWindow.restore();
}
mainWindow.focus();
}
});
}
app.on('ready', () =>
{
setupI18n(createMenu).then(() =>
{
// On other platforms the header is automatically set, but on windows
// we need to force the name so it doesn't appear as `electron.app.Electron`
if (process.platform === 'win32')
{
app.setAppUserModelId('Time to Leave');
}
createWindow();
createMenu();
setupCalendarStore();
setLanguageChangedCallback(createMenu);
triggerStartupDialogs();
setInterval(refreshOnDayChange, 60 * 60 * 1000);
const { powerMonitor } = require('electron');
powerMonitor.on('unlock-screen', () => { checkIdleAndNotify(); });
powerMonitor.on('resume', () => { checkIdleAndNotify(); });
});
});
// Emitted before the application starts closing its windows.
// It's not emitted when closing the windows
app.on('before-quit', () =>
{
app.isQuitting = true;
});
// Quit when all windows are closed.
app.on('window-all-closed', () =>
{
app.quit();
});
app.on('activate', () =>
{
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
const mainWindow = getMainWindow();
if (mainWindow === null)
{
createWindow();
}
else
{
mainWindow.show();
}
});
const env = process.env.NODE_ENV || 'development';
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
if (env === 'development')
{
try
{
require('electron-reloader')(module);
}
catch (_)
{
// eslint-disable-next-line no-empty
// We don't need to do anything in this block.
}
}