Skip to content

Commit

Permalink
feat: 允许歌词窗口不超出任意屏幕而不仅是主屏
Browse files Browse the repository at this point in the history
  • Loading branch information
14Kay committed Oct 12, 2024
1 parent 489bbb6 commit 329cbd1
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/common/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ declare namespace LX {
interface EnvParams {
deeplink?: string | null
cmdParams: CmdParams
workAreaSize?: Electron.Size
workArea?: Electron.Rectangle
}

interface HotKey {
Expand Down
2 changes: 1 addition & 1 deletion src/lang/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
"setting__desktop_lyric_line_gap_add": "Increase spacing",
"setting__desktop_lyric_line_gap_dec": "Reduce spacing",
"setting__desktop_lyric_lock": "Lock lyrics",
"setting__desktop_lyric_lock_screen": "It is not allowed to drag the lyrics window out of the main screen",
"setting__desktop_lyric_lock_screen": "It is not allowed to drag the lyrics window out of the current screen",
"setting__desktop_lyric_played_color": "color played",
"setting__desktop_lyric_reset": "Reset",
"setting__desktop_lyric_reset_window": "Reset window settings",
Expand Down
2 changes: 1 addition & 1 deletion src/lang/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
"setting__desktop_lyric_line_gap_add": "加大间距",
"setting__desktop_lyric_line_gap_dec": "减小间距",
"setting__desktop_lyric_lock": "锁定歌词",
"setting__desktop_lyric_lock_screen": "不允许歌词窗口拖出主屏幕之外",
"setting__desktop_lyric_lock_screen": "不允许歌词窗口拖出当前屏幕之外",
"setting__desktop_lyric_played_color": "已播放颜色",
"setting__desktop_lyric_reset": "重置",
"setting__desktop_lyric_reset_window": "重置窗口设置",
Expand Down
2 changes: 1 addition & 1 deletion src/lang/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
"setting__desktop_lyric_line_gap_add": "加大間距",
"setting__desktop_lyric_line_gap_dec": "減小間距",
"setting__desktop_lyric_lock": "鎖定歌詞",
"setting__desktop_lyric_lock_screen": "不允許歌詞視窗拖出主畫面之外",
"setting__desktop_lyric_lock_screen": "不允許歌詞視窗拖出目前畫面之外",
"setting__desktop_lyric_played_color": "已播放顏色",
"setting__desktop_lyric_reset": "重置",
"setting__desktop_lyric_reset_window": "重置視窗設定",
Expand Down
2 changes: 1 addition & 1 deletion src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const listenerAppEvent = (startApp: () => void) => {
})

const initScreenParams = () => {
global.envParams.workAreaSize = screen.getPrimaryDisplay().workAreaSize
global.envParams.workArea = screen.getPrimaryDisplay().workArea
}
app.on('ready', () => {
screen.on('display-metrics-changed', initScreenParams)
Expand Down
15 changes: 14 additions & 1 deletion src/main/modules/winLyric/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { isLinux } from '@common/utils'
import { closeWindow, createWindow, getBounds, isExistWindow, alwaysOnTopTools, setBounds, setIgnoreMouseEvents, setSkipTaskbar } from './main'
import { sendConfigChange } from './rendererEvent'
import { buildLyricConfig, getLyricWindowBounds, initWindowSize, watchConfigKeys } from './utils'
import { buildLyricConfig, getLyricWindowBounds, initWindowSize, watchConfigKeys, setWorkArea } from './utils'
import { screen } from 'electron'

let isLock: boolean
let isEnable: boolean
Expand Down Expand Up @@ -53,6 +54,18 @@ export const setLrcConfig = (keys: Array<keyof LX.AppSetting>, setting: Partial<
}
if (keys.includes('desktopLyric.isLockScreen') && isLockScreen != global.lx.appSetting['desktopLyric.isLockScreen']) {
isLockScreen = global.lx.appSetting['desktopLyric.isLockScreen']
const bounds = getBounds()
const displays = screen.getAllDisplays()
const displayOn = displays.find((display: Electron.Display) => {
const { x, y, width, height } = display.bounds
return (
bounds.x >= x &&
bounds.y >= y &&
bounds.x < x + width &&
bounds.y < y + height
)
})
setWorkArea(displayOn!.workArea || displays[0].workArea)
if (global.lx.appSetting['desktopLyric.isLockScreen']) {
setBounds(getLyricWindowBounds(getBounds(), {
x: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/main/modules/winLyric/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ const winEvent = () => {

export const createWindow = () => {
closeWindow()
if (!global.envParams.workAreaSize) return
if (!global.envParams.workArea) return
let x = global.lx.appSetting['desktopLyric.x']
let y = global.lx.appSetting['desktopLyric.y']
let width = global.lx.appSetting['desktopLyric.width']
let height = global.lx.appSetting['desktopLyric.height']
let isAlwaysOnTop = global.lx.appSetting['desktopLyric.isAlwaysOnTop']
// let isLockScreen = global.lx.appSetting['desktopLyric.isLockScreen']
let isShowTaskbar = global.lx.appSetting['desktopLyric.isShowTaskbar']
// let { width: screenWidth, height: screenHeight } = global.envParams.workAreaSize
// let { width: screenWidth, height: screenHeight } = global.envParams.workArea
const winSize = initWindowSize(x, y, width, height)
global.lx.event_app.update_config({
'desktopLyric.x': winSize.x,
Expand Down
35 changes: 18 additions & 17 deletions src/main/modules/winLyric/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export let minHeight = 50
// return bounds
// }

export const setWorkArea = (workArea: Electron.Rectangle) => {
global.envParams.workArea = workArea
}

/**
*
* @param bounds 当前设置
Expand All @@ -19,24 +23,21 @@ export const getLyricWindowBounds = (bounds: Electron.Rectangle, { x = 0, y = 0,
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 workArea = global.envParams.workArea
if (!workArea) return bounds

const maxX = global.envParams.workAreaSize.width - w
const maxY = global.envParams.workAreaSize.height - h
const maxWinW = workArea.width
const maxWinH = workArea.height

x += bounds.x
y += bounds.y
// 限制宽高不超过工作区域
w = Math.min(w, maxWinW)
h = Math.min(h, maxWinH)

if (x > maxX) x = maxX
else if (x < 0) x = 0
const maxX = workArea.x + maxWinW - w
const maxY = workArea.y + maxWinH - h

if (y > maxY) y = maxY
else if (y < 0) y = 0
x = Math.max(workArea.x, Math.min(x + bounds.x, maxX))
y = Math.max(workArea.y, Math.min(y + bounds.y, maxY))
} else {
y += bounds.y
x += bounds.x
Expand Down Expand Up @@ -97,9 +98,9 @@ export const initWindowSize = (x: LX.AppSetting['desktopLyric.x'], y: LX.AppSett
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
if (global.envParams.workArea) {
x = global.envParams.workArea.width - width
y = global.envParams.workArea.height - height
} else {
x = y = 0
}
Expand Down

0 comments on commit 329cbd1

Please sign in to comment.