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(layers): ctrl+c copies the focused layer to the clipboard

Josh Corbett84123c8826f2
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
index 2fda333017..1307576a05 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.browser.test.tsx
@@ -137,6 +137,9 @@ const manyLayers = (count: number): CanvasNodeContract[] =>
let dispatchExternal: (mutation: CanvasProjectMutation) => void = () => undefined;
const thumbnailRequests = vi.fn();
+const exportBakedLayerBlob = vi.fn(() =>
+ Promise.resolve({ blob: new Blob(['png'], { type: 'image/png' }), status: 'ok' as const })
+);
const refusalChecks = vi.fn();
const revealRequests = vi.fn();
@@ -179,7 +182,7 @@ const Harness = ({ initialNodes }: { initialNodes: CanvasNodeContract[] }) => {
};
},
},
- exports: { hasExportableLayerContent: () => false },
+ exports: { exportBakedLayerBlob: exportBakedLayerBlob, hasExportableLayerContent: () => false },
interaction: { get: () => false },
layers: {
commitPrepared: (_label: string, edit: PreparedDocumentEdit) => {
@@ -388,6 +391,19 @@ describe('LayersTree keyboard and accessibility', () => {
paint('bottom', 'Bottom'),
];
+ it('copies the focused layer to the clipboard on ctrl+c', async () => {
+ await renderTree(nested());
+ const write = vi.spyOn(navigator.clipboard, 'write').mockResolvedValue();
+ try {
+ treeitem('Top').focus();
+ await act(() => userEvent.keyboard('{Control>}c{/Control}'));
+ await expect.poll(() => write.mock.calls.length).toBe(1);
+ expect(exportBakedLayerBlob).toHaveBeenCalledWith('top', { includeDisabled: true });
+ } finally {
+ write.mockRestore();
+ }
+ });
+
it('exposes one tab stop across every control and full tree semantics', async () => {
await renderTree(nested());
expect(tabStops()).toHaveLength(1);
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
index 90a9affdb1..f2963e9f15 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayersTree.tsx
@@ -9,6 +9,7 @@ import { Box, Text } from '@chakra-ui/react';
import { DndContext, DragOverlay, pointerWithin, PointerSensor, useSensor, useSensors } from '@dnd-kit/core';
import { restrictToVerticalAxis } from '@dnd-kit/modifiers';
import { useMountEffect } from '@platform/react/useMountEffect';
+import { toaster } from '@platform/ui';
import { Scrollable } from '@platform/ui/Scrollable';
import { getDocumentIndex, getDocumentNode, lookupDocumentNodeState } from '@workbench/canvas-engine/api';
import {
@@ -40,6 +41,7 @@ import {
type ProjectedChildRow,
} from './layerChildRows';
import { clearLayerChildSelection, selectLayerChild, useLayerChildSelection } from './layerChildSelection';
+import { copyLayerToClipboard } from './layerExportActions';
import { createAdjustmentId } from './layerOps';
import {
flattenPanelRows,
@@ -307,6 +309,7 @@ export const LayersTree = ({
dispatch,
document,
editingLocked,
+ engine,
onRevealProperties,
panel,
panelRows,
@@ -323,6 +326,7 @@ export const LayersTree = ({
dispatch,
document,
editingLocked,
+ engine,
onRevealProperties,
panel,
panelRows,
@@ -400,6 +404,23 @@ export const LayersTree = ({
}
return;
}
+ if (
+ !childRow &&
+ !isHeaderKey(key) &&
+ (event.ctrlKey || event.metaKey) &&
+ !event.shiftKey &&
+ !event.altKey &&
+ event.key.toLowerCase() === 'c'
+ ) {
+ event.preventDefault();
+ const { engine: currentEngine } = latest.current;
+ if (currentEngine) {
+ void copyLayerToClipboard(key, { exportLayer: currentEngine.exports.exportBakedLayerBlob })
+ .then((status) => status === 'ok' || Promise.reject(new Error(status)))
+ .catch(() => toaster.create({ title: t('widgets.layers.actions.copyFailed'), type: 'warning' }));
+ }
+ return;
+ }
if (childRow && (event.key === 'Delete' || event.key === 'Backspace')) {
event.preventDefault();
if (!latest.current.editingLocked) {
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.test.ts
index 5b630e4e14..16e2f4b730 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.test.ts
@@ -1,7 +1,7 @@
import { accountLifecycle } from '@platform/state/accountLifecycle';
import { describe, expect, it, vi } from 'vitest';
-import { copyBlobToClipboard, saveLayerToAssets } from './layerExportActions';
+import { copyBlobToClipboard, copyLayerToClipboard, saveLayerToAssets } from './layerExportActions';
describe('copyBlobToClipboard', () => {
it('writes a ClipboardItem using the blob MIME type', async () => {
@@ -32,6 +32,34 @@ describe('copyBlobToClipboard', () => {
});
});
+describe('copyLayerToClipboard', () => {
+ it('bakes the layer with disabled content and writes it; a refused export reports its status', async () => {
+ const write = vi.fn(() => Promise.resolve());
+ class TestClipboardItem {
+ presentationStyle: PresentationStyle = 'unspecified';
+ types: string[] = [];
+ constructor(items: Record<string, Blob>) {
+ this.types = Object.keys(items);
+ }
+ getType(): Promise<Blob> {
+ return Promise.resolve(new Blob());
+ }
+ }
+ const blob = new Blob(['pixels'], { type: 'image/png' });
+ const exportLayer = vi.fn((layerId: string) =>
+ Promise.resolve(layerId === 'ok' ? { blob, status: 'ok' as const } : { status: 'empty' as const })
+ );
+ const deps = { ClipboardItemCtor: TestClipboardItem, clipboard: { write }, exportLayer };
+
+ await expect(copyLayerToClipboard('ok', deps)).resolves.toBe('ok');
+ expect(exportLayer).toHaveBeenCalledWith('ok', { includeDisabled: true });
+ expect(write).toHaveBeenCalledTimes(1);
+
+ await expect(copyLayerToClipboard('blank', deps)).resolves.toBe('empty');
+ expect(write).toHaveBeenCalledTimes(1);
+ });
+});
+
describe('saveLayerToAssets', () => {
it('threads the account signal through export and upload', async () => {
const owner = accountLifecycle.activate('layer-export-a', ':user:layer-export-a');
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.ts
index f88835f396..de7316e78f 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerExportActions.ts
@@ -27,6 +27,23 @@ export const copyBlobToClipboard = async (blob: Blob, deps: CopyBlobToClipboardD
type LayerAssetExportFailureStatus = Exclude<ExportBakedLayerBlobResult['status'], 'ok'>;
type LayerAssetExportResult = { status: 'ok'; blob: Blob } | { status: LayerAssetExportFailureStatus };
+interface CopyLayerToClipboardDeps extends CopyBlobToClipboardDeps {
+ exportLayer(layerId: string, options: { includeDisabled: boolean }): Promise<LayerAssetExportResult>;
+}
+
+/** Bakes the layer (disabled or not) to PNG and puts it on the clipboard; a failed export reports its status. */
+export const copyLayerToClipboard = async (
+ layerId: string,
+ { exportLayer, ...clipboard }: CopyLayerToClipboardDeps
+): Promise<'ok' | LayerAssetExportFailureStatus> => {
+ const result = await exportLayer(layerId, { includeDisabled: true });
+ if (result.status !== 'ok') {
+ return result.status;
+ }
+ await copyBlobToClipboard(result.blob, clipboard);
+ return 'ok';
+};
+
interface SaveLayerToAssetsDeps {
exportLayer(
layerId: string,