重写桌面歌词窗口坐标的计算逻辑

pull/1155/head
lyswhut 2023-01-18 15:43:26 +08:00
parent 4fa88dd7a0
commit 82bea640c4
5 changed files with 48 additions and 66 deletions

View File

@ -2,3 +2,4 @@
### 修复 ### 修复
- 修复声音输出设备更改时后的自动暂停播放设置无效的问题 - 修复声音输出设备更改时后的自动暂停播放设置无效的问题
- 重写桌面歌词窗口坐标的计算逻辑修复桌面歌词移动到最边缘时某些情况下在启用歌词后会出现窗口偏移的问题远古bug了

View File

@ -76,8 +76,8 @@ declare namespace LX {
interface NewBounds { interface NewBounds {
x?: number | null x: number
y?: number y: number
w: number w: number
h: number h: number
} }

View File

@ -1,7 +1,7 @@
import { join } from 'path' import { join } from 'path'
import { BrowserWindow } from 'electron' import { BrowserWindow } from 'electron'
import { debounce, isLinux } from '@common/utils' import { debounce, isLinux } from '@common/utils'
import { getLyricWindowBounds, offset } from './utils' import { getLyricWindowBounds, minHeight, minWidth, padding } from './utils'
import { mainSend } from '@common/mainIpc' import { mainSend } from '@common/mainIpc'
import { encodePath } from '@common/utils/electron' import { encodePath } from '@common/utils/electron'
@ -81,15 +81,20 @@ export const createWindow = () => {
let width = global.lx.appSetting['desktopLyric.width'] let width = global.lx.appSetting['desktopLyric.width']
let height = global.lx.appSetting['desktopLyric.height'] let height = global.lx.appSetting['desktopLyric.height']
let isAlwaysOnTop = global.lx.appSetting['desktopLyric.isAlwaysOnTop'] let isAlwaysOnTop = global.lx.appSetting['desktopLyric.isAlwaysOnTop']
let isLockScreen = global.lx.appSetting['desktopLyric.isLockScreen'] // let isLockScreen = global.lx.appSetting['desktopLyric.isLockScreen']
let isShowTaskbar = global.lx.appSetting['desktopLyric.isShowTaskbar'] let isShowTaskbar = global.lx.appSetting['desktopLyric.isShowTaskbar']
let { width: screenWidth, height: screenHeight } = global.envParams.workAreaSize // let { width: screenWidth, height: screenHeight } = global.envParams.workAreaSize
if (x == null || y == null) { if (x == null || y == null) {
x = screenWidth - width + offset if (width < minWidth) width = minWidth
y = screenHeight - height + offset if (height < minHeight) height = minHeight
if (global.envParams.workAreaSize) {
x = global.envParams.workAreaSize.width + padding - width
y = global.envParams.workAreaSize.height + padding - height
} else {
x = y = -padding
} }
if (isLockScreen) { } else {
let bounds = getLyricWindowBounds({ x, y, width, height }, { x: null, y: 0, w: width, h: height }) let bounds = getLyricWindowBounds({ x, y, width, height }, { x: 0, y: 0, w: width, h: height })
x = bounds.x x = bounds.x
y = bounds.y y = bounds.y
width = bounds.width width = bounds.width

View File

@ -1,77 +1,52 @@
// 设置窗口位置、大小 // 设置窗口位置、大小
let winX export const padding = 8
let winY export let minWidth = 80
let wasW export let minHeight = 50
let wasH
export const offset = 8
let minWidth = 80
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 => { 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 (w < minWidth) w = minWidth
if (h < minHeight) h = minHeight if (h < minHeight) h = minHeight
if (global.lx.appSetting['desktopLyric.isLockScreen']) { if (global.lx.appSetting['desktopLyric.isLockScreen']) {
if (!global.envParams.workAreaSize) return bounds if (!global.envParams.workAreaSize) return bounds
wasW = (global.envParams.workAreaSize.width ?? 0) + offset const maxWinW = global.envParams.workAreaSize.width + padding * 2
wasH = (global.envParams.workAreaSize.height ?? 0) + offset const maxWinH = global.envParams.workAreaSize.height + padding * 2
if (w > wasW + offset) w = wasW + offset if (w > maxWinW) w = maxWinW
if (h > wasH + offset) h = wasH + offset if (h > maxWinH) h = maxWinH
if (x == null) {
if (bounds.x > wasW - w) { const maxX = global.envParams.workAreaSize.width + padding - w
x = wasW - w - bounds.x const minX = -padding
} else if (bounds.x < -offset) { const maxY = global.envParams.workAreaSize.height + padding - h
x = bounds.x + offset const minY = -padding
x += bounds.x
y += bounds.y
if (x > maxX) x = maxX
else if (x < minX) x = minX
if (y > maxY) y = maxY
else if (y < minY) y = minY
} else { } else {
x = 0
}
if (bounds.y > wasH - h) {
y = wasH - h - bounds.y
} else if (bounds.y < -offset) {
y = bounds.y + offset
} else {
y = 0
}
}
winX = bounds.x + x
winY = bounds.y + y
if (x != 0) {
if (winX < -offset) {
winX = -offset
} else if (winX + w > wasW) {
winX = wasW - w
}
}
if (y != 0) {
if (winY < -offset) {
winY = -offset
} else if (winY + h > wasH) {
winY = wasH - h
}
}
x = winX
y = winY
if (x + w > wasW) w = wasW - x
if (y + h > wasH) h = wasH - y
} else {
if (x == null) {
x = 0
y = 0
}
y += bounds.y y += bounds.y
x += bounds.x x += bounds.x
} }
bounds.width = w
bounds.height = h
bounds.x = x
bounds.y = y
// console.log('util bounds', bounds) // console.log('util bounds', bounds)
return bounds return { width: w, height: h, x, y }
} }

View File

@ -29,6 +29,7 @@ export default () => {
let bounds: LX.DesktopLyric.NewBounds = { let bounds: LX.DesktopLyric.NewBounds = {
w: 0, w: 0,
h: 0, h: 0,
x: 0,
y: 0, y: 0,
} }
let temp let temp