| import { useCallback, useEffect, useRef } from 'react'; |
| import { useEnv } from '@/context/EnvContext'; |
| import { useThemeStore } from '@/store/themeStore'; |
| import { useSettingsStore } from '@/store/settingsStore'; |
| import { themes, applyCustomTheme, Palette } from '@/styles/themes'; |
| import { getStatusBarHeight, setSystemUIVisibility } from '@/utils/bridge'; |
| import { getOSPlatform } from '@/utils/misc'; |
| import { parseWebViewVersion } from '@/utils/ua'; |
|
|
| type UseThemeProps = { |
| systemUIVisible?: boolean; |
| appThemeColor?: keyof Palette; |
| }; |
|
|
| export const useTheme = ({ |
| systemUIVisible = true, |
| appThemeColor = 'base-100', |
| }: UseThemeProps = {}) => { |
| const { appService } = useEnv(); |
| const { settings } = useSettingsStore(); |
| const isEink = settings?.globalViewSettings?.isEink; |
| const isColorEink = settings?.globalViewSettings?.isColorEink; |
| const isBwEink = isEink && !isColorEink; |
| const { |
| themeColor, |
| isDarkMode, |
| showSystemUI, |
| dismissSystemUI, |
| updateAppTheme, |
| setStatusBarHeight, |
| systemUIAlwaysHidden, |
| setSystemUIAlwaysHidden, |
| } = useThemeStore(); |
|
|
| const useFallbackColors = useRef(false); |
|
|
| useEffect(() => { |
| updateAppTheme(appThemeColor); |
| if (appService?.isAndroidApp) { |
| getStatusBarHeight().then((res) => { |
| if (res.height && res.height > 0) { |
| setStatusBarHeight(res.height / window.devicePixelRatio); |
| } |
| }); |
| } |
| |
| }, [appService?.isAndroidApp]); |
|
|
| const handleSystemUIVisibility = useCallback(() => { |
| if (!appService?.isMobileApp) return; |
|
|
| const visible = !!(systemUIVisible && !systemUIAlwaysHidden); |
| if (visible) { |
| showSystemUI(); |
| } else { |
| dismissSystemUI(); |
| } |
| setSystemUIVisibility({ visible, darkMode: isDarkMode }); |
| |
| }, [appService, isDarkMode, systemUIVisible]); |
|
|
| useEffect(() => { |
| if (appService?.isMobileApp) { |
| handleSystemUIVisibility(); |
| } |
| |
| }, [handleSystemUIVisibility]); |
|
|
| useEffect(() => { |
| if (!appService?.isMobileApp) return; |
|
|
| handleSystemUIVisibility(); |
| const handleVisibilityChange = () => { |
| if (document.visibilityState === 'visible') { |
| handleSystemUIVisibility(); |
| } |
| }; |
| const handleOrientationChange = () => { |
| if (appService?.isIOSApp && getOSPlatform() === 'ios') { |
| |
| |
| |
| const systemUIAlwaysHidden = screen.orientation?.type.includes('landscape'); |
| setSystemUIAlwaysHidden(systemUIAlwaysHidden); |
| handleSystemUIVisibility(); |
| } |
| }; |
| document.addEventListener('visibilitychange', handleVisibilityChange); |
| screen.orientation?.addEventListener('change', handleOrientationChange); |
| return () => { |
| document.removeEventListener('visibilitychange', handleVisibilityChange); |
| screen.orientation?.removeEventListener('change', handleOrientationChange); |
| }; |
| |
| }, [handleSystemUIVisibility]); |
|
|
| useEffect(() => { |
| if (!appService?.isAndroidApp) return; |
| const webViewVersion = parseWebViewVersion(appService); |
| |
| useFallbackColors.current = webViewVersion < 111; |
| }, [appService]); |
|
|
| useEffect(() => { |
| if (!themeColor || !themes.find((t) => t.name === themeColor)) return; |
| if (useFallbackColors.current) { |
| applyCustomTheme(undefined, themeColor, true); |
| } |
| }, [themeColor]); |
|
|
| useEffect(() => { |
| const customThemes = settings.globalReadSettings?.customThemes ?? []; |
| customThemes.forEach((customTheme) => { |
| applyCustomTheme(customTheme, undefined, useFallbackColors.current); |
| }); |
| localStorage.setItem('customThemes', JSON.stringify(customThemes)); |
| }, [settings.globalReadSettings?.customThemes]); |
|
|
| useEffect(() => { |
| const colorScheme = isDarkMode ? 'dark' : 'light'; |
| document.documentElement.setAttribute('data-theme', `${themeColor}-${colorScheme}`); |
| document.documentElement.style.setProperty('color-scheme', colorScheme); |
| document.documentElement.style.setProperty( |
| '--overlayer-highlight-opacity', |
| isBwEink ? '1.0' : '0.3', |
| ); |
| document.documentElement.style.setProperty( |
| '--overlayer-highlight-blend-mode', |
| isBwEink ? 'difference' : isDarkMode ? 'lighten' : 'normal', |
| ); |
| document.documentElement.style.setProperty( |
| '--bg-texture-blend-mode', |
| isDarkMode ? 'lighten' : 'multiply', |
| ); |
| }, [themeColor, isDarkMode, isBwEink]); |
| }; |
|
|