forked from thamara/time-to-leave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
207 lines (188 loc) · 6.5 KB
/
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const { app, BrowserWindow, Menu, dialog, ipcMain, Tray } = require('electron');
const path = require('path');
const Store = require('electron-store');
const { savePreferences } = require('./js/UserPreferences.js');
let savedPreferences = null;
ipcMain.on('PREFERENCE_SAVE_DATA_NEEDED', (event, preferences) => {
savedPreferences = preferences;
});
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;
let tray;
const store = new Store();
const macOS = process.platform === 'darwin';
var iconpath = path.join(__dirname, macOS ? 'assets/timer.png' : 'assets/timer.ico');
var trayIcon = path.join(__dirname, macOS ? 'assets/timer-16.png' : 'assets/timer.ico');
function createWindow () {
// Create the browser window.
var menu = Menu.buildFromTemplate([
{
label: 'Menu',
submenu: [
{
label: 'Preferences',
accelerator: macOS ? 'Command+,' : 'Control+,',
click () {
const htmlPath = path.join('file://', __dirname, 'src/preferences.html');
let prefWindow = new BrowserWindow({ width: 600,
height: 450,
parent: win,
resizable: false,
icon: iconpath,
webPreferences: {
nodeIntegration: true
} });
prefWindow.setMenu(null);
prefWindow.loadURL(htmlPath);
prefWindow.show();
//prefWindow.webContents.openDevTools()
prefWindow.on('close', function () {
prefWindow = null;
savePreferences(savedPreferences);
win.webContents.send('PREFERENCE_SAVED', savedPreferences);
});
},
},
{
label:'Clear database',
click() {
const options = {
type: 'question',
buttons: ['Cancel', 'Yes, please', 'No, thanks'],
defaultId: 2,
title: 'Clear database',
message: 'Are you sure you want to clear all the data?',
};
dialog.showMessageBox(null, options, (response) => {
if (response == 1) {
store.clear();
win.reload();
}
});
}
},
{type:'separator'},
{
label:'Exit',
accelerator: macOS ? 'CommandOrControl+Q' : 'Control+Q',
click() {
app.isQuiting = true;
app.quit();
}
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Cut',
accelerator: 'Command+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'Command+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'Command+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'Command+A',
selector: 'selectAll:'
}
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'CommandOrControl+R',
click () {
BrowserWindow.getFocusedWindow().reload();
}
},
{
label: 'Toggle Developer Tools',
accelerator: macOS ? 'Command+Alt+I' : 'Control+Shift+I',
click () {
BrowserWindow.getFocusedWindow().toggleDevTools();
}
}
]
}
]);
win = new BrowserWindow({
width: 1000,
height: 800,
icon: iconpath,
webPreferences: {
nodeIntegration: true
}
});
if (macOS) {
Menu.setApplicationMenu(menu);
} else {
win.setMenu(menu);
}
// and load the index.html of the app.
win.loadFile(path.join(__dirname, 'index.html'));
tray = new Tray(trayIcon);
var contextMenu = Menu.buildFromTemplate([
{
label: 'Show App', click: function () {
win.show();
}
},
{
label: 'Quit', click: function () {
app.isQuiting = true;
app.quit();
}
}
]);
tray.on('click', function handleCliked() {
win.show();
});
tray.on('right-click', function handleCliked() {
tray.popUpContextMenu(contextMenu);
});
// Open the DevTools.
//win.webContents.openDevTools();
win.on('minimize',function(event){
event.preventDefault();
win.hide();
});
// Emitted when the window is closed.
win.on('closed', function (event) {
if(app.isQuiting != undefined && !app.isQuiting){
event.preventDefault();
win.hide();
}
return false;
});
}
// 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.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
app.isQuiting = true;
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.
if (win === null) {
createWindow();
}
});
// 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.