-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
117 lines (94 loc) · 2.5 KB
/
main.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
/// <reference path="main.d.ts" />
// ONE DAY add test suite http://www.protractortest.org/#/
const {app, BrowserWindow, ipcMain, Menu, Tray} = require('electron');
const url = require('url');
const path = require('path');
const Store = require('electron-store');
// // Enable live reload for Electron too
// require('electron-reload')(__dirname, {
// // Note that the path to electron may vary according to the main file
// electron: require(`${__dirname}/node_modules/electron`)
// });
let mainWindow;
let tray = null;
// TODO fix tray context menu behavior
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
},
icon: path.join(__dirname, '/dist/icons/icon@2.png')
});
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, `/dist/index.html`),
protocol: 'file:',
slashes: true
})
);
// Open the DevTools.
// mainWindow.webContents.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null;
});
mainWindow.removeMenu();
mainWindow.on('minimize', (event) => {
event.preventDefault();
mainWindow.hide();
});
mainWindow.on('close', (event) => {
if (!app.isQuiting){
event.preventDefault();
mainWindow.hide();
}
return false;
});
tray = new Tray(path.join(__dirname, '/dist/icons/icon@2.png'));
const contextMenu = Menu.buildFromTemplate([
{ label: 'Open', click: () => {
mainWindow.show();
} },
{ label: 'Quit', click: () => {
app.isQuiting = true;
app.quit();
} }
]);
tray.setToolTip('Gerbil Time Tracker');
tray.setTitle('Gerbil Time Tracker');
tray.setIgnoreDoubleClickEvents(true);
tray.setContextMenu(contextMenu);
tray.on('click', () => {
tray.popUpContextMenu();
});
tray.on('double-click', () => {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
});
}
function saveData(data) {
const store = new Store();
store.set('tasks', data);
}
function loadData() {
const store = new Store();
const tasks = store.get('tasks');
return tasks ? tasks : [];
}
ipcMain.on('save', (event, arg) =>
saveData(arg)
);
ipcMain.on('load', (event, arg) =>
event.sender.send('loadResult', loadData())
);
app.on('ready', () => {
createWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') { app.quit(); }
});
app.on('activate', () => {
if (mainWindow === null) { createWindow(); }
});
app.whenReady().then(() => {
});