Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { isAnyOf } from '@reduxjs/toolkit';
import { logger } from 'app/logging/logger';
import type { AppStartListening } from 'app/store/middleware/listenerMiddleware';
import { canvasBatchIdsReset, commitStagingAreaImage, discardStagedImages } from 'features/canvas/store/canvasSlice';
import {
canvasBatchIdsReset,
commitStagingAreaImage,
discardStagedImages,
resetCanvas,
setInitialCanvasImage,
} from 'features/canvas/store/canvasSlice';
import { addToast } from 'features/system/store/systemSlice';
import { t } from 'i18next';
import { queueApi } from 'services/api/endpoints/queue';

const matcher = isAnyOf(commitStagingAreaImage, discardStagedImages);
const matcher = isAnyOf(commitStagingAreaImage, discardStagedImages, resetCanvas, setInitialCanvasImage);

export const addCommitStagingAreaImageListener = (startAppListening: AppStartListening) => {
startAppListening({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@ const selector = createMemoizedSelector(selectCanvasSlice, (canvas) => {
const ClearStagingIntermediatesIconButton = () => {
const dispatch = useAppDispatch();
const { t } = useTranslation();
const totalStagedImages = useAppSelector((s) => s.canvas.layerState.stagingArea.images.length);

const handleDiscardStagingArea = useCallback(() => {
dispatch(discardStagedImages());
}, [dispatch]);

const handleDiscardStagingImage = useCallback(() => {
dispatch(discardStagedImage());
}, [dispatch]);
// Discarding all staged images triggers cancelation of all canvas batches. It's too easy to accidentally
// click the discard button, so to prevent accidental cancelation of all batches, we only discard the current
// image if there are more than one staged images.
if (totalStagedImages > 1) {
dispatch(discardStagedImage());
}
}, [dispatch, totalStagedImages]);

return (
<>
Expand All @@ -67,6 +73,7 @@ const ClearStagingIntermediatesIconButton = () => {
onClick={handleDiscardStagingImage}
colorScheme="invokeBlue"
fontSize={16}
isDisabled={totalStagedImages <= 1}
/>
<IconButton
tooltip={`${t('unifiedCanvas.discardAll')} (Esc)`}
Expand Down
58 changes: 23 additions & 35 deletions invokeai/frontend/web/src/features/canvas/store/canvasSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ export const canvasSlice = createSlice({
],
};
state.futureLayerStates = [];
state.batchIds = [];

const newScale = calculateScale(
stageDimensions.width,
Expand Down Expand Up @@ -286,40 +285,14 @@ export const canvasSlice = createSlice({
},
discardStagedImages: (state) => {
pushToPrevLayerStates(state);

state.layerState.stagingArea = deepClone(initialLayerState.stagingArea);

resetStagingArea(state);
state.futureLayerStates = [];
state.shouldShowStagingOutline = true;
state.shouldShowStagingImage = true;
state.batchIds = [];
},
discardStagedImage: (state) => {
const { images, selectedImageIndex } = state.layerState.stagingArea;
pushToPrevLayerStates(state);

images.splice(selectedImageIndex, 1);

if (images.length === 0) {
pushToPrevLayerStates(state);

state.layerState.stagingArea = deepClone(initialLayerState.stagingArea);

state.futureLayerStates = [];
state.shouldShowStagingOutline = true;
state.shouldShowStagingImage = true;
state.batchIds = [];
}

if (selectedImageIndex >= images.length) {
state.layerState.stagingArea.selectedImageIndex = images.length - 1;
}

if (!images.length) {
state.shouldShowStagingImage = false;
state.shouldShowStagingOutline = false;
}

state.layerState.stagingArea.selectedImageIndex = Math.max(0, images.length - 1);
state.futureLayerStates = [];
},
addFillRect: (state) => {
Expand Down Expand Up @@ -433,7 +406,6 @@ export const canvasSlice = createSlice({
pushToPrevLayerStates(state);
state.layerState = deepClone(initialLayerState);
state.futureLayerStates = [];
state.batchIds = [];
state.boundingBoxCoordinates = {
...initialCanvasState.boundingBoxCoordinates,
};
Expand Down Expand Up @@ -534,12 +506,9 @@ export const canvasSlice = createSlice({
...imageToCommit,
});
}
state.layerState.stagingArea = deepClone(initialLayerState.stagingArea);

resetStagingArea(state);
state.futureLayerStates = [];
state.shouldShowStagingOutline = true;
state.shouldShowStagingImage = true;
state.batchIds = [];
},
setBoundingBoxScaleMethod: {
reducer: (state, action: PayloadActionWithOptimalDimension<BoundingBoxScaleMethod>) => {
Expand Down Expand Up @@ -647,12 +616,19 @@ export const canvasSlice = createSlice({
if (batch_status.in_progress === 0 && batch_status.pending === 0) {
state.batchIds = state.batchIds.filter((id) => id !== batch_status.batch_id);
}

const queueItemStatus = action.payload.data.queue_item.status;
if (queueItemStatus === 'canceled' || queueItemStatus === 'failed') {
resetStagingAreaIfEmpty(state);
}
});
builder.addMatcher(queueApi.endpoints.clearQueue.matchFulfilled, (state) => {
state.batchIds = [];
resetStagingAreaIfEmpty(state);
});
builder.addMatcher(queueApi.endpoints.cancelByBatchIds.matchFulfilled, (state, action) => {
state.batchIds = state.batchIds.filter((id) => !action.meta.arg.originalArgs.batch_ids.includes(id));
resetStagingAreaIfEmpty(state);
});
},
});
Expand Down Expand Up @@ -726,7 +702,7 @@ export const canvasPersistConfig: PersistConfig<CanvasState> = {
name: canvasSlice.name,
initialState: initialCanvasState,
migrate: migrateCanvasState,
persistDenylist: [],
persistDenylist: ['shouldShowStagingImage', 'shouldShowStagingOutline'],
};

const pushToPrevLayerStates = (state: CanvasState) => {
Expand All @@ -742,3 +718,15 @@ const pushToFutureLayerStates = (state: CanvasState) => {
state.futureLayerStates = state.futureLayerStates.slice(0, MAX_HISTORY);
}
};

const resetStagingAreaIfEmpty = (state: CanvasState) => {
if (state.batchIds.length === 0 && state.layerState.stagingArea.images.length === 0) {
resetStagingArea(state);
}
};

const resetStagingArea = (state: CanvasState) => {
state.layerState.stagingArea = { ...initialCanvasState.layerState.stagingArea };
state.shouldShowStagingImage = initialCanvasState.shouldShowStagingImage;
state.shouldShowStagingOutline = initialCanvasState.shouldShowStagingOutline;
};