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

Viewer theme can be switched between dark or light theme #433

Merged
merged 8 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changes in version 1.3.1 (in development)

### New Features

* Users can now change the application theme from light, dark or system mode from settings.

### Fixes

* The `<Time>` parameter is now no longer required to calculate the statistics
Expand Down
4 changes: 3 additions & 1 deletion src/components/HelpButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import MarkdownPopover from "@/components/MarkdownPopover";
interface HelpButtonProps {
size?: "small" | "medium" | "large";
helpUrl?: string;
applicationTheme?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use hook.

}

export default function HelpButton({ size, helpUrl }: HelpButtonProps) {
export default function HelpButton({ size, helpUrl, applicationTheme }: HelpButtonProps) {
const [helpAnchorEl, setHelpAnchorEl] = useState<HTMLButtonElement | null>(
null,
);
Expand All @@ -59,6 +60,7 @@ export default function HelpButton({ size, helpUrl }: HelpButtonProps) {
open={!!helpAnchorEl}
onClose={handleHelpClose}
markdownText={helpText}
applicationTheme={applicationTheme}
/>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/MarkdownPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { styled } from "@mui/system";

const styles = makeStyles({
dialog: (theme) => ({
backgroundColor: theme.palette.grey[200],
backgroundColor: theme.palette.mode === "dark" ? theme.palette.grey[800] : theme.palette.grey[200],
}),
appBar: {
position: "relative",
Expand Down
13 changes: 13 additions & 0 deletions src/components/MarkdownPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,32 @@ interface MarkdownPopoverProps {
open: boolean;
onClose?: () => void;
markdownText?: string;
applicationTheme?: string;
}

export default function MarkdownPopover({
anchorEl,
markdownText,
open,
onClose,
applicationTheme,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use hook.

}: MarkdownPopoverProps) {
if (!markdownText) {
return null;
}

const components = {
a: (props: Record<string, unknown>) => {
const { node: _, ...rest } = props;
return (
<a
{...rest}
style={{
color: applicationTheme ? "#90caf9" : "#1e90ff",
}}
/>
);
},
code: (props: Record<string, unknown>) => {
const { node: _, ...rest } = props;
return <code {...rest} style={{ color: "green" }} />;
Expand Down
28 changes: 27 additions & 1 deletion src/components/SettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import {
LocateMode,
TimeSeriesChartType,
TimeAnimationInterval,
APPLICATION_THEMES,
ApplicationThemes,
Comment on lines +45 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two of them?

} from "@/states/controlState";
import { GEOGRAPHIC_CRS, WEB_MERCATOR_CRS } from "@/model/proj";
import {
Expand Down Expand Up @@ -238,6 +240,13 @@ const SettingsDialog: React.FC<SettingsDialogProps> = ({
openDialog("userOverlays");
};

function handleApplicationThemeChange(
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) {
updateSettings({
applicationTheme: event.target.value as ApplicationThemes,
});
}
const overlayLayer = findLayer(overlayLayers, settings.selectedOverlayId);
const overlayLabel = getLayerTitle(overlayLayer);

Expand Down Expand Up @@ -279,8 +288,25 @@ const SettingsDialog: React.FC<SettingsDialogProps> = ({
))}
</TextField>
</SettingsSubPanel>
<SettingsSubPanel
label={i18n.get("Change application theme")}
>
<TextField
variant="standard"
select
sx={styles.textField}
value={settings.applicationTheme}
onChange={handleApplicationThemeChange}
margin="normal"
>
{APPLICATION_THEMES.map(([value, label]) => (
<MenuItem key={value} value={value}>
{i18n.get(label)}
</MenuItem>
))}
</TextField>
</SettingsSubPanel>
</SettingsPanel>

<SettingsPanel title={i18n.get("Time-Series")}>
<SettingsSubPanel
label={i18n.get("Show chart after adding a place")}
Expand Down
3 changes: 3 additions & 0 deletions src/components/UserVariablesDialog/UserVariablesDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ interface UserVariablesDialogProps {
) => void;
expressionCapabilities: ExpressionCapabilities | null;
serverUrl: string;
applicationTheme: string;
}

export default function UserVariablesDialog({
Expand All @@ -73,6 +74,7 @@ export default function UserVariablesDialog({
updateDatasetUserVariables,
expressionCapabilities,
serverUrl,
applicationTheme,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use context hook?

}: UserVariablesDialogProps) {
const [localUserVariables, setLocalUserVariables] =
useState<UserVariable[]>(userVariables);
Expand Down Expand Up @@ -139,6 +141,7 @@ export default function UserVariablesDialog({
<HelpButton
size="medium"
helpUrl={i18n.get("docs/user-variables.en.md")}
applicationTheme={applicationTheme}
/>
</Box>
<Box>
Expand Down
44 changes: 30 additions & 14 deletions src/connected/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
CssBaseline,
StyledEngineProvider,
ThemeProvider,
useMediaQuery,
} from "@mui/material";

import { Config } from "@/config";
Expand All @@ -48,35 +49,50 @@ import UserVariablesDialog from "./UserVariablesDialog";

interface AppProps {
compact: boolean;
applicationTheme: string;
}

// noinspection JSUnusedLocalSymbols
const mapStateToProps = (_state: AppState) => {
return {
compact: Config.instance.branding.compact,
applicationTheme: _state.controlState.applicationTheme,
};
};

const mapDispatchToProps = {};

const newTheme = () =>
createTheme({
typography: {
fontSize: 12,
htmlFontSize: 14,
},
palette: {
mode: Config.instance.branding.themeName,
primary: Config.instance.branding.primaryColor,
secondary: Config.instance.branding.secondaryColor,
},
});
const _App: React.FC<AppProps> = ({ compact, applicationTheme }) => {

const systemMode = useMediaQuery("(prefers-color-scheme: dark)") ? "dark" : "light";

const createAppTheme = React.useCallback(() => {
const mode =
applicationTheme === "system"
? systemMode
: applicationTheme === "dark"
? "dark"
: "light";

return createTheme({
typography: {
fontSize: 12,
htmlFontSize: 14,
},
palette: {
mode: mode,
primary: Config.instance.branding.primaryColor,
secondary: Config.instance.branding.secondaryColor,
},
});
}, [applicationTheme, systemMode]);

const theme = createAppTheme();

const _App: React.FC<AppProps> = ({ compact }) => {
return (
<AuthWrapper>
<StyledEngineProvider injectFirst>
<ThemeProvider theme={newTheme()}>
<ThemeProvider theme={theme}>
<CssBaseline />
{!compact && <AppBar />}
<AppPane />
Expand Down
1 change: 1 addition & 0 deletions src/connected/UserVariablesDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const mapStateToProps = (state: AppState) => {
userVariables: selectedUserVariablesSelector(state),
expressionCapabilities: expressionCapabilitiesSelector(state),
serverUrl: selectedServerSelector(state).url,
applicationTheme: state.controlState.applicationTheme,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current theme should be available through the MUI theme context object. Please double-check the MUI docs on how to that (usually using the React ? useContext()` hook.

};
};

Expand Down
10 changes: 10 additions & 0 deletions src/states/controlState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,16 @@ export const sidebarPanelIds: SidebarPanelId[] = [
"volume",
];

export const APPLICATION_THEMES: [ApplicationThemes, string][] = [
["light", "Light"],
["dark", "Dark"],
["system", "System"],
];
export type VolumeRenderMode = "mip" | "aip" | "iso";
export type VolumeStatus = "loading" | "ok" | "error";
export type VolumeState = { status: VolumeStatus; message?: string };
export type VolumeStates = { [volumeId: string]: VolumeState };
export type ApplicationThemes = "light" | "dark" | "system";

export interface ControlState {
selectedDatasetId: string | null;
Expand Down Expand Up @@ -160,6 +166,7 @@ export interface ControlState {
exportPlacesAsCollection: boolean;
exportZipArchive: boolean;
exportFileName: string;
applicationTheme: string;
}

export function newControlState(): ControlState {
Expand Down Expand Up @@ -235,6 +242,9 @@ export function newControlState(): ControlState {
exportPlacesAsCollection: true,
exportZipArchive: true,
exportFileName: "export",
applicationTheme: Config.instance.branding.themeName
? Config.instance.branding.themeName
: "system",
};
return loadUserSettings(state);
}
Expand Down
2 changes: 2 additions & 0 deletions src/states/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function storeUserSettings(settings: ControlState) {
storage.setPrimitiveProperty("exportFileName", settings);
storage.setPrimitiveProperty("userPlacesFormatName", settings);
storage.setObjectProperty("userPlacesFormatOptions", settings);
storage.setPrimitiveProperty("applicationTheme", settings);
if (import.meta.env.DEV) {
console.debug("Stored user settings:", settings);
}
Expand Down Expand Up @@ -213,6 +214,7 @@ export function loadUserSettings(defaultSettings: ControlState): ControlState {
settings,
defaultSettings,
);
storage.getStringProperty("applicationTheme", settings, defaultSettings);
if (import.meta.env.DEV) {
console.debug("Loaded user settings:", settings);
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/branding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface Branding {
windowTitle: string;
windowIcon: string | null;
compact: boolean;
themeName: "dark" | "light";
themeName: "light";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A type with one choice? Why does this make sense?
The branding should provide the initial theme.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix that.

primaryColor: PaletteColorOptions;
secondaryColor: PaletteColorOptions;
headerBackgroundColor?: string;
Expand Down
Loading