Skip to content

Commit a223280

Browse files
committed
Add 快捷键控制 resolve #29
1 parent 743cdd0 commit a223280

File tree

10 files changed

+484
-49
lines changed

10 files changed

+484
-49
lines changed

src/main/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {app} from 'electron'
1+
import {app, globalShortcut} from 'electron'
22
import initTray from './tray'
33
import initUpdater from './updater'
44
import initWindow from './window'
@@ -41,6 +41,11 @@ app.on('window-all-closed', () => {
4141
}
4242
})
4343

44+
app.on('will-quit', () => {
45+
// 清空所有快捷键
46+
globalShortcut.unregisterAll()
47+
})
48+
4449
app.on('activate', () => {
4550
if (mainWindow === null) {
4651
createWindow()

src/main/menu.js

+63-26
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,69 @@
1-
import {Menu} from 'electron'
1+
import {Menu, ipcMain, globalShortcut} from 'electron'
22

3-
export default function initMenu() {
4-
const template = [
5-
{
6-
label: 'Music',
7-
submenu: [
8-
{
9-
label: '关于',
10-
click() {
11-
require('electron').shell.openExternal('https://github.com/sunzongzheng/music')
12-
}
3+
const template = [
4+
{
5+
label: 'Music',
6+
submenu: [
7+
{
8+
label: '关于',
9+
click() {
10+
require('electron').shell.openExternal('https://github.com/sunzongzheng/music')
1311
}
14-
]
15-
},
16-
{
17-
label: '编辑',
18-
submenu: [
19-
{role: 'undo', label: '撤销'},
20-
{role: 'redo', label: '重做'},
21-
{type: 'separator'},
22-
{role: 'cut', label: '剪贴'},
23-
{role: 'copy', label: '拷贝'},
24-
{role: 'paste', label: '粘贴'},
25-
{role: 'delete', label: '删除'},
26-
{role: 'selectall', label: '全选'}
27-
]
12+
}
13+
]
14+
},
15+
{
16+
label: '编辑',
17+
submenu: [
18+
{role: 'undo', label: '撤销'},
19+
{role: 'redo', label: '重做'},
20+
{type: 'separator'},
21+
{role: 'cut', label: '剪贴'},
22+
{role: 'copy', label: '拷贝'},
23+
{role: 'paste', label: '粘贴'},
24+
{role: 'delete', label: '删除'},
25+
{role: 'selectall', label: '全选'}
26+
]
27+
}
28+
]
29+
30+
ipcMain.on('register-hotKey', (event, {hotKey, enableGlobal}) => {
31+
console.log(hotKey, enableGlobal)
32+
globalShortcut.unregisterAll()
33+
const submenu = []
34+
hotKey.forEach(single => {
35+
submenu.push({
36+
label: single.name,
37+
accelerator: single.key,
38+
click: () => {
39+
event.sender.send('hotKey-control', single.value)
40+
}
41+
})
42+
if (enableGlobal) {
43+
const res = globalShortcut.register(single.global, () => {
44+
event.sender.send('hotKey-control', single.value)
45+
})
46+
if (res) {
47+
console.log(`${single.global}注册成功`)
48+
} else {
49+
console.log(`${single.global}注册失败`)
50+
}
2851
}
29-
]
52+
})
53+
if (!enableGlobal) {
54+
globalShortcut.unregisterAll()
55+
}
56+
if (template[2]) {
57+
template[2].submenu = submenu
58+
} else {
59+
template.push({
60+
label: '控制',
61+
submenu
62+
})
63+
}
64+
initMenu()
65+
})
66+
export default function initMenu() {
3067
const menu = Menu.buildFromTemplate(template)
3168
Menu.setApplicationMenu(menu)
3269
}

src/renderer/App.vue

+38-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import downloadProgress from './view/components/progress/index.vue'
2323
import eventBus from './eventBus/searchResult'
2424
import updateAlert from './view/components/updateAlert.vue'
25+
import {mapState, mapActions, mapMutations} from 'vuex'
2526
2627
export default {
2728
components: {
@@ -37,7 +38,13 @@
3738
refresh: false
3839
}
3940
},
41+
computed: {
42+
...mapState('hot-key', ['hotKey', 'enableGlobal']),
43+
...mapState('play', ['volume']),
44+
},
4045
methods: {
46+
...mapActions('c_playlist', ['last', 'next']),
47+
...mapMutations('play', ['pauseChange', 'update']),
4148
// 登录成功回调
4249
loginSuccessed(event, info) {
4350
console.log(info)
@@ -58,6 +65,29 @@
5865
message: h(updateAlert),
5966
duration: 0
6067
})
68+
},
69+
// 快捷键控制
70+
hotKeyControl(event, key) {
71+
switch (key) {
72+
case 'playPause':
73+
this.pauseChange()
74+
break
75+
case 'last':
76+
this.last()
77+
break
78+
case 'next':
79+
this.next()
80+
break
81+
case 'volumeIncrease':
82+
this.update({
83+
volume: this.volume + 10 > 100 ? 100 : this.volume + 10
84+
})
85+
break
86+
case 'volumeDecrease':
87+
this.update({
88+
volume: this.volume - 10 < 0 ? 0 : this.volume - 10
89+
})
90+
}
6191
}
6292
},
6393
watch: {
@@ -66,10 +96,14 @@
6696
}
6797
},
6898
created() {
69-
// 初始化离线歌单
70-
Vue.$store.dispatch('offline-playlist/init')
71-
this.$ipc.on('loginSuccessed', this.loginSuccessed)
72-
this.$ipc.on('update-alert', this.updateAlert)
99+
Vue.$store.dispatch('offline-playlist/init') // 初始化离线歌单
100+
this.$ipc.on('loginSuccessed', this.loginSuccessed) // 监听登录成功
101+
this.$ipc.on('update-alert', this.updateAlert) // 监听版本更新
102+
this.$ipc.send('register-hotKey', {
103+
hotKey: this.hotKey,
104+
enableGlobal: this.enableGlobal
105+
})
106+
this.$ipc.on('hotKey-control', this.hotKeyControl)
73107
if (localStorage.token) {
74108
this.$store.dispatch('user/init')
75109
}

src/renderer/filters/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ Vue.filter('date', (val, format = 'YYYY-MM-DD') => {
3131
})
3232
Vue.filter('dateDiff', (val, format) => {
3333
return moment(val).fromNow()
34+
})
35+
Vue.filter('keyboardChar', (val) => {
36+
if (!val) return ''
37+
return val.split('+').map(item => Vue.$store.getters['hot-key/registerKey2Char'][item]).join(' ')
3438
})

0 commit comments

Comments
 (0)