feishin/src/renderer/features/player/components/full-screen-player.tsx

505 lines
20 KiB
TypeScript
Raw Normal View History

import { Divider, Group } from '@mantine/core';
import { useHotkeys } from '@mantine/hooks';
import { motion, Variants } from 'framer-motion';
import { useLayoutEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { RiArrowDownSLine, RiSettings3Line } from 'react-icons/ri';
import { useLocation } from 'react-router';
import styled from 'styled-components';
import {
Button,
NumberInput,
Option,
Popover,
Select,
Slider,
Switch,
} from '/@/renderer/components';
import { TableConfigDropdown } from '/@/renderer/components/virtual-table';
import { FullScreenPlayerImage } from '/@/renderer/features/player/components/full-screen-player-image';
import { FullScreenPlayerQueue } from '/@/renderer/features/player/components/full-screen-player-queue';
2025-05-20 19:23:36 -07:00
import { useFastAverageColor } from '/@/renderer/hooks';
import {
2023-07-01 19:10:05 -07:00
useCurrentSong,
useFullScreenPlayerStore,
useFullScreenPlayerStoreActions,
useLyricsSettings,
useSettingsStore,
useSettingsStoreActions,
2023-07-01 19:10:05 -07:00
useWindowSettings,
} from '/@/renderer/store';
2025-05-20 19:23:36 -07:00
import { Platform } from '/@/shared/types/types';
const Container = styled(motion.div)`
2023-07-01 19:10:05 -07:00
position: absolute;
top: 0;
left: 0;
z-index: 200;
display: flex;
justify-content: center;
padding: 2rem;
@media screen and (orientation: portrait) {
2023-07-01 19:10:05 -07:00
padding: 2rem 2rem 1rem;
}
`;
const ResponsiveContainer = styled.div`
2023-07-01 19:10:05 -07:00
display: grid;
grid-template-rows: minmax(0, 1fr);
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
gap: 2rem 2rem;
width: 100%;
max-width: 2560px;
margin-top: 5rem;
@media screen and (orientation: portrait) {
2023-07-01 19:10:05 -07:00
grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
grid-template-columns: minmax(0, 1fr);
margin-top: 0;
}
`;
interface BackgroundImageOverlayProps {
$blur: number;
}
const BackgroundImageOverlay = styled.div<BackgroundImageOverlayProps>`
2023-07-01 19:10:05 -07:00
position: absolute;
top: 0;
left: 0;
z-index: -1;
width: 100%;
height: 100%;
background: var(--bg-header-overlay);
backdrop-filter: blur(${({ $blur }) => $blur}rem);
`;
const mainBackground = 'var(--main-bg)';
const Controls = () => {
const { t } = useTranslation();
const {
dynamicBackground,
dynamicImageBlur,
dynamicIsImage,
expanded,
opacity,
useImageAspectRatio,
} = useFullScreenPlayerStore();
2023-07-01 19:10:05 -07:00
const { setStore } = useFullScreenPlayerStoreActions();
const { setSettings } = useSettingsStoreActions();
const lyricConfig = useLyricsSettings();
2023-07-01 19:10:05 -07:00
const handleToggleFullScreenPlayer = () => {
setStore({ expanded: !expanded });
};
const handleLyricsSettings = (property: string, value: any) => {
setSettings({
lyrics: {
...useSettingsStore.getState().lyrics,
[property]: value,
},
});
};
2023-07-01 19:10:05 -07:00
useHotkeys([['Escape', handleToggleFullScreenPlayer]]);
return (
<Group
p="1rem"
pos="absolute"
spacing="sm"
sx={{
background: `rgb(var(--main-bg-transparent), ${opacity}%)`,
2023-07-01 19:10:05 -07:00
left: 0,
top: 0,
2023-07-01 19:10:05 -07:00
}}
>
<Button
compact
onClick={handleToggleFullScreenPlayer}
2023-07-01 19:10:05 -07:00
size="sm"
tooltip={{ label: t('common.minimize', { postProcess: 'titleCase' }) }}
2023-07-01 19:10:05 -07:00
variant="subtle"
>
<RiArrowDownSLine size="2rem" />
</Button>
<Popover position="bottom-start">
<Popover.Target>
<Button
compact
size="sm"
tooltip={{ label: t('common.configure', { postProcess: 'titleCase' }) }}
2023-07-01 19:10:05 -07:00
variant="subtle"
>
<RiSettings3Line size="1.5rem" />
</Button>
</Popover.Target>
<Popover.Dropdown>
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.dynamicBackground', {
postProcess: 'sentenceCase',
})}
</Option.Label>
2023-07-01 19:10:05 -07:00
<Option.Control>
<Switch
defaultChecked={dynamicBackground}
onChange={(e) =>
setStore({
dynamicBackground: e.target.checked,
})
}
/>
</Option.Control>
</Option>
{dynamicBackground && (
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.dynamicIsImage', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Switch
defaultChecked={dynamicIsImage}
onChange={(e) =>
setStore({
dynamicIsImage: e.target.checked,
})
}
/>
</Option.Control>
</Option>
)}
{dynamicBackground && dynamicIsImage && (
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.dynamicImageBlur', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Slider
defaultValue={dynamicImageBlur}
label={(e) => `${e} rem`}
max={6}
min={0}
onChangeEnd={(e) => setStore({ dynamicImageBlur: Number(e) })}
step={0.5}
w="100%"
/>
</Option.Control>
</Option>
)}
{dynamicBackground && (
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.opacity', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Slider
defaultValue={opacity}
label={(e) => `${e} %`}
max={100}
2024-03-01 19:54:30 -08:00
min={0}
onChangeEnd={(e) => setStore({ opacity: Number(e) })}
w="100%"
/>
</Option.Control>
</Option>
)}
2023-07-01 19:10:05 -07:00
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.useImageAspectRatio', {
postProcess: 'sentenceCase',
})}
</Option.Label>
2023-07-01 19:10:05 -07:00
<Option.Control>
<Switch
checked={useImageAspectRatio}
2023-07-01 19:10:05 -07:00
onChange={(e) =>
setStore({
useImageAspectRatio: e.target.checked,
})
}
/>
</Option.Control>
</Option>
<Divider my="sm" />
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.followCurrentLyric', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Switch
checked={lyricConfig.follow}
onChange={(e) =>
handleLyricsSettings('follow', e.currentTarget.checked)
}
/>
</Option.Control>
</Option>
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.showLyricProvider', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Switch
checked={lyricConfig.showProvider}
onChange={(e) =>
handleLyricsSettings('showProvider', e.currentTarget.checked)
}
/>
</Option.Control>
</Option>
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.showLyricMatch', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Switch
checked={lyricConfig.showMatch}
onChange={(e) =>
handleLyricsSettings('showMatch', e.currentTarget.checked)
}
/>
</Option.Control>
</Option>
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.lyricSize', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Group
noWrap
w="100%"
>
<Slider
defaultValue={lyricConfig.fontSize}
label={(e) =>
`${t('page.fullscreenPlayer.config.synchronized', {
postProcess: 'titleCase',
})}: ${e}px`
}
max={72}
min={8}
onChangeEnd={(e) => handleLyricsSettings('fontSize', Number(e))}
w="100%"
/>
<Slider
defaultValue={lyricConfig.fontSize}
label={(e) =>
`${t('page.fullscreenPlayer.config.unsynchronized', {
postProcess: 'sentenceCase',
})}: ${e}px`
}
max={72}
min={8}
onChangeEnd={(e) =>
handleLyricsSettings('fontSizeUnsync', Number(e))
}
w="100%"
/>
</Group>
</Option.Control>
</Option>
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.lyricGap', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Group
noWrap
w="100%"
>
<Slider
defaultValue={lyricConfig.gap}
label={(e) => `Synchronized: ${e}px`}
max={50}
min={0}
onChangeEnd={(e) => handleLyricsSettings('gap', Number(e))}
w="100%"
/>
<Slider
defaultValue={lyricConfig.gap}
label={(e) => `Unsynchronized: ${e}px`}
max={50}
min={0}
onChangeEnd={(e) =>
handleLyricsSettings('gapUnsync', Number(e))
}
w="100%"
/>
</Group>
</Option.Control>
</Option>
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.lyricAlignment', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<Select
data={[
{
label: t('common.left', {
postProcess: 'titleCase',
}),
value: 'left',
},
{
label: t('common.center', {
postProcess: 'titleCase',
}),
value: 'center',
},
{
label: t('common.right', {
postProcess: 'titleCase',
}),
value: 'right',
},
]}
onChange={(e) => handleLyricsSettings('alignment', e)}
value={lyricConfig.alignment}
/>
</Option.Control>
</Option>
<Option>
<Option.Label>
{t('page.fullscreenPlayer.config.lyricOffset', {
postProcess: 'sentenceCase',
})}
</Option.Label>
<Option.Control>
<NumberInput
defaultValue={lyricConfig.delayMs}
hideControls={false}
onBlur={(e) =>
handleLyricsSettings('delayMs', Number(e.currentTarget.value))
}
step={10}
/>
</Option.Control>
</Option>
<Divider my="sm" />
2023-07-01 19:10:05 -07:00
<TableConfigDropdown type="fullScreen" />
</Popover.Dropdown>
</Popover>
</Group>
);
};
const containerVariants: Variants = {
2023-07-01 19:10:05 -07:00
closed: (custom) => {
const { windowBarStyle } = custom;
return {
height:
windowBarStyle === Platform.WINDOWS || windowBarStyle === Platform.MACOS
? 'calc(100vh - 120px)'
: 'calc(100vh - 90px)',
position: 'absolute',
top: '100vh',
transition: {
duration: 0.5,
ease: 'easeInOut',
},
width: '100vw',
y: -100,
};
},
open: (custom) => {
const { background, backgroundImage, dynamicBackground, windowBarStyle } = custom;
2023-07-01 19:10:05 -07:00
return {
background: dynamicBackground ? backgroundImage : mainBackground,
backgroundColor: dynamicBackground ? background : mainBackground,
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
backgroundSize: 'cover',
2023-07-01 19:10:05 -07:00
height:
windowBarStyle === Platform.WINDOWS || windowBarStyle === Platform.MACOS
? 'calc(100vh - 120px)'
: 'calc(100vh - 90px)',
left: 0,
position: 'absolute',
top: 0,
transition: {
background: {
duration: 0.5,
ease: 'easeInOut',
},
delay: 0.1,
duration: 0.5,
ease: 'easeInOut',
},
width: '100vw',
y: 0,
};
},
};
export const FullScreenPlayer = () => {
const { dynamicBackground, dynamicImageBlur, dynamicIsImage } = useFullScreenPlayerStore();
2023-07-01 19:10:05 -07:00
const { setStore } = useFullScreenPlayerStoreActions();
const { windowBarStyle } = useWindowSettings();
const location = useLocation();
const isOpenedRef = useRef<boolean | null>(null);
useLayoutEffect(() => {
if (isOpenedRef.current !== null) {
setStore({ expanded: false });
}
isOpenedRef.current = true;
}, [location, setStore]);
const currentSong = useCurrentSong();
const { color: background } = useFastAverageColor({
algorithm: 'dominant',
src: currentSong?.imageUrl,
srcLoaded: true,
});
2023-07-01 19:10:05 -07:00
const imageUrl = currentSong?.imageUrl && currentSong.imageUrl.replace(/size=\d+/g, 'size=500');
const backgroundImage =
imageUrl && dynamicIsImage
? `url("${imageUrl.replace(currentSong.id, currentSong.albumId)}"), url("${imageUrl}")`
: mainBackground;
2023-07-01 19:10:05 -07:00
return (
<Container
animate="open"
custom={{ background, backgroundImage, dynamicBackground, windowBarStyle }}
2023-07-01 19:10:05 -07:00
exit="closed"
initial="closed"
transition={{ duration: 2 }}
variants={containerVariants}
>
<Controls />
{dynamicBackground && <BackgroundImageOverlay $blur={dynamicImageBlur} />}
2023-07-01 19:10:05 -07:00
<ResponsiveContainer>
<FullScreenPlayerImage />
<FullScreenPlayerQueue />
</ResponsiveContainer>
</Container>
);
};