@@ -2,11 +2,8 @@ import { createSelector } from '@reduxjs/toolkit';
22import { createMemoizedSelector } from 'app/store/createMemoizedSelector' ;
33import { useAppDispatch , useAppSelector } from 'app/store/storeHooks' ;
44import { deepClone } from 'common/util/deepClone' ;
5- import { CanvasEntityAdapterBase } from 'features/controlLayers/konva/CanvasEntity/CanvasEntityAdapterBase' ;
65import { getPrefixedId } from 'features/controlLayers/konva/util' ;
7- import { canvasReset } from 'features/controlLayers/store/actions' ;
86import {
9- bboxChangedFromCanvas ,
107 controlLayerAdded ,
118 inpaintMaskAdded ,
129 rasterLayerAdded ,
@@ -17,37 +14,20 @@ import {
1714 rgPositivePromptChanged ,
1815} from 'features/controlLayers/store/canvasSlice' ;
1916import { selectBase } from 'features/controlLayers/store/paramsSlice' ;
20- import {
21- selectBboxModelBase ,
22- selectBboxRect ,
23- selectCanvasSlice ,
24- selectEntityOrThrow ,
25- } from 'features/controlLayers/store/selectors' ;
17+ import { selectCanvasSlice , selectEntityOrThrow } from 'features/controlLayers/store/selectors' ;
2618import type {
27- CanvasControlLayerState ,
2819 CanvasEntityIdentifier ,
29- CanvasInpaintMaskState ,
30- CanvasRasterLayerState ,
3120 CanvasRegionalGuidanceState ,
3221 ControlNetConfig ,
3322 IPAdapterConfig ,
3423 T2IAdapterConfig ,
3524} from 'features/controlLayers/store/types' ;
36- import {
37- imageDTOToImageObject ,
38- initialControlNet ,
39- initialIPAdapter ,
40- initialT2IAdapter ,
41- } from 'features/controlLayers/store/util' ;
42- import { calculateNewSize } from 'features/controlLayers/util/getScaledBoundingBoxDimensions' ;
25+ import { initialControlNet , initialIPAdapter , initialT2IAdapter } from 'features/controlLayers/store/util' ;
4326import { zModelIdentifierField } from 'features/nodes/types/common' ;
44- import { getOptimalDimension } from 'features/parameters/util/optimalDimension' ;
4527import { useCallback } from 'react' ;
4628import { modelConfigsAdapterSelectors , selectModelConfigsQuery } from 'services/api/endpoints/models' ;
47- import type { ControlNetModelConfig , ImageDTO , IPAdapterModelConfig , T2IAdapterModelConfig } from 'services/api/types' ;
29+ import type { ControlNetModelConfig , IPAdapterModelConfig , T2IAdapterModelConfig } from 'services/api/types' ;
4830import { isControlNetOrT2IAdapterModelConfig , isIPAdapterModelConfig } from 'services/api/types' ;
49- import type { Equals } from 'tsafe' ;
50- import { assert } from 'tsafe' ;
5131
5232/** @knipignore */
5333export const selectDefaultControlAdapter = createSelector (
@@ -110,150 +90,6 @@ export const useAddRasterLayer = () => {
11090 return func ;
11191} ;
11292
113- export const useNewRasterLayerFromImage = ( ) => {
114- const dispatch = useAppDispatch ( ) ;
115- const bboxRect = useAppSelector ( selectBboxRect ) ;
116- const func = useCallback (
117- ( imageDTO : ImageDTO ) => {
118- const imageObject = imageDTOToImageObject ( imageDTO ) ;
119- const overrides : Partial < CanvasRasterLayerState > = {
120- position : { x : bboxRect . x , y : bboxRect . y } ,
121- objects : [ imageObject ] ,
122- } ;
123- dispatch ( rasterLayerAdded ( { overrides, isSelected : true } ) ) ;
124- } ,
125- [ bboxRect . x , bboxRect . y , dispatch ]
126- ) ;
127-
128- return func ;
129- } ;
130-
131- export const useNewControlLayerFromImage = ( ) => {
132- const dispatch = useAppDispatch ( ) ;
133- const bboxRect = useAppSelector ( selectBboxRect ) ;
134- const func = useCallback (
135- ( imageDTO : ImageDTO ) => {
136- const imageObject = imageDTOToImageObject ( imageDTO ) ;
137- const overrides : Partial < CanvasControlLayerState > = {
138- position : { x : bboxRect . x , y : bboxRect . y } ,
139- objects : [ imageObject ] ,
140- } ;
141- dispatch ( controlLayerAdded ( { overrides, isSelected : true } ) ) ;
142- } ,
143- [ bboxRect . x , bboxRect . y , dispatch ]
144- ) ;
145-
146- return func ;
147- } ;
148-
149- export const useNewInpaintMaskFromImage = ( ) => {
150- const dispatch = useAppDispatch ( ) ;
151- const bboxRect = useAppSelector ( selectBboxRect ) ;
152- const func = useCallback (
153- ( imageDTO : ImageDTO ) => {
154- const imageObject = imageDTOToImageObject ( imageDTO ) ;
155- const overrides : Partial < CanvasInpaintMaskState > = {
156- position : { x : bboxRect . x , y : bboxRect . y } ,
157- objects : [ imageObject ] ,
158- } ;
159- dispatch ( inpaintMaskAdded ( { overrides, isSelected : true } ) ) ;
160- } ,
161- [ bboxRect . x , bboxRect . y , dispatch ]
162- ) ;
163-
164- return func ;
165- } ;
166-
167- export const useNewRegionalGuidanceFromImage = ( ) => {
168- const dispatch = useAppDispatch ( ) ;
169- const bboxRect = useAppSelector ( selectBboxRect ) ;
170- const func = useCallback (
171- ( imageDTO : ImageDTO ) => {
172- const imageObject = imageDTOToImageObject ( imageDTO ) ;
173- const overrides : Partial < CanvasRegionalGuidanceState > = {
174- position : { x : bboxRect . x , y : bboxRect . y } ,
175- objects : [ imageObject ] ,
176- } ;
177- dispatch ( rgAdded ( { overrides, isSelected : true } ) ) ;
178- } ,
179- [ bboxRect . x , bboxRect . y , dispatch ]
180- ) ;
181-
182- return func ;
183- } ;
184-
185- /**
186- * Returns a function that adds a new canvas with the given image as the initial image, replicating the img2img flow:
187- * - Reset the canvas
188- * - Resize the bbox to the image's aspect ratio at the optimal size for the selected model
189- * - Add the image as a raster layer
190- * - Resizes the layer to fit the bbox using the 'fill' strategy
191- *
192- * This allows the user to immediately generate a new image from the given image without any additional steps.
193- */
194- export const useNewCanvasFromImage = ( ) => {
195- const dispatch = useAppDispatch ( ) ;
196- const bboxRect = useAppSelector ( selectBboxRect ) ;
197- const base = useAppSelector ( selectBboxModelBase ) ;
198- const func = useCallback (
199- ( imageDTO : ImageDTO , type : CanvasRasterLayerState [ 'type' ] | CanvasControlLayerState [ 'type' ] ) => {
200- // Calculate the new bbox dimensions to fit the image's aspect ratio at the optimal size
201- const ratio = imageDTO . width / imageDTO . height ;
202- const optimalDimension = getOptimalDimension ( base ) ;
203- const { width, height } = calculateNewSize ( ratio , optimalDimension ** 2 , base ) ;
204-
205- // The overrides need to include the layer's ID so we can transform the layer it is initialized
206- let overrides : Partial < CanvasRasterLayerState > | Partial < CanvasControlLayerState > ;
207-
208- if ( type === 'raster_layer' ) {
209- overrides = {
210- id : getPrefixedId ( 'raster_layer' ) ,
211- position : { x : bboxRect . x , y : bboxRect . y } ,
212- objects : [ imageDTOToImageObject ( imageDTO ) ] ,
213- } satisfies Partial < CanvasRasterLayerState > ;
214- } else if ( type === 'control_layer' ) {
215- overrides = {
216- id : getPrefixedId ( 'control_layer' ) ,
217- position : { x : bboxRect . x , y : bboxRect . y } ,
218- objects : [ imageDTOToImageObject ( imageDTO ) ] ,
219- } satisfies Partial < CanvasControlLayerState > ;
220- } else {
221- // Catch unhandled types
222- assert < Equals < typeof type , never > > ( false ) ;
223- }
224-
225- CanvasEntityAdapterBase . registerInitCallback ( async ( adapter ) => {
226- // Skip the callback if the adapter is not the one we are creating
227- if ( adapter . id !== overrides . id ) {
228- return false ;
229- }
230- // Fit the layer to the bbox w/ fill strategy
231- await adapter . transformer . startTransform ( { silent : true } ) ;
232- adapter . transformer . fitToBboxFill ( ) ;
233- await adapter . transformer . applyTransform ( ) ;
234- return true ;
235- } ) ;
236-
237- dispatch ( canvasReset ( ) ) ;
238- // The `bboxChangedFromCanvas` reducer does no validation! Careful!
239- dispatch ( bboxChangedFromCanvas ( { x : 0 , y : 0 , width, height } ) ) ;
240-
241- // The type casts are safe because the type is checked above
242- if ( type === 'raster_layer' ) {
243- dispatch ( rasterLayerAdded ( { overrides : overrides as Partial < CanvasRasterLayerState > , isSelected : true } ) ) ;
244- } else if ( type === 'control_layer' ) {
245- dispatch ( controlLayerAdded ( { overrides : overrides as Partial < CanvasControlLayerState > , isSelected : true } ) ) ;
246- } else {
247- // Catch unhandled types
248- assert < Equals < typeof type , never > > ( false ) ;
249- }
250- } ,
251- [ base , bboxRect . x , bboxRect . y , dispatch ]
252- ) ;
253-
254- return func ;
255- } ;
256-
25793export const useAddInpaintMask = ( ) => {
25894 const dispatch = useAppDispatch ( ) ;
25995 const func = useCallback ( ( ) => {
0 commit comments