2024-09-26 17:02:44 +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 Box from '@mui/material/Box';
|
|
|
|
|
import { useCallback, useLayoutEffect, useState } from 'react';
|
|
|
|
|
import { useTheme } from '@mui/material/styles';
|
2025-08-16 02:02:13 +02:00
|
|
|
import { ReaderTapZoneService } from '@/features/reader/tap-zones/ReaderTapZoneService.ts';
|
2025-08-16 02:14:49 +02:00
|
|
|
import { useNavBarContext } from '@/features/navigation-bar/NavbarContext.tsx';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { useResizeObserver } from '@/base/hooks/useResizeObserver.tsx';
|
2025-09-20 14:15:40 +02:00
|
|
|
import { ReadingDirection } from '@/features/reader/Reader.types.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { withPropsFrom } from '@/base/hoc/withPropsFrom.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { NavbarContextType } from '@/features/navigation-bar/NavigationBar.types.ts';
|
2025-09-20 17:53:56 +02:00
|
|
|
import { useReaderStore, useReaderStoreShallow } from '@/features/reader/stores/ReaderStore.ts';
|
2024-09-26 17:02:44 +02:00
|
|
|
|
|
|
|
|
const CANVAS_ID = 'reader-tap-zone-layout-canvas';
|
|
|
|
|
|
2025-09-20 14:41:05 +02:00
|
|
|
const BaseTapZoneLayout = ({ readerNavBarWidth }: Pick<NavbarContextType, 'readerNavBarWidth'>) => {
|
2024-09-26 17:02:44 +02:00
|
|
|
const theme = useTheme();
|
2025-09-20 14:15:40 +02:00
|
|
|
const { tapZoneLayout, tapZoneInvertMode, readingDirection } = useReaderStoreShallow((state) => ({
|
|
|
|
|
tapZoneLayout: state.settings.tapZoneLayout.value,
|
|
|
|
|
tapZoneInvertMode: state.settings.tapZoneInvertMode.value,
|
|
|
|
|
readingDirection: state.settings.readingDirection.value,
|
|
|
|
|
}));
|
2025-09-20 14:41:05 +02:00
|
|
|
const showPreview = useReaderStore((state) => state.tapZone.showPreview);
|
2024-09-26 17:02:44 +02:00
|
|
|
|
|
|
|
|
const [width, setWidth] = useState(0);
|
|
|
|
|
const [height, setHeight] = useState(0);
|
|
|
|
|
const [tapZoneLayoutElement, setTapZoneLayoutElement] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
useResizeObserver(
|
|
|
|
|
tapZoneLayoutElement,
|
|
|
|
|
useCallback(() => {
|
|
|
|
|
setWidth(tapZoneLayoutElement?.clientWidth ?? 0);
|
|
|
|
|
setHeight(tapZoneLayoutElement?.clientHeight ?? 0);
|
|
|
|
|
}, [tapZoneLayoutElement]),
|
|
|
|
|
);
|
|
|
|
|
|
2024-12-21 03:46:55 +01:00
|
|
|
const canvas = ReaderTapZoneService.getOrCreateCanvas(tapZoneLayout, width, height, theme.typography.h3, {
|
|
|
|
|
vertical: tapZoneInvertMode.vertical,
|
|
|
|
|
horizontal: tapZoneInvertMode.horizontal,
|
|
|
|
|
isRTL: readingDirection === ReadingDirection.RTL,
|
2024-09-26 17:02:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const canvasContainerElement = document.getElementById(CANVAS_ID);
|
|
|
|
|
canvasContainerElement?.replaceChildren(canvas);
|
2024-12-30 16:32:30 +01:00
|
|
|
}, [canvas, showPreview]);
|
2024-09-26 17:02:44 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
ref={setTapZoneLayoutElement}
|
|
|
|
|
sx={{
|
|
|
|
|
position: 'fixed',
|
|
|
|
|
top: 0,
|
|
|
|
|
left: readerNavBarWidth,
|
|
|
|
|
right: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
pointerEvents: 'none',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{showPreview && <div id={CANVAS_ID} />}
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-12-21 03:46:55 +01:00
|
|
|
|
2025-09-20 14:41:05 +02:00
|
|
|
export const TapZoneLayout = withPropsFrom(BaseTapZoneLayout, [useNavBarContext], ['readerNavBarWidth']);
|