From 601029c371b5b7cb2b85a45c2e644cbf9f09c20f Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sat, 21 Sep 2024 12:53:14 +0800 Subject: [PATCH] fix theme select --- src/main/resolve/theme.ts | 12 +++--- src/renderer/src/App.tsx | 35 ++++++++--------- .../components/settings/general-config.tsx | 38 +++++++++---------- 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/main/resolve/theme.ts b/src/main/resolve/theme.ts index f5701b0..29a7f9d 100644 --- a/src/main/resolve/theme.ts +++ b/src/main/resolve/theme.ts @@ -21,7 +21,11 @@ export async function resolveThemes(): Promise<{ key: string; label: string }[]> return { key: file, label: name } }) ) - return [{ key: 'default.css', label: '默认' }, ...themes] + if (themes.find((theme) => theme.key === 'default.css')) { + return themes + } else { + return [{ key: 'default.css', label: '默认' }, ...themes] + } } export async function fetchThemes(): Promise { @@ -51,12 +55,6 @@ export async function importThemes(files: string[]): Promise { } export async function applyTheme(theme: string): Promise { - if (theme === 'default.css') { - if (insertedCSSKey) { - await mainWindow?.webContents.removeInsertedCSS(insertedCSSKey) - } - return - } if (!existsSync(path.join(themesDir(), theme))) return const css = await readFile(path.join(themesDir(), theme), 'utf-8') if (insertedCSSKey) { diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 14bf605..6a24308 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -67,6 +67,23 @@ const App: React.FC = () => { const location = useLocation() const page = useRoutes(routes) + const changeTheme = async (): Promise => { + setNativeTheme(appTheme) + setTheme(appTheme) + if (customTheme) await applyTheme(customTheme) + if (!useWindowFrame) { + const options = { height: 48 } as TitleBarOverlayOptions + try { + if (platform !== 'darwin') { + options.color = window.getComputedStyle(document.documentElement).backgroundColor + options.symbolColor = window.getComputedStyle(document.documentElement).color + } + setTitleBarOverlay(options) + } catch (e) { + // ignore + } + } + } useEffect(() => { setOrder(siderOrder) }, [siderOrder]) @@ -80,23 +97,7 @@ const App: React.FC = () => { }, []) useEffect(() => { - setNativeTheme(appTheme) - setTheme(appTheme) - if (customTheme) applyTheme(customTheme) - if (!useWindowFrame) { - setTimeout(() => { - const options = { height: 48 } as TitleBarOverlayOptions - try { - if (platform !== 'darwin') { - options.color = window.getComputedStyle(document.documentElement).backgroundColor - options.symbolColor = window.getComputedStyle(document.documentElement).color - } - setTitleBarOverlay(options) - } catch (e) { - // ignore - } - }, 0) - } + changeTheme() }, [appTheme, systemTheme, customTheme]) const onDragEnd = async (event: DragEndEvent): Promise => { diff --git a/src/renderer/src/components/settings/general-config.tsx b/src/renderer/src/components/settings/general-config.tsx index ccea986..fe84f29 100644 --- a/src/renderer/src/components/settings/general-config.tsx +++ b/src/renderer/src/components/settings/general-config.tsx @@ -24,9 +24,7 @@ import { IoIosHelpCircle, IoMdCloudDownload } from 'react-icons/io' const GeneralConfig: React.FC = () => { const { data: enable, mutate: mutateEnable } = useSWR('checkAutoRun', checkAutoRun) const { appConfig, patchAppConfig } = useAppConfig() - const [customThemes, setCustomThemes] = React.useState([ - { key: 'default.css', label: '默认', content: '' } - ]) + const [customThemes, setCustomThemes] = useState<{ key: string; label: string }[]>() const [fetching, setFetching] = useState(false) const { setTheme } = useTheme() const { @@ -269,22 +267,24 @@ const GeneralConfig: React.FC = () => { } > - + {customThemes && ( + + )}