-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
193 lines (170 loc) · 4.87 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
const { app, BrowserWindow, Tray, Menu, MenuItem, ipcMain, nativeTheme } = require('electron')
const path = require('path')
const Store = require('electron-store');
const AutoLaunch = require('auto-launch')
const DatabaseOps = require('./database.js')
const url = ""
//For storing pre-config -> save in local AppData
const store = new Store();
//For main data
const db = new DatabaseOps()
const createWindow = async () => {
// create the browser window.
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 450,
icon: path.join(__dirname, 'app-icon.png'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
}
})
//create widget window (child window)
const childWin = new BrowserWindow({
parent: mainWindow,
frame: false,
opacity: 0.9,
transparent: true,
width: 450,
maxHeight: 128,
show: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.removeMenu()
mainWindow.setMenu(null)
await mainWindow.loadFile('src/index.html')
if (store.has('theme'))
nativeTheme.themeSource = store.get('theme')
// mainWindow.webContents.openDevTools()
mainWindow.on('minimize', async function (event) {
event.preventDefault();
if (process.platform === 'darwin') {
app.dock.hide();
}
mainWindow.hide();
if (store.has("child-pos") && store.has("child-size")) {
const pos = store.get("child-pos")
const size = store.get("child-size")
childWin.setPosition(pos[0], pos[1])
childWin.setSize(size[0], size[1])
}
if (store.has("widget-alway-top"))
childWin.setAlwaysOnTop(store.get("widget-alway-top"))
childWin.removeMenu()
childWin.setMenu(null)
childWin.loadFile('src/index-child.html')
// childWin.webContents.openDevTools()
childWin.show();
});
mainWindow.on('close', () => {
store.set("child-pos", childWin.getPosition())
store.set("child-size", childWin.getSize())
});
//popup menu
//context menu
const ctxMenu = new Menu()
ctxMenu.append(new MenuItem({
label: "Open App", click: function () {
mainWindow.show();
store.set("child-pos", childWin.getPosition())
store.set("child-size", childWin.getSize())
childWin.hide();
}
}))
childWin.webContents.on("context-menu", (event, params) => {
ctxMenu.popup(childWin, params.x, params.y)
})
//tray menu
var appIcon = null;
appIcon = new Tray(path.join(__dirname, 'app-icon.png'));
var contextMenu = Menu.buildFromTemplate([
{
label: 'Open App', click: function () {
mainWindow.show();
store.set("child-pos", childWin.getPosition())
store.set("child-size", childWin.getSize())
childWin.hide();
}
},
{
label: 'Quit', click: function () {
store.set("child-pos", childWin.getPosition())
store.set("child-size", childWin.getSize())
app.isQuiting = true;
app.quit();
}
}
]);
appIcon.setToolTip('Counties');
appIcon.setContextMenu(contextMenu);
}
app.whenReady().then(() => {
createWindow()
//auto-launch
var autoLaunch = new AutoLaunch({
name: 'Counties',
path: app.getPath('exe'),
});
if (!store.has("auto-launch")) {
store.set("auto-launch", false)
}
store.get('auto-launch') ? autoLaunch.enable() : autoLaunch.disable()
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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
ipcMain.handle("close-win", () => {
app.quit()
})
ipcMain.handle("add-task", async (event, data) => {
const res = await db.add(data)
return res
})
ipcMain.handle("get-tasks", async () => {
const res = await db.getAllData()
return res.rows
})
ipcMain.handle("done-task", async (event, data) => {
const res = await db.updateDoneTask(data)
return res
})
ipcMain.handle("delete-task", async (event, data) => {
const res = await db.delete(data)
return res
})
ipcMain.handle('delete-batch-tasks', async (event, data) => {
const res = await db.deleteBulk(data)
return res
})
ipcMain.handle("update-date-task", async (event, data) => {
// console.log(data)
const res = await db.updateDateTask(data)
return res
})
ipcMain.handle('set-auto-launch', (event, data) => {
store.set('auto-launch', data)
})
ipcMain.handle('set-theme', (event, data) => {
if (data) {
store.set('theme', 'dark')
nativeTheme.themeSource = 'dark'
} else {
store.set('theme', 'light')
nativeTheme.themeSource = 'light'
}
})
ipcMain.handle('set-alway-top', (event, data) => {
store.set('widget-alway-top', data)
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
try {
require('electron-reloader')(module)
} catch (_) { }