diff --git a/src/main/resolve/theme.ts b/src/main/resolve/theme.ts index 29a7f9d..417bbc1 100644 --- a/src/main/resolve/theme.ts +++ b/src/main/resolve/theme.ts @@ -57,8 +57,6 @@ export async function importThemes(files: string[]): Promise { export async function applyTheme(theme: string): Promise { 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) } diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 6a24308..202ae73 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -66,11 +66,7 @@ const App: React.FC = () => { navigate = useNavigate() const location = useLocation() const page = useRoutes(routes) - - const changeTheme = async (): Promise => { - 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 => { const { active, over } = event diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index 8b209da..2cb697e 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -343,8 +343,22 @@ export async function importThemes(files: string[]): Promise { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('importThemes', files)) } +let applyThemeRunning = false +const waitList: string[] = [] export async function applyTheme(theme: string): Promise { - 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(