From 7a08cb5ddd6c5cb1727c420ca0c3ba939a5f2aca Mon Sep 17 00:00:00 2001 From: lyswhut Date: Fri, 29 Mar 2024 15:59:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=98=E7=9B=98=E8=8F=9C=E5=8D=95=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=92=AD=E6=94=BE=E3=80=81=E5=88=87=E6=AD=8C=E3=80=81?= =?UTF-8?q?=E6=94=B6=E8=97=8F=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 1 + src/main/modules/tray.ts | 133 +++++++++++++++--- src/main/modules/winMain/index.ts | 11 +- src/main/modules/winMain/main.ts | 1 + src/renderer/core/player/action.ts | 1 + .../core/useApp/usePlayer/usePlayProgress.ts | 3 +- 6 files changed, 125 insertions(+), 25 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index 1f3d6c34..a3ba30bb 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -3,6 +3,7 @@ - 主题编辑器添加“深色字体”选项,启用后将减少字体颜色梯度,各类字体(正文、标签字体等)颜色将更接近,这有助于解决创建全透明主题时可能出现的字体配色问题(#1799) - 新增在线自定义源导入功能,允许通过http/https链接导入自定义源 - 新增HTTP开放API服务,默认关闭,该服务可以为第三方软件提供调用LX的能力,可用API看说明文档(#1824) +- 托盘菜单新增播放、切歌、收藏控制 ### 优化 diff --git a/src/main/modules/tray.ts b/src/main/modules/tray.ts index 0559f879..7d6b6906 100644 --- a/src/main/modules/tray.ts +++ b/src/main/modules/tray.ts @@ -5,6 +5,7 @@ import { hideWindow as hideMainWindow, isExistWindow as isExistMainWindow, isShowWindow as isShowMainWindow, + sendTaskbarButtonClick, showWindow as showMainWindow, } from './winMain' import { quitApp } from '@main/app' @@ -13,6 +14,14 @@ let tray: Electron.Tray | null let isEnableTray: boolean = false let themeId: number +const playerState = { + empty: false, + collect: false, + play: false, + next: true, + prev: true, +} + const watchConfigKeys = [ 'desktopLyric.enable', 'desktopLyric.isLock', @@ -42,6 +51,12 @@ const themeList = [ const messages = { 'en-us': { + collect: 'Collection', + uncollect: 'Cancel collection', + play: 'Play', + pause: 'Pause', + next: 'Next song', + prev: 'Previous song', hide_win_main: 'Hide Main Window', show_win_main: 'Show Main Window', hide_win_lyric: 'Close desktop lyrics', @@ -53,6 +68,12 @@ const messages = { exit: 'Exit', }, 'zh-cn': { + collect: '收藏', + uncollect: '取消收藏', + play: '播放', + pause: '暂停', + next: '下一曲', + prev: '上一曲', hide_win_main: '隐藏主界面', show_win_main: '显示主界面', hide_win_lyric: '关闭桌面歌词', @@ -64,6 +85,12 @@ const messages = { exit: '退出', }, 'zh-tw': { + collect: '收藏', + uncollect: '取消收藏', + play: '播放', + pause: '暫停', + next: '下一曲', + prev: '上一曲', hide_win_main: '隱藏主界面', show_win_main: '顯示主界面', hide_win_lyric: '關閉桌面歌詞', @@ -120,25 +147,50 @@ const handleUpdateConfig = (config: any) => { global.lx.event_app.update_config(config) } +const createPlayerMenu = () => { + let menu: Electron.MenuItemConstructorOptions[] = [] + menu.push(playerState.play ? { + label: i18n.getMessage('pause'), + click() { + sendTaskbarButtonClick('pause') + }, + } : { + label: i18n.getMessage('play'), + click() { + sendTaskbarButtonClick('play') + }, + }) + menu.push({ + label: i18n.getMessage('prev'), + click() { + sendTaskbarButtonClick('prev') + }, + }) + menu.push({ + label: i18n.getMessage('next'), + click() { + sendTaskbarButtonClick('next') + }, + }) + menu.push(playerState.collect ? { + label: i18n.getMessage('uncollect'), + click() { + sendTaskbarButtonClick('unCollect') + }, + } : { + label: i18n.getMessage('collect'), + click() { + sendTaskbarButtonClick('collect') + }, + }) + return menu +} + export const createMenu = () => { if (!tray) return - let menu = [] - if (isExistMainWindow()) { - const isShow = isShowMainWindow() - menu.push(isShow - ? { - label: i18n.getMessage('hide_win_main'), - click() { - hideMainWindow() - }, - } - : { - label: i18n.getMessage('show_win_main'), - click() { - showMainWindow() - }, - }) - } + let menu: Electron.MenuItemConstructorOptions[] = createPlayerMenu() + if (playerState.empty) for (const m of menu) m.enabled = false + menu.push({ type: 'separator' }) menu.push(global.lx.appSetting['desktopLyric.enable'] ? { label: i18n.getMessage('hide_win_lyric'), @@ -178,6 +230,23 @@ export const createMenu = () => { handleUpdateConfig({ 'desktopLyric.isAlwaysOnTop': true }) }, }) + menu.push({ type: 'separator' }) + if (isExistMainWindow()) { + const isShow = isShowMainWindow() + menu.push(isShow + ? { + label: i18n.getMessage('hide_win_main'), + click() { + hideMainWindow() + }, + } + : { + label: i18n.getMessage('show_win_main'), + click() { + showMainWindow() + }, + }) + } menu.push({ label: i18n.getMessage('exit'), click() { @@ -241,4 +310,34 @@ export default () => { i18n.setLang(global.lx.appSetting['common.langId']) init() }) + + global.lx.event_app.on('player_status', (status) => { + let updated = false + if (status.status) { + switch (status.status) { + case 'paused': + playerState.play = false + playerState.empty &&= false + break + case 'error': + playerState.play = false + playerState.empty &&= false + break + case 'playing': + playerState.play = true + playerState.empty &&= false + break + case 'stoped': + playerState.play &&= false + playerState.empty = true + break + } + updated = true + } + if (status.collect != null) { + playerState.collect = status.collect + updated = true + } + if (updated) init() + }) } diff --git a/src/main/modules/winMain/index.ts b/src/main/modules/winMain/index.ts index 85e55f81..7086f124 100644 --- a/src/main/modules/winMain/index.ts +++ b/src/main/modules/winMain/index.ts @@ -48,7 +48,7 @@ export default () => { prev: true, } const progressStatus = { - progress: 0, + progress: -1, status: 'none' as Electron.ProgressBarOptions['mode'], } let showProgress = global.lx.appSetting['player.isShowTaskProgess'] @@ -87,12 +87,11 @@ export default () => { if (status.collect != null) taskBarButtonFlags.collect = status.collect setThumbarButtons(taskBarButtonFlags) } - if (status.progress) { + if (status.progress != null && global.lx.player_status.duration) { const progress = status.progress / global.lx.player_status.duration - if (progress.toFixed(2) == progressStatus.progress.toFixed(2)) return - progressStatus.progress = progress - if (showProgress) { - setProgressBar(progress, { + if (progress.toFixed(2) != progressStatus.progress.toFixed(2) && showProgress) { + progressStatus.progress = progress < 0.01 ? 0.01 : progress + setProgressBar(progressStatus.progress, { mode: progressStatus.status, }) } diff --git a/src/main/modules/winMain/main.ts b/src/main/modules/winMain/main.ts index f9988466..fe70929d 100644 --- a/src/main/modules/winMain/main.ts +++ b/src/main/modules/winMain/main.ts @@ -187,6 +187,7 @@ export const setWindowBounds = (options: Partial) => { } export const setProgressBar = (progress: number, options?: Electron.ProgressBarOptions) => { if (!browserWindow) return + console.log(progress, options) browserWindow.setProgressBar(progress, options) } export const setIgnoreMouseEvents = (ignore: boolean, options?: Electron.IgnoreMouseEventsOptions) => { diff --git a/src/renderer/core/player/action.ts b/src/renderer/core/player/action.ts index cca565c0..615772a5 100644 --- a/src/renderer/core/player/action.ts +++ b/src/renderer/core/player/action.ts @@ -143,6 +143,7 @@ const handleRestorePlay = async(restorePlayInfo: LX.Player.SavedPlayInfo) => { setImmediate(() => { if (musicInfo.id != playMusicInfo.musicInfo?.id) return window.app_event.setProgress(appSetting['player.isSavePlayTime'] ? restorePlayInfo.time : 0, restorePlayInfo.maxTime) + window.app_event.pause() }) diff --git a/src/renderer/core/useApp/usePlayer/usePlayProgress.ts b/src/renderer/core/useApp/usePlayer/usePlayProgress.ts index 4e0b451d..a7d1abd4 100644 --- a/src/renderer/core/useApp/usePlayer/usePlayProgress.ts +++ b/src/renderer/core/useApp/usePlayer/usePlayProgress.ts @@ -59,6 +59,7 @@ export default () => { const setProgress = (time: number, maxTime?: number) => { if (!musicInfo.id) return + if (maxTime != null) setMaxplayTime(maxTime) console.log('setProgress', time, maxTime) if (time > 0) restorePlayTime = time if (mediaBuffer.playTime) { @@ -69,8 +70,6 @@ export default () => { setNowPlayTime(time) setCurrentTime(time) - if (maxTime != null) setMaxplayTime(maxTime) - // if (!isPlay) audio.play() }