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(canvas): keep the tool options bar mounted for tools without options

Josh Corbettb138f1c62e98
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.json
index 51e65abe45..ffc65a4327 100644
--- a/invokeai/frontend/webv2/public/locales/en.json
+++ b/invokeai/frontend/webv2/public/locales/en.json
@@ -2300,6 +2300,11 @@
"unverified": "The edit could not be verified or reverted. Check the canvas before continuing."
},
"surface": "Canvas surface",
+ "toolHints": {
+ "colorPicker": "Click the canvas to sample a color.",
+ "sam": "Click an object to select it.",
+ "view": "Drag to pan and scroll to zoom."
+ },
"toolOptions": {
"applyTransform": "Apply",
"aspectRatio": "Aspect ratio",
@@ -2366,12 +2371,14 @@
"tools": {
"bbox": "Generation frame",
"brush": "Brush",
+ "colorPicker": "Color picker",
"eraser": "Eraser",
"gradient": "Gradient",
"label": "Tools",
"lasso": "Lasso select",
"marquee": "Marquee select",
"move": "Move",
+ "sam": "Select object",
"shape": "Shape",
"text": "Text",
"transform": "Transform",
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.test.ts
index ced5223f16..c7466c3894 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.test.ts
@@ -1,6 +1,66 @@
+import type { ToolId } from '@workbench/canvas-engine/api';
+import type { ComponentProps } from 'react';
+
+import { ChakraProvider } from '@chakra-ui/react';
+import { system } from '@theme/system';
+import { attachCanvasOperations } from '@workbench/canvas-operations/operationAccess';
+import { createInstance } from 'i18next';
+import { createElement } from 'react';
+import { renderToStaticMarkup } from 'react-dom/server';
+import { I18nextProvider } from 'react-i18next';
import { describe, expect, it } from 'vitest';
-import { resolveCanvasOptionsContent, TOOL_OPTIONS_COMPONENTS } from './ToolOptionsBar';
+import { resolveToolOptionsBarContent, TOOL_OPTIONS_COMPONENTS, ToolOptionsBar } from './ToolOptionsBar';
+
+const TOOL_IDS = Object.keys({
+ bbox: true,
+ brush: true,
+ colorPicker: true,
+ eraser: true,
+ gradient: true,
+ lasso: true,
+ marquee: true,
+ move: true,
+ sam: true,
+ shape: true,
+ text: true,
+ transform: true,
+ view: true,
+} satisfies Record<ToolId, true>) as ToolId[];
+
+const englishCatalogModules = import.meta.glob('../../../../../public/locales/en.json', {
+ eager: true,
+ import: 'default',
+});
+const enCatalog = Object.values(englishCatalogModules)[0] as {
+ widgets: { canvas: { toolHints: Record<string, string>; tools: Record<string, string> } };
+};
+const testI18n = createInstance();
+await testI18n.init({
+ initAsync: false,
+ lng: 'en',
+ resources: { en: { translation: enCatalog } },
+});
+
+const renderBar = (activeTool: ToolId): string => {
+ const engine = {
+ interaction: {
+ get: (key: string) => (key === 'activeTool' ? activeTool : undefined),
+ subscribe: () => () => undefined,
+ },
+ };
+ attachCanvasOperations(engine, {
+ getOperationState: () => ({ status: 'idle' }),
+ subscribeOperation: () => () => undefined,
+ } as never);
+ return renderToStaticMarkup(
+ createElement(
+ ChakraProvider,
+ { value: system } as ComponentProps<typeof ChakraProvider>,
+ createElement(I18nextProvider, { i18n: testI18n }, createElement(ToolOptionsBar, { engine: engine as never }))
+ )
+ );
+};
describe('TOOL_OPTIONS_COMPONENTS', () => {
it('has an entry for exactly the tools with dedicated options today (bbox, brush, eraser, gradient, lasso, marquee, move, shape, text, transform)', () => {
@@ -18,17 +78,34 @@ describe('TOOL_OPTIONS_COMPONENTS', () => {
]);
});
- it('has no entry for the view tool — the bar is omitted entirely for it', () => {
+ it('shows the hint for the view tool instead of unmounting the bar', () => {
expect(TOOL_OPTIONS_COMPONENTS.view).toBeUndefined();
- expect(resolveCanvasOptionsContent({ status: 'idle' }, 'view')).toBeNull();
+ expect(resolveToolOptionsBarContent({ status: 'idle' }, 'view')).toEqual({ kind: 'hint', tool: 'view' });
});
- it('has no entry for tools not yet implemented by the engine', () => {
+ it('shows the hint for tools without options', () => {
for (const toolId of ['colorPicker', 'sam'] as const) {
expect(TOOL_OPTIONS_COMPONENTS[toolId]).toBeUndefined();
+ expect(resolveToolOptionsBarContent({ status: 'idle' }, toolId)).toEqual({ kind: 'hint', tool: toolId });
}
});
+ it('names every tool and hints every tool without options in the English catalog', () => {
+ const { toolHints, tools } = enCatalog.widgets.canvas;
+ for (const toolId of TOOL_IDS) {
+ expect(tools[toolId], toolId).toEqual(expect.any(String));
+ if (!TOOL_OPTIONS_COMPONENTS[toolId]) {
+ expect(toolHints[toolId], toolId).toEqual(expect.any(String));
+ }
+ }
+ });
+
+ it('renders the tool identity and hint for the view tool instead of unmounting the bar', () => {
+ const markup = renderBar('view');
+ expect(markup).toContain(enCatalog.widgets.canvas.tools.view);
+ expect(markup).toContain(enCatalog.widgets.canvas.toolHints.view);
+ });
+
it('every registered entry is a defined component function', () => {
const components = Object.values(TOOL_OPTIONS_COMPONENTS);
expect(components.length).toBeGreaterThan(0);
@@ -38,9 +115,12 @@ describe('TOOL_OPTIONS_COMPONENTS', () => {
});
it('gives an active canvas operation priority over the active or temporary tool', () => {
- expect(resolveCanvasOptionsContent({ status: 'active' }, 'sam')).toBe('operation');
- expect(resolveCanvasOptionsContent({ status: 'active' }, 'view')).toBe('operation');
- expect(resolveCanvasOptionsContent({ status: 'idle' }, 'brush')).toBe('brush');
- expect(resolveCanvasOptionsContent({ status: 'idle' }, 'view')).toBeNull();
+ expect(resolveToolOptionsBarContent({ status: 'active' }, 'sam')).toEqual({ kind: 'operation' });
+ expect(resolveToolOptionsBarContent({ status: 'active' }, 'view')).toEqual({ kind: 'operation' });
+ expect(resolveToolOptionsBarContent({ status: 'idle' }, 'brush')).toEqual({
+ component: TOOL_OPTIONS_COMPONENTS.brush,
+ kind: 'options',
+ tool: 'brush',
+ });
});
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.tsx
index 26beb9fec0..4679780426 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/tool-options/ToolOptionsBar.tsx
@@ -3,10 +3,11 @@ import type { CanvasOperationState } from '@workbench/canvas-operations/api';
import type { CanvasEngineHandle } from '@workbench/widgets/canvas/useCanvasEngine';
import type { ComponentType } from 'react';
-import { HStack } from '@chakra-ui/react';
+import { HStack, Text } from '@chakra-ui/react';
import { BboxDetailsBar } from '@workbench/widgets/canvas/BboxDetailsBar';
import { CanvasFloatingBarDivider } from '@workbench/widgets/canvas/CanvasFloatingBar';
import { useCanvasActiveTool, useCanvasOperation } from '@workbench/widgets/canvas/engineStoreHooks';
+import { useTranslation } from 'react-i18next';
import { BboxOptions } from './BboxOptions';
import { BrushOptions } from './BrushOptions';
@@ -31,11 +32,7 @@ export interface ToolOptionsComponentProps {
engine: CanvasToolOptionsEngine;
}
-/**
- * Contextual options content per active tool. Tools without an entry here
- * (view, and anything not yet implemented) render no controls, and the bar
- * itself is omitted.
- */
+/** Contextual options content per active tool; tools without an entry show their hint instead. */
export const TOOL_OPTIONS_COMPONENTS: Partial<Record<ToolId, ComponentType<ToolOptionsComponentProps>>> = {
bbox: BboxOptions,
brush: BrushOptions,
@@ -49,47 +46,58 @@ export const TOOL_OPTIONS_COMPONENTS: Partial<Record<ToolId, ComponentType<ToolO
transform: TransformOptions,
};
-export const resolveCanvasOptionsContent = (
+export type ToolOptionsBarContent =
+ | { kind: 'operation' }
+ | { kind: 'options'; tool: ToolId; component: ComponentType<ToolOptionsComponentProps> }
+ | { kind: 'hint'; tool: ToolId };
+
+export const resolveToolOptionsBarContent = (
operation: Pick<CanvasOperationState, 'status'>,
activeTool: ToolId
-): 'operation' | ToolId | null => {
+): ToolOptionsBarContent => {
if (operation.status === 'active') {
- return 'operation';
+ return { kind: 'operation' };
}
- return TOOL_OPTIONS_COMPONENTS[activeTool] ? activeTool : null;
+ const component = TOOL_OPTIONS_COMPONENTS[activeTool];
+ return component ? { component, kind: 'options', tool: activeTool } : { kind: 'hint', tool: activeTool };
};
/**
- * The canvas's floating tool-options bar (bottom-center over the surface):
- * contextual controls for the active tool. Tool options read and write the
- * engine's transient option stores directly (`useBrushOptions` /
- * `useEraserOptions` + `engine.interaction.set(...)`) — there is no React state
- * mirror. Positioned by {@link CanvasWidgetView}; shares its look with the
- * staging bar via {@link CanvasFloatingBar}.
- *
- * The bar is purely contextual: a tool with no options renders nothing at all,
- * rather than an empty bar floating over the surface.
+ * The canvas's floating tool-options bar (bottom-center over the surface): the
+ * active tool's identity followed by its contextual controls, or its usage hint
+ * when it has none, so the bar keeps its place across tool switches.
+ * Tool options read and write the engine's transient option stores directly
+ * (`useBrushOptions` / `useEraserOptions` + `engine.interaction.set(...)`) —
+ * there is no React state mirror. Positioned by {@link CanvasWidgetView};
+ * shares its look with the staging bar via {@link CanvasFloatingBar}.
*/
export const ToolOptionsBar = ({ engine }: { engine: CanvasToolOptionsEngine }) => {
+ const { t } = useTranslation();
const activeTool = useCanvasActiveTool(engine);
const operation = useCanvasOperation(engine);
- const content = resolveCanvasOptionsContent(operation, activeTool);
- if (content === 'operation' && operation.status === 'active') {
+ const content = resolveToolOptionsBarContent(operation, activeTool);
+ if (content.kind === 'operation' && operation.status === 'active') {
return <CanvasOperationBar engine={engine} isExternalInteractionLocked={false} operation={operation} />;
}
- const OptionsComponent = content && content !== 'operation' ? TOOL_OPTIONS_COMPONENTS[content] : undefined;
+ const OptionsComponent = content.kind === 'options' ? content.component : undefined;
const hasBboxDetails = activeTool === 'bbox';
- if (!OptionsComponent && !hasBboxDetails) {
- return null;
- }
-
return (
<CanvasOptionsBar>
<HStack align="center" gap="3" minW="0" overflow="hidden">
+ <Text color="fg.muted" flexShrink={0} fontSize="xs" whiteSpace="nowrap">
+ {t(`widgets.canvas.tools.${activeTool}`)}
+ </Text>
+ <CanvasFloatingBarDivider />
{hasBboxDetails ? <BboxDetailsBar engine={engine} /> : null}
{hasBboxDetails && OptionsComponent ? <CanvasFloatingBarDivider /> : null}
- {OptionsComponent ? <OptionsComponent engine={engine} /> : null}
+ {OptionsComponent ? (
+ <OptionsComponent engine={engine} />
+ ) : (
+ <Text color="fg.subtle" fontSize="xs" minW="0" truncate>
+ {t(`widgets.canvas.toolHints.${activeTool}`)}
+ </Text>
+ )}
</HStack>
</CanvasOptionsBar>
);