From 0bbc661e93698312e162e0b8391aea20b553c9a9 Mon Sep 17 00:00:00 2001 From: Zephyruso <127948745+Zephyruso@users.noreply.github.com> Date: Sat, 2 Sep 2023 19:06:02 +0800 Subject: [PATCH] feat: auto switch theme --- src/App.tsx | 3 ++ src/i18n/en.ts | 3 ++ src/i18n/zh.ts | 3 ++ src/pages/Config.tsx | 65 ++++++++++++++++++++++++++++++++++++++++--- src/signals/config.ts | 36 ++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c97c9e5..b9c4009 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,6 +3,7 @@ import { Show, lazy, onMount } from 'solid-js' import { Header } from '~/components/Header' import { curTheme, selectedEndpoint } from '~/signals' import { ROUTE } from './config/enum' +import { useAutoSwitchTheme } from './signals/config' const Setup = lazy(() => import('~/pages/Setup')) const Overview = lazy(() => import('~/pages/Overview')) @@ -16,6 +17,8 @@ const Config = lazy(() => import('~/pages/Config')) export const App = () => { const navigate = useNavigate() + useAutoSwitchTheme() + onMount(async () => { if (!selectedEndpoint()) { navigate('/setup') diff --git a/src/i18n/en.ts b/src/i18n/en.ts index cf626ab..476cc49 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -40,4 +40,7 @@ export default { proxiesPreviewType: 'Proxies preview type', urlForDelayTest: 'Url for delay test', autoCloseConns: 'Automatically close all connections', + autoSwitchTheme: 'Automatically switch theme', + favDayTheme: 'Favorite light theme', + favNightTheme: 'Favorite dark theme', } diff --git a/src/i18n/zh.ts b/src/i18n/zh.ts index 6f1aeb7..eac9603 100644 --- a/src/i18n/zh.ts +++ b/src/i18n/zh.ts @@ -40,4 +40,7 @@ export default { proxiesPreviewType: '节点组预览样式', urlForDelayTest: '测速链接', autoCloseConns: '切换代理时自动断开全部连接', + autoSwitchTheme: '自动切换主题', + favDayTheme: '浅色主题偏好', + favNightTheme: '深色主题偏好', } diff --git a/src/pages/Config.tsx b/src/pages/Config.tsx index 3046dee..b9a4220 100644 --- a/src/pages/Config.tsx +++ b/src/pages/Config.tsx @@ -4,11 +4,19 @@ import { useI18n } from '@solid-primitives/i18n' import { For, Show, createSignal, onMount } from 'solid-js' import { z } from 'zod' import { PROXIES_PREVIEW_TYPE } from '~/config/enum' +import { themes } from '~/constants' import { useRequest } from '~/signals' import { + applyThemeByMode, autoCloseConns, + autoSwitchTheme, + favDayTheme, + favNightTheme, proxiesPreviewType, setAutoCloseConns, + setAutoSwitchTheme, + setFavDayTheme, + setFavNightTheme, setProxiesPreviewType, setUrlForDelayTest, urlForDelayTest, @@ -142,10 +150,59 @@ const ConfigForXd = () => { const [t] = useI18n() return ( -
+
+
+
{t('autoSwitchTheme')}
+ { + setAutoSwitchTheme(e.target.checked) + applyThemeByMode() + }} + /> +
+ +
+
{t('favDayTheme')}
+ +
+
+
{t('favNightTheme')}
+ +
+
{t('proxiesPreviewType')}
-
{(value) => ( @@ -176,11 +233,11 @@ const ConfigForXd = () => { />
-
+
{t('urlForDelayTest')}
setUrlForDelayTest(e.target?.value!)} /> diff --git a/src/signals/config.ts b/src/signals/config.ts index d45320f..362b339 100644 --- a/src/signals/config.ts +++ b/src/signals/config.ts @@ -1,6 +1,7 @@ import { makePersisted } from '@solid-primitives/storage' import { createSignal } from 'solid-js' import { PROXIES_PREVIEW_TYPE } from '~/config/enum' +import { setCurTheme } from '~/signals' export const [proxiesPreviewType, setProxiesPreviewType] = makePersisted( createSignal(PROXIES_PREVIEW_TYPE.BAR), @@ -14,3 +15,38 @@ export const [autoCloseConns, setAutoCloseConns] = makePersisted( createSignal(false), { name: 'autoCloseConns', storage: localStorage }, ) +export const [autoSwitchTheme, setAutoSwitchTheme] = makePersisted( + createSignal(false), + { name: 'autoSwitchTheme', storage: localStorage }, +) +export const [favDayTheme, setFavDayTheme] = makePersisted( + createSignal('light'), + { name: 'favDayTheme', storage: localStorage }, +) +export const [favNightTheme, setFavNightTheme] = makePersisted( + createSignal('night'), + { name: 'favNightTheme', storage: localStorage }, +) + +const setTheme = (isDark: boolean) => { + if (autoSwitchTheme()) { + if (isDark) { + setCurTheme(favNightTheme()) + } else { + setCurTheme(favDayTheme()) + } + } +} + +export function applyThemeByMode() { + setTheme(window.matchMedia('(prefers-color-scheme: dark)').matches) +} + +export function useAutoSwitchTheme() { + applyThemeByMode() + window + .matchMedia('(prefers-color-scheme: dark)') + .addEventListener('change', (event) => { + setTheme(event.matches) + }) +}