diff --git a/frontend/src/ts/config.ts b/frontend/src/ts/config.ts index bf9183d918fc..be2222ca1502 100644 --- a/frontend/src/ts/config.ts +++ b/frontend/src/ts/config.ts @@ -1324,6 +1324,16 @@ export function setAutoSwitchTheme( return true; } +export function setAutoSwitchThemeOff( +): void { + if (!config.autoSwitchTheme) return; + config.autoSwitchTheme = false; + saveToLocalStorage("autoSwitchTheme", undefined); + ConfigEvent.dispatch("autoSwitchTheme", config.autoSwitchTheme); + Notifications.add("Auto switch theme disabled", 0); + return; +} + export function setCustomTheme(boolean: boolean, nosave?: boolean): boolean { if (!isConfigValueValid("custom theme", boolean, ["boolean"])) return false; diff --git a/frontend/src/ts/controllers/theme-controller.ts b/frontend/src/ts/controllers/theme-controller.ts index a188c45dc9a4..943c4c82e1ab 100644 --- a/frontend/src/ts/controllers/theme-controller.ts +++ b/frontend/src/ts/controllers/theme-controller.ts @@ -9,6 +9,7 @@ import * as Notifications from "../elements/notifications"; import * as Loader from "../elements/loader"; import * as AnalyticsController from "../controllers/analytics-controller"; import { debounce } from "throttle-debounce"; +import { setAutoSwitchThemeOff } from '../config'; export let randomTheme: string | null = null; let isPreviewingTheme = false; @@ -120,10 +121,10 @@ export async function loadStyle(name: string): Promise { function apply( themeName: string, customColorsOverride?: string[], - isPreview = false + isPreview = false, + isAutoSwitch = false ): void { clearCustomTheme(); - const name = customColorsOverride ? "custom" : themeName; ThemeColors.reset(); @@ -152,6 +153,10 @@ function apply( // } updateFooterThemeName(isPreview ? themeName : undefined); }); + + if (!isAutoSwitch) { + setAutoSwitchThemeOff(); + } } function updateFooterThemeName(nameOverride?: string): void { @@ -177,8 +182,8 @@ const debouncedPreview = debounce( } ); -function set(themeIdentifier: string): void { - apply(themeIdentifier); +function set(themeIdentifier: string, isAutoSwitch: boolean = false): void { + apply(themeIdentifier, undefined, true, isAutoSwitch); } export function clearPreview(applyTheme = true): void { @@ -315,9 +320,9 @@ window ?.addEventListener?.("change", (event) => { if (!Config.autoSwitchTheme || Config.customTheme) return; if (event.matches) { - set(Config.themeDark); + set(Config.themeDark, true); } else { - set(Config.themeLight); + set(Config.themeLight, true); } }); @@ -337,18 +342,18 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => { } if (eventKey === "setThemes") { clearPreview(false); - if (eventValue) { - set("custom"); + if (Config.autoSwitchTheme) { + if ( + window.matchMedia && + window.matchMedia("(prefers-color-scheme: dark)").matches + ) { + set(Config.themeDark, true); + } else { + set(Config.themeLight, true); + } } else { - if (Config.autoSwitchTheme) { - if ( - window.matchMedia && - window.matchMedia("(prefers-color-scheme: dark)").matches - ) { - set(Config.themeDark); - } else { - set(Config.themeLight); - } + if (eventValue) { + set("custom"); } else { set(Config.theme); } @@ -363,9 +368,9 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => { window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ) { - set(Config.themeDark); + set(Config.themeDark, true); } else { - set(Config.themeLight); + set(Config.themeLight, true); } } else { set(Config.theme); @@ -380,7 +385,7 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => { ) && !nosave ) { - set(Config.themeLight); + set(Config.themeLight, true); } if ( eventKey === "themeDark" && @@ -389,6 +394,6 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => { window.matchMedia("(prefers-color-scheme: dark)").matches && !nosave ) { - set(Config.themeDark); + set(Config.themeDark, true); } }); diff --git a/frontend/static/html/pages/settings.html b/frontend/static/html/pages/settings.html index 71feed3b2a18..d54f5b1f9567 100644 --- a/frontend/static/html/pages/settings.html +++ b/frontend/static/html/pages/settings.html @@ -2094,7 +2094,7 @@
Enabling this will automatically switch the theme between light and dark - depending on the system theme (this will not override custom theme). + depending on the system theme.