From 52cd2d9d49f62dafc8a0f4c15eff9be9c2976caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Sun, 24 Feb 2019 02:12:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=B9=85=E9=99=8D=E4=BD=8E=E8=BF=B7?= =?UTF-8?q?=E4=BD=A0=E6=A8=A1=E5=BC=8F=E5=92=8C=E5=90=8E=E5=8F=B0=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E7=9A=84cpu=E5=8D=A0=E7=94=A8;=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=A4=E4=BA=92=E9=80=BB=E8=BE=91;=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E9=94=AE=E7=9A=84=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/js/app.js | 89 +++++++++++++++++++++++++++---------- src/js/mini-win.js | 16 +++++++ src/js/modules/play-ctrl.js | 4 ++ src/tools/shortcut.js | 13 +----- src/tools/windows.js | 2 +- 5 files changed, 87 insertions(+), 37 deletions(-) diff --git a/src/js/app.js b/src/js/app.js index 44e2458..8ad01a3 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -59,6 +59,7 @@ Anot({ state: { theme: appInit.theme || 1, // 1:macos, 2: deepin winFocus: false, + winShow: true, mod: 'local', searchTxt: '', playMode: Anot.ls('play-mode') >>> 0, // 0:all | 1:single | 2:random @@ -82,7 +83,7 @@ Anot({ loading: false, progress: 0 }, - skip: [], + skip: ['winShow'], computed: { views() { if (!this.mod) { @@ -216,6 +217,7 @@ Anot({ * 响应 全局快捷键的事件 */ WIN.on('gs-ctrl', ev => { + log('gs-ctrl: ', ev) switch (ev) { case 'prev': this.nextSong(-1) @@ -234,30 +236,8 @@ Anot({ SONIST.pause() this.draw() break - - case 'vu': - this.volume += 5 - if (this.volume >= 100) { - this.volume = 100 - } - SONIST.volume = this.volume - break - case 'vd': - this.volume -= 5 - if (this.volume <= 0) { - this.volume = 0 - } - SONIST.volume = this.volume - break - case 'mute': - this.muted = SONIST.mute() - break default: - if (ev.name === 'play-mode') { - this.playMode = ev.value - SONIST.mode = PLAY_MODE[ev.value] - Anot.ls('play-mode', ev.value) - } + break } }) @@ -267,6 +247,64 @@ Anot({ WIN.show() } }) + + Anot(document).bind('keydown', ev => { + if (ev.target === document.body) { + switch (ev.keyCode) { + case 32: // 空格 + this.play() + break + case 37: // 向左 (prev) + if (ev.metaKey) { + this.nextSong(-1) + } + break + case 39: // 向右 (next) + if (ev.metaKey) { + this.nextSong(1) + } + break + case 38: // 向上 (音量+ + if (ev.metaKey) { + this.volume += 5 + if (this.volume >= 100) { + this.volume = 100 + } + SONIST.volume = this.volume + } + break + case 40: // 向下 (音量- + if (ev.metaKey) { + this.volume -= 5 + if (this.volume <= 0) { + this.volume = 0 + } + SONIST.volume = this.volume + break + } + break + case 77: // M (迷你模式) + if (ev.metaKey && ev.altKey) { + this.change2mini() + } + break + case 82: // R (桌面歌词) + if (ev.metaKey) { + this.toggleDesktopLrc() + } + break + } + } + }) + + WIN.on('show', ev => { + this.winShow = true + this.draw() + }) + WIN.on('hide', ev => { + this.winShow = false + this.draw() + }) }, methods: { quit(force) { @@ -465,6 +503,9 @@ Anot({ LYRICS.__init__(it.id) }) } + if (!this.winShow) { + __MINI__.emit('mini-ctrl', this.isPlaying ? 'play' : 'pause') + } } } }, diff --git a/src/js/mini-win.js b/src/js/mini-win.js index 2ba9ed0..bbab502 100644 --- a/src/js/mini-win.js +++ b/src/js/mini-win.js @@ -34,6 +34,22 @@ Anot({ this.isPlaying = !!song.id this.playMode = Anot.ls('play-mode') >>> 0 }) + + WIN.on('mini-ctrl', ev => { + switch (ev) { + case 'play': + this.isPlaying = true + if (!this.curr.id) { + remote.app.__MAIN__.emit('mini-ctrl', 'play') + } + break + case 'pause': + this.isPlaying = false + break + default: + break + } + }) }, methods: { handleCtrl(ev) { diff --git a/src/js/modules/play-ctrl.js b/src/js/modules/play-ctrl.js index cc9a3d9..88ac762 100644 --- a/src/js/modules/play-ctrl.js +++ b/src/js/modules/play-ctrl.js @@ -90,6 +90,10 @@ export default { }, draw(force) { + //主窗口隐藏时, 暂停绘制, 以降低CPU开销 + if (!this.winShow) { + return clearInterval(this.timer) + } if (force) { this.__img1__ = new Image() this.__img2__ = new Image() diff --git a/src/tools/shortcut.js b/src/tools/shortcut.js index 677dcc2..06bc4a4 100644 --- a/src/tools/shortcut.js +++ b/src/tools/shortcut.js @@ -6,7 +6,7 @@ 'use strict' -const { app, globalShortcut: GS } = require('electron') +const { globalShortcut: GS } = require('electron') module.exports = { __init__(win) { @@ -23,16 +23,5 @@ module.exports = { GS.register('MediaPlayPause', _ => { win.emit('gs-ctrl', 'play') }) - - // 音量控制 - GS.register('VolumeUp', _ => { - win.emit('gs-ctrl', 'vu') - }) - GS.register('VolumeDown', _ => { - win.emit('gs-ctrl', 'vd') - }) - GS.register('VolumeMute', _ => { - win.emit('gs-ctrl', 'mute') - }) } } diff --git a/src/tools/windows.js b/src/tools/windows.js index 951560a..bac8561 100644 --- a/src/tools/windows.js +++ b/src/tools/windows.js @@ -33,7 +33,7 @@ exports.createMainWindow = function(icon) { win.on('ready-to-show', _ => { win.show() - // win.openDevTools() + win.openDevTools() }) return win