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';
|
2025-01-02 21:57:01 +01:00
|
|
|
import { ComponentProps, memo, useCallback, useLayoutEffect, useMemo, useState } from 'react';
|
2025-01-02 21:25:35 +01:00
|
|
|
import Slide, { SlideProps } from '@mui/material/Slide';
|
2025-08-16 02:02:13 +02:00
|
|
|
import { ReaderProgressBar } from '@/features/reader/overlay/progress-bar/ReaderProgressBar.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { useReaderStateChaptersContext } from '@/features/reader/contexts/state/ReaderStateChaptersContext.tsx';
|
|
|
|
|
import { ReaderService } from '@/features/reader/services/ReaderService.ts';
|
2025-08-16 02:02:13 +02:00
|
|
|
import {
|
|
|
|
|
getPage,
|
|
|
|
|
getProgressBarPositionInfo,
|
|
|
|
|
} from '@/features/reader/overlay/progress-bar/ReaderProgressBar.utils.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { getOptionForDirection } from '@/features/theme/services/ThemeCreator.ts';
|
2025-08-31 13:41:09 +02:00
|
|
|
import { IReaderSettings, ProgressBarPosition, ReaderStateChapters } from '@/features/reader/Reader.types.ts';
|
2025-08-16 02:02:13 +02:00
|
|
|
import { ReaderProgressBarDirectionWrapper } from '@/features/reader/overlay/progress-bar/components/ReaderProgressBarDirectionWrapper.tsx';
|
2025-08-16 02:14:49 +02:00
|
|
|
import { useReaderProgressBarContext } from '@/features/reader/overlay/progress-bar/ReaderProgressBarContext.tsx';
|
|
|
|
|
import { useReaderOverlayContext } from '@/features/reader/overlay/ReaderOverlayContext.tsx';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { withPropsFrom } from '@/base/hoc/withPropsFrom.tsx';
|
2025-08-16 02:02:13 +02:00
|
|
|
import { TReaderOverlayContext } from '@/features/reader/overlay/ReaderOverlay.types.ts';
|
|
|
|
|
import {
|
|
|
|
|
ReaderProgressBarProps,
|
|
|
|
|
TReaderProgressBarContext,
|
|
|
|
|
} from '@/features/reader/overlay/progress-bar/ReaderProgressBar.types.ts';
|
|
|
|
|
import { ReaderProgressBarSlotMobile } from '@/features/reader/overlay/progress-bar/mobile/components/ReaderProgressBarSlotMobile.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { userReaderStatePagesContext } from '@/features/reader/contexts/state/ReaderStatePagesContext.tsx';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { applyStyles } from '@/base/utils/ApplyStyles.ts';
|
|
|
|
|
import { useResizeObserver } from '@/base/hooks/useResizeObserver.tsx';
|
2025-08-16 02:02:13 +02:00
|
|
|
import { getProgressBarPosition } from '@/features/reader/settings/ReaderSettings.utils.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { ReaderControls } from '@/features/reader/services/ReaderControls.ts';
|
2025-08-31 16:38:26 +02:00
|
|
|
import { useReaderStoreShallow } from '@/features/reader/ReaderStore.ts';
|
2024-09-28 03:24:16 +02:00
|
|
|
|
2025-01-02 21:25:35 +01:00
|
|
|
const PROGRESS_BAR_POSITION_TO_SLIDE_DIRECTION: Record<ProgressBarPosition, SlideProps['direction']> = {
|
|
|
|
|
[ProgressBarPosition.BOTTOM]: 'up',
|
|
|
|
|
[ProgressBarPosition.LEFT]: 'right',
|
|
|
|
|
[ProgressBarPosition.RIGHT]: 'left',
|
2025-01-11 02:02:48 +01:00
|
|
|
// should never get accessed
|
|
|
|
|
[ProgressBarPosition.AUTO]: 'left',
|
2024-12-26 01:41:36 +01:00
|
|
|
};
|
|
|
|
|
|
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,
|
2025-01-02 21:25:35 +01:00
|
|
|
direction: readerDirection,
|
|
|
|
|
progressBarPosition,
|
|
|
|
|
progressBarPositionAutoVertical,
|
|
|
|
|
topOffset = 0,
|
|
|
|
|
bottomOffset = 0,
|
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'> &
|
2025-01-02 21:25:35 +01:00
|
|
|
Pick<ReaderProgressBarProps, 'currentPageIndex' | 'pages'> &
|
2025-08-31 13:41:09 +02:00
|
|
|
Pick<IReaderSettings, 'progressBarPosition' | 'progressBarPositionAutoVertical'> & {
|
2024-12-26 17:38:12 +01:00
|
|
|
direction: ReturnType<typeof ReaderService.useGetThemeDirection>;
|
2025-01-02 21:25:35 +01:00
|
|
|
topOffset?: number;
|
|
|
|
|
bottomOffset?: number;
|
2024-12-26 17:38:12 +01:00
|
|
|
}) => {
|
2025-01-20 17:33:13 +01:00
|
|
|
const openChapter = ReaderControls.useOpenChapter();
|
2025-08-31 16:38:26 +02:00
|
|
|
const scrollbar = useReaderStoreShallow((state) => state.scrollbar);
|
2024-09-28 03:24:16 +02:00
|
|
|
|
2025-01-02 21:25:35 +01:00
|
|
|
const [, setRefreshProgressBarPosition] = useState({});
|
|
|
|
|
useResizeObserver(
|
|
|
|
|
window.document.documentElement,
|
|
|
|
|
useCallback(() => setRefreshProgressBarPosition({}), []),
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-02 22:16:59 +01:00
|
|
|
const finalProgressBarPosition = getProgressBarPosition(
|
|
|
|
|
progressBarPosition,
|
|
|
|
|
progressBarPositionAutoVertical,
|
2025-01-17 01:20:22 +01:00
|
|
|
// scrollbar x size is already included in the top/bottom offset due to the progress bar being placed in the reader mobile bottom bar
|
|
|
|
|
topOffset + bottomOffset,
|
2025-08-31 16:38:26 +02:00
|
|
|
scrollbar.xSize,
|
2025-01-02 22:16:59 +01:00
|
|
|
);
|
2025-01-02 21:25:35 +01:00
|
|
|
|
|
|
|
|
const { isLeft, isRight, isVertical, isHorizontal } = getProgressBarPositionInfo(finalProgressBarPosition);
|
|
|
|
|
const finalReaderDirection = isHorizontal ? readerDirection : 'ltr';
|
2024-12-27 17:01:34 +01:00
|
|
|
const currentPagesIndex = useMemo(() => getPage(currentPageIndex, pages).pagesIndex, [currentPageIndex, pages]);
|
|
|
|
|
|
2025-01-02 21:57:01 +01:00
|
|
|
const progressBarSlotProps: ComponentProps<typeof ReaderProgressBar>['slotProps'] = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
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,
|
|
|
|
|
gap: 0,
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
alignItems: 'stretch',
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarSlotsActionArea: {
|
|
|
|
|
sx: {
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
width: '100%',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
px: 2,
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
height: '100%',
|
|
|
|
|
py: 2,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarSlotsContainer: {
|
|
|
|
|
sx: {
|
|
|
|
|
borderRadius: 100,
|
|
|
|
|
backgroundColor: 'background.default',
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
py: 1,
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
px: 1,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarSlot: {
|
|
|
|
|
sx: {
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
width: '20px',
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
height: '20px',
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarCurrentPageSlot: {
|
|
|
|
|
sx: {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
zIndex: 1,
|
|
|
|
|
cursor: 'inherit',
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
progressBarPageTexts: {
|
|
|
|
|
base: {
|
|
|
|
|
sx: {
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
py: 1,
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
px: 1,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
[isVertical, isHorizontal],
|
|
|
|
|
);
|
|
|
|
|
|
2024-12-27 17:01:34 +01:00
|
|
|
const progressBarCurrentPage = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
progressBarCurrentPage: (
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
position: 'absolute',
|
2025-01-02 21:25:35 +01:00
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
top: 'calc(100% - 6px)',
|
|
|
|
|
...applyStyles(currentPagesIndex === 0, {
|
|
|
|
|
top: '0',
|
|
|
|
|
}),
|
|
|
|
|
width: '75%',
|
|
|
|
|
height: '6px',
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
left: 'calc(100% - 0px)',
|
|
|
|
|
...applyStyles(currentPagesIndex === 0, {
|
|
|
|
|
left: '0',
|
|
|
|
|
}),
|
|
|
|
|
width: '6px',
|
|
|
|
|
height: '75%',
|
2024-12-27 17:01:34 +01:00
|
|
|
}),
|
|
|
|
|
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
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
}),
|
2025-01-02 21:25:35 +01:00
|
|
|
[currentPagesIndex, pages.length, isDragging, isVertical, isHorizontal],
|
2024-12-27 17:01:34 +01:00
|
|
|
);
|
|
|
|
|
|
2024-09-28 03:24:16 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
setIsMaximized(isVisible);
|
|
|
|
|
|
|
|
|
|
return () => setIsMaximized(false);
|
|
|
|
|
}, [isVisible]);
|
|
|
|
|
|
|
|
|
|
return (
|
2025-01-02 21:25:35 +01:00
|
|
|
<Slide direction={PROGRESS_BAR_POSITION_TO_SLIDE_DIRECTION[finalProgressBarPosition]} in={isVisible}>
|
|
|
|
|
<ReaderProgressBarDirectionWrapper
|
2024-09-28 03:24:16 +02:00
|
|
|
sx={{
|
2025-01-02 21:25:35 +01:00
|
|
|
position: 'relative',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'end',
|
|
|
|
|
height: '100%',
|
|
|
|
|
pointerEvents: 'none',
|
|
|
|
|
...applyStyles(isLeft, {
|
|
|
|
|
justifyContent: readerDirection === 'ltr' ? 'start' : 'end',
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isRight, {
|
|
|
|
|
justifyContent: readerDirection === 'ltr' ? 'end' : 'start',
|
|
|
|
|
}),
|
2024-09-28 03:24:16 +02:00
|
|
|
}}
|
|
|
|
|
>
|
2025-01-02 21:25:35 +01:00
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
p: 2,
|
|
|
|
|
gap: 1,
|
|
|
|
|
pointerEvents: 'all',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
height: '100%',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
width: '100%',
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
}),
|
2024-09-28 03:24:16 +02:00
|
|
|
}}
|
|
|
|
|
>
|
2025-01-02 21:25:35 +01:00
|
|
|
<IconButton
|
2025-01-20 17:33:13 +01:00
|
|
|
onClick={() => openChapter('previous')}
|
2025-01-02 21:25:35 +01:00
|
|
|
disabled={!previousChapter}
|
|
|
|
|
sx={{
|
|
|
|
|
backgroundColor: (theme) => alpha(theme.palette.background.paper, 0.85),
|
|
|
|
|
boxShadow: 2,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{getOptionForDirection(
|
|
|
|
|
<SkipPreviousIcon
|
|
|
|
|
sx={{
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
transform: 'rotate(90deg)',
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
/>,
|
|
|
|
|
<SkipNextIcon
|
|
|
|
|
sx={{
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
transform: 'rotate(90deg)',
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
/>,
|
|
|
|
|
finalReaderDirection,
|
|
|
|
|
)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
<ReaderProgressBar
|
2025-01-02 21:48:23 +01:00
|
|
|
direction={finalReaderDirection}
|
2025-01-02 21:25:35 +01:00
|
|
|
progressBarPosition={finalProgressBarPosition}
|
|
|
|
|
fullSegmentClicks={false}
|
|
|
|
|
createProgressBarSlot={useCallback(
|
|
|
|
|
(
|
|
|
|
|
page,
|
|
|
|
|
pagesIndex,
|
|
|
|
|
_primaryPageLoadState,
|
|
|
|
|
_secondaryPageLoadState,
|
|
|
|
|
isCurrentPage,
|
|
|
|
|
_isLeadingPage,
|
|
|
|
|
isTrailingPage,
|
|
|
|
|
totalPages,
|
|
|
|
|
) => (
|
|
|
|
|
<ReaderProgressBarSlotMobile
|
|
|
|
|
pageName={page.name}
|
|
|
|
|
isTrailingPage={isTrailingPage}
|
|
|
|
|
isCurrentPage={isCurrentPage}
|
|
|
|
|
pagesIndex={pagesIndex}
|
|
|
|
|
totalPages={totalPages}
|
|
|
|
|
isVertical={isVertical}
|
|
|
|
|
isHorizontal={isHorizontal}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
[isVertical, isHorizontal],
|
|
|
|
|
)}
|
|
|
|
|
slotProps={{
|
2025-01-02 21:57:01 +01:00
|
|
|
...progressBarSlotProps,
|
2025-01-02 21:25:35 +01:00
|
|
|
progressBarReadPages: {
|
|
|
|
|
sx: {
|
|
|
|
|
backgroundColor: 'primary.main',
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
width: '20px',
|
|
|
|
|
height: `${(Math.max(0, getPage(currentPageIndex, pages).pagesIndex) / (pages.length - 1)) * 100}%`,
|
|
|
|
|
borderRadius: '400px 400px 0 0',
|
|
|
|
|
}),
|
|
|
|
|
...applyStyles(isHorizontal, {
|
|
|
|
|
width: `${(Math.max(0, getPage(currentPageIndex, pages).pagesIndex) / (pages.length - 1)) * 100}%`,
|
|
|
|
|
height: '20px',
|
|
|
|
|
borderRadius: '400px 0 0 400px',
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
slots={progressBarCurrentPage}
|
|
|
|
|
/>
|
|
|
|
|
<IconButton
|
2025-01-20 17:33:13 +01:00
|
|
|
onClick={() => openChapter('next')}
|
2025-01-02 21:25:35 +01:00
|
|
|
disabled={!nextChapter}
|
|
|
|
|
sx={{ backgroundColor: (theme) => alpha(theme.palette.background.paper, 0.85), boxShadow: 2 }}
|
|
|
|
|
>
|
|
|
|
|
{getOptionForDirection(
|
|
|
|
|
<SkipNextIcon
|
|
|
|
|
sx={{
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
transform: 'rotate(90deg)',
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
/>,
|
|
|
|
|
<SkipPreviousIcon
|
|
|
|
|
sx={{
|
|
|
|
|
...applyStyles(isVertical, {
|
|
|
|
|
transform: 'rotate(90deg)',
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
/>,
|
|
|
|
|
finalReaderDirection,
|
|
|
|
|
)}
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Stack>
|
|
|
|
|
</ReaderProgressBarDirectionWrapper>
|
|
|
|
|
</Slide>
|
2024-09-28 03:24:16 +02:00
|
|
|
);
|
|
|
|
|
};
|
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() }),
|
2025-01-02 21:25:35 +01:00
|
|
|
ReaderService.useSettingsWithoutDefaultFlag,
|
2024-12-26 17:38:12 +01:00
|
|
|
],
|
2024-12-28 04:06:03 +01:00
|
|
|
[
|
|
|
|
|
'previousChapter',
|
|
|
|
|
'nextChapter',
|
|
|
|
|
'isVisible',
|
|
|
|
|
'setIsMaximized',
|
|
|
|
|
'isDragging',
|
|
|
|
|
'currentPageIndex',
|
|
|
|
|
'pages',
|
|
|
|
|
'direction',
|
2025-01-02 21:25:35 +01:00
|
|
|
'progressBarPosition',
|
|
|
|
|
'progressBarPositionAutoVertical',
|
2024-12-28 04:06:03 +01:00
|
|
|
],
|
2024-12-21 03:46:55 +01:00
|
|
|
);
|