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.

feat(video): use the aspect ratio picker for video dimension presets (#10130)

Lincoln Stein0b5801118a89
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.browser.test.tsx
index b4c4341a00..bcaf7acacf 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.browser.test.tsx
@@ -8,6 +8,7 @@ import type {
import { ChakraProvider } from '@chakra-ui/react';
import { calculateNewSize } from '@features/generation/core/settings';
+import { Field } from '@platform/ui/Field';
import { system } from '@theme/system';
import { createInstance } from 'i18next';
import { act, useState } from 'react';
@@ -536,3 +537,72 @@ describe('AspectRatioPicker saved ratios', () => {
await expect.element(trigger()).toHaveFocus();
});
});
+
+describe('AspectRatioPicker built-ins only', () => {
+ const renderFixed = async ({
+ disabled = false,
+ lockedRatio = 16 / 9,
+ valueText,
+ }: { disabled?: boolean; lockedRatio?: number; valueText?: string } = {}) => {
+ host = document.createElement('div');
+ host.style.width = '280px';
+ document.body.append(host);
+ root = createRoot(host);
+ const onChange = vi.fn();
+
+ await act(() =>
+ root!.render(
+ <I18nextProvider i18n={i18n}>
+ <ChakraProvider value={system}>
+ <Field label="Video ratio">
+ <AspectRatioPicker
+ disabled={disabled}
+ lockedRatio={lockedRatio}
+ valueText={valueText}
+ onChange={onChange}
+ />
+ </Field>
+ </ChakraProvider>
+ </I18nextProvider>
+ )
+ );
+
+ return { onChange };
+ };
+ const fixedTrigger = () => page.getByRole('button', { name: /^Video ratio/ });
+
+ it('offers the built-in grid without Free or saved ratios, and chooses a ratio', async () => {
+ const { onChange } = await renderFixed();
+
+ await expect.element(fixedTrigger()).toHaveAccessibleName('Video ratio 16:9');
+ // The visible field label names the trigger and targets it.
+ const label = document.querySelector('label')!;
+ expect(fixedTrigger().element().getAttribute('aria-labelledby')?.split(' ')[0]).toBe(label.id);
+ expect(label.htmlFor).toBe(fixedTrigger().element().id);
+ await userEvent.click(fixedTrigger());
+ await expect.poll(isOpen).toBe(true);
+ await expect.element(page.getByRole('radio', { exact: true, name: '16:9' })).toBeChecked();
+ await expect.element(page.getByRole('radio', { exact: true, name: '16:9' })).toHaveFocus();
+ expect(page.getByRole('radio').elements()).toHaveLength(9);
+ expect(page.getByRole('radio', { exact: true, name: 'Free' }).elements()).toHaveLength(0);
+ expect(dialog()?.textContent).not.toContain('Your ratios');
+ expect(page.getByRole('button', { name: 'Add ratio' }).elements()).toHaveLength(0);
+
+ // Home lands on the first tile, 21:9, and Down moves a row of five: 21:9 sits above 3:4.
+ await userEvent.keyboard('{Home}{ArrowDown}');
+ await expect.element(page.getByRole('radio', { exact: true, name: '3:4' })).toHaveFocus();
+ await userEvent.keyboard('{End}{Enter}');
+
+ expect(onChange).toHaveBeenCalledExactlyOnceWith(9 / 21);
+ await expect.poll(isOpen).toBe(false);
+ });
+
+ it('shows the override text on a disabled trigger that does not open', async () => {
+ await renderFixed({ disabled: true, valueText: 'From first frame' });
+
+ await expect.element(fixedTrigger()).toHaveAccessibleName('Video ratio From first frame');
+ await expect.element(fixedTrigger()).toBeDisabled();
+ await userEvent.click(fixedTrigger(), { force: true });
+ expect(isOpen()).toBe(false);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.tsx
index b64c4b55cf..59f69cde22 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioPicker.tsx
@@ -20,6 +20,7 @@ import {
Stack,
Text,
type TextProps,
+ useFieldContext,
} from '@chakra-ui/react';
import {
formatAspectRatio,
@@ -34,6 +35,7 @@ import {
} from '@features/generation/core/aspectRatioPresets';
import { ASPECT_RATIO_MAP, ASPECT_RATIO_OPTIONS } from '@features/generation/core/settings';
import { Button, IconButton } from '@platform/ui/Button';
+import { useFieldLabelId } from '@platform/ui/Field';
import { PopoverContent } from '@platform/ui/Popover';
import { Row } from '@platform/ui/Row';
import { Scrollable } from '@platform/ui/Scrollable';
@@ -52,7 +54,14 @@ interface Size {
height: number;
}
-export interface AspectRatioPickerProps {
+interface AspectRatioPickerCommonProps {
+ disabled?: boolean;
+ /** Shown on the trigger in place of the chosen ratio, e.g. the media that decides it while disabled. */
+ valueText?: ReactNode;
+}
+
+/** Built-ins, Free, and the account's saved ratios with their add and edit form. */
+export interface AspectRatioPickerFullProps extends AspectRatioPickerCommonProps {
/** The live size: the Free thumbnail's shape and the Add ratio form's prefill. */
currentSize: Size;
/** The ratio the lock holds, or null while unlocked (Free). */
@@ -64,6 +73,18 @@ export interface AspectRatioPickerProps {
onChange: (ratio: number | null) => void;
}
+/** The built-in ratio grid only: no Free and no saved ratios. */
+export interface AspectRatioPickerFixedProps extends AspectRatioPickerCommonProps {
+ currentSize?: never;
+ lockedRatio: number;
+ presets?: never;
+ getSizeForRatio?: never;
+ /** A built-in's ratio. Not called for the ratio already chosen. */
+ onChange: (ratio: number) => void;
+}
+
+export type AspectRatioPickerProps = AspectRatioPickerFullProps | AspectRatioPickerFixedProps;
+
type FormSubject = { mode: 'add' } | { mode: 'edit'; preset: AspectRatioPreset };
const POSITIONING = { placement: 'bottom-start' } as const;
@@ -411,28 +432,37 @@ const GroupLabel = ({ children, id }: { children: ReactNode; id: string }) => (
/**
* The aspect ratio control: built-in ratios as a grid of shapes, the account's saved ratios as rows, and an inline
* form to add or edit one. Choosing applies at once and closes; callers own what a ratio does to the size.
+ * Without `presets` it offers the built-in grid alone.
*/
-export const AspectRatioPicker = ({
- currentSize,
- getSizeForRatio,
- lockedRatio,
- presets,
- onChange,
-}: AspectRatioPickerProps) => {
+export const AspectRatioPicker = (props: AspectRatioPickerProps) => {
+ const { disabled, lockedRatio, valueText: valueTextOverride } = props;
+ // Like Select, an enclosing Field's label names the trigger and its disabled state applies; else a hidden label does.
+ const field = useFieldContext();
+ const fieldLabelId = useFieldLabelId();
+ const full = props.presets ? props : null;
+ const savedPresets = full?.presets.presets ?? [];
const { i18n, t } = useTranslation();
const locale = i18n.resolvedLanguage;
const [isOpen, setIsOpen] = useState(false);
const [form, setForm] = useState<FormSubject | null>(null);
const { containerRef, focus: focusKey, registerTarget } = useDeferredFocus();
- const labelId = useId();
+ const hiddenLabelId = useId();
+ const labelId = fieldLabelId ?? hiddenLabelId;
const valueId = useId();
const builtInsId = useId();
const savedId = useId();
const tileRefs = useRef<(HTMLButtonElement | null)[]>([]);
- const currentRatio = currentSize.height > 0 ? currentSize.width / currentSize.height : 1;
- const match = lockedRatio === null ? null : matchAspectRatio(lockedRatio, presets.presets);
- const builtIns = ASPECT_RATIO_OPTIONS.map((id) => ({ id, ratio: id === 'Free' ? null : ASPECT_RATIO_MAP[id].ratio }));
+ const currentRatio = full
+ ? full.currentSize.height > 0
+ ? full.currentSize.width / full.currentSize.height
+ : 1
+ : (lockedRatio ?? 1);
+ const match = lockedRatio === null ? null : matchAspectRatio(lockedRatio, savedPresets);
+ const builtIns = ASPECT_RATIO_OPTIONS.filter((id) => full || id !== 'Free').map((id) => ({
+ id,
+ ratio: id === 'Free' ? null : ASPECT_RATIO_MAP[id].ratio,
+ }));
// The built-in equal to the locked ratio is current even while a saved ratio names it, so choosing either closes.
const lockedBuiltInId = lockedRatio === null ? 'Free' : getLockedAspectRatioSettings(lockedRatio).aspectRatioId;
const currentTileIndex =
@@ -478,7 +508,8 @@ export const AspectRatioPicker = ({
const choose = (ratio: number | null, isCurrent: boolean) => {
if (!isCurrent) {
- onChange(ratio);
+ // Only Free is null, and the built-ins-only grid has no Free tile.
+ (props.onChange as (ratio: number | null) => void)(ratio);
}
setIsOpen(false);
@@ -500,6 +531,12 @@ export const AspectRatioPicker = ({
};
const submitForm = (draft: AspectRatioPresetDraft) => {
+ if (!full) {
+ return;
+ }
+
+ const { presets } = full;
+
if (form?.mode === 'edit') {
presets.update(form.preset.id, draft);
setForm(null);
@@ -518,6 +555,11 @@ export const AspectRatioPicker = ({
// Deleting is undoable (the port reports it with Undo), so it asks nothing. Focus moves to the next row's delete,
// the previous one's after the last row, or Add ratio once none are left.
const deletePreset = (preset: AspectRatioPreset) => {
+ if (!full) {
+ return;
+ }
+
+ const { presets } = full;
const index = presets.presets.findIndex((candidate) => candidate.id === preset.id);
const neighbour = presets.presets[index + 1] ?? presets.presets[index - 1];
@@ -552,10 +594,7 @@ export const AspectRatioPicker = ({
tileRefs.current[0] ??
null;
- // A new ratio starts from the locked ratio (a snapped size is only close to it), else from the size itself.
- const addTerms = getAspectRatioTerms(lockedRatio ?? currentRatio);
-
- const formFor = (subject: FormSubject) =>
+ const formFor = (subject: FormSubject, getSizeForRatio: (ratio: number) => Size) =>
subject.mode === 'edit' ? (
<PresetForm
key={subject.preset.id}
@@ -571,7 +610,8 @@ export const AspectRatioPicker = ({
<PresetForm
getSizeForRatio={getSizeForRatio}
registerFocusTarget={registerTarget}
- initial={{ label: '', terms: addTerms }}
+ // A new ratio starts from the locked ratio (a snapped size is only close to it), else from the size itself.
+ initial={{ label: '', terms: getAspectRatioTerms(lockedRatio ?? currentRatio) }}
submitLabel={t('common.add')}
title={t('widgets.generate.aspectRatioAdd')}
onCancel={closeForm}
@@ -581,6 +621,8 @@ export const AspectRatioPicker = ({
return (
<Popover.Root
+ // The field label's `for` points at its control, so the trigger takes that id.
+ ids={fieldLabelId && field ? { trigger: field.ids.control } : undefined}
initialFocusEl={getInitialFocus}
lazyMount
open={isOpen}
@@ -595,13 +637,16 @@ export const AspectRatioPicker = ({
}}
onOpenChange={handleOpenChange}
>
- <Text hidden id={labelId}>
- {t('widgets.generate.aspectRatio')}
- </Text>
+ {fieldLabelId ? null : (
+ <Text hidden id={hiddenLabelId}>
+ {t('widgets.generate.aspectRatio')}
+ </Text>
+ )}
<Popover.Trigger asChild>
<Button
aria-labelledby={`${labelId} ${valueId}`}
css={TRIGGER_CSS}
+ disabled={disabled || field?.disabled}
flex="1"
justifyContent="space-between"
minW="0"
@@ -610,8 +655,12 @@ export const AspectRatioPicker = ({
w="full"
>
<HStack as="span" gap="2" id={valueId} minW="0">
- <AspectRatioPreview boxSize="5" ratio={lockedRatio ?? currentRatio} />
- {valueText}
+ {valueTextOverride ?? (
+ <>
+ <AspectRatioPreview boxSize="5" ratio={lockedRatio ?? currentRatio} />
+ {valueText}
+ </>
+ )}
</HStack>
<Icon as={ChevronDownIcon} aria-hidden boxSize="3" color="fg.muted" flexShrink={0} />
</Button>
@@ -670,54 +719,60 @@ export const AspectRatioPicker = ({
);
})}
</Grid>
- <Separator borderColor="border.subtle" />
- <GroupLabel id={savedId}>{t('widgets.generate.aspectRatioYourRatios')}</GroupLabel>
- {presets.presets.length === 0 && form?.mode !== 'add' ? (
- <Text color="fg.subtle" fontSize="sm" px="1">
- {t('widgets.generate.aspectRatioYourRatiosEmpty')}
- </Text>
- ) : presets.presets.length > 0 ? (
- <Scrollable contentProps={SAVED_LIST_CONTENT_PROPS} maxH="12rem">
- <Stack aria-labelledby={savedId} gap="0.5" role="group">
- {presets.presets.map((preset) =>
- form?.mode === 'edit' && form.preset.id === preset.id ? (
- formFor(form)
- ) : (
- <PresetRow
- key={preset.id}
- registerFocusTarget={registerTarget}
- isCurrent={preset.id === currentPresetId}
- preset={preset}
- ratioText={formatPresetRatio(preset)}
- sizeText={formatSize(getSizeForRatio(getAspectRatioPresetRatio(preset)))}
- onChoose={() => choose(getAspectRatioPresetRatio(preset), preset.id === currentPresetId)}
- onDelete={() => deletePreset(preset)}
- onEdit={() => openForm({ mode: 'edit', preset })}
- />
- )
- )}
- </Stack>
- </Scrollable>
+ {full ? (
+ <>
+ <Separator borderColor="border.subtle" />
+ <GroupLabel id={savedId}>{t('widgets.generate.aspectRatioYourRatios')}</GroupLabel>
+ {full.presets.presets.length === 0 && form?.mode !== 'add' ? (
+ <Text color="fg.subtle" fontSize="sm" px="1">
+ {t('widgets.generate.aspectRatioYourRatiosEmpty')}
+ </Text>
+ ) : full.presets.presets.length > 0 ? (
+ <Scrollable contentProps={SAVED_LIST_CONTENT_PROPS} maxH="12rem">
+ <Stack aria-labelledby={savedId} gap="0.5" role="group">
+ {full.presets.presets.map((preset) =>
+ form?.mode === 'edit' && form.preset.id === preset.id ? (
+ formFor(form, full.getSizeForRatio)
+ ) : (
+ <PresetRow
+ key={preset.id}
+ registerFocusTarget={registerTarget}
+ isCurrent={preset.id === currentPresetId}
+ preset={preset}
+ ratioText={formatPresetRatio(preset)}
+ sizeText={formatSize(full.getSizeForRatio(getAspectRatioPresetRatio(preset)))}
+ onChoose={() =>
+ choose(getAspectRatioPresetRatio(preset), preset.id === currentPresetId)
+ }
+ onDelete={() => deletePreset(preset)}
+ onEdit={() => openForm({ mode: 'edit', preset })}
+ />
+ )
+ )}
+ </Stack>
+ </Scrollable>
+ ) : null}
+ {form?.mode === 'add' ? (
+ formFor(form, full.getSizeForRatio)
+ ) : full.presets.isFull ? (
+ <Text color="fg.subtle" fontSize="sm" px="1">
+ {t('widgets.generate.aspectRatioListFull', { count: MAX_ASPECT_RATIO_PRESETS })}
+ </Text>
+ ) : (
+ <Button
+ ref={registerTarget}
+ data-focus-key="add"
+ justifyContent="start"
+ px="1.5"
+ variant="ghost"
+ onClick={() => openForm({ mode: 'add' })}
+ >
+ <PlusIcon />
+ {t('widgets.generate.aspectRatioAdd')}
+ </Button>
+ )}
+ </>
) : null}
- {form?.mode === 'add' ? (
- formFor(form)
- ) : presets.isFull ? (
- <Text color="fg.subtle" fontSize="sm" px="1">
- {t('widgets.generate.aspectRatioListFull', { count: MAX_ASPECT_RATIO_PRESETS })}
- </Text>
- ) : (
- <Button
- ref={registerTarget}
- data-focus-key="add"
- justifyContent="start"
- px="1.5"
- variant="ghost"
- onClick={() => openForm({ mode: 'add' })}
- >
- <PlusIcon />
- {t('widgets.generate.aspectRatioAdd')}
- </Button>
- )}
</Stack>
</Popover.Body>
</PopoverContent>
diff --git a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
index 6c7a60103e..f4bf16f746 100644
--- a/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/features/video/ui/VideoWidgetView.tsx
@@ -1,4 +1,5 @@
import type {
+ VideoAspectRatioId,
VideoConditioningClip,
VideoReferenceItem,
VideoSourceClip,
@@ -6,14 +7,16 @@ import type {
} from '@features/video/core/types';
import { createListCollection, HStack, Stack, Switch, Text } from '@chakra-ui/react';
-import { GenerationSettingsSection, SeedField } from '@features/generation/components';
+import { AspectRatioPicker, GenerationSettingsSection, SeedField } from '@features/generation/components';
import {
getDynamicPromptsConfig,
+ getLockedAspectRatioSettings,
normalizeDynamicPromptsSettings,
sanitizeBatchCount,
} from '@features/generation/settings';
import { ensureModelsLoaded, useModelsSelector } from '@features/models';
import {
+ getVideoAspectRatioParts,
getVideoDurationSeconds,
invertVideoAspectRatioId,
LTX2_EXTEND_CONTEXT_FRAMES,
@@ -28,10 +31,10 @@ import {
getInitialVideoPatch,
getReferencesPatch,
isConditioningClipExcludingFrames,
+ isVideoAspectRatioId,
isVideoTargetResolution,
normalizeVideoWidgetValues,
resolveVideoMode,
- VIDEO_ASPECT_RATIO_IDS,
} from '@features/video/core/settings';
import {
getAcceleratorLoraChangeResult,
@@ -70,9 +73,10 @@ import { useVideoUi, useVideoUiActions } from './VideoUiContext';
const SWITCH_CHECKED_PROPS = { bg: 'accent.solid' };
-const ASPECT_RATIO_COLLECTION = createListCollection({
- items: VIDEO_ASPECT_RATIO_IDS.map((id) => ({ label: id, value: id })),
-});
+const getAspectRatio = (id: VideoAspectRatioId): number => {
+ const { width, height } = getVideoAspectRatioParts(id);
+ return width / height;
+};
const toTargetResolution = (value: string | undefined): VideoWidgetValues['targetResolution'] | null =>
isVideoTargetResolution(value) ? value : null;
@@ -223,11 +227,12 @@ export const VideoWidgetView = () => {
// Stable per-field setters preserve child memo boundaries.
const set = useMemo(
() => ({
- aspectRatio: ({ value }: { value: string[] }) => {
- const aspectRatioId = value[0];
+ // Matched to a built-in the way the picker matches its tiles.
+ aspectRatio: (ratio: number) => {
+ const { aspectRatioId } = getLockedAspectRatioSettings(ratio);
- if (aspectRatioId && (VIDEO_ASPECT_RATIO_IDS as readonly string[]).includes(aspectRatioId)) {
- patch({ aspectRatioId: aspectRatioId as VideoWidgetValues['aspectRatioId'] });
+ if (isVideoAspectRatioId(aspectRatioId)) {
+ patch({ aspectRatioId });
}
},
cfgScale: (cfgScale: number) => patch({ cfgScale }),
@@ -410,7 +415,6 @@ export const VideoWidgetView = () => {
width: dimensions.width,
});
}, [dimensions, policy.targetResolutions, t, values.targetResolution]);
- const aspectRatioValue = useMemo(() => [values.aspectRatioId], [values.aspectRatioId]);
const targetResolutionValue = useMemo(() => [values.targetResolution], [values.targetResolution]);
const framesSlider = useMemo(
@@ -701,13 +705,11 @@ export const VideoWidgetView = () => {
<Stack gap="3" p="2">
<Field helpText={derivedSizeText} label={t('widgets.video.aspectRatio')}>
<HStack gap="1">
- <Select
- collection={ASPECT_RATIO_COLLECTION}
+ <AspectRatioPicker
disabled={hasConditioningMedia}
- flex="1"
- value={aspectRatioValue}
+ lockedRatio={getAspectRatio(values.aspectRatioId)}
valueText={derivedSourceValueText}
- onValueChange={set.aspectRatio}
+ onChange={set.aspectRatio}
/>
<IconButton
aria-label={t('widgets.video.swapAspectRatio')}