Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

3.7 KB
Raw Blame
import type { CanvasDenoisingStrength, CanvasDenoisingStrengthProps } from '@features/generation/react';
import type { ReactNode } from 'react';

import { HStack } from '@chakra-ui/react';
import { useDebouncedDraftValue, useRegisterGenerateDraftFlusher } from '@features/generation/react';
import { ScrubberField } from '@platform/ui/ScrubberField';
import {
  CANVAS_DENOISING_STRENGTH_KEY,
  clampCanvasDenoisingStrength,
  DEFAULT_CANVAS_DENOISING_STRENGTH,
  MAX_CANVAS_DENOISING_STRENGTH,
  MIN_CANVAS_DENOISING_STRENGTH,
  readCanvasDenoisingStrength,
} from '@workbench/widgets/canvas/invoke/canvasStrength';
import { getProjectWidgetValues } from '@workbench/widgetState';
import { useActiveProjectSelector, useWorkbenchCommands } from '@workbench/WorkbenchContext';
import { createContext, use, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { DenoisingStrengthWave } from './DenoisingStrengthWave';

const STRENGTH_DEBOUNCE_MS = 250;

const formatStrengthPercent = (value: number): string => `${Math.round(value * 100)}%`;

const selectCanvasStrength = (project: Parameters<typeof getProjectWidgetValues>[0]): number =>
  readCanvasDenoisingStrength(getProjectWidgetValues(project, 'canvas'));

interface StrengthDraft {
  value: number;
  setValue: (value: number) => void;
}

const StrengthDraftContext = createContext<StrengthDraft | null>(null);

const useStrengthDraft = (): StrengthDraft => {
  const draft = use(StrengthDraftContext);
  if (!draft) {
    throw new Error('Denoising strength controls render inside GenerateDenoisingStrength.');
  }
  return draft;
};

/** Owns the strength draft; `children` keep element identity so a scrub re-renders only draft readers. */
const StrengthDraftProvider = ({ children }: { children: ReactNode }) => {
  const { widgets } = useWorkbenchCommands();
  const projectId = useActiveProjectSelector((project) => project.id);
  const strength = useActiveProjectSelector(selectCanvasStrength);

  const commitStrength = useCallback(
    (value: number) => {
      widgets.patchValues('canvas', { [CANVAS_DENOISING_STRENGTH_KEY]: clampCanvasDenoisingStrength(value) });
    },
    [widgets]
  );

  const { draftValue, flushDraftValue, setDraftValue } = useDebouncedDraftValue({
    delayMs: STRENGTH_DEBOUNCE_MS,
    onCommit: commitStrength,
    resetKey: projectId,
    value: strength,
  });

  useRegisterGenerateDraftFlusher(flushDraftValue);

  const draft = useMemo<StrengthDraft>(
    () => ({ setValue: setDraftValue, value: draftValue }),
    [draftValue, setDraftValue]
  );

  return <StrengthDraftContext value={draft}>{children}</StrengthDraftContext>;
};

const StrengthSummary = () => {
  const { value } = useStrengthDraft();
  return (
    <HStack as="span" display="inline-flex" gap="1.5" verticalAlign="middle">
      {formatStrengthPercent(value)}
      <DenoisingStrengthWave value={value} />
    </HStack>
  );
};

const StrengthField = () => {
  const { t } = useTranslation();
  const { setValue, value } = useStrengthDraft();
  return (
    <ScrubberField
      defaultValue={DEFAULT_CANVAS_DENOISING_STRENGTH}
      formatValue={formatStrengthPercent}
      hint="denoisingStrength"
      label={t('widgets.generate.denoisingStrength')}
      max={MAX_CANVAS_DENOISING_STRENGTH}
      min={MIN_CANVAS_DENOISING_STRENGTH}
      step={0.01}
      value={value}
      onChange={setValue}
    />
  );
};

const STRENGTH_SLOTS: CanvasDenoisingStrength = { field: <StrengthField />, summary: <StrengthSummary /> };

/** Lays out the Render section with canvas denoising strength: its summary in the title, its control under guidance. */
export const GenerateDenoisingStrength = ({ children }: CanvasDenoisingStrengthProps) => (
  <StrengthDraftProvider>{children(STRENGTH_SLOTS)}</StrengthDraftProvider>
);