Skip to content

Commit

Permalink
feat(ui): add new layer from image menu items for staging area
Browse files Browse the repository at this point in the history
The layers are disabled when created so as to not interfere with the canvas state.
  • Loading branch information
psychedelicious committed Dec 2, 2024
1 parent df84903 commit ca8313e
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StagingAreaToolbarDiscardSelectedButton } from 'features/controlLayers/
import { StagingAreaToolbarImageCountButton } from 'features/controlLayers/components/StagingArea/StagingAreaToolbarImageCountButton';
import { StagingAreaToolbarNextButton } from 'features/controlLayers/components/StagingArea/StagingAreaToolbarNextButton';
import { StagingAreaToolbarPrevButton } from 'features/controlLayers/components/StagingArea/StagingAreaToolbarPrevButton';
import { StagingAreaToolbarSaveAsMenu } from 'features/controlLayers/components/StagingArea/StagingAreaToolbarSaveAsMenu';
import { StagingAreaToolbarSaveSelectedToGalleryButton } from 'features/controlLayers/components/StagingArea/StagingAreaToolbarSaveSelectedToGalleryButton';
import { StagingAreaToolbarToggleShowResultsButton } from 'features/controlLayers/components/StagingArea/StagingAreaToolbarToggleShowResultsButton';
import { memo } from 'react';
Expand All @@ -21,6 +22,7 @@ export const StagingAreaToolbar = memo(() => {
<StagingAreaToolbarAcceptButton />
<StagingAreaToolbarToggleShowResultsButton />
<StagingAreaToolbarSaveSelectedToGalleryButton />
<StagingAreaToolbarSaveAsMenu />
<StagingAreaToolbarDiscardSelectedButton />
<StagingAreaToolbarDiscardAllButton />
</ButtonGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { IconButton, Menu, MenuButton, MenuItem, MenuList } from '@invoke-ai/ui-library';
import { useAppStore } from 'app/store/nanostores/store';
import { useAppSelector } from 'app/store/storeHooks';
import { NewLayerIcon } from 'features/controlLayers/components/common/icons';
import { selectSelectedImage } from 'features/controlLayers/store/canvasStagingAreaSlice';
import { createNewCanvasEntityFromImage } from 'features/imageActions/actions';
import { toast } from 'features/toast/toast';
import { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { PiDotsThreeBold } from 'react-icons/pi';
import { imageDTOToFile, uploadImage } from 'services/api/endpoints/images';

const uploadImageArg = { image_category: 'general', is_intermediate: true, silent: true } as const;

export const StagingAreaToolbarSaveAsMenu = memo(() => {
const { t } = useTranslation();
const selectedImage = useAppSelector(selectSelectedImage);
const store = useAppStore();

const onClickNewRasterLayerFromImage = useCallback(async () => {
if (!selectedImage) {
return;
}
const { dispatch, getState } = store;
const file = await imageDTOToFile(selectedImage.imageDTO);
const imageDTO = await uploadImage({ file, ...uploadImageArg });
createNewCanvasEntityFromImage({
imageDTO,
type: 'raster_layer',
dispatch,
getState,
overrides: { isEnabled: false }, // We are adding the layer while staging, it should be disabled by default
});
toast({
id: 'SENT_TO_CANVAS',
title: t('toast.sentToCanvas'),
status: 'success',
});
}, [selectedImage, store, t]);

const onClickNewControlLayerFromImage = useCallback(async () => {
if (!selectedImage) {
return;
}
const { dispatch, getState } = store;
const file = await imageDTOToFile(selectedImage.imageDTO);
const imageDTO = await uploadImage({ file, ...uploadImageArg });
createNewCanvasEntityFromImage({
imageDTO,
type: 'control_layer',
dispatch,
getState,
overrides: { isEnabled: false }, // We are adding the layer while staging, it should be disabled by default
});
toast({
id: 'SENT_TO_CANVAS',
title: t('toast.sentToCanvas'),
status: 'success',
});
}, [selectedImage, store, t]);

const onClickNewInpaintMaskFromImage = useCallback(async () => {
if (!selectedImage) {
return;
}
const { dispatch, getState } = store;
const file = await imageDTOToFile(selectedImage.imageDTO);
const imageDTO = await uploadImage({ file, ...uploadImageArg });
createNewCanvasEntityFromImage({
imageDTO,
type: 'inpaint_mask',
dispatch,
getState,
overrides: { isEnabled: false }, // We are adding the layer while staging, it should be disabled by default
});
toast({
id: 'SENT_TO_CANVAS',
title: t('toast.sentToCanvas'),
status: 'success',
});
}, [selectedImage, store, t]);

const onClickNewRegionalGuidanceFromImage = useCallback(async () => {
if (!selectedImage) {
return;
}
const { dispatch, getState } = store;
const file = await imageDTOToFile(selectedImage.imageDTO);
const imageDTO = await uploadImage({ file, ...uploadImageArg });
createNewCanvasEntityFromImage({
imageDTO,
type: 'regional_guidance',
dispatch,
getState,
overrides: { isEnabled: false }, // We are adding the layer while staging, it should be disabled by default
});
toast({
id: 'SENT_TO_CANVAS',
title: t('toast.sentToCanvas'),
status: 'success',
});
}, [selectedImage, store, t]);

return (
<Menu>
<MenuButton
as={IconButton}
aria-label={t('controlLayers.newLayerFromImage')}
tooltip={t('controlLayers.newLayerFromImage')}
icon={<PiDotsThreeBold />}
colorScheme="invokeBlue"
isDisabled={!selectedImage}
/>
<MenuList>
<MenuItem icon={<NewLayerIcon />} onClickCapture={onClickNewInpaintMaskFromImage} isDisabled={!selectedImage}>
{t('controlLayers.inpaintMask')}
</MenuItem>
<MenuItem
icon={<NewLayerIcon />}
onClickCapture={onClickNewRegionalGuidanceFromImage}
isDisabled={!selectedImage}
>
{t('controlLayers.regionalGuidance')}
</MenuItem>
<MenuItem icon={<NewLayerIcon />} onClickCapture={onClickNewControlLayerFromImage} isDisabled={!selectedImage}>
{t('controlLayers.controlLayer')}
</MenuItem>
<MenuItem icon={<NewLayerIcon />} onClickCapture={onClickNewRasterLayerFromImage} isDisabled={!selectedImage}>
{t('controlLayers.rasterLayer')}
</MenuItem>
</MenuList>
</Menu>
);
});

StagingAreaToolbarSaveAsMenu.displayName = 'StagingAreaToolbarSaveAsMenu';

0 comments on commit ca8313e

Please sign in to comment.