Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added option to choose color mode #1320

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/renderer/Core/I18n/getCoreResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export const getCoreResources = (): { namespace: string; resources: Resources<Tr
searchBarPlaceholderTextReset: "Reset",
searchBarShowIcon: "Show search icon",
themeName: "Theme",
colorMode: "Color Mode",
"colorMode.light": "Light",
"colorMode.dark": "Dark",
"colorMode.system": "System",
},
"de-CH": {
title: "Erscheinungsbild",
Expand All @@ -141,6 +145,10 @@ export const getCoreResources = (): { namespace: string; resources: Resources<Tr
searchBarPlaceholderTextReset: "Zurücksetzen",
searchBarShowIcon: "Suchsymbol anzeigen",
themeName: "Farbschema",
colorMode: "Farbmodus",
"colorMode.light": "Hell",
"colorMode.dark": "Dunkel",
"colorMode.system": "System",
},
},
},
Expand Down
38 changes: 32 additions & 6 deletions src/renderer/Core/Settings/Pages/Appearance/Appearance.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSetting } from "@Core/Hooks";
import { getAvailableThemes } from "@Core/Theme";
import { getAvailableColorModes, getAvailableThemes } from "@Core/Theme";
import { Dropdown, Option } from "@fluentui/react-components";
import { useTranslation } from "react-i18next";
import { Setting } from "../../Setting";
Expand All @@ -12,20 +12,26 @@ export const Appearance = () => {
const { t } = useTranslation("settingsAppearance");

const availableThemes = getAvailableThemes();
const availableColorModes = getAvailableColorModes();

const { value: themeName, updateValue: setThemeName } = useSetting<string>({
key: "appearance.themeName",
defaultValue: "Fluent UI Web",
});
const themes = [
{ value: "Microsoft Teams", label: "Microsoft Teams" },
{ value: "Fluent UI Web", label: "Fluent UI Web" },
];

const { value: colorMode, updateValue: setColorMode } = useSetting<string>({
key: "appearance.colorMode",
defaultValue: "system",
});

const updateTheme = (themeName: string) => {
setThemeName(themeName);
};

const updateColorMode = (mode: string) => {
setColorMode(mode);
};

return (
<SettingGroupList>
<SearchBarSettings />
Expand All @@ -34,7 +40,7 @@ export const Appearance = () => {
label={t("themeName")}
control={
<Dropdown
value={themes.find((t) => t.value === themeName)?.label}
value={themeName}
onOptionSelect={(_, { optionValue }) => optionValue && updateTheme(optionValue)}
selectedOptions={[themeName]}
>
Expand All @@ -46,6 +52,26 @@ export const Appearance = () => {
</Dropdown>
}
/>
<Setting
label={t("colorMode")}
control={
<Dropdown
value={t("colorMode." + colorMode)}
onOptionSelect={(_, { optionValue }) => optionValue && updateColorMode(optionValue)}
selectedOptions={[colorMode]}
>
{availableColorModes.map((colorMode: string | undefined) => (
<Option
key={`color-mode-option-${colorMode}`}
value={colorMode}
text={t("colorMode." + colorMode)}
>
{t("colorMode." + colorMode)}
</Option>
))}
</Dropdown>
}
/>
</SettingGroup>
</SettingGroupList>
);
Expand Down
26 changes: 24 additions & 2 deletions src/renderer/Core/Theme/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { useSetting } from "@Core/Hooks";
import { type Theme } from "@fluentui/react-theme";
import { useEffect, useState, type ReactNode } from "react";
import { ThemeContext } from "./ThemeContext";
import { getFluentUiTheme } from "./getFluentUiTheme";

export const ThemeProvider = ({ children }: { children: ReactNode }) => {
const initialShouldUseDarkColors = window.matchMedia("(prefers-color-scheme: dark)").matches;
const { value: colorMode } = useSetting<string>({
key: "appearance.colorMode",
defaultValue: "system",
});

const initialShouldUseDarkColors =
colorMode === "light"
? false
: colorMode === "dark"
? true
: window.matchMedia("(prefers-color-scheme: dark)").matches;

const [fluentUiTheme, setFluentUiTheme] = useState<Theme>(getFluentUiTheme(initialShouldUseDarkColors));
const [shouldUseDarkColors, setShouldUseDarkColors] = useState<boolean>(initialShouldUseDarkColors);
Expand All @@ -16,15 +27,26 @@ export const ThemeProvider = ({ children }: { children: ReactNode }) => {
};

const themeNameChangeListener = () => {
setFluentUiTheme(getFluentUiTheme(window.matchMedia("(prefers-color-scheme: dark)").matches));
const colorMode = window.ContextBridge.getSettingValue<string>("appearance.colorMode", "system");
const shouldUseDark =
colorMode === "dark"
? true
: colorMode === "light"
? false
: window.matchMedia("(prefers-color-scheme: dark)").matches;
console.log(colorMode);
console.log(shouldUseDark);
setFluentUiTheme(getFluentUiTheme(shouldUseDark));
};

window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", colorSchemeChangeListener);
window.ContextBridge.ipcRenderer.on("settingUpdated[appearance.themeName]", themeNameChangeListener);
window.ContextBridge.ipcRenderer.on("settingUpdated[appearance.colorMode]", themeNameChangeListener);

return () => {
window.matchMedia("(prefers-color-scheme: dark)").removeEventListener("change", colorSchemeChangeListener);
window.ContextBridge.ipcRenderer.off("settingUpdated[appearance.themeName]", themeNameChangeListener);
window.ContextBridge.ipcRenderer.off("settingUpdated[appearance.colorMode]", themeNameChangeListener);
};
}, []);

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/Core/Theme/getAvailableModes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const getAvailableColorModes = () => {
const availableColorModes: string[] = ["light", "dark", "system"];

return availableColorModes;
};
1 change: 1 addition & 0 deletions src/renderer/Core/Theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./getAvailableModes";
export * from "./getAvailableThemes";
export * from "./ThemeContext";