import { join } from 'path' import { BrowserWindow } from 'electron' import { debounce, encodePath, isLinux } from '@common/utils' import { getLyricWindowBounds } from './utils' import { mainSend } from '@common/mainIpc' // require('./event') // require('./rendererEvent') let browserWindow: Electron.BrowserWindow | null = null const setLyricsConfig = debounce((config: Partial) => { global.lx.event_app.update_config(config) }, 500) const winEvent = () => { if (!browserWindow) return // let bounds // lyricWindow.on('close', event => { // if (global.isQuitting || !global.appSetting.tray.enable || (!isWin && !global.isTrafficLightClose)) { // lyricWindow.setProgressBar(-1) // return // } // if (global.isTrafficLightClose) global.isTrafficLightClose = false // event.preventDefault() // lyricWindow.hide() // }) browserWindow.on('closed', () => { browserWindow = null }) browserWindow.on('move', () => { // bounds = browserWindow.getBounds() // console.log(bounds) const bounds = browserWindow!.getBounds() setLyricsConfig({ 'desktopLyric.x': bounds.x, 'desktopLyric.y': bounds.y, 'desktopLyric.width': bounds.width, 'desktopLyric.height': bounds.height, }) }) browserWindow.on('resize', () => { // bounds = browserWindow.getBounds() // console.log(bounds) const bounds = browserWindow!.getBounds() setLyricsConfig({ 'desktopLyric.x': bounds.x, 'desktopLyric.y': bounds.y, 'desktopLyric.width': bounds.width, 'desktopLyric.height': bounds.height, }) }) // browserWindow.on('restore', () => { // browserWindow.webContents.send('restore') // }) // browserWindow.on('focus', () => { // browserWindow.webContents.send('focus') // }) browserWindow.once('ready-to-show', () => { showWindow() if (global.lx.appSetting['desktopLyric.isLock']) { browserWindow!.setIgnoreMouseEvents(true, { forward: !isLinux && global.lx.appSetting['desktopLyric.isHoverHide'] }) } // linux下每次重开时貌似要重新设置置顶 if (isLinux && global.lx.appSetting['desktopLyric.isAlwaysOnTop']) { browserWindow!.setAlwaysOnTop(global.lx.appSetting['desktopLyric.isAlwaysOnTop'], 'screen-saver') } if (global.lx.appSetting['desktopLyric.isAlwaysOnTop'] && global.lx.appSetting['desktopLyric.isAlwaysOnTopLoop']) alwaysOnTopTools.startLoop() }) } export const createWindow = () => { closeWindow() if (!global.envParams.workAreaSize) 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 { width: screenWidth, height: screenHeight } = global.envParams.workAreaSize if (x == null || y == null) { x = screenWidth - width y = screenHeight - height } if (isLockScreen) { let bounds = getLyricWindowBounds({ x, y, width, height }, { x: null, y: 0, w: width, h: height }) x = bounds.x y = bounds.y width = bounds.width height = bounds.height } const { shouldUseDarkColors, theme } = global.lx.theme /** * Initial window options */ browserWindow = new BrowserWindow({ height, width, x, y, minWidth: 380, minHeight: 80, useContentSize: true, frame: false, transparent: true, // enableRemoteModule: false, // icon: join(global.__static, isWin ? 'icons/256x256.ico' : 'icons/512x512.png'), resizable: false, minimizable: false, maximizable: false, fullscreenable: false, show: false, alwaysOnTop: isAlwaysOnTop, skipTaskbar: true, webPreferences: { contextIsolation: false, webSecurity: false, sandbox: false, nodeIntegration: true, enableWebSQL: false, webgl: false, spellcheck: false, // 禁用拼写检查器 }, }) const winURL = global.isDev ? 'http://localhost:9081/lyric.html' : `file://${join(encodePath(__dirname), 'lyric.html')}` void browserWindow.loadURL(winURL + `?dark=${shouldUseDarkColors}&theme=${encodeURIComponent(JSON.stringify(theme))}`) winEvent() // browserWindow.webContents.openDevTools() } export const isExistWindow = (): boolean => !!browserWindow export const closeWindow = () => { if (!browserWindow) return browserWindow.close() } export const showWindow = () => { if (!browserWindow) return browserWindow.show() } export const sendEvent = (name: string, params?: T) => { if (!browserWindow) return mainSend(browserWindow, name, params) } export const getBounds = (): Electron.Rectangle => { if (!browserWindow) throw new Error('window is not available') return browserWindow.getBounds() } export const setBounds = (bounds: Electron.Rectangle) => { if (!browserWindow) return browserWindow.setBounds(bounds) } export const setIgnoreMouseEvents = (ignore: boolean, options?: Electron.IgnoreMouseEventsOptions) => { if (!browserWindow) return browserWindow.setIgnoreMouseEvents(ignore, options) } export const setAlwaysOnTop = (flag: boolean, level?: 'normal' | 'floating' | 'torn-off-menu' | 'modal-panel' | 'main-menu' | 'status' | 'pop-up-menu' | 'screen-saver' | undefined, relativeLevel?: number | undefined) => { if (!browserWindow) return browserWindow.setAlwaysOnTop(flag, level, relativeLevel) } export const getMainFrame = (): Electron.WebFrameMain | null => { if (!browserWindow) return null return browserWindow.webContents.mainFrame } interface AlwaysOnTopTools { timeout: NodeJS.Timeout | null setAlwaysOnTop: (isLoop: boolean) => void startLoop: () => void clearLoop: () => void } export const alwaysOnTopTools: AlwaysOnTopTools = { timeout: null, setAlwaysOnTop(isLoop) { this.clearLoop() setAlwaysOnTop(global.lx.appSetting['desktopLyric.isAlwaysOnTop'], 'screen-saver') // console.log(isLoop) if (isLoop) this.startLoop() }, startLoop() { this.clearLoop() this.timeout = setInterval(() => { if (!isExistWindow()) return this.clearLoop() setAlwaysOnTop(true, 'screen-saver') }, 1000) }, clearLoop() { if (!this.timeout) return clearInterval(this.timeout) this.timeout = null }, }