feat: add fallback mechanism for floating window startup failure

This commit is contained in:
ezequielnick 2025-08-09 13:48:55 +08:00
parent 294dd75b48
commit 58732ce653
2 changed files with 87 additions and 47 deletions

View File

@ -195,7 +195,11 @@ app.whenReady().then(async () => {
registerIpcMainHandlers() registerIpcMainHandlers()
await createWindow() await createWindow()
if (showFloating) { if (showFloating) {
showFloatingWindow() try {
await showFloatingWindow()
} catch (error) {
console.error('Failed to create floating window on startup:', error)
}
} }
if (!disableTray) { if (!disableTray) {
await createTray() await createTray()

View File

@ -9,11 +9,13 @@ import { buildContextMenu, showTrayIcon } from './tray'
export let floatingWindow: BrowserWindow | null = null export let floatingWindow: BrowserWindow | null = null
async function createFloatingWindow(): Promise<void> { async function createFloatingWindow(): Promise<void> {
try {
const floatingWindowState = windowStateKeeper({ const floatingWindowState = windowStateKeeper({
file: 'floating-window-state.json' file: 'floating-window-state.json'
}) })
const { customTheme = 'default.css' } = await getAppConfig() const { customTheme = 'default.css' } = await getAppConfig()
floatingWindow = new BrowserWindow({
const windowOptions: Electron.BrowserWindowConstructorOptions = {
width: 120, width: 120,
height: 42, height: 42,
x: floatingWindowState.x, x: floatingWindowState.x,
@ -28,18 +30,40 @@ async function createFloatingWindow(): Promise<void> {
maximizable: false, maximizable: false,
fullscreenable: false, fullscreenable: false,
closable: false, closable: false,
backgroundColor: '#00000000',
webPreferences: { webPreferences: {
preload: join(__dirname, '../preload/index.js'), preload: join(__dirname, '../preload/index.js'),
spellcheck: false, spellcheck: false,
sandbox: false sandbox: false,
nodeIntegration: false,
contextIsolation: true
} }
}) }
// windows 添加兼容性处理
if (process.platform === 'win32') {
windowOptions.hasShadow = false
windowOptions.webPreferences!.offscreen = false
}
floatingWindow = new BrowserWindow(windowOptions)
floatingWindowState.manage(floatingWindow) floatingWindowState.manage(floatingWindow)
floatingWindow.webContents.on('render-process-gone', (_, details) => {
console.error('Floating window render process gone:', details.reason)
floatingWindow = null
})
floatingWindow.on('ready-to-show', () => { floatingWindow.on('ready-to-show', () => {
try {
applyTheme(customTheme) applyTheme(customTheme)
floatingWindow?.show() floatingWindow?.show()
floatingWindow?.setAlwaysOnTop(true, 'screen-saver') floatingWindow?.setAlwaysOnTop(true, 'screen-saver')
} catch (error) {
console.error('Error in floating window ready-to-show:', error)
}
}) })
floatingWindow.on('moved', () => { floatingWindow.on('moved', () => {
if (floatingWindow) floatingWindowState.saveState(floatingWindow) if (floatingWindow) floatingWindowState.saveState(floatingWindow)
}) })
@ -49,18 +73,30 @@ async function createFloatingWindow(): Promise<void> {
floatingWindow?.webContents.send('appConfigUpdated') floatingWindow?.webContents.send('appConfigUpdated')
} }
}) })
if (is.dev && process.env['ELECTRON_RENDERER_URL']) { if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
floatingWindow.loadURL(`${process.env['ELECTRON_RENDERER_URL']}/floating.html`) await floatingWindow.loadURL(`${process.env['ELECTRON_RENDERER_URL']}/floating.html`)
} else { } else {
floatingWindow.loadFile(join(__dirname, '../renderer/floating.html')) await floatingWindow.loadFile(join(__dirname, '../renderer/floating.html'))
}
} catch (error) {
console.error('Failed to create floating window:', error)
floatingWindow = null
throw error
} }
} }
export async function showFloatingWindow(): Promise<void> { export async function showFloatingWindow(): Promise<void> {
if (floatingWindow) { try {
if (floatingWindow && !floatingWindow.isDestroyed()) {
floatingWindow.show() floatingWindow.show()
} else { } else {
createFloatingWindow() await createFloatingWindow()
}
} catch (error) {
console.error('Failed to show floating window:', error)
await patchAppConfig({ showFloatingWindow: false })
throw error
} }
} }