From 311ba8c04bee5476c9be89740b216bb130ef63c1 Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Thu, 2 May 2024 09:36:26 +1000 Subject: [PATCH] fix(ui): ensure canvas size is correctly updated when model changed Closes #6293 --- .../web/src/features/canvas/store/canvasSlice.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/invokeai/frontend/web/src/features/canvas/store/canvasSlice.ts b/invokeai/frontend/web/src/features/canvas/store/canvasSlice.ts index c316fee2b1..a22f23d9d3 100644 --- a/invokeai/frontend/web/src/features/canvas/store/canvasSlice.ts +++ b/invokeai/frontend/web/src/features/canvas/store/canvasSlice.ts @@ -8,6 +8,7 @@ import calculateScale from 'features/canvas/util/calculateScale'; import { STAGE_PADDING_PERCENTAGE } from 'features/canvas/util/constants'; import floorCoordinates from 'features/canvas/util/floorCoordinates'; import getScaledBoundingBoxDimensions from 'features/canvas/util/getScaledBoundingBoxDimensions'; +import { calculateNewSize } from 'features/parameters/components/ImageSize/calculateNewSize'; import { initialAspectRatioState } from 'features/parameters/components/ImageSize/constants'; import type { AspectRatioState } from 'features/parameters/components/ImageSize/types'; import { modelChanged } from 'features/parameters/store/generationSlice'; @@ -588,8 +589,9 @@ export const canvasSlice = createSlice({ }, extraReducers: (builder) => { builder.addCase(modelChanged, (state, action) => { - if (action.meta.previousModel?.base === action.payload?.base) { - // The base model hasn't changed, we don't need to optimize the size + const newModel = action.payload; + if (!newModel || action.meta.previousModel?.base === newModel.base) { + // Model was cleared or the base didn't change return; } const optimalDimension = getOptimalDimension(action.payload); @@ -597,14 +599,8 @@ export const canvasSlice = createSlice({ if (getIsSizeOptimal(width, height, optimalDimension)) { return; } - setBoundingBoxDimensionsReducer( - state, - { - width, - height, - }, - optimalDimension - ); + const newSize = calculateNewSize(state.aspectRatio.value, optimalDimension * optimalDimension); + setBoundingBoxDimensionsReducer(state, newSize, optimalDimension); }); builder.addCase(socketQueueItemStatusChanged, (state, action) => {