perf(lyric-window): 仅在锁定+可见时启用鼠标位置轮询

- 新增 set-lyric-lock-state IPC, 渲染端 watch isLock 同步到主进程
- 主进程通过 isLyricLocked + isLyricWindowVisible 控制轮询启停
- 监听窗口 show/hide/minimize/restore, 隐藏时停止 50ms 轮询
- 解锁状态下 DOM mouseenter/mouseleave 已足够, 无需轮询兜底
This commit is contained in:
alger
2026-04-19 15:31:23 +08:00
parent 0d960aa8d5
commit 6b22713854
2 changed files with 42 additions and 3 deletions
+37 -3
View File
@@ -10,8 +10,11 @@ let isDragging = false;
// 添加窗口大小变化防护
let originalSize = { width: 0, height: 0 };
// 轮询只在 "锁定 + 窗口可见" 时启用:解锁状态下 DOM 事件足够,避免常驻 50ms 轮询
let mousePresenceTimer: ReturnType<typeof setInterval> | null = null;
let lastMouseInside: boolean | null = null;
let isLyricLocked = false;
let isLyricWindowVisible = false;
const isPointInsideWindow = (
point: { x: number; y: number },
@@ -59,6 +62,14 @@ const startMousePresenceTracking = () => {
}, 50);
};
const syncMousePresenceTracking = () => {
if (isLyricLocked && isLyricWindowVisible && lyricWindow && !lyricWindow.isDestroyed()) {
startMousePresenceTracking();
} else {
stopMousePresenceTracking();
}
};
const createWin = () => {
console.log('Creating lyric window');
@@ -151,12 +162,32 @@ const createWin = () => {
// 监听窗口关闭事件
lyricWindow.on('closed', () => {
stopMousePresenceTracking();
isLyricLocked = false;
isLyricWindowVisible = false;
if (lyricWindow) {
lyricWindow.destroy();
lyricWindow = null;
}
});
// 窗口可见性变化时同步轮询状态,避免最小化/隐藏时空转
lyricWindow.on('show', () => {
isLyricWindowVisible = true;
syncMousePresenceTracking();
});
lyricWindow.on('hide', () => {
isLyricWindowVisible = false;
stopMousePresenceTracking();
});
lyricWindow.on('minimize', () => {
isLyricWindowVisible = false;
stopMousePresenceTracking();
});
lyricWindow.on('restore', () => {
isLyricWindowVisible = true;
syncMousePresenceTracking();
});
// 监听窗口大小变化事件,保存新的尺寸
lyricWindow.on('resize', () => {
// 如果正在拖动,忽略大小调整事件
@@ -184,7 +215,6 @@ export const loadLyricWindow = (ipcMain: IpcMain, mainWin: BrowserWindow): void
}
lyricWindow.focus();
lyricWindow.show();
startMousePresenceTracking();
return true;
}
return false;
@@ -219,7 +249,6 @@ export const loadLyricWindow = (ipcMain: IpcMain, mainWin: BrowserWindow): void
win.once('ready-to-show', () => {
console.log('Lyric window ready to show');
win.show();
startMousePresenceTracking();
});
});
@@ -248,7 +277,6 @@ export const loadLyricWindow = (ipcMain: IpcMain, mainWin: BrowserWindow): void
ipcMain.on('close-lyric', () => {
if (lyricWindow && !lyricWindow.isDestroyed()) {
stopMousePresenceTracking();
lyricWindow.webContents.send('lyric-window-close');
mainWin.webContents.send('lyric-control-back', 'close');
mainWin.webContents.send('lyric-window-closed');
@@ -257,6 +285,12 @@ export const loadLyricWindow = (ipcMain: IpcMain, mainWin: BrowserWindow): void
}
});
// 渲染端同步锁定状态 → 决定主进程是否需要轮询鼠标位置
ipcMain.on('set-lyric-lock-state', (_, isLocked: boolean) => {
isLyricLocked = isLocked;
syncMousePresenceTracking();
});
// 处理鼠标事件
ipcMain.on('mouseenter-lyric', () => {
if (lyricWindow && !lyricWindow.isDestroyed()) {
+5
View File
@@ -401,6 +401,8 @@ watch(
// 锁定时自动关闭主题色面板
showThemeColorPanel.value = false;
}
// 通知主进程,按需启停鼠标位置轮询
windowData.electron.ipcRenderer.send('set-lyric-lock-state', newLock);
}
);
@@ -794,6 +796,9 @@ onMounted(() => {
}
}
);
// 同步初始锁定状态到主进程,使其按需启动鼠标位置轮询
windowData.electron.ipcRenderer.send('set-lyric-lock-state', lyricSetting.value.isLock);
});
onUnmounted(() => {