Move theme files into new folder
This commit is contained in:
14
src/modules/theme/AppTheme.types.ts
Normal file
14
src/modules/theme/AppTheme.types.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { AppTheme } from '@/modules/theme/services/AppThemes.ts';
|
||||
|
||||
export type MetadataThemeSettings = {
|
||||
customThemes: Record<string, AppTheme>;
|
||||
mangaThumbnailBackdrop: boolean;
|
||||
};
|
||||
183
src/modules/theme/Themes.ts
Normal file
183
src/modules/theme/Themes.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { ThemeOptions } from '@mui/material/styles';
|
||||
import { t as translate } from 'i18next';
|
||||
|
||||
export type TBaseTheme = { isCustom: boolean; getName: () => string; muiTheme: ThemeOptions };
|
||||
|
||||
export const themes = {
|
||||
default: {
|
||||
isCustom: false,
|
||||
getName: () => translate('global.label.default'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#5b74ef',
|
||||
},
|
||||
secondary: {
|
||||
main: '#efd65b',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
lavender: {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.lavender'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#5c58dc',
|
||||
},
|
||||
secondary: {
|
||||
main: '#d8dc58',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
dune: {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.dune'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#897869',
|
||||
},
|
||||
secondary: {
|
||||
main: '#697a89',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
rosegold: {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.rosegold'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#E9A7A1',
|
||||
},
|
||||
secondary: {
|
||||
main: '#A1E3E9',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'forest dew': {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.forest_dew'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#53a584',
|
||||
},
|
||||
secondary: {
|
||||
main: '#a55374',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'montain sunset': {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.mountain_sunset'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#c55a77',
|
||||
},
|
||||
secondary: {
|
||||
main: '#5ac5a8',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
crimson: {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.crimson'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#DC143C',
|
||||
},
|
||||
secondary: {
|
||||
main: '#14DCB4',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'minty miracles': {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.minty_miracles'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#5CE6A1',
|
||||
},
|
||||
secondary: {
|
||||
main: '#E65CA1',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'orange juice': {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.orange_juice'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#ffb546',
|
||||
},
|
||||
secondary: {
|
||||
main: '#4690ff',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'bright pink': {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.bright_pink'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#FF007F',
|
||||
},
|
||||
secondary: {
|
||||
main: '#00FF80',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
veronica: {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.veronica'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#A020F0',
|
||||
},
|
||||
secondary: {
|
||||
main: '#70F020',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
'tree frog green': {
|
||||
isCustom: false,
|
||||
getName: () => translate('settings.appearance.theme.themes.tree_frog_green'),
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#8ace31',
|
||||
},
|
||||
secondary: {
|
||||
main: '#7531CE',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as const satisfies Record<string, TBaseTheme>;
|
||||
46
src/modules/theme/components/CreateThemeButton.tsx
Normal file
46
src/modules/theme/components/CreateThemeButton.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import Button from '@mui/material/Button';
|
||||
import AddCircleIcon from '@mui/icons-material/AddCircle';
|
||||
import { bindDialog, bindTrigger, usePopupState } from 'material-ui-popup-state/hooks';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import { ThemeCreationDialog } from '@/modules/theme/components/CreateThemeDialog.tsx';
|
||||
import { TypographyMaxLines } from '@/modules/core/components/TypographyMaxLines.tsx';
|
||||
|
||||
export const CreateThemeButton = () => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const popupState = usePopupState({ variant: 'popover', popupId: 'theme-creation-dialog' });
|
||||
|
||||
return (
|
||||
<>
|
||||
<Stack sx={{ alignItems: 'center', gap: 1, minWidth: '150px', maxWidth: '150px' }}>
|
||||
<Button
|
||||
sx={{
|
||||
width: '100%',
|
||||
height: '225px',
|
||||
}}
|
||||
variant="contained"
|
||||
size="large"
|
||||
{...bindTrigger(popupState)}
|
||||
>
|
||||
<AddCircleIcon fontSize="large" />
|
||||
</Button>
|
||||
<Tooltip title={t('settings.appearance.theme.create.title')} placement="top">
|
||||
<TypographyMaxLines sx={{ maxWidth: '100%' }}>
|
||||
{t('settings.appearance.theme.create.title')}
|
||||
</TypographyMaxLines>
|
||||
</Tooltip>
|
||||
</Stack>
|
||||
{popupState.isOpen && <ThemeCreationDialog bindDialogProps={bindDialog(popupState)} mode="create" />}
|
||||
</>
|
||||
);
|
||||
};
|
||||
221
src/modules/theme/components/CreateThemeDialog.tsx
Normal file
221
src/modules/theme/components/CreateThemeDialog.tsx
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { Trans, useTranslation } from 'react-i18next';
|
||||
import { useState } from 'react';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import Button from '@mui/material/Button';
|
||||
import { bindDialog } from 'material-ui-popup-state/hooks';
|
||||
import Dialog from '@mui/material/Dialog';
|
||||
import DialogTitle from '@mui/material/DialogTitle';
|
||||
import DialogContent from '@mui/material/DialogContent';
|
||||
import DialogActions from '@mui/material/DialogActions';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import Link from '@mui/material/Link';
|
||||
import { jsonrepair } from 'jsonrepair';
|
||||
import { jsonSaveParse } from '@/lib/HelperFunctions.ts';
|
||||
import { AppTheme, isThemeNameUnique } from '@/modules/theme/services/AppThemes.ts';
|
||||
import {
|
||||
createUpdateMetadataServerSettings,
|
||||
useMetadataServerSettings,
|
||||
} from '@/lib/metadata/metadataServerSettings.ts';
|
||||
import { makeToast } from '@/lib/ui/Toast.ts';
|
||||
import { EmptyView } from '@/modules/core/components/placeholder/EmptyView.tsx';
|
||||
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||
import { TranslationKey } from '@/Base.types.ts';
|
||||
import { MetadataThemeSettings } from '@/modules/theme/AppTheme.types.ts';
|
||||
|
||||
const baseCustomTheme: AppTheme = {
|
||||
id: '',
|
||||
isCustom: true,
|
||||
getName: () => '',
|
||||
muiTheme: {
|
||||
palette: {
|
||||
primary: {
|
||||
main: '#1976d2',
|
||||
},
|
||||
secondary: {
|
||||
main: '#9c27b0',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
type DialogMode = 'create' | 'edit';
|
||||
|
||||
const dialogModeToTranslationKey: Record<
|
||||
DialogMode,
|
||||
{
|
||||
title: TranslationKey;
|
||||
button: TranslationKey;
|
||||
action: TranslationKey;
|
||||
success: TranslationKey;
|
||||
failure: TranslationKey;
|
||||
}
|
||||
> = {
|
||||
create: {
|
||||
title: 'settings.appearance.theme.create.title',
|
||||
button: 'global.button.ok',
|
||||
action: 'settings.appearance.theme.create.action',
|
||||
success: 'settings.appearance.theme.create.success',
|
||||
failure: 'settings.appearance.theme.create.failure',
|
||||
},
|
||||
edit: {
|
||||
title: 'settings.appearance.theme.edit.title',
|
||||
button: 'global.button.save',
|
||||
action: 'settings.appearance.theme.edit.action',
|
||||
success: 'settings.appearance.theme.edit.success',
|
||||
failure: 'settings.appearance.theme.edit.failure',
|
||||
},
|
||||
};
|
||||
|
||||
export const ThemeCreationDialog = ({
|
||||
bindDialogProps,
|
||||
mode,
|
||||
appTheme = baseCustomTheme,
|
||||
}: {
|
||||
bindDialogProps: ReturnType<typeof bindDialog>;
|
||||
mode: DialogMode;
|
||||
appTheme?: AppTheme;
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const {
|
||||
settings: { customThemes },
|
||||
request: { loading, error, refetch },
|
||||
} = useMetadataServerSettings();
|
||||
|
||||
const updateCustomThemes = createUpdateMetadataServerSettings<keyof MetadataThemeSettings>((e) => {
|
||||
throw e;
|
||||
});
|
||||
|
||||
const [theme, setTheme] = useState<AppTheme>(appTheme);
|
||||
const [invalidName, setInvalidName] = useState(false);
|
||||
const [invalidTheme, setInvalidTheme] = useState(false);
|
||||
const [isCreating, setIsCreating] = useState(false);
|
||||
const [didThemeChange, setDidThemeChange] = useState(mode === 'create');
|
||||
|
||||
return (
|
||||
<Dialog {...bindDialogProps} fullWidth maxWidth="md">
|
||||
<DialogTitle>{t(dialogModeToTranslationKey[mode].title)}</DialogTitle>
|
||||
<DialogContent>
|
||||
{loading && <LoadingPlaceholder />}
|
||||
{error && (
|
||||
<EmptyView
|
||||
message={t('global.error.label.failed_to_load_data')}
|
||||
messageExtra={error.message}
|
||||
retry={() => refetch().catch(defaultPromiseErrorHandler('ThemeCreationDialog::refetch'))}
|
||||
/>
|
||||
)}
|
||||
{!error && (
|
||||
<Stack sx={{ gap: 2 }}>
|
||||
<Typography>
|
||||
<Trans i18nKey="settings.appearance.theme.create.dialog.info.creating_theme">
|
||||
Create a custom theme with the{' '}
|
||||
<Link
|
||||
href="https://zenoo.github.io/mui-theme-creator/"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
MUI Theme Creator
|
||||
</Link>{' '}
|
||||
, copy everything after "themeOptions" from "{'{'}" to "{'}'}
|
||||
" and paste it into the "theme" text field.
|
||||
</Trans>
|
||||
</Typography>
|
||||
<Typography>{t('settings.appearance.theme.create.dialog.info.theme_mode_lock')}</Typography>
|
||||
<TextField
|
||||
disabled={mode === 'edit'}
|
||||
label={t('settings.appearance.theme.create.dialog.theme_name')}
|
||||
value={theme.getName()}
|
||||
error={invalidName}
|
||||
helperText={invalidName && t('settings.appearance.theme.create.dialog.error.invalid_name')}
|
||||
onChange={(e) => {
|
||||
const name = e.target.value.trim();
|
||||
|
||||
const isValidLength = name.length <= 16;
|
||||
const isUnique = isThemeNameUnique(name, customThemes);
|
||||
setInvalidName(!isValidLength || !isUnique);
|
||||
|
||||
setTheme({
|
||||
...theme,
|
||||
id: e.target.value,
|
||||
getName: () => e.target.value,
|
||||
});
|
||||
}}
|
||||
/>
|
||||
<TextField
|
||||
label={t('settings.appearance.theme.title')}
|
||||
multiline
|
||||
error={invalidTheme}
|
||||
helperText={invalidTheme && t('settings.appearance.theme.create.dialog.error.invalid_json')}
|
||||
defaultValue={JSON.stringify(theme.muiTheme, null, 2)}
|
||||
onChange={(e) => {
|
||||
const invalidThemeStr = 'invalid';
|
||||
const newMuiTheme = (() => {
|
||||
try {
|
||||
return jsonrepair(e.target.value);
|
||||
} catch (_) {
|
||||
return invalidThemeStr;
|
||||
}
|
||||
})();
|
||||
|
||||
const newMuiThemeParsed = jsonSaveParse(newMuiTheme);
|
||||
const isInvalid = newMuiTheme === invalidThemeStr || !newMuiThemeParsed;
|
||||
const isThemeDifferentFromOriginal =
|
||||
JSON.stringify(newMuiThemeParsed) !== JSON.stringify(appTheme.muiTheme);
|
||||
|
||||
setDidThemeChange(mode === 'create' ? true : isThemeDifferentFromOriginal);
|
||||
setInvalidTheme(isInvalid);
|
||||
|
||||
const didPaletteChange =
|
||||
!isInvalid && JSON.stringify(newMuiThemeParsed) !== JSON.stringify(theme.muiTheme);
|
||||
if (didPaletteChange) {
|
||||
setTheme({ ...theme, muiTheme: newMuiThemeParsed });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</Stack>
|
||||
)}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button autoFocus onClick={bindDialogProps.onClose} color="primary">
|
||||
{t('global.button.cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
disabled={invalidTheme || invalidName || isCreating || !didThemeChange || !theme.id.length}
|
||||
onClick={(e) => {
|
||||
makeToast(t(dialogModeToTranslationKey[mode].action, { theme: theme.getName() }), 'info');
|
||||
|
||||
setIsCreating(true);
|
||||
updateCustomThemes('customThemes', { ...customThemes, [theme.id]: theme })
|
||||
.then(() => {
|
||||
makeToast(
|
||||
t(dialogModeToTranslationKey[mode].success, { theme: theme.getName() }),
|
||||
'success',
|
||||
);
|
||||
bindDialogProps.onClose(e);
|
||||
})
|
||||
.catch(() =>
|
||||
makeToast(
|
||||
t(dialogModeToTranslationKey[mode].failure, { theme: theme.getName() }),
|
||||
'error',
|
||||
),
|
||||
)
|
||||
.finally(() => setIsCreating(false));
|
||||
}}
|
||||
color="primary"
|
||||
>
|
||||
{t(dialogModeToTranslationKey[mode].button)}
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
94
src/modules/theme/components/ThemeList.tsx
Normal file
94
src/modules/theme/components/ThemeList.tsx
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import { useMemo } from 'react';
|
||||
import { appThemes } from '@/modules/theme/services/AppThemes.ts';
|
||||
import {
|
||||
createUpdateMetadataServerSettings,
|
||||
useMetadataServerSettings,
|
||||
} from '@/lib/metadata/metadataServerSettings.ts';
|
||||
import { makeToast } from '@/lib/ui/Toast.ts';
|
||||
import { EmptyView } from '@/modules/core/components/placeholder/EmptyView.tsx';
|
||||
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||
import { CreateThemeButton } from '@/modules/theme/components/CreateThemeButton.tsx';
|
||||
import { ThemePreview } from '@/modules/theme/components/ThemePreview.tsx';
|
||||
import { MetadataThemeSettings } from '@/modules/theme/AppTheme.types.ts';
|
||||
|
||||
export const ThemeList = () => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const {
|
||||
settings: { customThemes },
|
||||
request: { loading, error, refetch },
|
||||
} = useMetadataServerSettings();
|
||||
|
||||
const allThemes = useMemo(
|
||||
() => [
|
||||
...appThemes,
|
||||
...Object.values(customThemes).map((customTheme) => ({
|
||||
...customTheme,
|
||||
getName: () => customTheme.id,
|
||||
})),
|
||||
],
|
||||
[customThemes],
|
||||
);
|
||||
|
||||
const updateCustomThemes = createUpdateMetadataServerSettings<keyof MetadataThemeSettings>((e) => {
|
||||
throw e;
|
||||
});
|
||||
|
||||
if (loading) {
|
||||
return <LoadingPlaceholder />;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<EmptyView
|
||||
message={t('global.error.label.failed_to_load_data')}
|
||||
messageExtra={error.message}
|
||||
retry={() => refetch().catch(defaultPromiseErrorHandler('ThemeList::refetch'))}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Stack
|
||||
sx={{ flexDirection: 'row', flexWrap: 'no-wrap', alignItems: 'start', overflowX: 'auto', gap: 1, mx: 2 }}
|
||||
>
|
||||
<CreateThemeButton />
|
||||
{allThemes.map((theme) => (
|
||||
<ThemePreview
|
||||
key={theme.id}
|
||||
theme={theme}
|
||||
onDelete={() => {
|
||||
makeToast(t('settings.appearance.theme.delete.action', { theme: theme.getName() }), 'info');
|
||||
updateCustomThemes(
|
||||
'customThemes',
|
||||
Object.fromEntries(Object.entries(customThemes).filter(([id]) => id !== theme.id)),
|
||||
)
|
||||
.then(() =>
|
||||
makeToast(
|
||||
t('settings.appearance.theme.delete.success', { theme: theme.getName() }),
|
||||
'success',
|
||||
),
|
||||
)
|
||||
.catch(() =>
|
||||
makeToast(
|
||||
t('settings.appearance.theme.delete.failure', { theme: theme.getName() }),
|
||||
'error',
|
||||
),
|
||||
);
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
225
src/modules/theme/components/ThemePreview.tsx
Normal file
225
src/modules/theme/components/ThemePreview.tsx
Normal file
@@ -0,0 +1,225 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useContext, useMemo } from 'react';
|
||||
import Box from '@mui/material/Box';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import { styled, ThemeProvider } from '@mui/material/styles';
|
||||
import CheckCircleIcon from '@mui/icons-material/CheckCircle';
|
||||
import Card from '@mui/material/Card';
|
||||
import CardActionArea from '@mui/material/CardActionArea';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import IconButton from '@mui/material/IconButton';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import EditIcon from '@mui/icons-material/Edit';
|
||||
import { bindDialog, usePopupState } from 'material-ui-popup-state/hooks';
|
||||
import { ThemeModeContext } from '@/modules/theme/contexts/ThemeModeContext.tsx';
|
||||
import { AppTheme, hasMissingFonts, loadThemeFonts } from '@/modules/theme/services/AppThemes.ts';
|
||||
import { createTheme } from '@/theme.tsx';
|
||||
import { ThemeCreationDialog } from '@/modules/theme/components/CreateThemeDialog.tsx';
|
||||
import { makeToast } from '@/lib/ui/Toast.ts';
|
||||
import { TypographyMaxLines } from '@/modules/core/components/TypographyMaxLines.tsx';
|
||||
|
||||
const ThemePreviewBadge = styled(Box)(() => ({
|
||||
width: '15px',
|
||||
height: '20px',
|
||||
}));
|
||||
|
||||
export const ThemePreview = ({ theme, onDelete }: { theme: AppTheme; onDelete: () => void }) => {
|
||||
const { getName } = theme;
|
||||
|
||||
const { t } = useTranslation();
|
||||
const { themeMode, setAppTheme, appTheme, pureBlackMode } = useContext(ThemeModeContext);
|
||||
|
||||
const popupState = usePopupState({ variant: 'popover', popupId: `theme-edit-dialog-${theme.id}` });
|
||||
|
||||
const isSelected = theme.id === appTheme;
|
||||
|
||||
const muiTheme = useMemo(() => createTheme(themeMode, theme, pureBlackMode), [theme, themeMode, pureBlackMode]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Stack
|
||||
sx={{
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
minWidth: '150px',
|
||||
maxWidth: '150px',
|
||||
gap: 1,
|
||||
}}
|
||||
>
|
||||
<ThemeProvider theme={muiTheme}>
|
||||
<Card
|
||||
sx={{
|
||||
width: '100%',
|
||||
height: '225px',
|
||||
position: 'relative',
|
||||
}}
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
position: 'absolute',
|
||||
top: 0,
|
||||
right: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
border: '4px solid',
|
||||
borderColor: isSelected
|
||||
? muiTheme.palette.primary.light
|
||||
: muiTheme.palette.background.paper,
|
||||
zIndex: 1,
|
||||
pointerEvents: 'none',
|
||||
borderRadius: 1,
|
||||
}}
|
||||
/>
|
||||
<CardActionArea
|
||||
sx={{
|
||||
height: '100%',
|
||||
backgroundColor: 'background.default',
|
||||
}}
|
||||
onClick={() => {
|
||||
const needToLoadFonts = hasMissingFonts(theme.muiTheme);
|
||||
if (!needToLoadFonts) {
|
||||
setAppTheme(theme.id);
|
||||
return;
|
||||
}
|
||||
|
||||
makeToast(t('settings.appearance.theme.select.fonts.loading'), 'info');
|
||||
loadThemeFonts(theme.muiTheme)
|
||||
.then(() => setAppTheme(theme.id))
|
||||
.catch(() => makeToast(t('settings.appearance.theme.select.fonts.error'), 'error'));
|
||||
}}
|
||||
>
|
||||
<Stack sx={{ height: '100%', m: 0 }}>
|
||||
<Stack sx={{ height: '100%', gap: 2, p: 2 }}>
|
||||
<Stack
|
||||
sx={{
|
||||
maxHeight: '20px',
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'flex-start',
|
||||
}}
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
width: '65%',
|
||||
height: '20px',
|
||||
backgroundColor: 'primary.dark',
|
||||
borderRadius: 1,
|
||||
}}
|
||||
/>
|
||||
<Stack
|
||||
sx={{
|
||||
alignItems: 'center',
|
||||
gap: 1,
|
||||
mt: -0.25,
|
||||
}}
|
||||
>
|
||||
{isSelected && (
|
||||
<CheckCircleIcon
|
||||
sx={{
|
||||
visibility: isSelected ? 'visible' : 'hidden',
|
||||
color: 'primary.light',
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{!isSelected && theme.isCustom && (
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
onDelete();
|
||||
}}
|
||||
component="div"
|
||||
size="small"
|
||||
sx={{ mt: -0.5 }}
|
||||
>
|
||||
<Tooltip title={t('global.button.delete')}>
|
||||
<DeleteIcon />
|
||||
</Tooltip>
|
||||
</IconButton>
|
||||
)}
|
||||
{theme.isCustom && (
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
popupState.open();
|
||||
}}
|
||||
component="div"
|
||||
size="small"
|
||||
>
|
||||
<Tooltip title={t('global.button.edit')}>
|
||||
<EditIcon />
|
||||
</Tooltip>
|
||||
</IconButton>
|
||||
)}
|
||||
</Stack>
|
||||
</Stack>
|
||||
<Box
|
||||
sx={{
|
||||
width: '55%',
|
||||
height: '65%',
|
||||
backgroundColor: 'background.paper',
|
||||
p: 1,
|
||||
borderRadius: 1,
|
||||
}}
|
||||
>
|
||||
<Stack
|
||||
sx={{
|
||||
width: 'max-content',
|
||||
flexDirection: 'row',
|
||||
overflow: 'hidden',
|
||||
borderRadius: 1,
|
||||
}}
|
||||
>
|
||||
<ThemePreviewBadge sx={{ backgroundColor: 'primary.main' }} />
|
||||
<ThemePreviewBadge sx={{ backgroundColor: 'secondary.main' }} />
|
||||
</Stack>
|
||||
</Box>
|
||||
</Stack>
|
||||
<Stack
|
||||
sx={{
|
||||
height: '25%',
|
||||
backgroundColor: muiTheme.palette.background.paper,
|
||||
alignItems: 'center',
|
||||
flexDirection: 'row',
|
||||
gap: 2,
|
||||
p: 2,
|
||||
}}
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
width: '20px',
|
||||
height: '20px',
|
||||
borderRadius: '50%',
|
||||
backgroundColor: 'primary.main',
|
||||
}}
|
||||
/>
|
||||
<Box
|
||||
sx={{
|
||||
flexGrow: 1,
|
||||
height: '20px',
|
||||
borderRadius: 1,
|
||||
backgroundColor: 'primary.light',
|
||||
}}
|
||||
/>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</CardActionArea>
|
||||
</Card>
|
||||
</ThemeProvider>
|
||||
<Tooltip title={getName()} placement="top">
|
||||
<TypographyMaxLines sx={{ maxWidth: '100%' }}>{getName()}</TypographyMaxLines>
|
||||
</Tooltip>
|
||||
</Stack>
|
||||
<ThemeCreationDialog bindDialogProps={bindDialog(popupState)} mode="edit" appTheme={theme} />
|
||||
</>
|
||||
);
|
||||
};
|
||||
34
src/modules/theme/contexts/ThemeModeContext.tsx
Normal file
34
src/modules/theme/contexts/ThemeModeContext.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import { AppThemes } from '@/modules/theme/services/AppThemes.ts';
|
||||
|
||||
export enum ThemeMode {
|
||||
SYSTEM = 'system',
|
||||
DARK = 'dark',
|
||||
LIGHT = 'light',
|
||||
}
|
||||
|
||||
type ContextType = {
|
||||
appTheme: AppThemes;
|
||||
setAppTheme: React.Dispatch<React.SetStateAction<AppThemes>>;
|
||||
themeMode: ThemeMode;
|
||||
setThemeMode: React.Dispatch<React.SetStateAction<ThemeMode>>;
|
||||
pureBlackMode: boolean;
|
||||
setPureBlackMode: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
};
|
||||
|
||||
export const ThemeModeContext = React.createContext<ContextType>({
|
||||
appTheme: 'default',
|
||||
setAppTheme: (): void => {},
|
||||
themeMode: ThemeMode.SYSTEM,
|
||||
setThemeMode: (): void => {},
|
||||
pureBlackMode: false,
|
||||
setPureBlackMode: (): void => {},
|
||||
});
|
||||
102
src/modules/theme/services/AppThemes.ts
Normal file
102
src/modules/theme/services/AppThemes.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { ThemeOptions } from '@mui/material/styles';
|
||||
import WebFont from 'webfontloader';
|
||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||
import { ControlledPromise } from '@/lib/ControlledPromise.ts';
|
||||
import { TBaseTheme, themes } from '@/modules/theme/Themes.ts';
|
||||
|
||||
export type AppThemes = keyof typeof themes | string;
|
||||
|
||||
export type AppTheme = TBaseTheme & { id: AppThemes; isCustom: boolean };
|
||||
|
||||
export const appThemes = (Object.entries(themes) as [AppThemes, TBaseTheme][]).map(([id, theme]) => ({
|
||||
id,
|
||||
...theme,
|
||||
})) satisfies AppTheme[];
|
||||
|
||||
export const getTheme = (id: AppThemes, customThemes: Record<string, AppTheme> = {}): AppTheme => {
|
||||
try {
|
||||
const allThemes = { ...themes, ...customThemes };
|
||||
const theme = (allThemes[id as keyof typeof themes] as AppTheme) ?? themes.default;
|
||||
|
||||
return {
|
||||
// @ts-ignore - custom themes do not have a "getName" function
|
||||
getName: () => id,
|
||||
...theme,
|
||||
};
|
||||
} catch (e) {
|
||||
defaultPromiseErrorHandler('getTheme')(e);
|
||||
}
|
||||
return { id, ...themes[id as keyof typeof themes] };
|
||||
};
|
||||
|
||||
export const isThemeNameUnique = (id: string, customThemes: Record<string, AppTheme>): boolean =>
|
||||
Object.keys({ ...themes, ...customThemes }).every((themeId) => themeId.toLowerCase() !== id.toLowerCase());
|
||||
|
||||
const getFontsFromTheme = (obj: Record<string, any>, fonts: string[] = []): string[] => {
|
||||
const tmpFonts = [...fonts];
|
||||
|
||||
const propertyNames = Object.keys(obj);
|
||||
for (const propertyName of propertyNames) {
|
||||
const propertyValue = obj[propertyName];
|
||||
const propertyType = typeof obj[propertyName];
|
||||
|
||||
const isValidFontProperty = propertyName === 'fontFamily' && propertyType === 'string';
|
||||
if (isValidFontProperty) {
|
||||
tmpFonts.push(propertyValue as string);
|
||||
// eslint-disable-next-line no-continue
|
||||
continue;
|
||||
}
|
||||
|
||||
const isObject = propertyType === 'object';
|
||||
if (isObject) {
|
||||
tmpFonts.push(...getFontsFromTheme(propertyValue as Record<string, any>, tmpFonts));
|
||||
}
|
||||
}
|
||||
|
||||
return [...new Set(tmpFonts)];
|
||||
};
|
||||
|
||||
const loadedFonts: string[] = [];
|
||||
|
||||
export const hasMissingFonts = (theme: ThemeOptions): boolean => {
|
||||
const themeFonts = getFontsFromTheme(theme.typography ?? {});
|
||||
return !!themeFonts.length && themeFonts.some((font) => !loadedFonts.includes(font));
|
||||
};
|
||||
|
||||
export const loadThemeFonts = async (theme: ThemeOptions): Promise<void> => {
|
||||
const themeFonts = getFontsFromTheme(theme.typography ?? {});
|
||||
const missingThemeFonts = themeFonts.filter((font) => !loadedFonts.includes(font));
|
||||
|
||||
if (!missingThemeFonts.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const loadFontsPromise = new ControlledPromise();
|
||||
try {
|
||||
WebFont.load({
|
||||
google: {
|
||||
families: missingThemeFonts,
|
||||
},
|
||||
active: () => {
|
||||
loadedFonts.push(...missingThemeFonts);
|
||||
loadFontsPromise.resolve();
|
||||
},
|
||||
inactive: () => {
|
||||
loadFontsPromise.reject();
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
defaultPromiseErrorHandler('AppThemes::loadFonts')(e);
|
||||
loadFontsPromise.reject(e);
|
||||
}
|
||||
|
||||
await loadFontsPromise.promise;
|
||||
};
|
||||
Reference in New Issue
Block a user