2022-11-12 16:22:39 +01:00
|
|
|
import { BlurView, BlurViewProps } from '@react-native-community/blur';
|
2022-04-09 15:48:01 +02:00
|
|
|
import React, { PropsWithChildren } from 'react';
|
2021-02-13 12:14:57 +01:00
|
|
|
import { useContext } from 'react';
|
2023-04-11 18:27:55 +02:00
|
|
|
import { ColorSchemeName, Platform, StyleSheet, View, useColorScheme } from 'react-native';
|
2023-06-19 22:26:41 +02:00
|
|
|
import { useTypedSelector } from '@/store';
|
|
|
|
|
import { ColorScheme } from '@/store/settings/types';
|
2024-02-12 00:01:09 +01:00
|
|
|
import { useAccessibilitySetting } from 'react-native-accessibility-settings';
|
2022-05-04 22:46:19 +02:00
|
|
|
|
|
|
|
|
const majorPlatformVersion = typeof Platform.Version === 'string' ? parseInt(Platform.Version, 10) : Platform.Version;
|
2021-02-11 23:43:21 +01:00
|
|
|
|
2021-02-13 12:14:57 +01:00
|
|
|
/**
|
|
|
|
|
* Function for generating both the dark and light stylesheets, so that they
|
|
|
|
|
* don't have to be generate on every individual component render
|
|
|
|
|
*/
|
2024-02-12 00:01:09 +01:00
|
|
|
function generateStyles(scheme: ColorSchemeName, highContrast: boolean) {
|
2021-02-11 23:43:21 +01:00
|
|
|
return StyleSheet.create({
|
|
|
|
|
text: {
|
|
|
|
|
color: scheme === 'dark' ? '#fff' : '#000',
|
2022-04-09 15:48:01 +02:00
|
|
|
fontSize: 14,
|
|
|
|
|
fontFamily: 'Inter',
|
2021-02-11 23:43:21 +01:00
|
|
|
},
|
|
|
|
|
textHalfOpacity: {
|
2024-02-12 00:01:09 +01:00
|
|
|
color: highContrast
|
|
|
|
|
? (scheme === 'dark' ? '#ffffffbb' : '#000000bb')
|
|
|
|
|
: (scheme === 'dark' ? '#ffffff88' : '#00000088'),
|
2022-04-09 15:48:01 +02:00
|
|
|
fontSize: 14,
|
|
|
|
|
},
|
|
|
|
|
textQuarterOpacity: {
|
2024-02-12 00:01:09 +01:00
|
|
|
color: highContrast
|
|
|
|
|
? (scheme === 'dark' ? '#ffffff88' : '#00000088')
|
|
|
|
|
: (scheme === 'dark' ? '#ffffff44' : '#00000044'),
|
2022-04-09 15:48:01 +02:00
|
|
|
fontSize: 14,
|
2021-02-11 23:43:21 +01:00
|
|
|
},
|
|
|
|
|
view: {
|
2022-04-09 15:48:01 +02:00
|
|
|
backgroundColor: scheme === 'dark' ? '#111' : '#fff',
|
2021-02-11 23:43:21 +01:00
|
|
|
},
|
|
|
|
|
border: {
|
|
|
|
|
borderColor: scheme === 'dark' ? '#262626' : '#ddd',
|
|
|
|
|
},
|
|
|
|
|
activeBackground: {
|
2024-02-12 00:01:09 +01:00
|
|
|
backgroundColor: highContrast
|
|
|
|
|
? `#8b513c${scheme === 'dark' ? '26' : '10'}`
|
|
|
|
|
: `#FF3C00${scheme === 'dark' ? '26' : '16'}`,
|
2021-02-11 23:43:21 +01:00
|
|
|
},
|
|
|
|
|
imageBackground: {
|
2022-05-17 23:04:45 +02:00
|
|
|
backgroundColor: scheme === 'dark' ? '#191919' : '#eee',
|
2022-05-16 22:16:45 +02:00
|
|
|
borderWidth: 0.5,
|
2022-05-16 22:28:13 +02:00
|
|
|
borderColor: scheme === 'dark' ? '#262626' : '#ddd',
|
2021-02-11 23:43:21 +01:00
|
|
|
},
|
|
|
|
|
modal: {
|
2022-05-11 22:13:42 +02:00
|
|
|
backgroundColor: scheme === 'dark' ? '#000' : '#fff',
|
2021-02-11 23:43:21 +01:00
|
|
|
},
|
|
|
|
|
modalInner: {
|
|
|
|
|
backgroundColor: scheme === 'dark' ? '#000' : '#fff',
|
|
|
|
|
},
|
|
|
|
|
button: {
|
2024-02-12 00:01:09 +01:00
|
|
|
backgroundColor: highContrast
|
|
|
|
|
? (scheme === 'dark' ? '#ffffff0f' : '#0000000f')
|
|
|
|
|
: (scheme === 'dark' ? '#ffffff09' : '#00000009'),
|
2021-02-11 23:43:21 +01:00
|
|
|
},
|
|
|
|
|
input: {
|
2022-05-17 23:04:45 +02:00
|
|
|
backgroundColor: scheme === 'dark' ? '#191919' : '#f3f3f3',
|
2021-02-11 23:43:21 +01:00
|
|
|
color: scheme === 'dark' ? '#fff' : '#000',
|
|
|
|
|
},
|
2021-02-13 15:34:43 +01:00
|
|
|
stackHeader: {
|
|
|
|
|
color: scheme === 'dark' ? 'white' : 'black'
|
2022-05-10 23:52:58 +02:00
|
|
|
},
|
|
|
|
|
icon: {
|
|
|
|
|
color: scheme === 'dark' ? '#ffffff4d' : '#0000004d',
|
|
|
|
|
},
|
|
|
|
|
divider: {
|
2022-05-10 23:56:20 +02:00
|
|
|
backgroundColor: scheme === 'dark' ? '#333' : '#eee',
|
2022-05-10 23:52:58 +02:00
|
|
|
},
|
2022-05-16 22:16:45 +02:00
|
|
|
filter: {
|
2022-05-17 23:04:45 +02:00
|
|
|
backgroundColor: scheme === 'dark' ? '#191919' : '#f3f3f3',
|
2022-05-16 22:16:45 +02:00
|
|
|
},
|
2024-02-12 00:01:09 +01:00
|
|
|
themeColor: {
|
|
|
|
|
color: highContrast
|
|
|
|
|
? scheme === 'dark' ? '#FF7A1C' : '#c93400'
|
|
|
|
|
: '#FF3C00',
|
|
|
|
|
},
|
|
|
|
|
themeColorHalfOpacity: {
|
|
|
|
|
color: highContrast
|
|
|
|
|
? scheme === 'dark' ? '#FF7A1Cbb' : '#c93400bb'
|
|
|
|
|
: '#FF3C0088',
|
|
|
|
|
},
|
|
|
|
|
themeColorQuarterOpacity: {
|
|
|
|
|
color: highContrast
|
|
|
|
|
? scheme === 'dark' ? '#FF7A1C88' : '#c9340088'
|
|
|
|
|
: '#FF3C0044',
|
|
|
|
|
},
|
|
|
|
|
themeBackground: {
|
|
|
|
|
backgroundColor: highContrast
|
|
|
|
|
? scheme === 'dark' ? '#FF7A1C' : '#c93400'
|
|
|
|
|
: '#FF3C00',
|
|
|
|
|
}
|
2021-02-11 23:43:21 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 12:14:57 +01:00
|
|
|
// Prerender both stylesheets
|
2024-02-12 00:01:09 +01:00
|
|
|
export const themes: Record<'dark' | 'light' | 'dark-highcontrast' | 'light-highcontrast', ReturnType<typeof generateStyles>> = {
|
|
|
|
|
'dark': generateStyles('dark', false),
|
|
|
|
|
'light': generateStyles('light', false),
|
|
|
|
|
'dark-highcontrast': generateStyles('dark', true),
|
|
|
|
|
'light-highcontrast': generateStyles('light', true),
|
2021-02-13 12:14:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Create context for supplying the theming information
|
|
|
|
|
export const ColorSchemeContext = React.createContext(themes.dark);
|
|
|
|
|
|
2024-07-22 13:17:26 +02:00
|
|
|
/**
|
|
|
|
|
* This hook returns the proper color scheme, taking into account potential user overrides.
|
|
|
|
|
*/
|
|
|
|
|
export function useUserOrSystemScheme() {
|
|
|
|
|
const systemScheme = useColorScheme();
|
|
|
|
|
const userScheme = useTypedSelector((state) => state.settings.colorScheme);
|
|
|
|
|
return userScheme === ColorScheme.System ? systemScheme : userScheme;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 21:01:21 +02:00
|
|
|
/**
|
|
|
|
|
* This provider contains the logic for settings the right theme on the ColorSchemeContext.
|
|
|
|
|
*/
|
|
|
|
|
export function ColorSchemeProvider({ children }: PropsWithChildren<{}>) {
|
2024-02-12 00:01:09 +01:00
|
|
|
const highContrast = useAccessibilitySetting('darkerSystemColors');
|
2024-07-22 13:17:26 +02:00
|
|
|
const scheme = useUserOrSystemScheme();
|
2024-02-12 00:01:09 +01:00
|
|
|
const theme = highContrast
|
|
|
|
|
? themes[`${scheme || 'light'}-highcontrast`]
|
|
|
|
|
: themes[scheme || 'light'];
|
2023-04-28 21:01:21 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ColorSchemeContext.Provider value={theme}>
|
|
|
|
|
{children}
|
|
|
|
|
</ColorSchemeContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 12:14:57 +01:00
|
|
|
/**
|
|
|
|
|
* Retrieves the default styles object in hook form
|
|
|
|
|
*/
|
|
|
|
|
export default function useDefaultStyles() {
|
|
|
|
|
return useContext(ColorSchemeContext);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-11 23:43:21 +01:00
|
|
|
interface DefaultStylesProviderProps {
|
|
|
|
|
children: (defaultStyles: ReturnType<typeof useDefaultStyles>) => JSX.Element;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 12:14:57 +01:00
|
|
|
/**
|
|
|
|
|
* A render props component to supply the defaultStyles object.
|
|
|
|
|
*/
|
2021-02-11 23:43:21 +01:00
|
|
|
export function DefaultStylesProvider(props: DefaultStylesProviderProps) {
|
|
|
|
|
const defaultStyles = useDefaultStyles();
|
|
|
|
|
|
|
|
|
|
return props.children(defaultStyles);
|
2022-04-09 15:48:01 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-12 16:22:39 +01:00
|
|
|
export function ColoredBlurView(props: PropsWithChildren<BlurViewProps>) {
|
2023-04-28 21:01:21 +02:00
|
|
|
const systemScheme = useColorScheme();
|
|
|
|
|
const userScheme = useTypedSelector((state) => state.settings.colorScheme);
|
|
|
|
|
const scheme = userScheme === ColorScheme.System ? systemScheme : userScheme;
|
2022-04-09 15:48:01 +02:00
|
|
|
|
2022-06-09 23:37:07 +02:00
|
|
|
return Platform.OS === 'ios' ? (
|
2022-05-04 22:46:19 +02:00
|
|
|
<BlurView
|
|
|
|
|
{...props}
|
|
|
|
|
blurType={Platform.OS === 'ios' && majorPlatformVersion >= 13
|
2023-04-28 21:01:21 +02:00
|
|
|
? scheme === 'dark' ? 'materialDark' : 'materialLight'
|
2022-05-04 22:46:19 +02:00
|
|
|
: scheme === 'dark' ? 'extraDark' : 'xlight'
|
|
|
|
|
} />
|
2022-06-09 23:37:07 +02:00
|
|
|
) : (
|
2023-04-11 18:27:55 +02:00
|
|
|
<View {...props} style={[ props.style, {
|
2023-04-23 01:25:43 +02:00
|
|
|
backgroundColor: scheme === 'light' ? '#f6f6f6fb' : '#333333fb',
|
2023-04-11 18:27:55 +02:00
|
|
|
} ]} />
|
2022-04-09 15:48:01 +02:00
|
|
|
);
|
2021-02-11 23:43:21 +01:00
|
|
|
}
|