2024-09-28 03:24:16 +02:00
|
|
|
/*
|
|
|
|
|
* 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 IconButton from '@mui/material/IconButton';
|
|
|
|
|
import SkipPreviousIcon from '@mui/icons-material/SkipPrevious';
|
|
|
|
|
import SkipNextIcon from '@mui/icons-material/SkipNext';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
|
|
|
|
import { alpha } from '@mui/material/styles';
|
|
|
|
|
import Box from '@mui/material/Box';
|
2024-12-27 17:01:34 +01:00
|
|
|
import { ComponentProps, memo, useCallback, useLayoutEffect, useMemo } from 'react';
|
2024-09-28 03:24:16 +02:00
|
|
|
import { ReaderProgressBar } from '@/modules/reader/components/overlay/progress-bar/ReaderProgressBar.tsx';
|
|
|
|
|
import { useReaderStateChaptersContext } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx';
|
|
|
|
|
import { ReaderService } from '@/modules/reader/services/ReaderService.ts';
|
2024-12-25 23:50:58 +01:00
|
|
|
import { getPage } from '@/modules/reader/utils/ReaderProgressBar.utils.tsx';
|
2024-12-11 13:59:50 +01:00
|
|
|
import { getOptionForDirection } from '@/modules/theme/services/ThemeCreator.ts';
|
2025-01-02 15:15:43 +01:00
|
|
|
import { ProgressBarPosition, ReaderResumeMode, ReaderStateChapters } from '@/modules/reader/types/Reader.types.ts';
|
2024-09-28 03:24:16 +02:00
|
|
|
import { ReaderProgressBarDirectionWrapper } from '@/modules/reader/components/overlay/progress-bar/ReaderProgressBarDirectionWrapper.tsx';
|
|
|
|
|
import { useReaderProgressBarContext } from '@/modules/reader/contexts/ReaderProgressBarContext.tsx';
|
|
|
|
|
import { useReaderOverlayContext } from '@/modules/reader/contexts/ReaderOverlayContext.tsx';
|
2024-12-21 03:46:55 +01:00
|
|
|
import { withPropsFrom } from '@/modules/core/hoc/withPropsFrom.tsx';
|
|
|
|
|
import { TReaderOverlayContext } from '@/modules/reader/types/ReaderOverlay.types.ts';
|
2024-12-26 17:38:12 +01:00
|
|
|
import { ReaderProgressBarProps, TReaderProgressBarContext } from '@/modules/reader/types/ReaderProgressBar.types.ts';
|
2024-12-26 01:09:33 +01:00
|
|
|
import { ReaderProgressBarSlotMobile } from '@/modules/reader/components/overlay/progress-bar/mobile/ReaderProgressBarSlotMobile.tsx';
|
2024-12-26 17:38:12 +01:00
|
|
|
import { userReaderStatePagesContext } from '@/modules/reader/contexts/state/ReaderStatePagesContext.tsx';
|
2024-12-27 17:01:34 +01:00
|
|
|
import { applyStyles } from '@/modules/core/utils/ApplyStyles.ts';
|
2024-09-28 03:24:16 +02:00
|
|
|
|
2024-12-26 01:41:36 +01:00
|
|
|
const PROGRESS_BAR_SLOT_PROPS: ComponentProps<typeof ReaderProgressBar>['slotProps'] = {
|
|
|
|
|
container: {
|
|
|
|
|
sx: {
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
position: 'relative',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyItems: 'center',
|
|
|
|
|
alignItems: 'stretch',
|
|
|
|
|
backgroundColor: (theme) => alpha(theme.palette.background.paper, 0.85),
|
|
|
|
|
borderRadius: 100,
|
|
|
|
|
boxShadow: 2,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarRoot: {
|
|
|
|
|
sx: {
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
alignItems: 'stretch',
|
|
|
|
|
gap: 0,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarSlotsActionArea: {
|
|
|
|
|
sx: {
|
|
|
|
|
height: '100%',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
py: 2,
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarSlotsContainer: {
|
|
|
|
|
sx: {
|
|
|
|
|
borderRadius: 100,
|
2024-12-27 17:01:34 +01:00
|
|
|
backgroundColor: 'background.default',
|
2024-12-26 01:41:36 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarSlot: {
|
|
|
|
|
sx: {
|
|
|
|
|
height: '20px',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarCurrentPageSlot: {
|
|
|
|
|
sx: {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
zIndex: 1,
|
2024-12-28 04:06:03 +01:00
|
|
|
cursor: 'inherit',
|
2024-12-26 01:41:36 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarPageTexts: {
|
|
|
|
|
base: { px: 1 },
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-21 03:46:55 +01:00
|
|
|
const BaseMobileReaderProgressBar = ({
|
|
|
|
|
previousChapter,
|
|
|
|
|
nextChapter,
|
|
|
|
|
isVisible,
|
|
|
|
|
setIsMaximized,
|
2024-12-28 04:06:03 +01:00
|
|
|
isDragging,
|
2024-12-26 17:38:12 +01:00
|
|
|
currentPageIndex,
|
|
|
|
|
pages,
|
|
|
|
|
direction,
|
2024-12-21 03:46:55 +01:00
|
|
|
}: Pick<ReaderStateChapters, 'previousChapter' | 'nextChapter'> &
|
|
|
|
|
Pick<TReaderOverlayContext, 'isVisible'> &
|
2024-12-28 04:06:03 +01:00
|
|
|
Pick<TReaderProgressBarContext, 'setIsMaximized' | 'isDragging'> &
|
2024-12-26 17:38:12 +01:00
|
|
|
Pick<ReaderProgressBarProps, 'currentPageIndex' | 'pages'> & {
|
|
|
|
|
direction: ReturnType<typeof ReaderService.useGetThemeDirection>;
|
|
|
|
|
}) => {
|
2024-12-06 23:59:39 +01:00
|
|
|
const openNextChapter = ReaderService.useNavigateToChapter(nextChapter, ReaderResumeMode.START);
|
|
|
|
|
const openPreviousChapter = ReaderService.useNavigateToChapter(previousChapter, ReaderResumeMode.END);
|
2024-09-28 03:24:16 +02:00
|
|
|
|
2024-12-27 17:01:34 +01:00
|
|
|
const currentPagesIndex = useMemo(() => getPage(currentPageIndex, pages).pagesIndex, [currentPageIndex, pages]);
|
|
|
|
|
|
|
|
|
|
const progressBarCurrentPage = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
progressBarCurrentPage: (
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
left: 'calc(100% - 6px)',
|
|
|
|
|
...applyStyles(currentPagesIndex === 0, {
|
|
|
|
|
left: '0',
|
|
|
|
|
}),
|
|
|
|
|
width: '6px',
|
|
|
|
|
height: '75%',
|
|
|
|
|
backgroundColor: 'primary.main',
|
|
|
|
|
borderRadius: 100,
|
2024-12-28 04:06:03 +01:00
|
|
|
cursor: isDragging ? 'grabbing' : 'grab',
|
2024-12-27 17:01:34 +01:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
}),
|
2024-12-28 04:06:03 +01:00
|
|
|
[currentPagesIndex, pages.length, isDragging],
|
2024-12-27 17:01:34 +01:00
|
|
|
);
|
|
|
|
|
|
2024-09-28 03:24:16 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
setIsMaximized(isVisible);
|
|
|
|
|
|
|
|
|
|
return () => setIsMaximized(false);
|
|
|
|
|
}, [isVisible]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ReaderProgressBarDirectionWrapper>
|
|
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
px: 2,
|
|
|
|
|
gap: 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={openPreviousChapter}
|
|
|
|
|
disabled={!previousChapter}
|
2024-12-23 02:54:12 +01:00
|
|
|
sx={{ backgroundColor: (theme) => alpha(theme.palette.background.paper, 0.85), boxShadow: 2 }}
|
2024-09-28 03:24:16 +02:00
|
|
|
>
|
|
|
|
|
{getOptionForDirection(<SkipPreviousIcon />, <SkipNextIcon />, direction)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
<ReaderProgressBar
|
2025-01-02 15:15:43 +01:00
|
|
|
progressBarPosition={ProgressBarPosition.BOTTOM}
|
2024-12-28 04:05:29 +01:00
|
|
|
fullSegmentClicks={false}
|
2024-12-26 01:41:36 +01:00
|
|
|
createProgressBarSlot={useCallback(
|
2024-12-27 17:01:34 +01:00
|
|
|
(page, pagesIndex, _2, _3, _4, _5, isTrailingPage, totalPages) => (
|
|
|
|
|
<ReaderProgressBarSlotMobile
|
|
|
|
|
pageName={page.name}
|
|
|
|
|
isTrailingPage={isTrailingPage}
|
|
|
|
|
pagesIndex={pagesIndex}
|
|
|
|
|
totalPages={totalPages}
|
|
|
|
|
/>
|
2024-12-26 01:41:36 +01:00
|
|
|
),
|
|
|
|
|
[],
|
|
|
|
|
)}
|
2024-09-28 03:24:16 +02:00
|
|
|
slotProps={{
|
2024-12-26 01:41:36 +01:00
|
|
|
...PROGRESS_BAR_SLOT_PROPS,
|
2024-09-28 03:24:16 +02:00
|
|
|
progressBarReadPages: {
|
|
|
|
|
sx: {
|
|
|
|
|
height: '20px',
|
|
|
|
|
backgroundColor: 'primary.main',
|
|
|
|
|
borderRadius: '400px 0 0 400px',
|
2024-12-27 17:01:34 +01:00
|
|
|
width: `${(Math.max(0, getPage(currentPageIndex, pages).pagesIndex) / (pages.length - 1)) * 100}%`,
|
2024-09-28 03:24:16 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}}
|
2024-12-27 17:01:34 +01:00
|
|
|
slots={progressBarCurrentPage}
|
2024-09-28 03:24:16 +02:00
|
|
|
/>
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={openNextChapter}
|
|
|
|
|
disabled={!nextChapter}
|
2024-12-23 02:54:12 +01:00
|
|
|
sx={{ backgroundColor: (theme) => alpha(theme.palette.background.paper, 0.85), boxShadow: 2 }}
|
2024-09-28 03:24:16 +02:00
|
|
|
>
|
|
|
|
|
{getOptionForDirection(<SkipNextIcon />, <SkipPreviousIcon />, direction)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Stack>
|
|
|
|
|
</ReaderProgressBarDirectionWrapper>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-12-21 03:46:55 +01:00
|
|
|
|
|
|
|
|
export const MobileReaderProgressBar = withPropsFrom(
|
2024-12-22 00:41:56 +01:00
|
|
|
memo(BaseMobileReaderProgressBar),
|
2024-12-26 17:38:12 +01:00
|
|
|
[
|
|
|
|
|
useReaderStateChaptersContext,
|
|
|
|
|
useReaderOverlayContext,
|
|
|
|
|
useReaderProgressBarContext,
|
|
|
|
|
userReaderStatePagesContext,
|
|
|
|
|
() => ({ direction: ReaderService.useGetThemeDirection() }),
|
|
|
|
|
],
|
2024-12-28 04:06:03 +01:00
|
|
|
[
|
|
|
|
|
'previousChapter',
|
|
|
|
|
'nextChapter',
|
|
|
|
|
'isVisible',
|
|
|
|
|
'setIsMaximized',
|
|
|
|
|
'isDragging',
|
|
|
|
|
'currentPageIndex',
|
|
|
|
|
'pages',
|
|
|
|
|
'direction',
|
|
|
|
|
],
|
2024-12-21 03:46:55 +01:00
|
|
|
);
|