-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
taskProgress.ts
63 lines (62 loc) · 2.03 KB
/
taskProgress.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
import { ElectronController } from '@/ElectronController'
import { darkIcon } from '@/utils/icons'
import { kTasks } from '@xmcl/runtime/task'
import { Notification } from 'electron'
import { ControllerPlugin } from './plugin'
/**
* Setup task progress bar
*/
export const taskProgressPlugin: ControllerPlugin = function (this: ElectronController) {
const notify = (type: 'finish' | 'fail') => {
const t = this.i18n.t
if (this.activeWindow && this.activeWindow.isFocused()) {
// this.activeWindow.webContents.send('notification', n)
} else if ((type === 'finish' || type === 'fail')) {
const notification = new Notification({
title: type === 'finish' ? t('task.success') : t('task.fail'),
body: t('task.continue'),
icon: darkIcon,
})
notification.show()
notification.on('click', () => {
if (this.activeWindow?.isVisible()) {
this.activeWindow.focus()
} else {
this.activeWindow?.show()
}
})
} else {
// this.app.broadcast('notification', n)
}
}
this.app.registry.get(kTasks).then((tasks) => {
tasks.emitter.on('update', (uid, task) => {
if (tasks.getActiveTask() === task) {
if (this.activeWindow && !this.activeWindow.isDestroyed()) {
const progress = task.progress / task.total
if (Number.isNaN(progress) || progress > 1) {
this.activeWindow.setProgressBar(-1)
} else {
this.activeWindow.setProgressBar(progress)
}
}
}
})
tasks.emitter.on('success', (_, task) => {
if (tasks.getActiveTask() === task) {
if (this.activeWindow && !this.activeWindow.isDestroyed()) {
this.activeWindow.setProgressBar(-1)
}
notify('finish')
}
})
tasks.emitter.on('fail', (_, task) => {
if (tasks.getActiveTask() === task) {
if (this.activeWindow && !this.activeWindow.isDestroyed()) {
this.activeWindow.setProgressBar(-1)
}
notify('fail')
}
})
})
}