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(canvas): isolate surface context menu boundary

joshistoast61f1f8861c2b
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasSurfaceContextLayout.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasSurfaceContextLayout.test.tsx
new file mode 100644
index 0000000000..b001ef778b
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasSurfaceContextLayout.test.tsx
@@ -0,0 +1,40 @@
+import type { ReactNode } from 'react';
+
+import { ChakraProvider } from '@chakra-ui/react';
+import { system } from '@theme/system';
+import { renderToStaticMarkup } from 'react-dom/server';
+import { describe, expect, it } from 'vitest';
+
+import { CanvasSurfaceContextLayout } from './CanvasSurfaceContextLayout';
+
+const engineSurface = <span data-engine-surface="">Surface</span>;
+const noop = () => undefined;
+
+const renderLayout = (surface: ReactNode): string =>
+ renderToStaticMarkup(
+ <ChakraProvider value={system}>
+ <div data-canvas-widget="">
+ <CanvasSurfaceContextLayout surface={surface} onContextMenu={noop}>
+ <div data-canvas-chrome="">Chrome</div>
+ </CanvasSurfaceContextLayout>
+ </div>
+ </ChakraProvider>
+ );
+
+describe('CanvasSurfaceContextLayout', () => {
+ it('keeps one empty context-menu owner mounted beside chrome without an engine surface', () => {
+ const markup = renderLayout(null);
+
+ expect(markup.match(/data-canvas-context-menu-owner/g)).toHaveLength(1);
+ expect(markup).toMatch(/data-canvas-context-menu-owner=""[^>]*><\/div><div data-canvas-chrome="">Chrome<\/div>/);
+ });
+
+ it('contains the engine surface inside the owner while chrome remains its sibling', () => {
+ const markup = renderLayout(engineSurface);
+
+ expect(markup.match(/data-canvas-context-menu-owner/g)).toHaveLength(1);
+ expect(markup).toMatch(
+ /data-canvas-context-menu-owner=""[^>]*><span data-engine-surface="">Surface<\/span><\/div><div data-canvas-chrome="">Chrome<\/div>/
+ );
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasSurfaceContextLayout.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasSurfaceContextLayout.tsx
new file mode 100644
index 0000000000..7269227ffa
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasSurfaceContextLayout.tsx
@@ -0,0 +1,26 @@
+import type { BoxProps } from '@chakra-ui/react';
+import type { MouseEventHandler, ReactNode } from 'react';
+
+import { Box } from '@chakra-ui/react';
+
+const CANVAS_SURFACE_LAYER_LAYOUT = {
+ inset: '0',
+ position: 'absolute',
+} satisfies BoxProps;
+
+export const CanvasSurfaceContextLayout = ({
+ children,
+ onContextMenu,
+ surface,
+}: {
+ children: ReactNode;
+ onContextMenu: MouseEventHandler<HTMLDivElement>;
+ surface: ReactNode;
+}) => (
+ <>
+ <Box {...CANVAS_SURFACE_LAYER_LAYOUT} data-canvas-context-menu-owner="" onContextMenu={onContextMenu}>
+ {surface}
+ </Box>
+ {children}
+ </>
+);
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasWidgetView.tsx
index aa79aea93e..a8c428efbc 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/CanvasWidgetView.tsx
@@ -39,6 +39,7 @@ import {
resolveCanvasSettings,
} from './canvasSettings';
import { CanvasSurface } from './CanvasSurface';
+import { CanvasSurfaceContextLayout } from './CanvasSurfaceContextLayout';
import { resolveCheckerColors } from './checkerColors';
import { useCanvasOperation } from './engineStoreHooks';
import { StagingBar } from './StagingBar';
@@ -411,79 +412,73 @@ export const CanvasWidgetView = ({ runtime }: WidgetViewProps) => {
() => <CanvasSaveToGallerySubmenu disabled={isSaveToGalleryDisabled} onSave={saveToGallery} />,
[isSaveToGalleryDisabled, saveToGallery]
);
+ const canvasSurface = useMemo(() => (engine ? <CanvasSurface engine={engine} /> : null), [engine]);
return (
- <Box
- aria-label={t('widgets.canvas.surface')}
- bg="bg.inset"
- h="full"
- overflow="hidden"
- position="relative"
- w="full"
- onContextMenu={handleSurfaceContextMenu}
- >
- {engine ? (
- <>
- <CanvasSurface engine={engine} />
- <ToolStrip engine={engine} isInteractionLocked={isInteractionLocked} />
- <CanvasLayerContextMenu
- additionalItems={saveToGallerySubmenu}
- dispatch={dispatch}
- engine={engine}
- layers={document.layers}
- target={layerContextMenuTarget}
- onClose={closeContextMenu}
- />
- </>
- ) : null}
- {contextMenuBranch === 'global' && contextMenuTarget ? (
- <CanvasGlobalContextMenu target={contextMenuTarget} onClose={closeContextMenu}>
- {saveToGallerySubmenu}
- </CanvasGlobalContextMenu>
- ) : null}
-
- {/*
- * Floating bottom-center chrome: the staging bar (when active) stacks
- * directly above the always-present tool options bar — "just like the
- * staging UI". The wrapper is click-through so the canvas stays
- * interactive around the bars; each bar re-enables pointer events.
- */}
- <CanvasBottomOverlay.Root>
- {hasStagingSlots || isCanvasGenerationInFlight ? (
- <CanvasBottomOverlay.Staging>
- <StagingBar
- antialiasProgressImages={antialiasProgressImages}
- areThumbnailsVisible={stagingArea.areThumbnailsVisible}
- autoSwitchMode={stagingArea.autoSwitchMode}
- hasMultipleSlots={hasMultipleStagingSlots}
- isGenerating={isCanvasGenerationInFlight}
- isVisible={stagingArea.isVisible}
- selectedCandidate={selectedCandidate}
- selectedImageIndex={stagingArea.selectedImageIndex}
- selectedSlot={selectedSlot}
- slots={stagingSlots}
- onAccept={() => dispatch({ type: 'acceptStagedImage' })}
- onCancelQueueItem={(queueItemId) => dispatch({ queueItemId, type: 'cancelQueueItem' })}
- onCycle={(direction) => dispatch({ direction, type: 'cycleStagedImage' })}
- onDiscardAll={() => dispatch({ type: 'discardAllStagedImages' })}
- onDiscardSelected={() => dispatch({ type: 'discardSelectedStagedImage' })}
- onSelectImage={(imageIndex) => dispatch({ imageIndex, type: 'setStagedImageIndex' })}
- onSetAutoSwitch={(mode) => dispatch({ mode, type: 'setCanvasStagingAutoSwitch' })}
- onToggleThumbnails={() => dispatch({ type: 'toggleCanvasStagingThumbnailsVisibility' })}
- onToggleVisibility={() => dispatch({ type: 'toggleCanvasStagingVisibility' })}
+ <Box aria-label={t('widgets.canvas.surface')} bg="bg.inset" h="full" overflow="hidden" position="relative" w="full">
+ <CanvasSurfaceContextLayout surface={canvasSurface} onContextMenu={handleSurfaceContextMenu}>
+ {engine ? (
+ <>
+ <ToolStrip engine={engine} isInteractionLocked={isInteractionLocked} />
+ <CanvasLayerContextMenu
+ additionalItems={saveToGallerySubmenu}
+ dispatch={dispatch}
+ engine={engine}
+ layers={document.layers}
+ target={layerContextMenuTarget}
+ onClose={closeContextMenu}
/>
- </CanvasBottomOverlay.Staging>
+ </>
+ ) : null}
+ {contextMenuBranch === 'global' && contextMenuTarget ? (
+ <CanvasGlobalContextMenu target={contextMenuTarget} onClose={closeContextMenu}>
+ {saveToGallerySubmenu}
+ </CanvasGlobalContextMenu>
) : null}
- <CanvasBottomOverlay.Controls>
- <CanvasBottomControls
- documentHeight={document.height}
- documentWidth={document.width}
- engine={engine}
- isExternalInteractionLocked={isInteractionLocked}
- operation={operation}
- />
- </CanvasBottomOverlay.Controls>
- </CanvasBottomOverlay.Root>
+
+ {/*
+ * Floating bottom-center chrome: the staging bar (when active) stacks
+ * directly above the always-present tool options bar — "just like the
+ * staging UI". The wrapper is click-through so the canvas stays
+ * interactive around the bars; each bar re-enables pointer events.
+ */}
+ <CanvasBottomOverlay.Root>
+ {hasStagingSlots || isCanvasGenerationInFlight ? (
+ <CanvasBottomOverlay.Staging>
+ <StagingBar
+ antialiasProgressImages={antialiasProgressImages}
+ areThumbnailsVisible={stagingArea.areThumbnailsVisible}
+ autoSwitchMode={stagingArea.autoSwitchMode}
+ hasMultipleSlots={hasMultipleStagingSlots}
+ isGenerating={isCanvasGenerationInFlight}
+ isVisible={stagingArea.isVisible}
+ selectedCandidate={selectedCandidate}
+ selectedImageIndex={stagingArea.selectedImageIndex}
+ selectedSlot={selectedSlot}
+ slots={stagingSlots}
+ onAccept={() => dispatch({ type: 'acceptStagedImage' })}
+ onCancelQueueItem={(queueItemId) => dispatch({ queueItemId, type: 'cancelQueueItem' })}
+ onCycle={(direction) => dispatch({ direction, type: 'cycleStagedImage' })}
+ onDiscardAll={() => dispatch({ type: 'discardAllStagedImages' })}
+ onDiscardSelected={() => dispatch({ type: 'discardSelectedStagedImage' })}
+ onSelectImage={(imageIndex) => dispatch({ imageIndex, type: 'setStagedImageIndex' })}
+ onSetAutoSwitch={(mode) => dispatch({ mode, type: 'setCanvasStagingAutoSwitch' })}
+ onToggleThumbnails={() => dispatch({ type: 'toggleCanvasStagingThumbnailsVisibility' })}
+ onToggleVisibility={() => dispatch({ type: 'toggleCanvasStagingVisibility' })}
+ />
+ </CanvasBottomOverlay.Staging>
+ ) : null}
+ <CanvasBottomOverlay.Controls>
+ <CanvasBottomControls
+ documentHeight={document.height}
+ documentWidth={document.width}
+ engine={engine}
+ isExternalInteractionLocked={isInteractionLocked}
+ operation={operation}
+ />
+ </CanvasBottomOverlay.Controls>
+ </CanvasBottomOverlay.Root>
+ </CanvasSurfaceContextLayout>
</Box>
);
};