From b0fa4bf5d0c109bcad406115ab3114657aab8776 Mon Sep 17 00:00:00 2001 From: illusia <64828020+EchoSixHIYA@users.noreply.github.com> Date: Fri, 4 Sep 2026 08:05:19 +0800 Subject: [PATCH] fix: make theme toggle respond on first click --- web/src/components/Icon.vue | 5 +++++ web/src/services/theme.ts | 10 +++++++++- web/src/views/AdminView.vue | 12 +++++++++--- web/src/views/WebClient.vue | 15 ++++++++++----- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/web/src/components/Icon.vue b/web/src/components/Icon.vue index aa389f2..bf779d1 100644 --- a/web/src/components/Icon.vue +++ b/web/src/components/Icon.vue @@ -68,6 +68,11 @@ + + diff --git a/web/src/services/theme.ts b/web/src/services/theme.ts index 9fdd814..4a39b93 100644 --- a/web/src/services/theme.ts +++ b/web/src/services/theme.ts @@ -17,8 +17,16 @@ export function saveTheme(theme: ThemeMode): void { applyTheme(theme); } +export function isDarkTheme(theme: ThemeMode): boolean { + if (theme === "dark") return true; + if (theme === "light") return false; + return typeof window !== "undefined" && typeof window.matchMedia === "function" + ? window.matchMedia("(prefers-color-scheme: dark)").matches + : false; +} + export function nextTheme(theme: ThemeMode): ThemeMode { - return theme === "system" ? "light" : theme === "light" ? "dark" : "system"; + return isDarkTheme(theme) ? "light" : "dark"; } applyTheme(getStoredTheme()); diff --git a/web/src/views/AdminView.vue b/web/src/views/AdminView.vue index d4c9e82..d19f958 100644 --- a/web/src/views/AdminView.vue +++ b/web/src/views/AdminView.vue @@ -28,7 +28,7 @@
-
{{ tr('adminConsole') }}

{{ currentPageTitle }}

{{ tr('gatewayRunning') }}
+
{{ tr('adminConsole') }}

{{ currentPageTitle }}

{{ tr('gatewayRunning') }}
{{ errorMessage }}
@@ -68,7 +68,7 @@ import { computed, onMounted, reactive, ref, watch } from "vue"; import { RouterLink, useRoute, useRouter } from "vue-router"; import Icon from "../components/Icon.vue"; import { combineTeamSpeakTarget, splitTeamSpeakTarget } from "../services/teamspeak-target.js"; -import { applyTheme, getStoredTheme, nextTheme, saveTheme, type ThemeMode } from "../services/theme.js"; +import { applyTheme, getStoredTheme, isDarkTheme, nextTheme, saveTheme, type ThemeMode } from "../services/theme.js"; type Language = "zh" | "en"; type Screen = "login" | "change-password" | "admin"; @@ -79,7 +79,8 @@ const route = useRoute(); const router = useRouter(); const language = ref((localStorage.getItem("webspeak:language") as Language) || "zh"); const themeMode = ref(getStoredTheme()); -const themeLabel = computed(() => themeMode.value === "dark" ? tr("themeDark") : themeMode.value === "light" ? tr("themeLight") : tr("themeSystem")); +const themeIcon = computed(() => isDarkTheme(themeMode.value) ? "sun" : "moon"); +const themeLabel = computed(() => isDarkTheme(themeMode.value) ? tr("switchToLightTheme") : tr("switchToDarkTheme")); applyTheme(themeMode.value); const loading = ref(true); const screen = ref("login"); @@ -122,6 +123,8 @@ const copy = { themeSystem: "跟随系统", themeLight: "浅色主题", themeDark: "深色主题", + switchToLightTheme: "切换到浅色主题", + switchToDarkTheme: "切换到深色主题", welcomeAdmin: "欢迎回来", loginLead: "输入管理员账号和密码以管理此 WebSpeak 实例。", adminPassword: "管理员密码", @@ -277,6 +280,8 @@ const copy = { themeSystem: "System theme", themeLight: "Light theme", themeDark: "Dark theme", + switchToLightTheme: "Switch to light theme", + switchToDarkTheme: "Switch to dark theme", welcomeAdmin: "Welcome back", loginLead: "Enter the admin account and password to manage this WebSpeak instance.", adminPassword: "Admin password", @@ -467,6 +472,7 @@ async function parseResponse(response: Response) { const value = await response.