Skip to content

Commit

Permalink
fix: auto switch theme behavior & footer theme indicator (sanidhyas3s…
Browse files Browse the repository at this point in the history
…, miodec) (#4677)

* fix: auto switch theme behavior & footer indicator

Changing manually to a preset or custom theme now
turns auto switch theme mode off with a notification.
And now the auto switch mode does override the custom
theme as well (statement in settings also updated) if it
is the later one set.
Fixes #4659, that is the footer theme is now correctly
displayed with auto switch themes as well.

* removed unnecessary function

---------

Co-authored-by: Miodec <jack@monkeytype.com>
  • Loading branch information
sanidhyas3s and Miodec committed Oct 2, 2023
1 parent 59be910 commit 5844f1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
45 changes: 24 additions & 21 deletions frontend/src/ts/controllers/theme-controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ThemeColors from "../elements/theme-colors";
import * as ChartController from "./chart-controller";
import * as Misc from "../utils/misc";
import Config from "../config";
import Config, { setAutoSwitchTheme } from "../config";
import * as BackgroundFilter from "../elements/custom-background-filter";
import * as ConfigEvent from "../observables/config-event";
import * as DB from "../db";
Expand Down Expand Up @@ -123,7 +123,6 @@ function apply(
isPreview = false
): void {
clearCustomTheme();

const name = customColorsOverride ? "custom" : themeName;

ThemeColors.reset();
Expand Down Expand Up @@ -177,8 +176,12 @@ const debouncedPreview = debounce(
}
);

function set(themeIdentifier: string): void {
apply(themeIdentifier);
function set(themeIdentifier: string, isAutoSwitch = false): void {
apply(themeIdentifier, undefined, true);

if (!isAutoSwitch && Config.autoSwitchTheme) {
setAutoSwitchTheme(false);
}
}

export function clearPreview(applyTheme = true): void {
Expand Down Expand Up @@ -315,9 +318,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);
}
});

Expand All @@ -337,18 +340,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);
}
Expand All @@ -363,9 +366,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);
Expand All @@ -380,7 +383,7 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => {
) &&
!nosave
) {
set(Config.themeLight);
set(Config.themeLight, true);
}
if (
eventKey === "themeDark" &&
Expand All @@ -389,6 +392,6 @@ ConfigEvent.subscribe((eventKey, eventValue, nosave) => {
window.matchMedia("(prefers-color-scheme: dark)").matches &&
!nosave
) {
set(Config.themeDark);
set(Config.themeDark, true);
}
});
2 changes: 1 addition & 1 deletion frontend/static/html/pages/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@
</div>
<div class="text">
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.
</div>
<div class="buttons">
<div
Expand Down

0 comments on commit 5844f1d

Please sign in to comment.