mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-02-18 01:11:20 -05:00
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { useCanvasManager } from 'features/controlLayers/contexts/CanvasManagerProviderGate';
|
|
import { useEntityAdapterSafe } from 'features/controlLayers/contexts/EntityAdapterContext';
|
|
import { useCanvasIsBusy } from 'features/controlLayers/hooks/useCanvasIsBusy';
|
|
import { useEntityIsEmpty } from 'features/controlLayers/hooks/useEntityIsEmpty';
|
|
import { useEntityIsLocked } from 'features/controlLayers/hooks/useEntityIsLocked';
|
|
import type { CanvasEntityIdentifier } from 'features/controlLayers/store/types';
|
|
import { isSegmentableEntityIdentifier } from 'features/controlLayers/store/types';
|
|
import { useCallback, useMemo } from 'react';
|
|
|
|
export const useEntitySegmentAnything = (entityIdentifier: CanvasEntityIdentifier | null) => {
|
|
const canvasManager = useCanvasManager();
|
|
const adapter = useEntityAdapterSafe(entityIdentifier);
|
|
const isBusy = useCanvasIsBusy();
|
|
const isLocked = useEntityIsLocked(entityIdentifier);
|
|
const isEmpty = useEntityIsEmpty(entityIdentifier);
|
|
|
|
const isDisabled = useMemo(() => {
|
|
if (!entityIdentifier) {
|
|
return true;
|
|
}
|
|
if (!isSegmentableEntityIdentifier(entityIdentifier)) {
|
|
return true;
|
|
}
|
|
if (!adapter) {
|
|
return true;
|
|
}
|
|
if (isBusy) {
|
|
return true;
|
|
}
|
|
if (isLocked) {
|
|
return true;
|
|
}
|
|
if (isEmpty) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}, [entityIdentifier, adapter, isBusy, isLocked, isEmpty]);
|
|
|
|
const start = useCallback(() => {
|
|
if (isDisabled) {
|
|
return;
|
|
}
|
|
if (!entityIdentifier) {
|
|
return;
|
|
}
|
|
if (!isSegmentableEntityIdentifier(entityIdentifier)) {
|
|
return;
|
|
}
|
|
const adapter = canvasManager.getAdapter(entityIdentifier);
|
|
if (!adapter) {
|
|
return;
|
|
}
|
|
adapter.segmentAnything.start();
|
|
}, [isDisabled, entityIdentifier, canvasManager]);
|
|
|
|
return { isDisabled, start } as const;
|
|
};
|