fix css inject

This commit is contained in:
pompurin404 2024-09-21 14:33:03 +08:00
parent cdc4c3fe3c
commit beb19f237d
No known key found for this signature in database
3 changed files with 27 additions and 11 deletions

View File

@ -57,8 +57,6 @@ export async function importThemes(files: string[]): Promise<void> {
export async function applyTheme(theme: string): Promise<void> {
if (!existsSync(path.join(themesDir(), theme))) return
const css = await readFile(path.join(themesDir(), theme), 'utf-8')
if (insertedCSSKey) {
await mainWindow?.webContents.removeInsertedCSS(insertedCSSKey)
}
await mainWindow?.webContents.removeInsertedCSS(insertedCSSKey || '')
insertedCSSKey = await mainWindow?.webContents.insertCSS(css)
}

View File

@ -66,11 +66,7 @@ const App: React.FC = () => {
navigate = useNavigate()
const location = useLocation()
const page = useRoutes(routes)
const changeTheme = async (): Promise<void> => {
setNativeTheme(appTheme)
setTheme(appTheme)
if (customTheme) await applyTheme(customTheme)
const setTitlebar = (): void => {
if (!useWindowFrame) {
const options = { height: 48 } as TitleBarOverlayOptions
try {
@ -97,8 +93,16 @@ const App: React.FC = () => {
}, [])
useEffect(() => {
changeTheme()
}, [appTheme, systemTheme, customTheme])
setNativeTheme(appTheme)
setTheme(appTheme)
setTitlebar()
}, [appTheme, systemTheme])
useEffect(() => {
applyTheme(customTheme || 'default.css').then(() => {
setTitlebar()
})
}, [customTheme])
const onDragEnd = async (event: DragEndEvent): Promise<void> => {
const { active, over } = event

View File

@ -343,8 +343,22 @@ export async function importThemes(files: string[]): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('importThemes', files))
}
let applyThemeRunning = false
const waitList: string[] = []
export async function applyTheme(theme: string): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('applyTheme', theme))
if (applyThemeRunning) {
waitList.push(theme)
return
}
applyThemeRunning = true
try {
return await ipcErrorWrapper(window.electron.ipcRenderer.invoke('applyTheme', theme))
} finally {
applyThemeRunning = false
if (waitList.length > 0) {
await applyTheme(waitList.shift() || '')
}
}
}
export async function registerShortcut(