-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathImageViewer.tsx
113 lines (97 loc) · 3.63 KB
/
ImageViewer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Box, Flex, IconButton } from '@invoke-ai/ui-library';
import { useAppSelector } from 'app/store/storeHooks';
import { useFocusRegion, useIsRegionFocused } from 'common/hooks/focus';
import { useAssertSingleton } from 'common/hooks/useAssertSingleton';
import { CanvasAlertsSendingToCanvas } from 'features/controlLayers/components/CanvasAlerts/CanvasAlertsSendingTo';
import { CompareToolbar } from 'features/gallery/components/ImageViewer/CompareToolbar';
import CurrentImagePreview from 'features/gallery/components/ImageViewer/CurrentImagePreview';
import { ImageComparison } from 'features/gallery/components/ImageViewer/ImageComparison';
import { ImageComparisonDroppable } from 'features/gallery/components/ImageViewer/ImageComparisonDroppable';
import { ViewerToolbar } from 'features/gallery/components/ImageViewer/ViewerToolbar';
import { selectHasImageToCompare } from 'features/gallery/store/gallerySelectors';
import type { ReactNode } from 'react';
import { memo, useRef } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import { useTranslation } from 'react-i18next';
import { PiXBold } from 'react-icons/pi';
import { useMeasure } from 'react-use';
import { useImageViewer } from './useImageViewer';
type Props = {
closeButton?: ReactNode;
};
const useFocusRegionOptions = {
focusOnMount: true,
};
export const ImageViewer = memo(({ closeButton }: Props) => {
useAssertSingleton('ImageViewer');
const hasImageToCompare = useAppSelector(selectHasImageToCompare);
const [containerRef, containerDims] = useMeasure<HTMLDivElement>();
const ref = useRef<HTMLDivElement>(null);
useFocusRegion('viewer', ref, useFocusRegionOptions);
const isRegionFocused = useIsRegionFocused('viewer');
return (
<Flex
ref={ref}
tabIndex={-1}
layerStyle="first"
p={2}
borderRadius="base"
borderWidth={1}
borderColor={isRegionFocused ? 'blue.300' : 'transparent'}
position="absolute"
flexDirection="column"
top={0}
right={0}
bottom={0}
left={0}
rowGap={4}
alignItems="center"
justifyContent="center"
transition="border-color 0.1s"
>
{hasImageToCompare && <CompareToolbar />}
{!hasImageToCompare && <ViewerToolbar closeButton={closeButton} />}
<Box ref={containerRef} w="full" h="full">
{!hasImageToCompare && <CurrentImagePreview />}
{hasImageToCompare && <ImageComparison containerDims={containerDims} />}
</Box>
<ImageComparisonDroppable />
<Box position="absolute" top={14} insetInlineStart={2}>
<CanvasAlertsSendingToCanvas />
</Box>
</Flex>
);
});
ImageViewer.displayName = 'ImageViewer';
export const GatedImageViewer = memo(() => {
const imageViewer = useImageViewer();
if (!imageViewer.isOpen) {
return null;
}
return <ImageViewer closeButton={<ImageViewerCloseButton />} />;
});
GatedImageViewer.displayName = 'GatedImageViewer';
const ImageViewerCloseButton = memo(() => {
const { t } = useTranslation();
const imageViewer = useImageViewer();
useAssertSingleton('ImageViewerCloseButton');
useHotkeys('esc', imageViewer.close);
return (
<IconButton
tooltip={t('gallery.closeViewer')}
aria-label={t('gallery.closeViewer')}
icon={<PiXBold />}
variant="ghost"
onClick={imageViewer.close}
/>
);
});
ImageViewerCloseButton.displayName = 'ImageViewerCloseButton';
const GatedImageViewerCloseButton = memo(() => {
const imageViewer = useImageViewer();
if (!imageViewer.isOpen) {
return null;
}
return <ImageViewerCloseButton />;
});
GatedImageViewerCloseButton.displayName = 'GatedImageViewerCloseButton';