-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathmainWindowChannel.ts
111 lines (99 loc) · 2.85 KB
/
mainWindowChannel.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
import { onProvideMainWindowChannel } from '@lyric/utils/ipc'
import { onBeforeUnmount } from '@common/utils/vueTools'
import { setMusicInfo, setIsPlay } from '../store/action'
import { pause, play, setLyric, setLyricOffset, setPlaybackRate, stop } from './lyric'
import { lyrics } from '@lyric/store/lyric'
let mainWindowPort: Electron.IpcRendererEvent['ports'][0] | null = null
export const sendDesktopLyricInfo = (info: LX.DesktopLyric.WinMainActions) => {
if (mainWindowPort == null) return
mainWindowPort.postMessage({ action: info })
}
const listeners: Array<(event: LX.DesktopLyric.LyricActions) => void> = []
const handleDesktopLyricMessage = (event: LX.DesktopLyric.LyricActions) => {
switch (event.action) {
case 'set_info':
setMusicInfo({
id: event.data.id,
singer: event.data.singer,
name: event.data.name,
album: event.data.album,
})
lyrics.lyric = event.data.lrc ?? ''
lyrics.tlyric = event.data.tlrc
lyrics.rlyric = event.data.rlrc
lyrics.lxlyric = event.data.lxlrc
setLyric()
if (event.data.isPlay) {
setImmediate(() => {
getStatus()
})
}
break
case 'set_lyric':
lyrics.lyric = event.data.lrc ?? ''
lyrics.tlyric = event.data.tlrc
lyrics.rlyric = event.data.rlrc
lyrics.lxlyric = event.data.lxlrc
setLyric()
break
case 'set_status':
setIsPlay(event.data.isPlay)
if (event.data.isPlay) play(event.data.played_time)
else pause()
break
case 'set_offset':
setLyricOffset(event.data)
break
case 'set_playbackRate':
setPlaybackRate(event.data)
break
case 'set_pause':
setIsPlay(false)
pause()
break
case 'set_play':
setIsPlay(true)
play(event.data)
break
case 'set_stop':
setIsPlay(false)
stop()
break
default:
for (const listener of listeners) {
listener(event)
}
break
}
}
export const init = () => {
onProvideMainWindowChannel(({ event }) => {
const [port] = event.ports
mainWindowPort = port
// ... register a handler to receive results ...
port.onmessage = ({ data }) => {
handleDesktopLyricMessage(data)
// console.log('received result:', data)
}
// ... and start sending it work!
port.onmessageerror = (event) => {
console.log('onmessageerror', event)
}
getInfo()
})
}
export const useEvent = (listener: (event: LX.DesktopLyric.LyricActions) => void) => {
listeners.push(listener)
onBeforeUnmount(() => {
listeners.splice(listeners.indexOf(listener), 1)
})
}
export const getInfo = () => {
sendDesktopLyricInfo('get_info')
}
export const getAnalyserDataArray = () => {
sendDesktopLyricInfo('get_analyser_data_array')
}
export const getStatus = () => {
sendDesktopLyricInfo('get_status')
}