Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds double-click to reset view to 100% #2436

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions frontend/src/common/hooks/useSingleAndDoubleClick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// https://stackoverflow.com/a/73731908

import { useEffect, useState } from 'react';

export function useSingleAndDoubleClick(
handleSingleClick: () => void,
handleDoubleClick: () => void,
delay = 250
) {
const [click, setClick] = useState(0);

useEffect(() => {
const timer = setTimeout(() => {
if (click === 1) {
handleSingleClick();
}
setClick(0);
}, delay);

if (click === 2) {
handleDoubleClick();
}

return () => clearTimeout(timer);
}, [click, handleSingleClick, handleDoubleClick, delay]);

return () => setClick((prev) => prev + 1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
} from 'features/canvas/store/canvasTypes';
import { ChangeEvent } from 'react';
import { useTranslation } from 'react-i18next';
import { useSingleAndDoubleClick } from 'common/hooks/useSingleAndDoubleClick';

export const selector = createSelector(
[systemSelector, canvasSelector, isStagingSelector],
Expand Down Expand Up @@ -156,7 +157,12 @@ const IAICanvasOutpaintingControls = () => {

const handleSelectMoveTool = () => dispatch(setTool('move'));

const handleResetCanvasView = () => {
const handleClickResetCanvasView = useSingleAndDoubleClick(
() => handleResetCanvasView(false),
() => handleResetCanvasView(true)
);

const handleResetCanvasView = (shouldScaleTo1 = false) => {
const canvasBaseLayer = getCanvasBaseLayer();
if (!canvasBaseLayer) return;
const clientRect = canvasBaseLayer.getClientRect({
Expand All @@ -165,6 +171,7 @@ const IAICanvasOutpaintingControls = () => {
dispatch(
resetCanvasView({
contentRect: clientRect,
shouldScaleTo1,
})
);
};
Expand Down Expand Up @@ -247,7 +254,7 @@ const IAICanvasOutpaintingControls = () => {
aria-label={`${t('unifiedcanvas:resetView')} (R)`}
tooltip={`${t('unifiedcanvas:resetView')} (R)`}
icon={<FaCrosshairs />}
onClick={handleResetCanvasView}
onClick={handleClickResetCanvasView}
/>
</ButtonGroup>

Expand Down
19 changes: 11 additions & 8 deletions frontend/src/features/canvas/store/canvasSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,23 +602,26 @@ export const canvasSlice = createSlice({
state,
action: PayloadAction<{
contentRect: IRect;
shouldScaleTo1?: boolean;
}>
) => {
const { contentRect } = action.payload;
const { contentRect, shouldScaleTo1 } = action.payload;
const {
stageDimensions: { width: stageWidth, height: stageHeight },
} = state;

const { x, y, width, height } = contentRect;

if (width !== 0 && height !== 0) {
const newScale = calculateScale(
stageWidth,
stageHeight,
width,
height,
STAGE_PADDING_PERCENTAGE
);
const newScale = shouldScaleTo1
? 1
: calculateScale(
stageWidth,
stageHeight,
width,
height,
STAGE_PADDING_PERCENTAGE
);

const newCoordinates = calculateCoordinates(
stageWidth,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useAppDispatch } from 'app/storeHooks';
import IAIIconButton from 'common/components/IAIIconButton';
import { useSingleAndDoubleClick } from 'common/hooks/useSingleAndDoubleClick';
import { resetCanvasView } from 'features/canvas/store/canvasSlice';
import { getCanvasBaseLayer } from 'features/canvas/util/konvaInstanceProvider';
import React from 'react';
Expand All @@ -24,7 +25,12 @@ export default function UnifiedCanvasResetView() {
[canvasBaseLayer]
);

const handleResetCanvasView = () => {
const handleClickResetCanvasView = useSingleAndDoubleClick(
() => handleResetCanvasView(false),
() => handleResetCanvasView(true)
);

const handleResetCanvasView = (shouldScaleTo1 = false) => {
const canvasBaseLayer = getCanvasBaseLayer();
if (!canvasBaseLayer) return;
const clientRect = canvasBaseLayer.getClientRect({
Expand All @@ -33,6 +39,7 @@ export default function UnifiedCanvasResetView() {
dispatch(
resetCanvasView({
contentRect: clientRect,
shouldScaleTo1,
})
);
};
Expand All @@ -41,7 +48,7 @@ export default function UnifiedCanvasResetView() {
aria-label={`${t('unifiedcanvas:resetView')} (R)`}
tooltip={`${t('unifiedcanvas:resetView')} (R)`}
icon={<FaCrosshairs />}
onClick={handleResetCanvasView}
onClick={handleClickResetCanvasView}
/>
);
}