-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (95 loc) · 2.57 KB
/
index.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
const path = require('path');
const { menubar } = require('menubar');
const { BrowserWindow } = require('electron');
const commandLineArgs = require('command-line-args');
const args = commandLineArgs([
{ name: 'debug', alias: 'd', type: Boolean }
]);
/**
* Get icon path based on day of the month.
* @param {string} appPath
*/
const getTodaysIcon = (appPath) => args.debug
? path.resolve(
`icons/tray/icon_${(new Date().getDate()).toString().padStart(2, '0')}.png`
)
: path.resolve(
appPath,
'..',
`icons/tray/icon_${(new Date().getDate()).toString().padStart(2, '0')}.png`
);
/**
* Update the tray icon.
* @param {Tray} tray
* @param {string} appPath
*/
const setIcon = (tray, appPath) => {
tray.setImage(getTodaysIcon(appPath));
}
/**
* Open new windows in a separate, movable, closable window.
* @param {*} event
* @param {*} url
* @param {*} frameName
* @param {*} disposition
* @param {*} options
* @param {*} additionalFeatures
* @param {*} referrer
* @param {*} postBody
*/
const onNewWindow = (event, url, frameName, disposition, options, additionalFeatures, referrer, postBody) => {
event.preventDefault();
const win = new BrowserWindow({
webContents: options.webContents, // use existing webContents if provided
show: false,
autoHideMenuBar: false,
movable: true,
resizable: true,
focusable: true,
closable: true,
});
win.once('ready-to-show', () => win.show());
if (!options.webContents) {
const loadOptions = {
httpReferrer: referrer
};
if (postBody != null) {
const { data, contentType, boundary } = postBody;
loadOptions.postData = postBody.data;
loadOptions.extraHeaders = `content-type: ${contentType}; boundary=${boundary}`;
}
win.loadURL(url, loadOptions); // existing webContents will be navigated automatically
}
event.newGuest = win;
};
/**
* Create the main menubar application.
*/
const mb = menubar({
dir: path.resolve(__dirname),
index: `https://google.com/calendar`,
browserWindow: {
width: 600,
height: 800,
movable: true,
resizable: true,
focusable: true
}
});
/**
* Update the tray when the app is ready.
*/
mb.on('ready', () => {
// Set the tray icon using today's date
setIcon(mb.tray, mb.app.getAppPath());
// Check every 30s to see if tray icon needs updating to today's date
setInterval(() => {
setIcon(mb.tray, mb.app.getAppPath());
}, 30000);
});
/**
* Listen for new windows being opened.
*/
mb.on('after-create-window', () => {
mb.window.webContents.on('new-window', onNewWindow);
});