Files
lx-music-desktop/src/main/modules/winLyric/utils.ts
2023-08-17 17:49:47 +08:00

119 lines
3.3 KiB
TypeScript

// 设置窗口位置、大小
export let minWidth = 80
export let minHeight = 50
// const updateBounds = (bounds: Bounds) => {
// bounds.x = bounds.x
// return bounds
// }
/**
*
* @param bounds 当前设置
* @param param 新设置(相对于当前设置)
* @returns
*/
export const getLyricWindowBounds = (bounds: Electron.Rectangle, { x = 0, y = 0, w = 0, h = 0 }: LX.DesktopLyric.NewBounds): Electron.Rectangle => {
if (w < minWidth) w = minWidth
if (h < minHeight) h = minHeight
if (global.lx.appSetting['desktopLyric.isLockScreen']) {
if (!global.envParams.workAreaSize) return bounds
const maxWinW = global.envParams.workAreaSize.width
const maxWinH = global.envParams.workAreaSize.height
if (w > maxWinW) w = maxWinW
if (h > maxWinH) h = maxWinH
const maxX = global.envParams.workAreaSize.width - w
const maxY = global.envParams.workAreaSize.height - h
x += bounds.x
y += bounds.y
if (x > maxX) x = maxX
else if (x < 0) x = 0
if (y > maxY) y = maxY
else if (y < 0) y = 0
} else {
y += bounds.y
x += bounds.x
}
// console.log('util bounds', bounds)
return { width: w, height: h, x, y }
}
export const watchConfigKeys = [
'desktopLyric.enable',
'desktopLyric.isLock',
'desktopLyric.isAlwaysOnTop',
'desktopLyric.isAlwaysOnTopLoop',
'desktopLyric.isShowTaskbar',
'desktopLyric.audioVisualization',
'desktopLyric.width',
'desktopLyric.height',
'desktopLyric.x',
'desktopLyric.y',
'desktopLyric.isLockScreen',
'desktopLyric.isDelayScroll',
'desktopLyric.scrollAlign',
'desktopLyric.isHoverHide',
'desktopLyric.direction',
'desktopLyric.style.align',
'desktopLyric.style.lyricUnplayColor',
'desktopLyric.style.lyricPlayedColor',
'desktopLyric.style.lyricShadowColor',
'desktopLyric.style.font',
'desktopLyric.style.fontSize',
'desktopLyric.style.lineGap',
// 'desktopLyric.style.fontWeight',
'desktopLyric.style.opacity',
'desktopLyric.style.ellipsis',
'desktopLyric.style.isFontWeightFont',
'desktopLyric.style.isFontWeightLine',
'desktopLyric.style.isFontWeightExtended',
'desktopLyric.style.isZoomActiveLrc',
'common.langId',
'player.isShowLyricTranslation',
'player.isShowLyricRoma',
'player.isPlayLxlrc',
'player.playbackRate',
] as const
export const buildLyricConfig = (appSetting: Partial<LX.AppSetting>): Partial<LX.DesktopLyric.Config> => {
const setting: Partial<LX.DesktopLyric.Config> = {}
for (const key of watchConfigKeys) {
if (key in appSetting) setting[key] = appSetting[key]
}
return setting
}
export const initWindowSize = (x: LX.AppSetting['desktopLyric.x'], y: LX.AppSetting['desktopLyric.y'], width: LX.AppSetting['desktopLyric.width'], height: LX.AppSetting['desktopLyric.height']) => {
if (x == null || y == null) {
if (width < minWidth) width = minWidth
if (height < minHeight) height = minHeight
if (global.envParams.workAreaSize) {
x = global.envParams.workAreaSize.width - width
y = global.envParams.workAreaSize.height - height
} else {
x = y = 0
}
} else {
let bounds = getLyricWindowBounds({ x, y, width, height }, { x: 0, y: 0, w: width, h: height })
x = bounds.x
y = bounds.y
width = bounds.width
height = bounds.height
}
return {
x,
y,
width,
height,
}
}