Skip to content

Commit

Permalink
新增播放歌曲时阻止电脑休眠(#1563
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Jan 22, 2024
1 parent 47635c9 commit b13f10d
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 3 deletions.
5 changes: 5 additions & 0 deletions publish/changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- 若自定义源初始化失败,将会出现弹窗提示初始化失败的详情
- 添加win7_x64架构的安装版安装包构建
- 新增播放歌曲时阻止电脑休眠,默认启用,可到设置-播放设置关闭(#1563

### 优化

Expand All @@ -34,6 +35,10 @@
- 修复临时列表变更会意外触发同步的问题
- 修复最小化后再隐藏窗口时,托盘菜单的显示主界面功能异常的问题

### 变更

- 播放歌曲时默认会阻止系统进入休眠状态,若你不行软件阻止系统休眠,可以到设置-播放设置取消勾选“播放歌曲时阻止电脑休眠”设置

### 其他

- 移除所有内置源,由于收到腾讯投诉要求停止提供软件内置的连接到他们平台的在线播放及下载服务,所以从即日(2023年10月18日)起LX本身不再提供上述服务
Expand Down
1 change: 1 addition & 0 deletions src/common/defaultSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const defaultSetting: LX.AppSetting = {
'player.highQuality': false,
'player.isShowTaskProgess': true,
'player.volume': 1,
'player.powerSaveBlocker': true,
'player.isMute': false,
'player.playbackRate': 1,
'player.preservesPitch': true,
Expand Down
1 change: 1 addition & 0 deletions src/common/ipcNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const modules = {
show_dialog: 'show_dialog',
open_dir_in_explorer: 'open_dir_in_explorer',
open_dev_tools: 'open_dev_tools',
set_power_save_blocker: 'set_power_save_blocker',

progress: 'progress',
change_tray: 'change_tray',
Expand Down
5 changes: 5 additions & 0 deletions src/common/types/app_setting.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ declare global {
*/
'player.volume': number

/**
* 播放歌曲时是否阻止电脑休眠
*/
'player.powerSaveBlocker': boolean

/**
* 是否静音
*/
Expand Down
1 change: 1 addition & 0 deletions src/lang/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@
"setting__play_mediaDevice_title": "Select a media device for audio output",
"setting__play_media_device_error_tip": "This function conflicts with advanced audio functions (audio visualization, sound effect settings). These functions have been enabled when you start the software this time. This setting is not available for now. Please close these functions and restart the software before modifying this setting.",
"setting__play_media_device_tip": "This feature conflicts with Audio Visualization, both cannot be enabled at the same time, would you like to turn Audio Visualization off and apply the selected audio output settings?",
"setting__play_power_save_blocker": "Prevent computer from sleeping while playing songs",
"setting__play_quality": "Priority playback of 320K quality songs (if available)",
"setting__play_save_play_time": "Remember playback progress",
"setting__play_startup_auto_play": "Play music automatically after launching the software",
Expand Down
1 change: 1 addition & 0 deletions src/lang/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@
"setting__play_mediaDevice_title": "选择声音输出的媒体设备",
"setting__play_media_device_error_tip": "此功能与高级音频功能(音频可视化、音效设置)冲突,你本次启动软件时已启用这些功能,此设置暂不可用,请 关闭这些功能 并 重启 软件后,再来修改此设置。",
"setting__play_media_device_tip": "此功能与音频可视化功能冲突,两者无法同时启用,是否将音频可视化关闭 并 应用所选音频输出设置?",
"setting__play_power_save_blocker": "播放歌曲时阻止电脑休眠",
"setting__play_quality": "优先播放320K品质的歌曲(如果可用)",
"setting__play_save_play_time": "记住播放进度",
"setting__play_startup_auto_play": "启动软件后自动播放音乐",
Expand Down
1 change: 1 addition & 0 deletions src/lang/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@
"setting__play_mediaDevice_title": "選擇聲音輸出的媒體設備",
"setting__play_media_device_error_tip": "此功能與進階音訊功能(音訊視覺化、音效設定)衝突,你本次啟動軟體時已啟用這些功能,此設定暫不可用,請 關閉這些功能 並 重新啟動 軟體後,再來修改此設定。",
"setting__play_media_device_tip": "此功能與音訊視覺化功能衝突,兩者無法同時啟用,是否將音訊視覺化關閉 並 應用所選音訊輸出設定?",
"setting__play_power_save_blocker": "播放歌曲時阻止電腦休眠",
"setting__play_quality": "優先播放320K品質的歌曲(如果可用)",
"setting__play_save_play_time": "記住播放進度",
"setting__play_startup_auto_play": "啟動軟體後自動播放音樂",
Expand Down
5 changes: 4 additions & 1 deletion src/main/modules/winMain/rendererEvent/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
showSaveDialog,
} from '@main/modules/winMain'
import { quitApp } from '@main/app'
import { getAllThemes, removeTheme, saveTheme } from '@main/utils'
import { getAllThemes, removeTheme, saveTheme, setPowerSaveBlocker } from '@main/utils'
import { openDirInExplorer } from '@common/utils/electron'

export default () => {
Expand Down Expand Up @@ -55,6 +55,9 @@ export default () => {
mainOn(WIN_MAIN_RENDERER_EVENT_NAME.focus, () => {
showWindow()
})
mainOn<boolean>(WIN_MAIN_RENDERER_EVENT_NAME.set_power_save_blocker, ({ params: enabled }) => {
setPowerSaveBlocker(enabled)
})
mainOn<boolean>(WIN_MAIN_RENDERER_EVENT_NAME.close, ({ params: isForce }) => {
if (isForce) {
app.exit(0)
Expand Down
14 changes: 13 additions & 1 deletion src/main/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { STORE_NAMES, URL_SCHEME_RXP } from '@common/constants'
import defaultSetting from '@common/defaultSetting'
import defaultHotKey from '@common/defaultHotKey'
import { migrateDataJson, migrateHotKey, migrateUserApi, parseDataFile } from './migrate'
import { nativeTheme } from 'electron'
import { nativeTheme, powerSaveBlocker } from 'electron'
import { joinPath } from '@common/utils/nodejs'
import themes from '@common/theme/index.json'

Expand Down Expand Up @@ -280,3 +280,15 @@ export const getTheme = () => {
},
}
}

let powerSaveBlockerId: number | null = null
export const setPowerSaveBlocker = (enabled: boolean) => {
let isEnabled = powerSaveBlockerId != null && powerSaveBlocker.isStarted(powerSaveBlockerId)
if (enabled) {
if (isEnabled) return
powerSaveBlockerId = powerSaveBlocker.start('prevent-app-suspension')
} else {
if (!isEnabled) return
if (powerSaveBlocker.stop(powerSaveBlockerId!)) powerSaveBlockerId = null
}
}
22 changes: 22 additions & 0 deletions src/renderer/core/player/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { qualityList } from '@renderer/store'
import { clearPlayedList } from '@renderer/store/player/action'
import { appSetting } from '@renderer/store/setting'
import { dislikeInfo } from '@renderer/store/dislikeList'
import { setPowerSaveBlocker as setPowerSaveBlockerRemote } from '@renderer/utils/ipc'

export const getPlayType = (highQuality: boolean, musicInfo: LX.Music.MusicInfo | LX.Download.ListItem): LX.Quality | null => {
if ('progress' in musicInfo || musicInfo.source == 'local') return null
Expand Down Expand Up @@ -41,3 +42,24 @@ export const filterList = async({ playedList, listId, list, playerMusicInfo, isN
return { filteredList: markRawList(filteredList), playerIndex }
}

let timeout: NodeJS.Timer | null = null
const clearTimer = () => {
if (!timeout) return
clearTimeout(timeout)
timeout = null
}
export const setPowerSaveBlocker = (enabled: boolean, force = false) => {
if (enabled) {
clearTimer()
if (!force && !appSetting['player.powerSaveBlocker']) return
setPowerSaveBlockerRemote(true)
} else if (force) {
clearTimer()
setPowerSaveBlockerRemote(false)
} else {
if (timeout) return
timeout = setTimeout(() => {
setPowerSaveBlockerRemote(false)
}, 60_000 * 1.5)
}
}
16 changes: 15 additions & 1 deletion src/renderer/core/useApp/usePlayer/usePlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { HOTKEY_PLAYER } from '@common/hotKey'
import { playNext, pause, playPrev, togglePlay, collectMusic, uncollectMusic, dislikeMusic } from '@renderer/core/player'
import usePlaybackRate from './usePlaybackRate'
import useSoundEffect from './useSoundEffect'
import { setPowerSaveBlocker } from '@renderer/core/player/utils'


export default () => {
Expand All @@ -53,12 +54,20 @@ export default () => {
void playPrev()
}

const addPowerSaveBlocker = () => {
setPowerSaveBlocker(true)
}
const removePowerSaveBlocker = () => {
setPowerSaveBlocker(false)
}

const setPlayStatus = () => {
setPlay(true)
}
const setPauseStatus = () => {
setPlay(false)
if (window.lx.isPlayedStop) pause()
removePowerSaveBlocker()
}

const handleUpdatePlayInfo = () => {
Expand Down Expand Up @@ -88,6 +97,7 @@ export default () => {
setTitle(null)
setAllStatus('')
setStop()
removePowerSaveBlocker()
}

watch(() => appSetting['player.togglePlayMethod'], newValue => {
Expand All @@ -112,6 +122,8 @@ export default () => {
window.app_event.on('stop', setStopStatus)
window.app_event.on('musicToggled', handleUpdatePlayInfo)
window.app_event.on('playerCanplay', handleCanplay)
window.app_event.on('playerPlaying', addPowerSaveBlocker)
window.app_event.on('playerEmptied', removePowerSaveBlocker)

window.app_event.on('playerEnded', handleEnded)

Expand All @@ -132,7 +144,9 @@ export default () => {
window.app_event.off('error', setPauseStatus)
window.app_event.off('stop', setStopStatus)
window.app_event.off('musicToggled', handleUpdatePlayInfo)
window.app_event.on('playerCanplay', handleCanplay)
window.app_event.off('playerPlaying', addPowerSaveBlocker)
window.app_event.off('playerEmptied', removePowerSaveBlocker)
window.app_event.off('playerCanplay', handleCanplay)

window.app_event.off('playerEnded', handleEnded)
})
Expand Down
6 changes: 6 additions & 0 deletions src/renderer/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,12 @@ export const showHideWindowToggle = () => {
export const focusWindow = () => {
rendererSend(WIN_MAIN_RENDERER_EVENT_NAME.focus)
}
/**
* 是否启用电源锁
*/
export const setPowerSaveBlocker = (enabled: boolean) => {
rendererSend<boolean>(WIN_MAIN_RENDERER_EVENT_NAME.set_power_save_blocker, enabled)
}

/**
* 窗口获取焦点事件
Expand Down
14 changes: 14 additions & 0 deletions src/renderer/views/Setting/components/SettingPlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ dt#play {{ $t('setting__play') }}
dd
.gap-top
base-checkbox(id="setting_player_startup_auto_play" :model-value="appSetting['player.startupAutoPlay']" :label="$t('setting__play_startup_auto_play')" @update:model-value="updateSetting({'player.startupAutoPlay': $event})")
.gap-top
base-checkbox(id="setting_player_power_save_blocker" :model-value="appSetting['player.powerSaveBlocker']" :label="$t('setting__play_power_save_blocker')" @update:model-value="handleUpdatePowerSaveBlocker")
.gap-top
base-checkbox(id="setting_player_save_play_time" :model-value="appSetting['player.isSavePlayTime']" :label="$t('setting__play_save_play_time')" @update:model-value="updateSetting({'player.isSavePlayTime': $event})")
.gap-top
Expand Down Expand Up @@ -35,6 +37,8 @@ import { hasInitedAdvancedAudioFeatures } from '@renderer/plugins/player'
import { dialog } from '@renderer/plugins/Dialog'
import { useI18n } from '@renderer/plugins/i18n'
import { appSetting, updateSetting } from '@renderer/store/setting'
import { setPowerSaveBlocker } from '@renderer/core/player/utils'
import { isPlay } from '@renderer/store/player/state'
export default {
Expand Down Expand Up @@ -84,13 +88,23 @@ export default {
mediaDeviceId.value = val
})
const handleUpdatePowerSaveBlocker = (enabled) => {
if (enabled) {
if (isPlay.value) setPowerSaveBlocker(true, true)
} else {
setPowerSaveBlocker(false, true)
}
updateSetting({ 'player.powerSaveBlocker': enabled })
}
return {
appSetting,
updateSetting,
mediaDevices,
mediaDeviceId,
handleMediaDeviceIdChnage,
handleUpdatePowerSaveBlocker,
}
},
}
Expand Down

0 comments on commit b13f10d

Please sign in to comment.