From 9a7d5a383414290bdb42a5bd3a988429bb942edd Mon Sep 17 00:00:00 2001 From: alger Date: Mon, 16 Dec 2024 22:25:38 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E8=AE=B0=E5=BF=86=E6=AD=8C?= =?UTF-8?q?=E8=AF=8D=E7=AA=97=E5=8F=A3=E4=BD=8D=E7=BD=AE=20=E4=B8=BB?= =?UTF-8?q?=E7=AA=97=E5=8F=A3=E5=8F=AF=E5=85=B3=E9=97=AD=E6=AD=8C=E8=AF=8D?= =?UTF-8?q?=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/lyric.js | 28 +++++++++++++++++++++++++--- src/hooks/MusicHook.ts | 21 +++++++++------------ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/electron/lyric.js b/electron/lyric.js index ba03366..c35e308 100644 --- a/electron/lyric.js +++ b/electron/lyric.js @@ -1,14 +1,29 @@ -const { BrowserWindow, screen, ipcRenderer } = require('electron'); +const { BrowserWindow, screen } = require('electron'); const path = require('path'); +const Store = require('electron-store'); const config = require('./config'); +const store = new Store(); let lyricWindow = null; const createWin = () => { console.log('Creating lyric window'); + + // 获取保存的窗口位置 + const windowBounds = store.get('lyricWindowBounds') || {}; + const { x, y, width, height } = windowBounds; + + // 获取屏幕尺寸 + const { width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize; + + // 验证保存的位置是否有效 + const validPosition = x !== undefined && y !== undefined && x >= 0 && y >= 0 && x < screenWidth && y < screenHeight; + lyricWindow = new BrowserWindow({ - width: 800, - height: 200, + width: width || 800, + height: height || 200, + x: validPosition ? x : undefined, + y: validPosition ? y : undefined, frame: false, show: false, transparent: true, @@ -107,6 +122,13 @@ const loadLyricWindow = (ipcMain, mainWin) => { const newY = Math.max(0, Math.min(currentY + deltaY, screenHeight - windowHeight)); lyricWindow.setPosition(newX, newY); + + // 保存新位置 + store.set('lyricWindowBounds', { + ...lyricWindow.getBounds(), + x: newX, + y: newY, + }); }); // 添加鼠标穿透事件处理 diff --git a/src/hooks/MusicHook.ts b/src/hooks/MusicHook.ts index d775756..1f395d2 100644 --- a/src/hooks/MusicHook.ts +++ b/src/hooks/MusicHook.ts @@ -293,25 +293,22 @@ export const sendLyricToWin = () => { export const openLyric = () => { if (!isElectron.value) return; console.log('Opening lyric window with current song:', playMusic.value?.name); - windowData.electronAPI.openLyric(); - isLyricWindowOpen.value = true; - // 延迟一下初始化,确保窗口已经创建 - setTimeout(() => { - if (isLyricWindowOpen.value) { - console.log('Initializing lyric window with data:', { - hasLyrics: lrcArray.value.length > 0, - songName: playMusic.value?.name, - }); + isLyricWindowOpen.value = !isLyricWindowOpen.value; + if (isLyricWindowOpen.value) { + setTimeout(() => { + windowData.electronAPI.openLyric(); sendLyricToWin(); - } - }, 500); + }, 500); + sendLyricToWin(); + } else { + closeLyric(); + } }; // 添加关闭歌词窗口的方法 export const closeLyric = () => { if (!isElectron.value) return; - isLyricWindowOpen.value = false; windowData.electron.ipcRenderer.send('close-lyric'); };