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.

fix(webv2): show aspect ratio labels in the bbox options bar

joshistoast838db7d68fde
diff --git a/invokeai/frontend/webv2/src/features/generation/components.ts b/invokeai/frontend/webv2/src/features/generation/components.ts
index d21a3d1310..d562cfd34d 100644
--- a/invokeai/frontend/webv2/src/features/generation/components.ts
+++ b/invokeai/frontend/webv2/src/features/generation/components.ts
@@ -3,6 +3,12 @@
* and the settings section shell shared with other widgets.
* Curated, caller-driven export list — add a symbol only when a consumer needs it.
*/
+export {
+ AspectRatioLockButton,
+ AspectRatioSelect,
+ type AspectRatioLockButtonProps,
+ type AspectRatioSelectProps,
+} from './ui/shared/AspectRatioSelect';
export { NegativePromptField } from './ui/promptFields/NegativePromptField';
export { PositivePromptField } from './ui/promptFields/PositivePromptField';
export { PromptTextarea } from './ui/promptFields/PromptTextarea';
diff --git a/invokeai/frontend/webv2/src/features/generation/settings.ts b/invokeai/frontend/webv2/src/features/generation/settings.ts
index 00fbb1f463..05a2c9e682 100644
--- a/invokeai/frontend/webv2/src/features/generation/settings.ts
+++ b/invokeai/frontend/webv2/src/features/generation/settings.ts
@@ -4,6 +4,7 @@
* Curated, caller-driven export list — add a symbol only when a consumer needs it.
*/
export {
+ ASPECT_RATIO_MAP,
calculateNewSize,
clampDimension,
cloneGenerateWidgetValues,
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/GenerateDimensionFields.tsx b/invokeai/frontend/webv2/src/features/generation/ui/GenerateDimensionFields.tsx
index f814d776c6..179fadaad2 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/GenerateDimensionFields.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/GenerateDimensionFields.tsx
@@ -1,24 +1,22 @@
/* eslint-disable react/react-compiler, react-perf/jsx-no-new-object-as-prop, react-perf/jsx-no-new-function-as-prop, react-perf/jsx-no-new-array-as-prop, react-perf/jsx-no-jsx-as-prop */
-import type { SelectValueChangeDetails } from '@chakra-ui/react';
import type { AspectRatioId, GenerateModelConfig, GenerateSettings } from '@features/generation/core/types';
-import { Badge, Box, createListCollection, HStack, Icon, InputGroup, NumberInput, Stack, Text } from '@chakra-ui/react';
+import { Badge, Box, HStack, Icon, InputGroup, NumberInput, Stack } from '@chakra-ui/react';
import { getDefaultGenerateSettings, getGenerationDimensions } from '@features/generation/core/baseGenerationPolicies';
import {
ASPECT_RATIO_MAP,
- ASPECT_RATIO_OPTIONS,
calculateNewSize,
clampDimension,
- isAspectRatioId,
MAX_DIMENSION,
MIN_DIMENSION,
} from '@features/generation/core/settings';
-import { Field, IconButton, Select, Tooltip } from '@platform/ui';
-import { ArrowLeftRightIcon, LockIcon, LockOpenIcon, RulerDimensionLineIcon, ScalingIcon } from 'lucide-react';
-import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
+import { Field, IconButton, Tooltip } from '@platform/ui';
+import { ArrowLeftRightIcon, LockIcon, RulerDimensionLineIcon, ScalingIcon } from 'lucide-react';
+import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { AspectRatioPreview } from './shared/AspectRatioPreview';
+import { AspectRatioLockButton, AspectRatioSelect } from './shared/AspectRatioSelect';
import { GenerateCollapsibleSection } from './shared/GenerateCollapsibleSection';
import { ModelDefaultButton } from './shared/ModelDefaultButton';
@@ -30,7 +28,6 @@ interface GenerateDimensionFieldsProps {
}
type Dimensions = Pick<GenerateSettings, 'height' | 'width'>;
-type AspectRatioOption = { id: AspectRatioId; ratio: number };
/** The ratio to enforce, preferring the stored value and falling back to the current dimensions. */
const getActiveRatio = (settings: GenerateSettings): number =>
@@ -40,9 +37,6 @@ const getActiveRatio = (settings: GenerateSettings): number =>
? settings.width / settings.height
: 1;
-const getAspectRatioOptionRatio = (id: AspectRatioId, fallbackRatio: number): number =>
- id === 'Free' ? fallbackRatio : ASPECT_RATIO_MAP[id].ratio;
-
export const GenerateDimensionFields = ({
onCommit,
projectId,
@@ -62,27 +56,6 @@ export const GenerateDimensionFields = ({
const previousSettingsDimensionsRef = useRef<Dimensions>({ height: settings.height, width: settings.width });
const dimensionRatio = displayDimensions.height > 0 ? displayDimensions.width / displayDimensions.height : 1;
- const aspectRatioOptions = useMemo<AspectRatioOption[]>(
- () =>
- ASPECT_RATIO_OPTIONS.map((id) => ({
- id,
- ratio: getAspectRatioOptionRatio(id, dimensionRatio),
- })),
- [dimensionRatio]
- );
-
- const aspectRatioCollection = useMemo(
- () =>
- createListCollection({
- itemToString: (item: AspectRatioOption) => item.id,
- itemToValue: (item: AspectRatioOption) => item.id,
- items: aspectRatioOptions,
- }),
- [aspectRatioOptions]
- );
-
- const activeAspectRatioPreviewRatio = getAspectRatioOptionRatio(settings.aspectRatioId, dimensionRatio);
-
useEffect(() => {
onCommitRef.current = onCommit;
}, [onCommit]);
@@ -194,13 +167,7 @@ export const GenerateDimensionFields = ({
onCommit(patch);
};
- const setAspectRatioId = ({ value }: SelectValueChangeDetails<AspectRatioOption>) => {
- const id = value[0];
-
- if (!isAspectRatioId(id)) {
- return;
- }
-
+ const setAspectRatioId = (id: AspectRatioId) => {
if (id === 'Free') {
commitSettings({
aspectRatioId: 'Free',
@@ -278,50 +245,12 @@ export const GenerateDimensionFields = ({
>
<Field label={t('widgets.generate.aspectRatio')} p="2">
<HStack gap="1">
- <Select
- collection={aspectRatioCollection}
- contentProps={{ maxH: '18rem' }}
- flex="1"
- renderItem={(option) => (
- <HStack as="span" gap="2">
- <AspectRatioPreview boxSize="6" ratio={option.ratio} />
- <Text as="span" fontSize="xs">
- {option.id}
- </Text>
- </HStack>
- )}
- size="xs"
- value={[settings.aspectRatioId]}
- valueText={
- <HStack as="span" gap="2" minW="0">
- <AspectRatioPreview boxSize="5" ratio={activeAspectRatioPreviewRatio} />
- <Text as="span" fontSize="xs" truncate>
- {settings.aspectRatioId}
- </Text>
- </HStack>
- }
- onValueChange={setAspectRatioId}
+ <AspectRatioSelect
+ fallbackRatio={dimensionRatio}
+ value={settings.aspectRatioId}
+ onChange={setAspectRatioId}
/>
- <Tooltip
- content={
- settings.aspectRatioIsLocked
- ? t('widgets.generate.unlockAspectRatio')
- : t('widgets.generate.lockAspectRatio')
- }
- >
- <IconButton
- aria-label={
- settings.aspectRatioIsLocked
- ? t('widgets.generate.unlockAspectRatio')
- : t('widgets.generate.lockAspectRatio')
- }
- size="xs"
- variant={settings.aspectRatioIsLocked ? 'solid' : 'outline'}
- onClick={toggleLock}
- >
- {settings.aspectRatioIsLocked ? <LockIcon /> : <LockOpenIcon />}
- </IconButton>
- </Tooltip>
+ <AspectRatioLockButton isLocked={settings.aspectRatioIsLocked} onToggle={toggleLock} />
<Tooltip content={t('widgets.generate.swapWidthAndHeight')}>
<IconButton
aria-label={t('widgets.generate.swapWidthAndHeight')}
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioSelect.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioSelect.tsx
new file mode 100644
index 0000000000..3058b7005e
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/AspectRatioSelect.tsx
@@ -0,0 +1,118 @@
+/* eslint-disable react-perf/jsx-no-new-function-as-prop, react-perf/jsx-no-jsx-as-prop, react-perf/jsx-no-new-object-as-prop */
+import type { SelectTriggerProps, SelectValueChangeDetails } from '@chakra-ui/react';
+import type { AspectRatioId } from '@features/generation/core/types';
+
+import { createListCollection, HStack, Text } from '@chakra-ui/react';
+import { ASPECT_RATIO_MAP, ASPECT_RATIO_OPTIONS, isAspectRatioId } from '@features/generation/core/settings';
+import { IconButton, Select, Tooltip } from '@platform/ui';
+import { LockIcon, LockOpenIcon } from 'lucide-react';
+import { useMemo } from 'react';
+import { useTranslation } from 'react-i18next';
+
+import { AspectRatioPreview } from './AspectRatioPreview';
+
+type ControlSize = 'xs' | 'sm';
+
+type AspectRatioOption = { id: AspectRatioId; ratio: number };
+
+const SELECT_CONTENT_PROPS = { maxH: '18rem' } as const;
+
+/** `Free` has no fixed ratio, so its thumbnail mirrors whatever the caller is currently showing. */
+const getAspectRatioOptionRatio = (id: AspectRatioId, fallbackRatio: number): number =>
+ id === 'Free' ? fallbackRatio : ASPECT_RATIO_MAP[id].ratio;
+
+export interface AspectRatioSelectProps {
+ /** Ratio used to draw the `Free` thumbnail — normally the live width/height. */
+ fallbackRatio: number;
+ size?: ControlSize;
+ triggerProps?: SelectTriggerProps;
+ value: AspectRatioId;
+ onChange: (id: AspectRatioId) => void;
+}
+
+/**
+ * The aspect-ratio preset dropdown, with a proportion thumbnail beside each
+ * option. Presentational: callers own what a preset change does to their
+ * dimensions, which differs between a free-floating size and a placed frame.
+ */
+export const AspectRatioSelect = ({
+ fallbackRatio,
+ size = 'xs',
+ triggerProps,
+ value,
+ onChange,
+}: AspectRatioSelectProps) => {
+ const { t } = useTranslation();
+ const options = useMemo<AspectRatioOption[]>(
+ () => ASPECT_RATIO_OPTIONS.map((id) => ({ id, ratio: getAspectRatioOptionRatio(id, fallbackRatio) })),
+ [fallbackRatio]
+ );
+ const collection = useMemo(
+ () =>
+ createListCollection({
+ itemToString: (item: AspectRatioOption) => item.id,
+ itemToValue: (item: AspectRatioOption) => item.id,
+ items: options,
+ }),
+ [options]
+ );
+ const activePreviewRatio = getAspectRatioOptionRatio(value, fallbackRatio);
+ const selectValue = useMemo(() => [value], [value]);
+
+ const handleValueChange = ({ value: next }: SelectValueChangeDetails<AspectRatioOption>) => {
+ const id = next[0];
+
+ if (isAspectRatioId(id)) {
+ onChange(id);
+ }
+ };
+
+ return (
+ <Select
+ aria-label={t('widgets.generate.aspectRatio')}
+ collection={collection}
+ contentProps={SELECT_CONTENT_PROPS}
+ flex="1"
+ renderItem={(option) => (
+ <HStack as="span" gap="2">
+ <AspectRatioPreview boxSize="6" ratio={option.ratio} />
+ <Text as="span" fontSize="xs">
+ {option.id}
+ </Text>
+ </HStack>
+ )}
+ size={size}
+ triggerProps={triggerProps}
+ value={selectValue}
+ valueText={
+ <HStack as="span" gap="2" minW="0">
+ <AspectRatioPreview boxSize="5" ratio={activePreviewRatio} />
+ <Text as="span" fontSize="xs" truncate>
+ {value}
+ </Text>
+ </HStack>
+ }
+ onValueChange={handleValueChange}
+ />
+ );
+};
+
+export interface AspectRatioLockButtonProps {
+ isLocked: boolean;
+ size?: ControlSize;
+ onToggle: () => void;
+}
+
+/** Companion lock for {@link AspectRatioSelect}: a labelled icon button, not a bare dot. */
+export const AspectRatioLockButton = ({ isLocked, size = 'xs', onToggle }: AspectRatioLockButtonProps) => {
+ const { t } = useTranslation();
+ const label = isLocked ? t('widgets.generate.unlockAspectRatio') : t('widgets.generate.lockAspectRatio');
+
+ return (
+ <Tooltip content={label}>
+ <IconButton aria-label={label} size={size} variant={isLocked ? 'solid' : 'outline'} onClick={onToggle}>
+ {isLocked ? <LockIcon /> : <LockOpenIcon />}
+ </IconButton>
+ </Tooltip>
+ );
+};
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/BboxOptions.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/BboxOptions.tsx
index 2255b499df..651ad55f75 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/BboxOptions.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/BboxOptions.tsx
@@ -1,105 +1,70 @@
-import type { SelectValueChangeDetails } from '@chakra-ui/react';
+import type { AspectRatioId } from '@features/generation/contracts';
-import { createListCollection, HStack } from '@chakra-ui/react';
-import { Select, ToggleDot } from '@platform/ui';
+import { HStack } from '@chakra-ui/react';
+import { AspectRatioLockButton, AspectRatioSelect } from '@features/generation/components';
+import { ASPECT_RATIO_MAP, deriveAspectRatioId } from '@features/generation/settings';
import { constrainBboxToRatio } from '@workbench/canvas-engine/api';
-import { useCallback, useMemo } from 'react';
-import { useTranslation } from 'react-i18next';
+import { useCallback } from 'react';
import type { ToolOptionsComponentProps } from './ToolOptionsBar';
import { useBboxEditor } from './useBboxEditor';
-/** Aspect-ratio presets offered in the options bar. `null` ratio = Free (unlocked). */
-interface AspectPreset {
- id: string;
- ratio: number | null;
-}
-
-const ASPECT_PRESETS: readonly AspectPreset[] = [
- { id: 'Free', ratio: null },
- { id: '1:1', ratio: 1 },
- { id: '4:3', ratio: 4 / 3 },
- { id: '3:4', ratio: 3 / 4 },
- { id: '16:9', ratio: 16 / 9 },
- { id: '9:16', ratio: 9 / 16 },
-];
-
-const ASPECT_TRIGGER_PROPS = { minW: '5.5rem' } as const;
-
-/** The preset id whose ratio matches `aspectRatio` (within tolerance), or `null`. */
-const matchingPresetId = (aspectRatio: number): string | null =>
- ASPECT_PRESETS.find((preset) => preset.ratio !== null && Math.abs(preset.ratio - aspectRatio) < 1e-3)?.id ?? null;
+const ASPECT_TRIGGER_PROPS = { minW: '7.5rem' } as const;
/**
- * Bbox tool options: aspect-ratio preset select and aspect lock toggle. Numeric
- * frame details live in the separate bbox details bar.
+ * Bbox tool options: aspect-ratio preset select and aspect lock, sharing the
+ * generate widget's controls so both surfaces offer the same presets — the two
+ * are already kept in sync by `canvasDimsSync`. Numeric frame details live in
+ * the separate bbox details bar.
*/
export const BboxOptions = ({ engine }: ToolOptionsComponentProps) => {
- const { t } = useTranslation();
const { bbox, commitBbox, grid, options } = useBboxEditor(engine);
-
- const collection = useMemo(
- () =>
- createListCollection({
- itemToString: (item: AspectPreset) => item.id,
- itemToValue: (item: AspectPreset) => item.id,
- items: [...ASPECT_PRESETS],
- }),
- []
- );
-
- const selectValue = useMemo(
- () => [options.aspectLocked ? (matchingPresetId(options.aspectRatio) ?? 'Free') : 'Free'],
- [options.aspectLocked, options.aspectRatio]
- );
+ const bboxRatio = bbox.height > 0 ? bbox.width / bbox.height : 1;
+ // Derived from the live frame rather than the stored ratio, so a lock captured
+ // from a hand-drawn bbox reports the preset it actually matches.
+ const selectedId: AspectRatioId = options.aspectLocked ? deriveAspectRatioId(bbox.width, bbox.height) : 'Free';
const onAspectPresetChange = useCallback(
- ({ value }: SelectValueChangeDetails) => {
- const id = value[0];
- const preset = ASPECT_PRESETS.find((entry) => entry.id === id);
- if (!preset) {
- return;
- }
- if (preset.ratio === null) {
+ (id: AspectRatioId) => {
+ if (id === 'Free') {
engine.interaction.set('bboxOptions', { ...options, aspectLocked: false });
return;
}
- engine.interaction.set('bboxOptions', { aspectLocked: true, aspectRatio: preset.ratio });
- commitBbox(constrainBboxToRatio(bbox, preset.ratio, grid));
+
+ const ratio = ASPECT_RATIO_MAP[id].ratio;
+
+ engine.interaction.set('bboxOptions', { aspectLocked: true, aspectRatio: ratio });
+ // The bbox is a placed frame, so reshape it in place rather than
+ // re-deriving a free size from its pixel area.
+ commitBbox(constrainBboxToRatio(bbox, ratio, grid));
},
[bbox, commitBbox, engine, grid, options]
);
- const onLockToggle = useCallback(
- (checked: boolean) => {
- const aspectRatio =
- checked && bbox.height > 0 && matchingPresetId(options.aspectRatio) === null
- ? bbox.width / bbox.height
- : options.aspectRatio > 0
- ? options.aspectRatio
- : 1;
- engine.interaction.set('bboxOptions', { aspectLocked: checked, aspectRatio });
- },
- [bbox, engine, options.aspectRatio]
- );
+ const onLockToggle = useCallback(() => {
+ const checked = !options.aspectLocked;
+ // Locking a frame that matches no preset captures its current ratio so
+ // further edits preserve it.
+ const aspectRatio =
+ checked && bbox.height > 0 && deriveAspectRatioId(bbox.width, bbox.height) === 'Free'
+ ? bboxRatio
+ : options.aspectRatio > 0
+ ? options.aspectRatio
+ : 1;
+
+ engine.interaction.set('bboxOptions', { aspectLocked: checked, aspectRatio });
+ }, [bbox, bboxRatio, engine, options.aspectLocked, options.aspectRatio]);
return (
- <HStack align="center" gap="3">
- <Select
- aria-label={t('widgets.canvas.toolOptions.aspectRatio')}
- collection={collection}
- size="xs"
+ <HStack align="center" gap="1">
+ <AspectRatioSelect
+ fallbackRatio={bboxRatio}
triggerProps={ASPECT_TRIGGER_PROPS}
- value={selectValue}
- valueText={selectValue[0]}
- onValueChange={onAspectPresetChange}
- />
- <ToggleDot
- checked={options.aspectLocked}
- label={t('widgets.canvas.toolOptions.lockAspect')}
- onCheckedChange={onLockToggle}
+ value={selectedId}
+ onChange={onAspectPresetChange}
/>
+ <AspectRatioLockButton isLocked={options.aspectLocked} onToggle={onLockToggle} />
</HStack>
);
};