feat: 记忆歌词窗口位置 主窗口可关闭歌词窗口

This commit is contained in:
alger
2024-12-16 22:25:38 +08:00
parent 2037798fbe
commit 9a7d5a3834
2 changed files with 34 additions and 15 deletions
+25 -3
View File
@@ -1,14 +1,29 @@
const { BrowserWindow, screen, ipcRenderer } = require('electron'); const { BrowserWindow, screen } = require('electron');
const path = require('path'); const path = require('path');
const Store = require('electron-store');
const config = require('./config'); const config = require('./config');
const store = new Store();
let lyricWindow = null; let lyricWindow = null;
const createWin = () => { const createWin = () => {
console.log('Creating lyric window'); 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({ lyricWindow = new BrowserWindow({
width: 800, width: width || 800,
height: 200, height: height || 200,
x: validPosition ? x : undefined,
y: validPosition ? y : undefined,
frame: false, frame: false,
show: false, show: false,
transparent: true, transparent: true,
@@ -107,6 +122,13 @@ const loadLyricWindow = (ipcMain, mainWin) => {
const newY = Math.max(0, Math.min(currentY + deltaY, screenHeight - windowHeight)); const newY = Math.max(0, Math.min(currentY + deltaY, screenHeight - windowHeight));
lyricWindow.setPosition(newX, newY); lyricWindow.setPosition(newX, newY);
// 保存新位置
store.set('lyricWindowBounds', {
...lyricWindow.getBounds(),
x: newX,
y: newY,
});
}); });
// 添加鼠标穿透事件处理 // 添加鼠标穿透事件处理
+9 -12
View File
@@ -293,25 +293,22 @@ export const sendLyricToWin = () => {
export const openLyric = () => { export const openLyric = () => {
if (!isElectron.value) return; if (!isElectron.value) return;
console.log('Opening lyric window with current song:', playMusic.value?.name); console.log('Opening lyric window with current song:', playMusic.value?.name);
windowData.electronAPI.openLyric();
isLyricWindowOpen.value = true;
// 延迟一下初始化,确保窗口已经创建 isLyricWindowOpen.value = !isLyricWindowOpen.value;
setTimeout(() => { if (isLyricWindowOpen.value) {
if (isLyricWindowOpen.value) { setTimeout(() => {
console.log('Initializing lyric window with data:', { windowData.electronAPI.openLyric();
hasLyrics: lrcArray.value.length > 0,
songName: playMusic.value?.name,
});
sendLyricToWin(); sendLyricToWin();
} }, 500);
}, 500); sendLyricToWin();
} else {
closeLyric();
}
}; };
// 添加关闭歌词窗口的方法 // 添加关闭歌词窗口的方法
export const closeLyric = () => { export const closeLyric = () => {
if (!isElectron.value) return; if (!isElectron.value) return;
isLyricWindowOpen.value = false;
windowData.electron.ipcRenderer.send('close-lyric'); windowData.electron.ipcRenderer.send('close-lyric');
}; };