Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(canvas): respect disabled staging auto-switch

Josh Corbettbf761413add6
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/stagedResultController.integration.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/stagedResultController.integration.test.ts
index 83b8529b5e..5a8ee26339 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/stagedResultController.integration.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/stagedResultController.integration.test.ts
@@ -62,6 +62,7 @@ describe('staged result project-port integration', () => {
const selected = { ...candidate, placement: { ...candidate.placement, x: 90 } };
store.commands.canvas.appendStagingCandidate({ candidate: first, projectId });
store.commands.canvas.appendStagingCandidate({ candidate: selected, projectId });
+ store.commands.canvas.apply(projectId, { imageIndex: 1, type: 'setStagedImageIndex' });
const engine = createCanvasEngine({
backend: createTestStubRasterBackend(),
imageResolver: () => Promise.resolve(new Blob()),
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
index 468027c5a5..758cada970 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
@@ -1583,7 +1583,21 @@ describe('workbenchReducer Phase 5 generation flow', () => {
expect(activeProject.queue.items).toEqual([]);
});
- it('appends a workflow candidate to its explicit project and selects its resolved staging slot', () => {
+ it("keeps the selected workflow candidate when auto-switch is 'off'", () => {
+ let state = createInitialWorkbenchState();
+ const projectId = state.activeProjectId;
+ const placement = { height: 64, opacity: 1, width: 64, x: 0, y: 0 };
+ const firstCandidate = createStagingCandidate('first.png', 'layer-workflow:first', placement);
+ const secondCandidate = createStagingCandidate('second.png', 'layer-workflow:second', placement);
+
+ state = workbenchReducer(state, { candidate: firstCandidate, projectId, type: 'appendCanvasStagingCandidate' });
+ state = workbenchReducer(state, { candidate: secondCandidate, projectId, type: 'appendCanvasStagingCandidate' });
+
+ expect(getCanvas(state).stagingArea.autoSwitchMode).toBe('off');
+ expect(getCanvas(state).stagingArea.selectedImageIndex).toBe(0);
+ });
+
+ it("appends a workflow candidate to its explicit project and selects its resolved staging slot when auto-switch is 'latest'", () => {
let state = createInitialWorkbenchState();
const originProjectId = state.activeProjectId;
const firstCandidate = createStagingCandidate('first.png', 'layer-workflow:first', {
@@ -1601,6 +1615,7 @@ describe('workbenchReducer Phase 5 generation flow', () => {
y: 17,
});
+ state = workbenchReducer(state, { mode: 'latest', type: 'setCanvasStagingAutoSwitch' });
state = workbenchReducer(state, { type: 'createProject' });
const activeProjectId = state.activeProjectId;
state = workbenchReducer(state, {
@@ -1629,6 +1644,33 @@ describe('workbenchReducer Phase 5 generation flow', () => {
expect(getProject(state, activeProjectId).canvas.stagingArea.pendingImages).toEqual([]);
});
+ it("selects the exact appended workflow candidate in 'latest' mode when candidate keys are duplicated", () => {
+ let state = createInitialWorkbenchState();
+ const projectId = state.activeProjectId;
+ const firstCandidate = createStagingCandidate('duplicate.png', 'layer-workflow:duplicate', {
+ height: 64,
+ opacity: 1,
+ width: 64,
+ x: 10,
+ y: 0,
+ });
+ const latestCandidate = {
+ ...firstCandidate,
+ placement: { ...firstCandidate.placement, x: 90 },
+ };
+
+ state = workbenchReducer(state, { mode: 'latest', type: 'setCanvasStagingAutoSwitch' });
+ state = workbenchReducer(state, { candidate: firstCandidate, projectId, type: 'appendCanvasStagingCandidate' });
+ state = workbenchReducer(state, { candidate: latestCandidate, projectId, type: 'appendCanvasStagingCandidate' });
+
+ const project = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(project.canvas, project.queue.items)[
+ project.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ candidate: { placement: { x: 90 } }, kind: 'candidate' });
+ });
+
it('accepts a workflow candidate at its own placement, scale, and opacity', () => {
let state = withEmptyCanvas(createInitialWorkbenchState());
const projectId = state.activeProjectId;
@@ -1952,6 +1994,7 @@ describe('workbenchReducer Phase 5 generation flow', () => {
queueItemId: secondQueueItem.id,
type: 'routeQueueItemResults',
});
+ state = workbenchReducer(state, { imageIndex: 1, type: 'setStagedImageIndex' });
expect(
getCanvasStagingSlots(getCanvas(state), getActiveProject(state).queue.items).map((slot) => slot.kind)
@@ -1985,6 +2028,7 @@ describe('workbenchReducer Phase 5 generation flow', () => {
queueItemId: secondQueueItem.id,
type: 'routeQueueItemResults',
});
+ state = workbenchReducer(state, { imageIndex: 1, type: 'setStagedImageIndex' });
state = workbenchReducer(state, { type: 'discardSelectedStagedImage' });
expect(getActiveProject(state).canvas.stagingArea.pendingImages).toEqual([]);
@@ -2022,6 +2066,7 @@ describe('workbenchReducer Phase 5 generation flow', () => {
let state = submitGenerate(primeGenerate());
const firstQueueItem = getActiveProject(state).queue.items[0];
+ state = workbenchReducer(state, { mode: 'latest', type: 'setCanvasStagingAutoSwitch' });
state = workbenchReducer(state, {
images: [createImage('candidate-1.png', firstQueueItem.id)],
projectId: getActiveProject(state).id,
@@ -2227,6 +2272,38 @@ describe('workbenchReducer Phase 5 generation flow', () => {
expect(getActiveProject(state).queue.items.map((item) => item.status)).toEqual(['cancelled']);
});
+ it('keeps the selected staged candidate when completed queue history is cleared', () => {
+ let state = submitGenerate(primeGenerate());
+ const project = getActiveProject(state);
+ const queueItem = project.queue.items[0];
+
+ state = workbenchReducer(state, {
+ images: [createImage('completed-result.png', queueItem.id)],
+ projectId: project.id,
+ queueItemId: queueItem.id,
+ type: 'routeQueueItemResults',
+ });
+ state = workbenchReducer(state, {
+ candidate: createStagingCandidate('workflow-result.png', 'layer-workflow:result', {
+ height: 64,
+ opacity: 1,
+ width: 64,
+ x: 0,
+ y: 0,
+ }),
+ projectId: project.id,
+ type: 'appendCanvasStagingCandidate',
+ });
+ state = workbenchReducer(state, { type: 'clearCompletedQueueItems' });
+
+ const updatedProject = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(updatedProject.canvas, updatedProject.queue.items)[
+ updatedProject.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ candidate: { imageName: 'completed-result.png' }, kind: 'candidate' });
+ });
+
it('can mark stale reconciled queue items failed without creating notifications', () => {
let state = submitGenerate(primeGenerate());
const project = getActiveProject(state);
@@ -4691,6 +4768,203 @@ describe('workbenchReducer canvas staging auto-switch + canvas submission', () =
expect(getCanvas(state).stagingArea.selectedImageIndex).toBe(1);
});
+ it("'off' keeps the current candidate selected as results arrive", () => {
+ let state = submitGenerate(primeGenerate());
+
+ state = workbenchReducer(state, { mode: 'latest', type: 'setCanvasStagingAutoSwitch' });
+ state = stageResults(state, ['first.png', 'second.png']);
+ state = workbenchReducer(state, { mode: 'off', type: 'setCanvasStagingAutoSwitch' });
+ state = stageResults(state, ['first.png', 'second.png', 'third.png']);
+
+ expect(getCanvas(state).stagingArea.autoSwitchMode).toBe('off');
+ expect(getCanvas(state).stagingArea.selectedImageIndex).toBe(1);
+ });
+
+ it("'off' keeps the selected candidate when a workflow result is inserted before it", () => {
+ let state = submitGenerate(primeGenerate());
+
+ state = stageResults(state, ['generated.png']);
+ state = workbenchReducer(state, {
+ candidate: createStagingCandidate('workflow.png', 'layer-workflow:result', {
+ height: 64,
+ opacity: 1,
+ width: 64,
+ x: 0,
+ y: 0,
+ }),
+ projectId: state.activeProjectId,
+ type: 'appendCanvasStagingCandidate',
+ });
+
+ const project = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(project.canvas, project.queue.items)[
+ project.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ candidate: { imageName: 'generated.png' }, kind: 'candidate' });
+ });
+
+ it("'off' keeps the selected logical slot when its placeholder becomes a partial result", () => {
+ let state = submitGenerate(primeGenerate(undefined, { batchCount: 3 }));
+ const project = getActiveProject(state);
+ const queueItem = project.queue.items[0];
+
+ state = workbenchReducer(state, { mode: 'progress', type: 'setCanvasStagingAutoSwitch' });
+ state = workbenchReducer(state, {
+ backendItemIds: [11, 12, 13],
+ projectId: project.id,
+ queueItemId: queueItem.id,
+ type: 'markQueueItemBackendSubmitted',
+ });
+ state = workbenchReducer(state, { mode: 'off', type: 'setCanvasStagingAutoSwitch' });
+ state = workbenchReducer(state, {
+ backendItemId: 11,
+ images: [createImage('candidate-1.png', queueItem.id)],
+ projectId: project.id,
+ queueItemId: queueItem.id,
+ type: 'routeQueueItemPartialResults',
+ });
+
+ const updatedProject = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(updatedProject.canvas, updatedProject.queue.items)[
+ updatedProject.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ itemIndex: 1, kind: 'candidate' });
+ });
+
+ it("'off' keeps a later placeholder selected when an earlier partial result arrives", () => {
+ let state = submitGenerate(primeGenerate(undefined, { batchCount: 3 }));
+ const project = getActiveProject(state);
+ const queueItem = project.queue.items[0];
+
+ state = workbenchReducer(state, {
+ backendItemIds: [11, 12, 13],
+ projectId: project.id,
+ queueItemId: queueItem.id,
+ type: 'markQueueItemBackendSubmitted',
+ });
+ state = workbenchReducer(state, { imageIndex: 1, type: 'setStagedImageIndex' });
+ state = workbenchReducer(state, {
+ backendItemId: 11,
+ images: [createImage('candidate-1.png', queueItem.id)],
+ projectId: project.id,
+ queueItemId: queueItem.id,
+ type: 'routeQueueItemPartialResults',
+ });
+
+ const updatedProject = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(updatedProject.canvas, updatedProject.queue.items)[
+ updatedProject.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ itemIndex: 2, kind: 'placeholder' });
+ });
+
+ it("'off' keeps a later placeholder selected when an earlier partial has no visible images", () => {
+ let state = submitGenerate(primeGenerate(undefined, { batchCount: 3 }));
+ const project = getActiveProject(state);
+ const queueItem = project.queue.items[0];
+
+ state = workbenchReducer(state, {
+ backendItemIds: [11, 12, 13],
+ projectId: project.id,
+ queueItemId: queueItem.id,
+ type: 'markQueueItemBackendSubmitted',
+ });
+ state = workbenchReducer(state, { imageIndex: 1, type: 'setStagedImageIndex' });
+ state = workbenchReducer(state, {
+ backendItemId: 11,
+ images: [],
+ projectId: project.id,
+ queueItemId: queueItem.id,
+ type: 'routeQueueItemPartialResults',
+ });
+
+ const updatedProject = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(updatedProject.canvas, updatedProject.queue.items)[
+ updatedProject.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ itemIndex: 2, kind: 'placeholder' });
+ });
+
+ it("'off' keeps a candidate selected when an earlier final result has no visible images", () => {
+ let state = submitGenerate(primeGenerate(undefined, { batchCount: 1 }));
+ const firstProject = getActiveProject(state);
+ const firstQueueItem = firstProject.queue.items[0];
+
+ state = workbenchReducer(state, {
+ backendItemIds: [11],
+ projectId: firstProject.id,
+ queueItemId: firstQueueItem.id,
+ type: 'markQueueItemBackendSubmitted',
+ });
+ state = submitGenerate(primeGenerate(state, { batchCount: 2, positivePrompt: 'second prompt' }));
+
+ const secondProject = getActiveProject(state);
+ const secondQueueItem = secondProject.queue.items[0];
+
+ state = workbenchReducer(state, {
+ images: [createImage('candidate-1.png', secondQueueItem.id), createImage('candidate-2.png', secondQueueItem.id)],
+ projectId: secondProject.id,
+ queueItemId: secondQueueItem.id,
+ type: 'routeQueueItemResults',
+ });
+ state = workbenchReducer(state, { imageIndex: 1, type: 'setStagedImageIndex' });
+ state = workbenchReducer(state, {
+ images: [],
+ projectId: firstProject.id,
+ queueItemId: firstQueueItem.id,
+ type: 'routeQueueItemResults',
+ });
+
+ const updatedProject = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(updatedProject.canvas, updatedProject.queue.items)[
+ updatedProject.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ candidate: { imageName: 'candidate-1.png' }, kind: 'candidate' });
+ });
+
+ it("'off' keeps a candidate selected when an earlier placeholder is cancelled", () => {
+ let state = submitGenerate(primeGenerate(undefined, { batchCount: 1 }));
+ const firstProject = getActiveProject(state);
+ const firstQueueItem = firstProject.queue.items[0];
+
+ state = workbenchReducer(state, {
+ backendItemIds: [11],
+ projectId: firstProject.id,
+ queueItemId: firstQueueItem.id,
+ type: 'markQueueItemBackendSubmitted',
+ });
+ state = submitGenerate(primeGenerate(state, { batchCount: 2, positivePrompt: 'second prompt' }));
+
+ const secondProject = getActiveProject(state);
+ const secondQueueItem = secondProject.queue.items[0];
+
+ state = workbenchReducer(state, {
+ images: [createImage('candidate-1.png', secondQueueItem.id), createImage('candidate-2.png', secondQueueItem.id)],
+ projectId: secondProject.id,
+ queueItemId: secondQueueItem.id,
+ type: 'routeQueueItemResults',
+ });
+ state = workbenchReducer(state, { imageIndex: 1, type: 'setStagedImageIndex' });
+ state = workbenchReducer(state, {
+ backendItemId: 11,
+ projectId: firstProject.id,
+ queueItemId: firstQueueItem.id,
+ type: 'markQueueItemBackendCancelled',
+ });
+
+ const updatedProject = getActiveProject(state);
+ const selectedSlot = getCanvasStagingSlots(updatedProject.canvas, updatedProject.queue.items)[
+ updatedProject.canvas.stagingArea.selectedImageIndex
+ ];
+
+ expect(selectedSlot).toMatchObject({ candidate: { imageName: 'candidate-1.png' }, kind: 'candidate' });
+ });
+
it("'progress' selects the active in-progress placeholder", () => {
let state = submitGenerate(primeGenerate(undefined, { batchCount: 3 }));
const project = getActiveProject(state);
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
index 2689c6548d..966ce34794 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
@@ -142,6 +142,7 @@ import {
getCanvasStagingSlotCount,
getCanvasStagingSlots,
getFirstCanvasPlaceholderSlotIndex,
+ type CanvasStagingSlot,
} from './canvasStagingView';
import { cascadeDefaultGeometry, clampSizeToMinimum, nextStackOrder } from './floatingWindows';
import {
@@ -695,6 +696,42 @@ const getCanvasWithPendingImages = (
},
});
+const getSelectedCanvasStagingSlot = (project: Project): CanvasStagingSlot | undefined =>
+ getCanvasStagingSlots(project.canvas, project.queue.items)[project.canvas.stagingArea.selectedImageIndex];
+
+const findPreservedCanvasStagingSlotIndex = (
+ slots: CanvasStagingSlot[],
+ selectedSlot: CanvasStagingSlot | undefined
+): number => {
+ if (!selectedSlot) {
+ return -1;
+ }
+
+ if (selectedSlot.kind === 'candidate') {
+ const exactCandidateIndex = slots.findIndex(
+ (slot) => slot.kind === 'candidate' && slot.candidate === selectedSlot.candidate
+ );
+
+ if (exactCandidateIndex !== -1) {
+ return exactCandidateIndex;
+ }
+ }
+
+ const exactIdIndex = slots.findIndex((slot) => slot.id === selectedSlot.id);
+
+ if (exactIdIndex !== -1) {
+ return exactIdIndex;
+ }
+
+ if (selectedSlot.itemIndex === undefined) {
+ return -1;
+ }
+
+ return slots.findIndex(
+ (slot) => slot.queueItemId === selectedSlot.queueItemId && slot.itemIndex === selectedSlot.itemIndex
+ );
+};
+
const getCanvasStagingCandidateSlotIndex = (
project: Project,
pendingImages: CanvasStagingCandidateContract[],
@@ -704,10 +741,14 @@ const getCanvasStagingCandidateSlotIndex = (
return -1;
}
- return getCanvasStagingSlots(
- getCanvasWithPendingImages(project.canvas, pendingImages),
- project.queue.items
- ).findIndex(
+ const slots = getCanvasStagingSlots(getCanvasWithPendingImages(project.canvas, pendingImages), project.queue.items);
+ const exactIndex = slots.findIndex((slot) => slot.kind === 'candidate' && slot.candidate === target);
+
+ if (exactIndex !== -1) {
+ return exactIndex;
+ }
+
+ return slots.findIndex(
(slot) =>
slot.kind === 'candidate' &&
slot.candidate.sourceQueueItemId === target.sourceQueueItemId &&
@@ -718,11 +759,13 @@ const getCanvasStagingCandidateSlotIndex = (
const resolveStagingSelectionIndexForSlots = ({
incomingImages,
pendingImages,
+ previousSelectedSlot,
project,
slotCount,
}: {
incomingImages: CanvasStagingCandidateContract[];
pendingImages: CanvasStagingCandidateContract[];
+ previousSelectedSlot?: CanvasStagingSlot;
project: Project;
slotCount: number;
}): number => {
@@ -743,14 +786,21 @@ const resolveStagingSelectionIndexForSlots = ({
: clampStagedImageIndex(project.canvas.stagingArea.selectedImageIndex, slotCount);
}
- if (incomingImages.length === 0) {
+ if (project.canvas.stagingArea.autoSwitchMode === 'off' || incomingImages.length === 0) {
+ const selectedSlot = previousSelectedSlot ?? getSelectedCanvasStagingSlot(project);
+ const preservedSlotIndex = findPreservedCanvasStagingSlotIndex(
+ getCanvasStagingSlots(getCanvasWithPendingImages(project.canvas, pendingImages), project.queue.items),
+ selectedSlot
+ );
+
+ if (preservedSlotIndex !== -1) {
+ return preservedSlotIndex;
+ }
+
return clampStagedImageIndex(project.canvas.stagingArea.selectedImageIndex, slotCount);
}
- const selectedImage =
- project.canvas.stagingArea.autoSwitchMode === 'latest'
- ? (pendingImages[pendingImages.length - 1] ?? incomingImages[incomingImages.length - 1])
- : incomingImages[0];
+ const selectedImage = pendingImages[pendingImages.length - 1] ?? incomingImages[incomingImages.length - 1];
const selectedSlotIndex = getCanvasStagingCandidateSlotIndex(project, pendingImages, selectedImage);
return selectedSlotIndex === -1
@@ -762,7 +812,8 @@ const stageCanvasResultImages = (
project: Project,
queueItemId: string,
images: GeneratedImageContract[],
- sourceBackendItemIds?: readonly (number | undefined)[]
+ sourceBackendItemIds?: readonly (number | undefined)[],
+ previousSelectedSlot?: CanvasStagingSlot
): Project => {
const queueItem = project.queue.items.find((item) => item.id === queueItemId);
@@ -807,6 +858,7 @@ const stageCanvasResultImages = (
selectedImageIndex: resolveStagingSelectionIndexForSlots({
incomingImages: newImages,
pendingImages,
+ previousSelectedSlot,
project,
slotCount,
}),
@@ -819,11 +871,13 @@ const stageCanvasResultImages = (
const appendCanvasStagingCandidate = (project: Project, candidate: CanvasStagingCandidateContract): Project => {
const appendedCandidate = normalizeStagingCandidate(candidate, project.canvas.document);
const pendingImages = [...project.canvas.stagingArea.pendingImages, appendedCandidate];
- const candidateKey = `${appendedCandidate.sourceQueueItemId}:${appendedCandidate.imageName}`;
const slots = getCanvasStagingSlots(getCanvasWithPendingImages(project.canvas, pendingImages), project.queue.items);
- const selectedImageIndex = slots
- .map((slot) => (slot.kind === 'candidate' ? `${slot.candidate.sourceQueueItemId}:${slot.candidate.imageName}` : ''))
- .lastIndexOf(candidateKey);
+ const selectedImageIndex = resolveStagingSelectionIndexForSlots({
+ incomingImages: [appendedCandidate],
+ pendingImages,
+ project,
+ slotCount: slots.length,
+ });
return {
...project,
@@ -842,16 +896,20 @@ const appendCanvasStagingCandidate = (project: Project, candidate: CanvasStaging
};
};
-const clampCanvasStagingSelection = (project: Project): Project => {
- const slotCount = getCanvasStagingSlotCount(project.canvas, project.queue.items);
+const clampCanvasStagingSelection = (project: Project, previousSelectedSlot?: CanvasStagingSlot): Project => {
+ const slots = getCanvasStagingSlots(project.canvas, project.queue.items);
+ const slotCount = slots.length;
const placeholderIndex =
project.canvas.stagingArea.autoSwitchMode === 'progress'
? getFirstCanvasPlaceholderSlotIndex(project.canvas, project.queue.items)
: -1;
+ const preservedSlotIndex = findPreservedCanvasStagingSlotIndex(slots, previousSelectedSlot);
const selectedImageIndex =
- placeholderIndex === -1
- ? clampStagedImageIndex(project.canvas.stagingArea.selectedImageIndex, slotCount)
- : placeholderIndex;
+ placeholderIndex !== -1
+ ? placeholderIndex
+ : preservedSlotIndex !== -1
+ ? preservedSlotIndex
+ : clampStagedImageIndex(project.canvas.stagingArea.selectedImageIndex, slotCount);
const isVisible = slotCount > 0 ? project.canvas.stagingArea.isVisible : false;
if (
@@ -2825,6 +2883,7 @@ const routeQueueItemPartialResults = (
): Project => {
const queueItem = project.queue.items.find((item) => item.id === queueItemId);
const destination = queueItem?.snapshot.destination ?? project.invocation.destination;
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
const nextProject = updateQueueItem(project, queueItemId, (item) => ({
...item,
completedBackendItemIds: item.completedBackendItemIds?.includes(backendItemId)
@@ -2837,12 +2896,17 @@ const routeQueueItemPartialResults = (
return updateGalleryWithResultImages(nextProject, images);
}
+ if (images.length === 0) {
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ }
+
return clampCanvasStagingSelection(
stageCanvasResultImages(
nextProject,
queueItemId,
images,
- images.map(() => backendItemId)
+ images.map(() => backendItemId),
+ previousSelectedSlot
)
);
};
@@ -2850,6 +2914,7 @@ const routeQueueItemPartialResults = (
const routeQueueItemResults = (project: Project, queueItemId: string, images: GeneratedImageContract[]): Project => {
const queueItem = project.queue.items.find((item) => item.id === queueItemId);
const destination = queueItem?.snapshot.destination ?? project.invocation.destination;
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
const nextProject = updateQueueItem(project, queueItemId, (item) => ({
...item,
completedBackendItemIds: item.backendItemIds
@@ -2874,11 +2939,15 @@ const routeQueueItemResults = (project: Project, queueItemId: string, images: Ge
return nextProject;
}
+ if (images.length === 0) {
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ }
+
const sourceBackendItemIds = queueItem?.backendItemIds?.filter(
(backendItemId) => !queueItem.cancelledBackendItemIds?.includes(backendItemId)
);
- return stageCanvasResultImages(nextProject, queueItemId, images, sourceBackendItemIds);
+ return stageCanvasResultImages(nextProject, queueItemId, images, sourceBackendItemIds, previousSelectedSlot);
};
/**
@@ -3983,20 +4052,21 @@ export const __workbenchReducerInternal = (
);
}
case 'markQueueItemBackendSubmitted': {
- return updateProjectById(state, action.projectId, (project) =>
- clampCanvasStagingSelection(
- updateQueueItem(project, action.queueItemId, (item) => {
- const status = item.status === 'cancelled' ? 'cancelled' : 'running';
- const hasSameBackendItemIds =
- item.backendItemIds?.length === action.backendItemIds.length &&
- item.backendItemIds.every((id, index) => id === action.backendItemIds[index]);
-
- return item.backendBatchId === action.backendBatchId && hasSameBackendItemIds && item.status === status
- ? item
- : { ...item, backendBatchId: action.backendBatchId, backendItemIds: action.backendItemIds, status };
- })
- )
- );
+ return updateProjectById(state, action.projectId, (project) => {
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
+ const nextProject = updateQueueItem(project, action.queueItemId, (item) => {
+ const status = item.status === 'cancelled' ? 'cancelled' : 'running';
+ const hasSameBackendItemIds =
+ item.backendItemIds?.length === action.backendItemIds.length &&
+ item.backendItemIds.every((id, index) => id === action.backendItemIds[index]);
+
+ return item.backendBatchId === action.backendBatchId && hasSameBackendItemIds && item.status === status
+ ? item
+ : { ...item, backendBatchId: action.backendBatchId, backendItemIds: action.backendItemIds, status };
+ });
+
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ });
}
case 'setQueueItemStatus': {
const project = state.projects.find((project) => project.id === action.projectId);
@@ -4010,15 +4080,16 @@ export const __workbenchReducerInternal = (
return state;
}
- const nextState = updateProjectById(state, action.projectId, (project) =>
- clampCanvasStagingSelection(
- updateQueueItem(project, action.queueItemId, (item) => ({
- ...item,
- error: action.error,
- status: action.status,
- }))
- )
- );
+ const nextState = updateProjectById(state, action.projectId, (project) => {
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
+ const nextProject = updateQueueItem(project, action.queueItemId, (item) => ({
+ ...item,
+ error: action.error,
+ status: action.status,
+ }));
+
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ });
if (action.notify === false || (action.status !== 'failed' && action.status !== 'cancelled')) {
return nextState;
@@ -4055,6 +4126,7 @@ export const __workbenchReducerInternal = (
}
return updateProjectById(state, action.projectId, (project) => {
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
const nextProject = updateQueueItem(project, action.queueItemId, (item) => {
const cancelledBackendItemIds = mergeBackendItemId(item.cancelledBackendItemIds, action.backendItemId);
@@ -4065,7 +4137,7 @@ export const __workbenchReducerInternal = (
};
});
- return clampCanvasStagingSelection(nextProject);
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
});
}
case 'routeQueueItemResults': {
@@ -4383,8 +4455,9 @@ export const __workbenchReducerInternal = (
const targetProject = state.projects.find((project) => project.id === targetProjectId);
const queueItem = targetProject?.queue.items.find((item) => item.id === action.queueItemId);
const canCancelQueueItem = queueItem ? isCancellableQueueItem(queueItem) : false;
- const nextState = updateProjectById(state, targetProjectId, (project) =>
- clampCanvasStagingSelection({
+ const nextState = updateProjectById(state, targetProjectId, (project) => {
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
+ const nextProject: Project = {
...project,
queue: {
items: project.queue.items.map((item) => {
@@ -4395,8 +4468,10 @@ export const __workbenchReducerInternal = (
return { ...item, status: 'cancelled' };
}),
},
- })
- );
+ };
+
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ });
if (!targetProject || !queueItem || !canCancelQueueItem) {
return nextState;
@@ -4427,8 +4502,9 @@ export const __workbenchReducerInternal = (
const nextState: WorkbenchState = {
...state,
- projects: state.projects.map((project) =>
- clampCanvasStagingSelection({
+ projects: state.projects.map((project) => {
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
+ const nextProject: Project = {
...project,
queue: {
items: shouldApplyQueueBulkActionToProject(project, action.projectId)
@@ -4437,8 +4513,10 @@ export const __workbenchReducerInternal = (
)
: project.queue.items,
},
- })
- ),
+ };
+
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ }),
};
return addNotification(
@@ -4468,8 +4546,9 @@ export const __workbenchReducerInternal = (
const nextState: WorkbenchState = {
...state,
- projects: state.projects.map((project) =>
- clampCanvasStagingSelection({
+ projects: state.projects.map((project) => {
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
+ const nextProject: Project = {
...project,
queue: {
items: shouldApplyQueueBulkActionToProject(project, action.projectId)
@@ -4480,8 +4559,10 @@ export const __workbenchReducerInternal = (
)
: project.queue.items,
},
- })
- ),
+ };
+
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ }),
};
return addNotification(
@@ -4496,10 +4577,15 @@ export const __workbenchReducerInternal = (
case 'clearCompletedQueueItems': {
return {
...state,
- projects: state.projects.map((project) => ({
- ...project,
- queue: { items: project.queue.items.filter((item) => !isClearableQueueItem(item)) },
- })),
+ projects: state.projects.map((project) => {
+ const previousSelectedSlot = getSelectedCanvasStagingSlot(project);
+ const nextProject: Project = {
+ ...project,
+ queue: { items: project.queue.items.filter((item) => !isClearableQueueItem(item)) },
+ };
+
+ return clampCanvasStagingSelection(nextProject, previousSelectedSlot);
+ }),
};
}
case 'undoProjectChange': {