修复桌面歌词窗口不允许拖出桌面之外的位置计算偏移Bug

pull/389/head
lyswhut 2020-09-19 16:00:03 +08:00
parent 1c4e9ed474
commit 8aedde2422
5 changed files with 84 additions and 71 deletions

View File

@ -1,3 +1,3 @@
### 修复
- 修复某些情况下桌面歌词不会播放的问题
- 修复桌面歌词窗口不允许拖出桌面之外的位置计算偏移Bug

View File

@ -1,7 +1,7 @@
const { common: COMMON_EVENT_NAME, winLyric: WIN_LYRIC_EVENT_NAME, hotKey: HOT_KEY_EVENT_NAME, mainWindow: MAIN_WINDOW_EVENT_NAME } = require('../../events/_name')
const { mainSend, NAMES: { winLyric: ipcWinLyricNames } } = require('../../../common/ipc')
const { desktop_lyric } = require('../../../common/hotKey')
const { setLyricWindow } = require('./utils')
const { getLyricWindowBounds } = require('./utils')
let isLock = null
let isEnable = null
@ -27,6 +27,17 @@ const setLrcConfig = () => {
isAlwaysOnTop = desktopLyric.isAlwaysOnTop
global.modules.lyricWindow.setAlwaysOnTop(desktopLyric.isAlwaysOnTop, 'screen-saver')
}
if (isLockScreen != desktopLyric.isLockScreen) {
isLockScreen = desktopLyric.isLockScreen
if (desktopLyric.isLockScreen) {
global.modules.lyricWindow.setBounds(getLyricWindowBounds(global.modules.lyricWindow.getBounds(), {
x: null,
y: null,
w: desktopLyric.width,
h: desktopLyric.height,
}))
}
}
}
if (isEnable != desktopLyric.enable) {
isEnable = desktopLyric.enable
@ -36,17 +47,6 @@ const setLrcConfig = () => {
global.lx_event.winLyric.close()
}
}
if (isLockScreen != desktopLyric.isLockScreen) {
isLockScreen = desktopLyric.isLockScreen
if (desktopLyric.isLockScreen) {
setLyricWindow({
x: desktopLyric.x,
y: desktopLyric.y,
w: desktopLyric.width,
h: desktopLyric.height,
})
}
}
}
global.lx_event.common.on(COMMON_EVENT_NAME.config, name => {
if (WIN_LYRIC_EVENT_NAME.name === name) return

View File

@ -1,6 +1,7 @@
const { BrowserWindow } = require('electron')
const { winLyric: WIN_LYRIC_EVENT_NAME } = require('../../events/_name')
const { debounce } = require('../../../common/utils')
const { getLyricWindowBounds } = require('./utils')
require('./event')
require('./rendererEvent')
@ -67,29 +68,29 @@ const winEvent = lyricWindow => {
})
}
let offset = 8
const createWindow = () => {
if (global.modules.lyricWindow) return
if (!global.appSetting.desktopLyric.enable) return
// const windowSizeInfo = getWindowSizeInfo(global.appSetting)
let { x, y, width, height, isAlwaysOnTop } = global.appSetting.desktopLyric
let { width: screenWidth, height: screenHeight } = global.envParams.workAreaSize
screenWidth += offset * 2
screenHeight += offset * 2
if (x == null) {
x = screenWidth - width - offset
y = screenHeight - height - offset
x = screenWidth - width
y = screenHeight - height
}
if (global.appSetting.desktopLyric.isLockScreen) {
x = Math.max(-offset, screenWidth < (width + x) ? screenWidth - width : x)
y = Math.max(-offset, screenHeight < (height + y) ? screenHeight - height : y)
let bounds = getLyricWindowBounds({ x, y, width, height }, { x: null, y: null, w: width, h: height })
x = bounds.x
y = bounds.y
width = bounds.width
height = bounds.height
}
/**
* Initial window options
*/
global.modules.lyricWindow = new BrowserWindow({
height: Math.max(height > screenHeight ? screenHeight : height, 80),
width: Math.max(width > screenWidth ? screenWidth : width, 380),
height,
width,
x,
y,
minWidth: 380,

View File

@ -8,7 +8,7 @@ const {
},
} = require('../../../common/ipc')
const { winLyric: WIN_LYRIC_EVENT_NAME } = require('../../events/_name')
const { setLyricWindow } = require('./utils')
const { getLyricWindowBounds } = require('./utils')
mainOn(ipcWinLyricNames.get_lyric_info, (event, action) => {
if (!global.modules.mainWindow) return
@ -28,5 +28,5 @@ mainHandle(ipcWinLyricNames.get_lyric_config, async() => {
})
mainOn(ipcWinLyricNames.set_win_bounds, (event, options) => {
setLyricWindow(options)
global.modules.lyricWindow.setBounds(getLyricWindowBounds(global.modules.lyricWindow.getBounds(), options))
})

View File

@ -1,56 +1,68 @@
// 设置窗口位置、大小
let bounds
let winX
let winY
let wasW
let wasY
let wasH
let offset = 8
exports.setLyricWindow = ({ x = 0, y = 0, w = 0, h = 0 }) => {
if (!global.modules.lyricWindow) return
bounds = global.modules.lyricWindow.getBounds()
wasW = global.envParams.workAreaSize.width
wasY = global.envParams.workAreaSize.height + offset
let minWidth = 380
let minHeight = 80
exports.getLyricWindowBounds = (bounds, { x = 0, y = 0, w = 0, h = 0 }) => {
if (w < minWidth) w = minWidth
if (h < minHeight) h = minHeight
if (global.appSetting.desktopLyric.isLockScreen) {
wasW = global.envParams.workAreaSize.width + offset
wasH = global.envParams.workAreaSize.height + offset
if (w > wasW + offset) w = wasW + offset
if (h > wasH + offset) h = wasH + offset
if (x == null) {
if (global.appSetting.desktopLyric.x > wasW - w) {
x = wasW - w - global.appSetting.desktopLyric.x
} else if (global.appSetting.desktopLyric.x < -offset) {
x = global.appSetting.desktopLyric.x + offset
} else {
x = 0
}
if (global.appSetting.desktopLyric.y > wasH - h) {
y = wasH - h - global.appSetting.desktopLyric.y
} else if (global.appSetting.desktopLyric.y < -offset) {
y = global.appSetting.desktopLyric.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 {
y += bounds.y
x += bounds.x
}
bounds.width = w
bounds.height = h
if (bounds.width > wasW - offset) {
bounds.width = wasW - offset
} else if (bounds.width < 380) {
bounds.width = 380
}
if (bounds.height > wasY) {
bounds.height = wasY + offset
} else if (bounds.height < 80) {
bounds.height = 80
}
if (global.appSetting.desktopLyric.isLockScreen) {
if (x != 0) {
winX = bounds.x + x
if (winX > wasW - bounds.width + offset) {
winX = wasW - bounds.width + offset
} else if (winX < -offset) {
winX = -offset
}
bounds.x = winX
}
if (y != 0) {
winY = bounds.y + y
if (winY > wasY - bounds.height) {
winY = wasY - bounds.height
} else if (winY < -offset) {
winY = -offset
}
bounds.y = winY
}
} else {
if (x != 0) {
bounds.x = bounds.x + x
}
if (y != 0) {
bounds.y = bounds.y + y
}
}
// console.log(bounds, x, y, w, h)
global.modules.lyricWindow.setBounds(bounds)
bounds.x = x
bounds.y = y
// console.log('util bounds', bounds)
return bounds
}