/* * @Date: 2025-05-22 15:38:30 * @LastEditors: 陈子健 * @LastEditTime: 2025-05-23 14:27:20 * @FilePath: /mac-lyric-vue/electron-app/windows/search-window.js */ /* * @Date: 2025-05-22 15:38:30 * @LastEditors: 陈子健 * @LastEditTime: 2025-05-22 15:47:23 * @FilePath: /mac-lyric-vue/electron-app/windows/search-window.js */ const { BrowserWindow, ipcMain } = require('electron'); const path = require('path'); const i18next = require('../modules/i18n'); class SearchWindowManager { constructor() { this.window = null; } createWindow(loadBuiltFiles) { if (this.window) { this.window.focus(); return; } this.window = new BrowserWindow({ width: 600, // 调整宽度以匹配前端设计 height: 600, webPreferences: { nodeIntegration: false, contextIsolation: true, preload: path.join(__dirname, '../preload.js'), webSecurity: true }, frame: false, transparent: true, backgroundColor: '#80000000', // 半透明黑色背景 hasShadow: true, // 启用窗口阴影 resizable: false, alwaysOnTop: true, show: false, center: true, // 窗口居中 skipTaskbar: true, // 不在任务栏显示 titleBarStyle: 'hidden' }); // 加载搜索页面 if (process.env.NODE_ENV === 'development') { const url = 'http://localhost:5173'; console.log('开发环境:加载搜索页面', url); this.window.loadURL(url).catch(err => { console.error('加载搜索页面失败:', err); }); } else { console.log('生产环境:加载搜索页面'); loadBuiltFiles(this.window, '?search=true'); } // 添加更多调试日志 this.window.webContents.on('will-navigate', (event, url) => { console.log('窗口即将导航到:', url); }); this.window.webContents.on('did-navigate', (event, url) => { console.log('窗口已导航到:', url); }); this.window.webContents.on('did-navigate-in-page', (event, url) => { console.log('窗口页面内导航到:', url); }); // 窗口准备好时显示 this.window.once('ready-to-show', () => { console.log('搜索窗口准备就绪,准备显示'); this.window.show(); // 请求主窗口数据 console.log('请求主窗口数据'); ipcMain.emit('request-main-window-data'); // 发送当前语言到搜索窗口 const currentLang = i18next.language; console.log('发送当前语言到搜索窗口:', currentLang); this.window.webContents.send('change-language', currentLang); }); // 添加调试日志 this.window.webContents.on('did-finish-load', () => { console.log('搜索窗口加载完成'); // 确保窗口可见 this.window.show(); this.window.focus(); this.window.webContents.insertCSS(` body { -webkit-app-region: drag; } input, button, .no-drag { -webkit-app-region: no-drag; } `); }); this.window.webContents.on('did-fail-load', (event, errorCode, errorDescription) => { console.error('搜索窗口加载失败:', errorCode, errorDescription); }); // 窗口关闭时清理引用 this.window.on('closed', () => { console.log('搜索窗口已关闭'); this.window = null; }); // 开发环境下打开开发者工具 if (process.env.NODE_ENV === 'development') { this.window.webContents.openDevTools(); } return this.window; } // 接收主窗口数据的方法 receiveMainWindowData(data) { if (this.window && !this.window.isDestroyed()) { console.log('向搜索窗口发送数据:', data); this.window.webContents.send('main-window-data', data); } } closeWindow() { if (this.window) { this.window.close(); this.window = null; } } focusWindow() { if (this.window) { this.window.focus(); } } } module.exports = new SearchWindowManager();