-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (97 loc) · 2.52 KB
/
index.js
File metadata and controls
105 lines (97 loc) · 2.52 KB
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
const electron = require('electron')
const path = require('path')
const { app, BrowserWindow, ipcMain, Tray, Menu, screen, dialog } = electron
const iconPath = path.join(__dirname, './src/img/icon.png')
let mainWindow
let tray
let remindWindow
app.on('ready', () => {
mainWindow = new BrowserWindow({
frame: false,
resizable: false,
width: 800,
height: 600,
icon: iconPath,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: true,
contextIsolation: false
}
})
mainWindow.loadURL(`file://${__dirname}/src/main.html`)
mainWindow.removeMenu()
tray = new Tray(iconPath)
tray.setToolTip('Tasky')
tray.on('click', () => {
if (mainWindow.isVisible()) {
mainWindow.hide()
} else {
mainWindow.show()
}
})
tray.on('right-click', () => {
const menuConfig = Menu.buildFromTemplate([
{
label: '退出',
click: () => app.quit()
}
])
tray.popUpContextMenu(menuConfig)
})
})
// 主界面x关闭时,隐藏窗口
ipcMain.on('mainWindow:close', () => {
mainWindow.hide()
})
// 关闭提醒界面窗口
ipcMain.on('remindWindow:close', () => {
remindWindow.close()
})
ipcMain.on('setTaskTimer', (_event, time, task) => {
const now = new Date()
const date = new Date()
date.setHours(time.slice(0, 2), time.slice(3), 0)
const timeout = date.getTime() - now.getTime()
setTimeout(() => {
createRemindWindow(task)
}, timeout)
})
function createRemindWindow(task) {
if (remindWindow) remindWindow.close()
remindWindow = new BrowserWindow({
height: 450,
width: 360,
resizable: false,
frame: false,
icon: iconPath,
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
})
remindWindow.removeMenu()
const size = screen.getPrimaryDisplay().workAreaSize
// 确保出现在右下角
const { y } = tray.getBounds()
const { height, width } = remindWindow.getBounds()
const yPosition = process.platform === 'darwin' ? y : y - height
remindWindow.setBounds({
x: size.width - width,
y: yPosition,
height,
width
})
remindWindow.setAlwaysOnTop(true)
remindWindow.loadURL(`file://${__dirname}/src/remind.html`)
remindWindow.show()
remindWindow.webContents.send('setTask', task)
remindWindow.on('closed', () => { remindWindow = null })
// 50s后自动关闭
setTimeout(() => {
remindWindow && remindWindow.close()
}, 50 * 1000)
}
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})