@@ -46,6 +46,8 @@ import { useCallback } from 'react';
4646import { modelConfigsAdapterSelectors , selectModelConfigsQuery } from 'services/api/endpoints/models' ;
4747import type { ControlNetModelConfig , ImageDTO , IPAdapterModelConfig , T2IAdapterModelConfig } from 'services/api/types' ;
4848import { isControlNetOrT2IAdapterModelConfig , isIPAdapterModelConfig } from 'services/api/types' ;
49+ import type { Equals } from 'tsafe' ;
50+ import { assert } from 'tsafe' ;
4951
5052export const selectDefaultControlAdapter = createSelector (
5153 selectModelConfigsQuery ,
@@ -194,18 +196,31 @@ export const useNewCanvasFromImage = () => {
194196 const bboxRect = useAppSelector ( selectBboxRect ) ;
195197 const base = useAppSelector ( selectBboxModelBase ) ;
196198 const func = useCallback (
197- ( imageDTO : ImageDTO ) => {
199+ ( imageDTO : ImageDTO , type : CanvasRasterLayerState [ 'type' ] | CanvasControlLayerState [ 'type' ] ) => {
198200 // Calculate the new bbox dimensions to fit the image's aspect ratio at the optimal size
199201 const ratio = imageDTO . width / imageDTO . height ;
200202 const optimalDimension = getOptimalDimension ( base ) ;
201203 const { width, height } = calculateNewSize ( ratio , optimalDimension ** 2 , base ) ;
202204
203205 // The overrides need to include the layer's ID so we can transform the layer it is initialized
204- const overrides = {
205- id : getPrefixedId ( 'raster_layer' ) ,
206- position : { x : bboxRect . x , y : bboxRect . y } ,
207- objects : [ imageDTOToImageObject ( imageDTO ) ] ,
208- } satisfies Partial < CanvasRasterLayerState > ;
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+ }
209224
210225 CanvasEntityAdapterBase . registerInitCallback ( async ( adapter ) => {
211226 // Skip the callback if the adapter is not the one we are creating
@@ -222,7 +237,16 @@ export const useNewCanvasFromImage = () => {
222237 dispatch ( canvasReset ( ) ) ;
223238 // The `bboxChangedFromCanvas` reducer does no validation! Careful!
224239 dispatch ( bboxChangedFromCanvas ( { x : 0 , y : 0 , width, height } ) ) ;
225- dispatch ( rasterLayerAdded ( { overrides, isSelected : true } ) ) ;
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+ }
226250 } ,
227251 [ base , bboxRect . x , bboxRect . y , dispatch ]
228252 ) ;
0 commit comments