mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 13:10:30 +08:00
debug floating window
This commit is contained in:
parent
4a192586fc
commit
eb41bae23b
@ -5,15 +5,47 @@ import { join } from 'path'
|
|||||||
import { getAppConfig, patchAppConfig } from '../config'
|
import { getAppConfig, patchAppConfig } from '../config'
|
||||||
import { applyTheme } from './theme'
|
import { applyTheme } from './theme'
|
||||||
import { buildContextMenu, showTrayIcon } from './tray'
|
import { buildContextMenu, showTrayIcon } from './tray'
|
||||||
|
import { writeFile } from 'fs/promises'
|
||||||
|
import { logDir } from '../utils/dirs'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
export let floatingWindow: BrowserWindow | null = null
|
export let floatingWindow: BrowserWindow | null = null
|
||||||
|
|
||||||
|
// 悬浮窗日志记录
|
||||||
|
async function logFloatingWindow(message: string, error?: any): Promise<void> {
|
||||||
|
try {
|
||||||
|
const timestamp = new Date().toISOString()
|
||||||
|
const logMessage = error
|
||||||
|
? `[${timestamp}] [FloatingWindow] ${message}: ${error}\n`
|
||||||
|
: `[${timestamp}] [FloatingWindow] ${message}\n`
|
||||||
|
|
||||||
|
const logPath = path.join(logDir(), 'floating-window.log')
|
||||||
|
await writeFile(logPath, logMessage, { flag: 'a' })
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error(`[FloatingWindow] ${message}:`, error)
|
||||||
|
} else {
|
||||||
|
console.log(`[FloatingWindow] ${message}`)
|
||||||
|
}
|
||||||
|
} catch (logError) {
|
||||||
|
|
||||||
|
console.error('[FloatingWindow] Failed to write log:', logError)
|
||||||
|
console.log(`[FloatingWindow] Original message: ${message}`, error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function createFloatingWindow(): Promise<void> {
|
async function createFloatingWindow(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
await logFloatingWindow('Starting to create floating window...')
|
||||||
const floatingWindowState = windowStateKeeper({
|
const floatingWindowState = windowStateKeeper({
|
||||||
file: 'floating-window-state.json'
|
file: 'floating-window-state.json'
|
||||||
})
|
})
|
||||||
|
await logFloatingWindow('Window state keeper initialized')
|
||||||
const { customTheme = 'default.css' } = await getAppConfig()
|
const { customTheme = 'default.css' } = await getAppConfig()
|
||||||
|
await logFloatingWindow(`App config loaded, theme: ${customTheme}`)
|
||||||
|
|
||||||
|
const safeMode = process.env.FLOATING_SAFE_MODE === 'true'
|
||||||
|
await logFloatingWindow(`Safe mode: ${safeMode}`)
|
||||||
|
|
||||||
const windowOptions: Electron.BrowserWindowConstructorOptions = {
|
const windowOptions: Electron.BrowserWindowConstructorOptions = {
|
||||||
width: 120,
|
width: 120,
|
||||||
@ -21,16 +53,16 @@ async function createFloatingWindow(): Promise<void> {
|
|||||||
x: floatingWindowState.x,
|
x: floatingWindowState.x,
|
||||||
y: floatingWindowState.y,
|
y: floatingWindowState.y,
|
||||||
show: false,
|
show: false,
|
||||||
frame: false,
|
frame: safeMode ? true : false,
|
||||||
alwaysOnTop: true,
|
alwaysOnTop: !safeMode,
|
||||||
resizable: false,
|
resizable: safeMode,
|
||||||
transparent: true,
|
transparent: !safeMode,
|
||||||
skipTaskbar: true,
|
skipTaskbar: !safeMode,
|
||||||
minimizable: false,
|
minimizable: safeMode,
|
||||||
maximizable: false,
|
maximizable: safeMode,
|
||||||
fullscreenable: false,
|
fullscreenable: false,
|
||||||
closable: false,
|
closable: safeMode,
|
||||||
backgroundColor: '#00000000',
|
backgroundColor: safeMode ? '#ffffff' : '#00000000',
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: join(__dirname, '../preload/index.js'),
|
preload: join(__dirname, '../preload/index.js'),
|
||||||
spellcheck: false,
|
spellcheck: false,
|
||||||
@ -42,25 +74,32 @@ async function createFloatingWindow(): Promise<void> {
|
|||||||
|
|
||||||
// windows 添加兼容性处理
|
// windows 添加兼容性处理
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
windowOptions.hasShadow = false
|
windowOptions.hasShadow = !safeMode
|
||||||
windowOptions.webPreferences!.offscreen = false
|
windowOptions.webPreferences!.offscreen = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await logFloatingWindow(`Creating BrowserWindow with options: ${JSON.stringify(windowOptions, null, 2)}`)
|
||||||
floatingWindow = new BrowserWindow(windowOptions)
|
floatingWindow = new BrowserWindow(windowOptions)
|
||||||
|
await logFloatingWindow('BrowserWindow created successfully')
|
||||||
floatingWindowState.manage(floatingWindow)
|
floatingWindowState.manage(floatingWindow)
|
||||||
|
await logFloatingWindow('Window state management attached')
|
||||||
|
|
||||||
floatingWindow.webContents.on('render-process-gone', (_, details) => {
|
floatingWindow.webContents.on('render-process-gone', async (_, details) => {
|
||||||
console.error('Floating window render process gone:', details.reason)
|
await logFloatingWindow('Render process gone', details.reason)
|
||||||
floatingWindow = null
|
floatingWindow = null
|
||||||
})
|
})
|
||||||
|
|
||||||
floatingWindow.on('ready-to-show', () => {
|
floatingWindow.on('ready-to-show', async () => {
|
||||||
try {
|
try {
|
||||||
|
await logFloatingWindow('Window ready to show, applying theme...')
|
||||||
applyTheme(customTheme)
|
applyTheme(customTheme)
|
||||||
|
await logFloatingWindow('Theme applied, showing window...')
|
||||||
floatingWindow?.show()
|
floatingWindow?.show()
|
||||||
|
await logFloatingWindow('Window shown, setting always on top...')
|
||||||
floatingWindow?.setAlwaysOnTop(true, 'screen-saver')
|
floatingWindow?.setAlwaysOnTop(true, 'screen-saver')
|
||||||
|
await logFloatingWindow('Floating window setup completed successfully')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in floating window ready-to-show:', error)
|
await logFloatingWindow('Error in ready-to-show', error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -74,13 +113,22 @@ async function createFloatingWindow(): Promise<void> {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
await logFloatingWindow('Loading page...')
|
||||||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||||||
await floatingWindow.loadURL(`${process.env['ELECTRON_RENDERER_URL']}/floating.html`)
|
const devUrl = `${process.env['ELECTRON_RENDERER_URL']}/floating.html`
|
||||||
|
await logFloatingWindow(`Loading dev URL: ${devUrl}`)
|
||||||
|
await floatingWindow.loadURL(devUrl)
|
||||||
} else {
|
} else {
|
||||||
await floatingWindow.loadFile(join(__dirname, '../renderer/floating.html'))
|
const filePath = join(__dirname, '../renderer/floating.html')
|
||||||
|
await logFloatingWindow(`Loading file: ${filePath}`)
|
||||||
|
await floatingWindow.loadFile(filePath)
|
||||||
}
|
}
|
||||||
|
await logFloatingWindow('Page loaded successfully')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to create floating window:', error)
|
await logFloatingWindow('Failed to create floating window', error)
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logFloatingWindow(`Error stack: ${error.stack}`)
|
||||||
|
}
|
||||||
floatingWindow = null
|
floatingWindow = null
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
@ -89,13 +137,16 @@ async function createFloatingWindow(): Promise<void> {
|
|||||||
export async function showFloatingWindow(): Promise<void> {
|
export async function showFloatingWindow(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
if (floatingWindow && !floatingWindow.isDestroyed()) {
|
if (floatingWindow && !floatingWindow.isDestroyed()) {
|
||||||
|
await logFloatingWindow('Showing existing floating window')
|
||||||
floatingWindow.show()
|
floatingWindow.show()
|
||||||
} else {
|
} else {
|
||||||
|
await logFloatingWindow('Creating new floating window')
|
||||||
await createFloatingWindow()
|
await createFloatingWindow()
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to show floating window:', error)
|
await logFloatingWindow('Failed to show floating window', error)
|
||||||
await patchAppConfig({ showFloatingWindow: false })
|
await patchAppConfig({ showFloatingWindow: false })
|
||||||
|
await logFloatingWindow('Disabled floating window in config due to error')
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user