From afe594b8f6b30e9b11caa8969ae586a727fc5071 Mon Sep 17 00:00:00 2001 From: xycld Date: Wed, 18 Nov 2026 11:03:05 -0500 Subject: [PATCH] feat(studio): pick several cards with a box and find your way with a minimap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A left drag on the empty board now draws a box that picks every card it touches; Shift, Ctrl or Cmd clicks add or remove one, Ctrl+A picks all, Esc keeps only the selected card. Picked cards move together, and a bar over them offers 排整齐 and 删除 (asked once; Delete does the same). Panning moves to the right or middle button, Space held, or the 平移 tool; a right-click that does not move still opens the menu. A minimap in the board's corner shows every card and the part on screen; a press there moves the view, and new cards placed near it keep clear of it. F fits. --- apps/desktop/e2e/specs/board.e2e.ts | 190 +++++++- .../src/renderer/src/pages/studio/Board.tsx | 418 ++++++++++++++++-- .../src/pages/studio/BoardControls.tsx | 35 ++ .../renderer/src/pages/studio/CanvasCard.tsx | 14 +- .../src/renderer/src/pages/studio/Minimap.tsx | 147 ++++++ .../renderer/src/pages/studio/StudioPage.tsx | 38 +- .../src/renderer/src/pages/studio/Toolbar.tsx | 13 +- .../renderer/src/pages/studio/selection.ts | 86 ++++ .../test/renderer/studio/selection.test.ts | 61 +++ docs/reference/studio-node-board.md | 8 +- 10 files changed, 948 insertions(+), 62 deletions(-) create mode 100644 apps/desktop/src/renderer/src/pages/studio/Minimap.tsx create mode 100644 apps/desktop/src/renderer/src/pages/studio/selection.ts create mode 100644 apps/desktop/test/renderer/studio/selection.test.ts diff --git a/apps/desktop/e2e/specs/board.e2e.ts b/apps/desktop/e2e/specs/board.e2e.ts index 888317ab..ed5dae18 100644 --- a/apps/desktop/e2e/specs/board.e2e.ts +++ b/apps/desktop/e2e/specs/board.e2e.ts @@ -7,6 +7,7 @@ import { describe, expect, it } from 'vitest' import type { Page } from 'playwright-core' import type { CanvasInputs, EngineFeatures, JobRecord } from '@shared/types' import { settle, useApp, type E2eApp } from '../support/app' +import { expectVisualRules } from '../support/visual' const ENGINE_JOBS = 'http:POST /api/engine-jobs' @@ -115,7 +116,8 @@ async function openBlankMenu(page: Page, from?: { right: number; top: number }): const board = (await page.locator('[data-board]').boundingBox())! const at = from ? { x: board.x + board.width - from.right, y: board.y + from.top } - : { x: board.x + board.width - 120, y: board.y + board.height - 120 } + : // Near the bottom-right corner, clear of the minimap that sits in it. + { x: board.x + board.width - 260, y: board.y + board.height - 80 } await page.mouse.click(at.x, at.y, { button: 'right' }) await page.getByRole('menu', { name: '画板' }).waitFor() return at @@ -328,6 +330,10 @@ describe('studio board', () => { return box.x + box.width <= board.x + board.width && box.y + box.height <= board.y + board.height } await expect.poll(inside).toBe(true) + // …and clear of the minimap in that corner. + const map = (await page.locator('[data-minimap]').boundingBox())! + const placed = (await page.locator('article[data-canvas-kind="image"][aria-current="true"]').boundingBox())! + expect(placed.x + placed.width <= map.x || placed.y + placed.height <= map.y).toBe(true) }) it('right-click on a card shows its menu; on a wire, 断开连线', async () => { @@ -443,23 +449,42 @@ describe('studio board', () => { await expect.poll(async () => (await savedY())!, { timeout: 10_000 }).toBeGreaterThan(before + 40) }) - it('pans by dragging the empty board and zooms with the wheel; new cards land where the click was', async () => { + it('pans with the right or middle button or Space held; a right-click that does not move still opens the menu', async () => { const page = await openBoard(app) const card = page.locator('article[data-canvas-card="canvas-1"]') - const start = (await card.boundingBox())! const board = (await page.locator('[data-board]').boundingBox())! - const from = { x: board.x + board.width - 60, y: board.y + board.height - 60 } - await page.mouse.move(from.x, from.y) + const from = { x: board.x + board.width - 300, y: board.y + board.height - 60 } + const offset = async (): Promise<{ x: number; y: number }> => { + const box = (await card.boundingBox())! + return { x: Math.round(box.x), y: Math.round(box.y) } + } + + for (const button of ['right', 'middle'] as const) { + const start = await offset() + await page.mouse.move(from.x, from.y) + await page.mouse.down({ button }) + await page.mouse.move(from.x - 120, from.y - 80, { steps: 5 }) + // The board follows the pointer while it is still held. + expect(await offset()).toEqual({ x: start.x - 120, y: start.y - 80 }) + await page.mouse.up({ button }) + await settle(page) + expect(await offset()).toEqual({ x: start.x - 120, y: start.y - 80 }) + await page.mouse.move(from.x, from.y) + } + // A right-button pan does not leave the menu behind. + expect(await page.getByRole('menu', { name: '画板' }).count()).toBe(0) + + // Space held over the board: a left drag pans, even when it starts on a card. + const start = await offset() + const header = await center(page, 'article[data-canvas-card="canvas-1"] header') + await page.mouse.move(header.x + 40, header.y) + await page.keyboard.down(' ') await page.mouse.down() - await page.mouse.move(from.x - 120, from.y - 80, { steps: 5 }) - // The board follows the pointer while it is still held. - const during = (await card.boundingBox())! - expect(Math.round(during.x - start.x)).toBe(-120) - expect(Math.round(during.y - start.y)).toBe(-80) + await page.mouse.move(header.x - 20, header.y + 30, { steps: 4 }) await page.mouse.up() + await page.keyboard.up(' ') await settle(page) - const after = (await card.boundingBox())! - expect(Math.round(after.x - start.x)).toBe(-120) + expect(await offset()).toEqual({ x: start.x - 60, y: start.y + 30 }) const zoom = page.getByRole('toolbar', { name: '画布工具' }).getByRole('button', { name: /^\d+%$/ }) const zoomBefore = await zoom.textContent() @@ -474,8 +499,149 @@ describe('studio board', () => { const created = (await page.locator('article[data-canvas-kind="layout"][aria-current="true"]').boundingBox())! expect(Math.abs(created.x - at.x)).toBeLessThan(4) expect(Math.abs(created.y - at.y)).toBeLessThan(4) + + // F shows every card again. + await page.keyboard.press('f') + for (const id of ['img-1', 'img-2', 'canvas-1']) { + const box = (await page.locator(`article[data-canvas-card="${id}"]`).boundingBox())! + expect(box.x).toBeGreaterThanOrEqual(board.x) + expect(box.x + box.width).toBeLessThanOrEqual(board.x + board.width) + } + }) + + it('draws a box on the empty board to pick cards; they move together, 排整齐 lines them up, 删除 asks once', async () => { + const page = await openBoard(app) + const saved = (id: string): Promise<{ x: number; y: number } | undefined> => + page.evaluate((cardId) => { + const found = window.__mock.state.projects[0]?.canvases.find((canvas) => canvas.id === cardId) + return found ? { x: found.x, y: found.y } : undefined + }, id) + const first = (await page.locator('article[data-canvas-card="img-1"]').boundingBox())! + const second = (await page.locator('article[data-canvas-card="img-2"] [data-card-body]').boundingBox())! + // From the empty board above and left of the pictures to the bottom of the second one: it touches both, not the 画面. + await page.mouse.move(first.x - 16, first.y - 16) + await page.mouse.down() + await page.mouse.move(second.x + second.width / 2, second.y + second.height - 10, { steps: 6 }) + await expect.poll(() => page.locator('[data-select-box]').count()).toBe(1) + await page.mouse.up() + expect(await page.locator('[data-select-box]').count()).toBe(0) + + const bar = page.getByRole('toolbar', { name: '已选 2 张' }) + await bar.waitFor() + for (const id of ['img-1', 'img-2']) + expect(await page.locator(`article[data-canvas-card="${id}"]`).getAttribute('aria-label')).toContain('已选中') + expect(await page.locator('article[data-canvas-card="canvas-1"]').getAttribute('aria-label')).not.toContain('已选中') + // The left panel shows the first picked card. + await expect.poll(() => page.evaluate(() => window.__mock.state.projects[0]?.selectedCanvasId), { timeout: 10_000 }).toBe('img-1') + + // Dragging one picked card moves the other by the same amount. + const before = { a: (await saved('img-1'))!, b: (await saved('img-2'))! } + const header = await center(page, 'article[data-canvas-card="img-2"] header') + await page.mouse.move(header.x + 30, header.y) + await page.mouse.down() + await page.mouse.move(header.x + 130, header.y + 40, { steps: 6 }) + await page.mouse.up() + await expect.poll(async () => (await saved('img-2'))!.x - before.b.x, { timeout: 10_000 }).toBeGreaterThan(60) + const moved = { a: (await saved('img-1'))!, b: (await saved('img-2'))! } + expect(moved.a.x - before.a.x).toBe(moved.b.x - before.b.x) + expect(moved.a.y - before.a.y).toBe(moved.b.y - before.b.y) + expect(await bar.isVisible()).toBe(true) + + // 排整齐: two cards side by side, top edges level. + await bar.getByRole('button', { name: '排整齐' }).click() + await expect.poll(async () => (await saved('img-1'))!.y === (await saved('img-2'))!.y, { timeout: 10_000 }).toBe(true) + expect((await saved('img-2'))!.x).toBeGreaterThan((await saved('img-1'))!.x) + await app.screenshot('board-picked-light') + + await bar.getByRole('button', { name: '删除' }).click() + const dialog = page.getByRole('dialog', { name: '删除这 2 张卡片?' }) + await dialog.waitFor() + await dialog.getByRole('button', { name: '删除' }).click() + await expect.poll(() => page.locator('article[data-canvas-card]').count()).toBe(1) + expect(await page.locator('article[data-canvas-card="canvas-1"]').count()).toBe(1) + expect(await bar.count()).toBe(0) }) + it('Shift-click adds a card, Esc keeps only the selected one, Ctrl+A picks all, Delete asks once', async () => { + const page = await openBoard(app) + const bar = page.locator('[data-selection-bar]') + await page + .locator('article[data-canvas-card="img-1"] header button') + .first() + .click({ modifiers: ['Shift'] }) + await expect.poll(() => bar.getAttribute('aria-label')).toBe('已选 2 张') + // A Shift-click on a picked card takes it out again. + await page + .locator('article[data-canvas-card="img-1"] header button') + .first() + .click({ modifiers: ['Shift'] }) + await expect.poll(() => bar.count()).toBe(0) + await page + .locator('article[data-canvas-card="img-2"] header button') + .first() + .click({ modifiers: ['Shift'] }) + await expect.poll(() => bar.getAttribute('aria-label')).toBe('已选 2 张') + await page.keyboard.press('Escape') + await expect.poll(() => bar.count()).toBe(0) + expect(await page.locator('article[data-canvas-card="img-2"]').getAttribute('aria-current')).toBe('true') + + await page.keyboard.press('Control+a') + await expect.poll(() => bar.getAttribute('aria-label')).toBe('已选 3 张') + await page.keyboard.press('Delete') + const dialog = page.getByRole('dialog', { name: '删除这 3 张卡片?' }) + await dialog.waitFor() + await dialog.getByRole('button', { name: '取消' }).click() + expect(await page.locator('article[data-canvas-card]').count()).toBe(3) + + // A plain click on one card picks it alone. + await page.locator('article[data-canvas-card="canvas-1"] header button').first().click() + await expect.poll(() => bar.count()).toBe(0) + + // Picking ends when another card is selected some other way; an old member selected again comes alone. + await page.keyboard.press('Control+a') + await expect.poll(() => bar.getAttribute('aria-label')).toBe('已选 3 张') + await openBlankMenu(page) + await page.getByRole('menuitem', { name: '新建画面' }).click() + await expect.poll(() => page.locator('article[data-canvas-card]').count()).toBe(4) + await expect.poll(() => bar.count()).toBe(0) + await page.locator('article[data-canvas-card="img-1"] header').click({ button: 'right' }) + await page.getByRole('menu', { name: '小猫照片 操作' }).waitFor() + await page.keyboard.press('Escape') + expect(await bar.count()).toBe(0) + }) + + for (const theme of ['light', 'dark'] as const) { + it(`shows the whole board in a minimap (${theme}); a press there moves the view, and it can be hidden`, async () => { + const page = await openBoard(app, { theme }) + const minimap = page.getByRole('group', { name: '小地图' }) + await minimap.waitFor() + expect(await minimap.locator('[data-minimap-card]').count()).toBe(3) + await page.keyboard.press('Control+a') + await page.getByRole('toolbar', { name: '已选 3 张' }).waitFor() + expect(await minimap.locator('[data-minimap-card][data-picked]').count()).toBe(3) + // The group stays quiet: no + on the selected 画面 until it is hovered. + await page.mouse.move(5, 5) + await expect.poll(() => page.locator('[data-input-adder]').count()).toBe(0) + await expectVisualRules(page, `board minimap and picked cards (${theme})`) + await app.screenshot(`board-minimap-${theme}`) + + // A press on the 画面 in the minimap centres the view on it. + const board = (await page.locator('[data-board]').boundingBox())! + const dot = await center(page, '[data-minimap-card="img-2"]') + await page.mouse.click(dot.x, dot.y) + await settle(page) + const target = await center(page, 'article[data-canvas-card="img-2"]') + expect(Math.abs(target.x - (board.x + board.width / 2))).toBeLessThan(20) + expect(Math.abs(target.y - (board.y + board.height / 2))).toBeLessThan(20) + + const toggle = page.getByRole('toolbar', { name: '画布工具' }).getByRole('button', { name: '小地图' }) + expect(await toggle.getAttribute('aria-pressed')).toBe('true') + await toggle.click() + expect(await minimap.count()).toBe(0) + expect(await toggle.getAttribute('aria-pressed')).toBe('false') + }) + } + it("pulls a wire from anywhere along a picture's right edge", async () => { const page = await openBoard(app) const body = (await page.locator('article[data-canvas-card="img-1"] [data-card-body]').boundingBox())! diff --git a/apps/desktop/src/renderer/src/pages/studio/Board.tsx b/apps/desktop/src/renderer/src/pages/studio/Board.tsx index 36106e1f..89c40c6b 100644 --- a/apps/desktop/src/renderer/src/pages/studio/Board.tsx +++ b/apps/desktop/src/renderer/src/pages/studio/Board.tsx @@ -21,6 +21,7 @@ import { nextFreeSpot, toBoardPoint, zoomAround, + type Rect, type View, } from './boardLayout' import { CanvasCard, type CardMenuAction, type CardStrip } from './CanvasCard' @@ -40,7 +41,9 @@ import { CardToolbar, MaskBar, Toolbar } from './Toolbar' import { useCardDrag } from './useCardDrag' import { useWireConnect } from './useWireConnect' import { useWireSelection } from './useWireSelection' -import { InputAdder, InputThumbs, StartRow } from './BoardControls' +import { InputAdder, InputThumbs, SelectionBar, StartRow } from './BoardControls' +import { MINIMAP_BOX, Minimap } from './Minimap' +import { boundsOf, boxBetween, cardsInBox, readingOrder, tidyPlaces, toggled } from './selection' import { BoardMenu, type BoardMenuEntry } from './BoardMenu' import type { PortActions } from './SlotMenu' import { inputSlots, socketY } from './ports' @@ -101,6 +104,8 @@ export interface BoardProps { onShowActivity: (jobId?: string) => void onCopy: (canvas: ProjectCanvas) => void onDelete: (canvas: ProjectCanvas) => void + /** Delete several picked cards at once (after asking). */ + onDeleteMany: (ids: string[]) => void /** What the engine can do (undefined while loading, null when none is usable). */ engineFeatures: EngineFeatures | null | undefined /** The 生成 footer's readiness: says why the mask tool and the link menu are off while the engine is not ready. */ @@ -157,6 +162,10 @@ function worldTransform(view: View): string { return `translate(${view.pan.x}px, ${view.pan.y}px) scale(${view.zoom})` } +function boxStyle(rect: Rect): Partial { + return { transform: `translate(${rect.x}px, ${rect.y}px)`, width: `${rect.w}px`, height: `${rect.h}px` } +} + function isTyping(target: EventTarget | null): boolean { return Boolean((target as HTMLElement | null)?.closest?.('input, textarea, select, [contenteditable="true"]')) } @@ -172,19 +181,42 @@ export function Board(props: BoardProps): React.JSX.Element { */ const viewRef = useRef(view) const world = useRef(null) + /** Who draws from the live view without re-rendering (the minimap). */ + const viewListeners = useRef(new Set<() => void>()) + const subscribeView = useCallback((listener: () => void) => { + viewListeners.current.add(listener) + return () => void viewListeners.current.delete(listener) + }, []) const showView = (next: View): void => { viewRef.current = next if (world.current) world.current.style.transform = worldTransform(next) + for (const listener of viewListeners.current) listener() } const changeView = useCallback((update: View | ((current: View) => View)) => { const next = typeof update === 'function' ? update(viewRef.current) : update // Written straight away too: React skips the style when it equals the last one it rendered, which a pan may have moved past. viewRef.current = next if (world.current) world.current.style.transform = worldTransform(next) + for (const listener of viewListeners.current) listener() setView(next) }, []) const [panning, setPanning] = useState(false) - const pan = useRef<{ pointerId: number; x: number; y: number; origin: View['pan'] } | null>(null) + const pan = useRef<{ + pointerId: number + x: number + y: number + origin: View['pan'] + /** A right-button pan: a press that does not move still opens the menu. */ + right?: { moved: boolean; target: HTMLElement; menu: { clientX: number; clientY: number } | null } + } | null>(null) + /** Space held over the board: a press anywhere pans. */ + const [spaceHeld, setSpaceHeld] = useState(false) + const overBoard = useRef(false) + /** After a right-button pan, the menu the button's release would open is not wanted. */ + const menuQuiet = useRef(0) + const [minimapOn, setMinimapOn] = useState(true) + const minimapOnRef = useRef(minimapOn) + minimapOnRef.current = minimapOn const [brush, setBrush] = useState({ mode: 'paint', size: BRUSH_DEFAULT }) const [maskCommand, setMaskCommand] = useState(undefined) /** Last pointer position over the board, where a pasted picture lands. */ @@ -199,14 +231,50 @@ export function Board(props: BoardProps): React.JSX.Element { ) // Results folded into a 画面's filmstrip are not cards on the board. const folded = useMemo(() => foldedRunIds(props.canvases), [props.canvases]) - /** A card being dragged: drawn where the pointer has it, and written to the project once, when it is let go. */ - const [dragAt, setDragAt] = useState<{ id: string; x: number; y: number } | null>(null) + /** + * Cards being dragged (one, or all picked ones), moved by dx, dy: drawn there, + * and written to the project once, when they are let go. + */ + const [dragAt, setDragAt] = useState<{ ids: readonly string[]; dx: number; dy: number } | null>(null) const onBoard = useMemo(() => boardCanvases(props.canvases, folded), [props.canvases, folded]) const cards = useMemo( - () => (dragAt ? onBoard.map((canvas) => (canvas.id === dragAt.id ? { ...canvas, x: dragAt.x, y: dragAt.y } : canvas)) : onBoard), + () => + dragAt + ? onBoard.map((canvas) => + dragAt.ids.includes(canvas.id) ? { ...canvas, x: canvas.x + dragAt.dx, y: canvas.y + dragAt.dy } : canvas, + ) + : onBoard, [onBoard, dragAt], ) + /** + * Cards picked together with the selected one (Shift or Ctrl clicks, a box, + * Ctrl+A). They count only while they include the selected card, so + * selecting a card any other way (a new card, the assistant) picks it alone. + */ + const [group, setGroup] = useState([]) + const selection = useMemo(() => { + const here = new Set(onBoard.map((canvas) => canvas.id)) + const primary = props.selectedCanvasId && here.has(props.selectedCanvasId) ? props.selectedCanvasId : null + const ids = group.filter((id) => here.has(id)) + if (primary && ids.length > 1 && ids.includes(primary)) return ids + return primary ? [primary] : [] + }, [group, onBoard, props.selectedCanvasId]) + const multi = selection.length > 1 + /** A box being drawn on the empty board: the cards picked before it (with Shift or Ctrl) and the ones it touches. */ + const [boxPick, setBoxPick] = useState<{ base: readonly string[]; touched: readonly string[] } | null>(null) + const box = useRef<{ + pointerId: number + from: Point + moved: boolean + base: readonly string[] + touched: readonly string[] + /** The box in viewport pixels, kept for the element that appears after the first move. */ + screen?: Rect + } | null>(null) + const boxElement = useRef(null) + const picked = useMemo(() => new Set(boxPick ? [...boxPick.base, ...boxPick.touched] : selection), [boxPick, selection]) + const [hoveredWire, setHoveredWire] = useState(null) /** The box whose field is open; `fresh` while it is the one just drawn. */ const [editingRegion, setEditingRegion] = useState<{ canvasId: string; id: string; fresh: boolean } | null>(null) @@ -236,22 +304,29 @@ export function Board(props: BoardProps): React.JSX.Element { latest.current = props const zoomRef = useRef(view.zoom) zoomRef.current = view.zoom + /** The cards a drag moves and where the dragged one started. */ + const dragGroup = useRef<{ ids: readonly string[]; origin: Point }>({ ids: [], origin: { x: 0, y: 0 } }) // A drag step renders the whole board (wires follow the card), so steps within one frame render once. - const dragFrame = useRef({ frame: 0, at: null as { id: string; x: number; y: number } | null }) - const cardMoveRef = useRef((id: string, x: number, y: number) => { + const dragFrame = useRef({ frame: 0, at: null as { ids: readonly string[]; dx: number; dy: number } | null }) + const cardMoveRef = useRef((_id: string, x: number, y: number) => { const pending = dragFrame.current - pending.at = { id, x, y } + const { ids, origin } = dragGroup.current + pending.at = { ids, dx: x - origin.x, dy: y - origin.y } if (pending.frame) return pending.frame = requestAnimationFrame(() => { pending.frame = 0 setDragAt(pending.at) }) }) - const cardDropRef = useRef((id: string, x: number, y: number) => { + const cardDropRef = useRef((_id: string, x: number, y: number) => { cancelAnimationFrame(dragFrame.current.frame) dragFrame.current = { frame: 0, at: null } setDragAt(null) - latest.current.onMoveCanvas(id, x, y) + const { ids, origin } = dragGroup.current + for (const id of ids) { + const canvas = latest.current.canvases.find((item) => item.id === id) + if (canvas) latest.current.onMoveCanvas(id, canvas.x + x - origin.x, canvas.y + y - origin.y) + } }) const cardDrag = useCardDrag({ zoom: zoomRef, onMove: cardMoveRef, onDrop: cardDropRef }) const callbacks = useStableCallbacks() @@ -350,8 +425,21 @@ export function Board(props: BoardProps): React.JSX.Element { const margin = 16 // Past the right or bottom edge it moves back; the top-left corner always stays in sight. const fit = (value: number, low: number, high: number): number => Math.round(Math.max(low, Math.min(value, high))) - const fx = fit(rect.x, (margin - pan.x) / zoom, (size.width - margin - pan.x) / zoom - rect.w) - const fy = fit(rect.y, (FIT_PADDING.top - pan.y) / zoom, (size.height - margin - pan.y) / zoom - rect.h) + const top = (FIT_PADDING.top - pan.y) / zoom + const left = (margin - pan.x) / zoom + let fx = fit(rect.x, left, (size.width - margin - pan.x) / zoom - rect.w) + let fy = fit(rect.y, top, (size.height - margin - pan.y) / zoom - rect.h) + // Nor does it land under the minimap: above it when there is room, otherwise left of it. + if (minimapOnRef.current) { + const mapLeft = size.width - MINIMAP_BOX.margin - MINIMAP_BOX.width + const mapTop = size.height - MINIMAP_BOX.margin - MINIMAP_BOX.height + const under = fx * zoom + pan.x + rect.w * zoom > mapLeft - margin && fy * zoom + pan.y + rect.h * zoom > mapTop - margin + if (under) { + const above = Math.round((mapTop - margin - pan.y) / zoom - rect.h) + if (above >= top) fy = above + else fx = Math.max(Math.round(left), Math.round((mapLeft - margin - pan.x) / zoom - rect.w)) + } + } if (fx !== canvas.x || fy !== canvas.y) latest.current.onMoveCanvas(canvas.id, fx, fy) }, []) @@ -396,6 +484,74 @@ export function Board(props: BoardProps): React.JSX.Element { } }, [changeView]) + // Keys for the board: Space pans while held, Ctrl+A picks every card, Delete removes the picked ones, + // Esc drops the others, F shows everything. A hidden page (another route) or a dialog takes none. + const keysRef = useRef<(event: KeyboardEvent) => void>(() => undefined) + keysRef.current = (event) => { + if (!viewport.current?.offsetParent || isTyping(event.target) || document.querySelector('dialog[open]')) return + const key = event.key.toLowerCase() + const target = event.target as HTMLElement | null + if (key === ' ') { + // A focused control keeps Space for itself (a button presses on it). + if (target?.closest?.('button, a, summary, [role="button"], [role="menuitem"]')) return + if (!overBoard.current && !spaceHeld) return + event.preventDefault() + if (!spaceHeld) setSpaceHeld(true) + return + } + // The rest only while the board has focus (or nothing has): Ctrl+A in the assistant or the activity panel selects text there. + if (target && target !== document.body && !viewport.current.contains(target)) return + if ((event.ctrlKey || event.metaKey) && key === 'a' && !event.altKey) { + event.preventDefault() + const ids = readingOrder(onBoard).map((canvas) => canvas.id) + setGroup(ids) + if (ids[0] && !ids.includes(props.selectedCanvasId ?? '')) props.onSelectCanvas(ids[0]) + return + } + if (event.ctrlKey || event.metaKey || event.altKey) return + if ((key === 'delete' || key === 'backspace') && !props.selectedRegionId && !selectedWireId && selection.length) { + event.preventDefault() + if (multi) props.onDeleteMany(selection) + else { + const card = onBoard.find((canvas) => canvas.id === selection[0]) + if (card) props.onDelete(card) + } + } else if (key === 'escape' && multi) setGroup([]) + else if (key === 'f') fit() + } + useEffect(() => { + const onDown = (event: KeyboardEvent): void => keysRef.current(event) + const onUp = (event: KeyboardEvent): void => { + if (event.key === ' ') setSpaceHeld(false) + } + window.addEventListener('keydown', onDown) + window.addEventListener('keyup', onUp) + return () => { + window.removeEventListener('keydown', onDown) + window.removeEventListener('keyup', onUp) + } + }, []) + + // With several cards picked, a selected box on one of them would take Delete for itself. + useEffect(() => { + if (multi) latest.current.onSelectRegion(null) + }, [multi]) + // A card selected any other way (a new card, the assistant, a right-click) ends the group, + // so it cannot come back when an old member is selected again. + useEffect(() => { + setGroup((current) => (current.length && !current.includes(props.selectedCanvasId ?? '') ? [] : current)) + }, [props.selectedCanvasId]) + // The minimap's tint follows the board's size too (side panels opening, the window resized). + useEffect(() => { + const element = viewport.current + if (!element) return + const observer = new ResizeObserver(() => { + for (const listener of viewListeners.current) listener() + }) + observer.observe(element) + return () => observer.disconnect() + }, []) + // The 遮罩 tool only works on a canvas with a start picture. useEffect(() => { if (props.tool === 'mask' && !mask.enabled) props.onTool('select') @@ -413,10 +569,44 @@ export function Board(props: BoardProps): React.JSX.Element { setPanning(false) setView(viewRef.current) if (viewport.current?.hasPointerCapture(gesture.pointerId)) viewport.current.releasePointerCapture(gesture.pointerId) + if (!gesture.right) return + // Where the menu comes on the press (Linux), it waited for the release; where it comes after (Windows), it opens on its own. + if (gesture.right.moved) menuQuiet.current = performance.now() + 400 + else if (gesture.right.menu) openMenuAt(gesture.right.menu.clientX, gesture.right.menu.clientY, gesture.right.target) + } + + /** Ends a box: the touched cards join the picked ones; a press that did not move clears them. */ + const endBox = (): void => { + const gesture = box.current + if (!gesture) return + box.current = null + setBoxPick(null) + if (viewport.current?.hasPointerCapture(gesture.pointerId)) viewport.current.releasePointerCapture(gesture.pointerId) + if (!gesture.moved) { + if (!gesture.base.length) setGroup([]) + return + } + const ids = [...new Set([...gesture.base, ...gesture.touched])] + if (!ids.length) { + setGroup([]) + return + } + const ordered = readingOrder(latest.current.canvases.filter((canvas) => ids.includes(canvas.id))).map((canvas) => canvas.id) + const primary = latest.current.selectedCanvasId + setGroup(ordered) + if (!primary || !ordered.includes(primary)) latest.current.onSelectCanvas(ordered[0]!) + } + // Leaving the window ends any gesture: the button may be let go elsewhere. + const blurRef = useRef(() => undefined as void) + blurRef.current = () => { + endPan() + endBox() + setSpaceHeld(false) } useEffect(() => { - window.addEventListener('blur', endPan) - return () => window.removeEventListener('blur', endPan) + const onBlur = (): void => blurRef.current() + window.addEventListener('blur', onBlur) + return () => window.removeEventListener('blur', onBlur) }, []) const boardPoint = (clientX: number, clientY: number): { x: number; y: number } => { @@ -429,26 +619,82 @@ export function Board(props: BoardProps): React.JSX.Element { // Pasting needs the board focused; a press anywhere that is not a control focuses it. if (!target.closest('button, input, textarea, select, a, [contenteditable="true"]')) viewport.current?.focus({ preventScroll: true }) if (!target.closest('[data-wire], [data-wire-chip]')) setSelectedWireId(null) - if (target.closest('[data-board-ui]')) return - const onCard = Boolean(target.closest('[data-canvas-card], [data-new-canvas]')) - // Middle button, the 平移 tool, or a drag on the empty board pans. - const wantsPan = event.button === 1 || (event.button === 0 && (props.tool === 'pan' || !onCard)) - if (!wantsPan || pan.current) return + if (target.closest('[data-board-ui]') || pan.current || box.current) return + const onCard = Boolean(target.closest('[data-canvas-card]')) + // The middle or right button, Space, or the 平移 tool pans; a left drag on the empty board draws a box. + const wantsPan = event.button === 1 || event.button === 2 || (event.button === 0 && (props.tool === 'pan' || spaceHeld)) + if (wantsPan) { + event.preventDefault() + event.stopPropagation() + pan.current = { + pointerId: event.pointerId, + x: event.clientX, + y: event.clientY, + origin: viewRef.current.pan, + ...(event.button === 2 ? { right: { moved: false, target, menu: null } } : {}), + } + setPanning(event.button !== 2) + event.currentTarget.setPointerCapture(event.pointerId) + return + } + if (event.button !== 0 || onCard) return event.preventDefault() event.stopPropagation() - if (!onCard && event.button === 0) props.onSelectRegion(null) - pan.current = { pointerId: event.pointerId, x: event.clientX, y: event.clientY, origin: viewRef.current.pan } - setPanning(true) + props.onSelectRegion(null) + const rect = viewport.current!.getBoundingClientRect() + const additive = event.shiftKey || event.ctrlKey || event.metaKey + box.current = { + pointerId: event.pointerId, + from: { x: event.clientX - rect.left, y: event.clientY - rect.top }, + moved: false, + base: additive ? selection : [], + touched: [], + } event.currentTarget.setPointerCapture(event.pointerId) } const onPointerMove = (event: React.PointerEvent): void => { pointer.current = boardPoint(event.clientX, event.clientY) + const drawing = box.current + if (drawing && drawing.pointerId === event.pointerId) { + if (event.buttons === 0) { + endBox() + return + } + const rect = viewport.current!.getBoundingClientRect() + const to = { x: event.clientX - rect.left, y: event.clientY - rect.top } + if (!drawing.moved && Math.abs(to.x - drawing.from.x) + Math.abs(to.y - drawing.from.y) < 4) return + if (!drawing.moved) { + drawing.moved = true + setBoxPick({ base: drawing.base, touched: [] }) + } + const screen = boxBetween(drawing.from, to) + drawing.screen = screen + if (boxElement.current) Object.assign(boxElement.current.style, boxStyle(screen)) + const { zoom, pan: offset } = viewRef.current + const touched = cardsInBox(orderedRef.current, { + x: (screen.x - offset.x) / zoom, + y: (screen.y - offset.y) / zoom, + w: screen.w / zoom, + h: screen.h / zoom, + }) + // Re-render only when the touched cards change, not on every move. + if (touched.join() !== drawing.touched.join()) { + drawing.touched = touched + setBoxPick({ base: drawing.base, touched }) + } + return + } const gesture = pan.current if (!gesture || gesture.pointerId !== event.pointerId) return if (event.buttons === 0) { endPan() return } + if (gesture.right && !gesture.right.moved) { + if (Math.abs(event.clientX - gesture.x) + Math.abs(event.clientY - gesture.y) < 4) return + gesture.right.moved = true + setPanning(true) + } showView({ ...viewRef.current, pan: { x: gesture.origin.x + event.clientX - gesture.x, y: gesture.origin.y + event.clientY - gesture.y }, @@ -456,7 +702,9 @@ export function Board(props: BoardProps): React.JSX.Element { } const startCardDrag = (canvas: ProjectCanvas, event: React.PointerEvent): void => { - if (event.button !== 0 || props.tool === 'pan') return + if (event.button !== 0 || props.tool === 'pan' || spaceHeld) return + // A picked card takes the other picked ones along. + dragGroup.current = { ids: multi && selection.includes(canvas.id) ? selection : [canvas.id], origin: { x: canvas.x, y: canvas.y } } cardDrag.start(canvas, event) } @@ -610,14 +858,48 @@ export function Board(props: BoardProps): React.JSX.Element { !canvas.inputs?.references?.links.length && !canvas.inputs?.styles?.links.length + /** Shift, Ctrl or ⌘ click: add the card to the picked ones (it becomes the selected one) or take it out. */ + const pickToggle = (id: string): void => { + const next = toggled(selection, id) + if (!next.length) return + setGroup(next) + if (next.includes(id)) props.onSelectCanvas(id) + else if (id === props.selectedCanvasId) props.onSelectCanvas(next[next.length - 1]!) + } + + /** 排整齐: the picked cards in rows, from where the group starts. */ + const tidyPicked = (): void => { + const places = tidyPlaces(onBoard.filter((canvas) => selection.includes(canvas.id))) + for (const place of places) props.onMoveCanvas(place.id, place.x, place.y) + } + + /** The minimap moves the view: centred on a board point, at the same zoom. */ + const centerAt = (point: Point, done: boolean): void => { + const size = viewportSize() + const { zoom } = viewRef.current + const next = { zoom, pan: { x: size.width / 2 - point.x * zoom, y: size.height / 2 - point.y * zoom } } + if (done) changeView(next) + else showView(next) + } + const openMenu = (event: React.MouseEvent): void => { // Text fields keep the system menu (copy, paste). if (isTyping(event.target)) return event.preventDefault() + const gesture = pan.current + if (gesture?.right) { + // The right button is still down (the menu comes on the press here): it may become a pan. + gesture.right.menu = { clientX: event.clientX, clientY: event.clientY } + return + } + if (performance.now() < menuQuiet.current) return + openMenuAt(event.clientX, event.clientY, event.target as HTMLElement) + } + + const openMenuAt = (clientX: number, clientY: number, target: HTMLElement): void => { if (connect.draft) return const rect = viewport.current?.getBoundingClientRect() - const at = { x: event.clientX - (rect?.left ?? 0), y: event.clientY - (rect?.top ?? 0) } - const target = event.target as HTMLElement + const at = { x: clientX - (rect?.left ?? 0), y: clientY - (rect?.top ?? 0) } const cardId = target.closest('article[data-canvas-card]')?.dataset.canvasCard const card = cardId ? cards.find((canvas) => canvas.id === cardId) : undefined const regionId = target.closest('[data-region-id]')?.dataset.regionId @@ -643,12 +925,15 @@ export function Board(props: BoardProps): React.JSX.Element { } // The board's own controls (toolbar, menus) have no menu of their own. if (target.closest('[data-board-ui]')) return - setContextMenu({ at, label: '画板', entries: blankMenu(boardPoint(event.clientX, event.clientY)) }) + setContextMenu({ at, label: '画板', entries: blankMenu(boardPoint(clientX, clientY)) }) } // Wire labels show for the cards being looked at: the selected one and the one under the pointer. const focusIds = new Set([props.selectedCanvasId, hoveredCard].filter((id): id is string => Boolean(id))) // The + that adds an input: on the hovered or selected 画面, below its linked inputs. - const adderCanvas = connect.draft ? undefined : cards.find((canvas) => canvas.id === (hoveredCard ?? props.selectedCanvasId)) + // With several cards picked, only the hovered one shows it: the group stays quiet. + const adderCanvas = connect.draft + ? undefined + : cards.find((canvas) => canvas.id === (hoveredCard ?? (multi ? null : props.selectedCanvasId))) const adderSlots = adderCanvas?.kind === 'layout' ? slotsFor(adderCanvas).filter((slot) => !slot.linked) : [] const adderPorts = adderCanvas ? wires.filter((wire) => wire.kind === 'input' && wire.targetId === adderCanvas.id).map((wire) => wire.to.y) @@ -667,6 +952,7 @@ export function Board(props: BoardProps): React.JSX.Element { } : null const selectedDrawn = selected ? (cards.find((canvas) => canvas.id === selected.id) ?? selected) : null + const pickedBounds = multi && !boxPick && !connect.draft ? boundsOf(cards.filter((canvas) => selection.includes(canvas.id))) : null // An empty project: one blank 画面, with the ways to start from a picture under it. const only = cards.length === 1 ? cards[0]! : null const startFor = @@ -686,15 +972,28 @@ export function Board(props: BoardProps): React.JSX.Element { data-panning={panning || undefined} className={cn( 'relative h-full min-h-0 overflow-hidden bg-board outline-none', - (panning || props.tool === 'pan') && 'cursor-grab', + (panning || spaceHeld || props.tool === 'pan') && 'cursor-grab', panning && 'cursor-grabbing', )} onPointerDownCapture={onPointerDownCapture} onPointerMove={onPointerMove} - onPointerUp={endPan} - onPointerCancel={endPan} + onPointerUp={() => { + endPan() + endBox() + }} + onPointerCancel={() => { + endPan() + endBox() + }} onLostPointerCapture={(event) => { if (pan.current?.pointerId === event.pointerId) endPan() + if (box.current?.pointerId === event.pointerId) endBox() + }} + onPointerEnter={() => { + overBoard.current = true + }} + onPointerLeave={() => { + overBoard.current = false }} onAuxClick={(event) => { if (event.button === 1) event.preventDefault() @@ -710,7 +1009,15 @@ export function Board(props: BoardProps): React.JSX.Element { onPaste={onPaste} onContextMenu={openMenu} > - + {props.tool === 'mask' && mask.enabled ? ( props.onChangeDescription(canvas.id, text), stopParse: () => props.onStopParse(canvas.id), portDown: (event: React.PointerEvent) => connect.start(canvas, event), - select: () => { + select: (keys?: { shiftKey: boolean; ctrlKey: boolean; metaKey: boolean }) => { if (cardDrag.isDragTail()) return + if (keys && (keys.shiftKey || keys.ctrlKey || keys.metaKey)) { + pickToggle(canvas.id) + return + } + setGroup([]) if (canvas.id !== props.selectedCanvasId) props.onSelectCanvas(canvas.id) }, selectRegion: (id: string | null) => props.onSelectRegion(id), @@ -813,7 +1125,8 @@ export function Board(props: BoardProps): React.JSX.Element { } : null const busy = props.busyIds.has(canvas.id) - const isSelected = canvas.id === props.selectedCanvasId + // With several picked, none of them is the one being edited: a drag on any face moves them all. + const isSelected = canvas.id === props.selectedCanvasId && !multi const init = canvas.kind === 'layout' ? canvas.inputs?.init : undefined const initPath = init ? linkSourceImage(init.link, props.canvases) : null const showMask = Boolean(init && isSelected && (props.tool === 'mask' || canvas.inputs?.mask)) @@ -837,7 +1150,9 @@ export function Board(props: BoardProps): React.JSX.Element { return shown })} onMenuSelect={on.menuSelect} - selected={isSelected} + // While a box is drawn, only the cards it will pick are ringed. + selected={isSelected && (!boxPick || picked.has(canvas.id))} + marked={picked.has(canvas.id)} dragging={cardDrag.draggingId === canvas.id} tool={props.tool} selectedRegionId={props.selectedRegionId} @@ -901,7 +1216,7 @@ export function Board(props: BoardProps): React.JSX.Element { ) })} - {selected && selected.kind === 'layout' && !selected.upscale && !connect.draft ? ( + {selected && selected.kind === 'layout' && !selected.upscale && !connect.draft && !multi ? ( // A 画面's tools sit on the selected card, above its title.
) : null} + {pickedBounds ? ( + props.onDeleteMany(selection)} + /> + ) : null} {startFor ? ( + {boxPick ? ( + // The box being drawn: a tint, no outline (see ui-rules). Moved by onPointerMove without a render. +
{ + boxElement.current = element + if (element && box.current?.screen) Object.assign(element.style, boxStyle(box.current.screen)) + }} + aria-hidden="true" + data-select-box + className="pointer-events-none absolute left-0 top-0 z-[8] rounded-[4px] bg-accent-soft" + /> + ) : null} + {minimapOn ? ( + linkSourceImage({ canvasId: id }, props.canvases)} + picked={picked} + busy={props.busyIds} + view={() => viewRef.current} + subscribe={subscribeView} + boardSize={viewportSize} + onCenter={centerAt} + /> + ) : null} {contextMenu ? ( ) } + +/** Above several picked cards: how many, 排整齐 and 删除. Moving them is a drag on any of them. */ +export function SelectionBar({ + point, + zoom, + count, + onTidy, + onDelete, +}: { + /** Top middle of the picked cards, in board pixels. */ + point: Point + zoom: number + count: number + onTidy: () => void + onDelete: () => void +}): React.JSX.Element { + return ( +
+ 已选 {count} 张 + + +
+ ) +} diff --git a/apps/desktop/src/renderer/src/pages/studio/CanvasCard.tsx b/apps/desktop/src/renderer/src/pages/studio/CanvasCard.tsx index 34a9a300..1c0eae98 100644 --- a/apps/desktop/src/renderer/src/pages/studio/CanvasCard.tsx +++ b/apps/desktop/src/renderer/src/pages/studio/CanvasCard.tsx @@ -49,7 +49,10 @@ export type CardMenuEntry = Omit, 'on export interface CanvasCardProps { canvas: ProjectCanvas view: CardJobView + /** The one selected card: its face can be edited, its footer and menu stay out. */ selected: boolean + /** Picked along with others (or alone): ringed like the selected card, nothing more. */ + marked?: boolean dragging: boolean tool: BoardTool selectedRegionId: string | null @@ -105,7 +108,8 @@ export interface CanvasCardProps { onChangeDescription?: (text: string) => void /** Things to try, on the face of a new board's only 画面; a click writes one as the description. */ ideas?: readonly string[] - onSelect: () => void + /** A click on the card; Shift, Ctrl or ⌘ adds it to the picked cards or takes it out. */ + onSelect: (keys?: { shiftKey: boolean; ctrlKey: boolean; metaKey: boolean }) => void onSelectRegion: (id: string | null) => void onChangeLayers: (update: (layers: ProjectLayer[]) => ProjectLayer[]) => void /** A box was just drawn: its field opens. */ @@ -274,9 +278,9 @@ function CanvasCardView(props: CanvasCardProps): React.JSX.Element { data-canvas-card={canvas.id} data-canvas-kind={canvas.kind} data-node={canvas.upscale ? 'upscale' : undefined} - aria-label={`${canvas.title}${selected ? ',已选中' : ''}`} + aria-label={`${canvas.title}${selected || props.marked ? ',已选中' : ''}`} aria-current={selected ? 'true' : undefined} - onClick={props.onSelect} + onClick={(event) => props.onSelect(event)} onPointerEnter={() => props.onHoverChange?.(true)} onPointerLeave={() => props.onHoverChange?.(false)} className={cn('group/card absolute flex flex-col transition-opacity', props.inert && 'opacity-40', props.dragging && 'z-10')} @@ -293,7 +297,7 @@ function CanvasCardView(props: CanvasCardProps): React.JSX.Element { type="button" onClick={(event) => { event.stopPropagation() - props.onSelect() + props.onSelect(event) }} className="min-w-0 truncate text-left font-semibold text-ink" > @@ -358,7 +362,7 @@ function CanvasCardView(props: CanvasCardProps): React.JSX.Element { data-card-body className={cn( 'relative select-none overflow-hidden rounded-md bg-well-strong transition-shadow', - selected && 'ring-2 ring-accent ring-offset-2 ring-offset-board', + (selected || props.marked) && 'ring-2 ring-accent ring-offset-2 ring-offset-board', view.active && !preview && 'studio-card-running', )} style={{ height: bodyHeight }} diff --git a/apps/desktop/src/renderer/src/pages/studio/Minimap.tsx b/apps/desktop/src/renderer/src/pages/studio/Minimap.tsx new file mode 100644 index 00000000..4902557e --- /dev/null +++ b/apps/desktop/src/renderer/src/pages/studio/Minimap.tsx @@ -0,0 +1,147 @@ +import { useEffect, useRef } from 'react' +import type { ProjectCanvas } from '@shared/types' +import { cn } from '../../components/ui' +import { cardRect, type View } from './boardLayout' +import { OutputImage } from './OutputImage' +import { boundsOf, minimapFit, type MinimapFit } from './selection' +import type { Point } from './wires' + +const WIDTH = 208 +const HEIGHT = 140 +/** Where the minimap sits: its size and its distance from the board's bottom-right corner, in screen pixels. */ +export const MINIMAP_BOX = { width: WIDTH, height: HEIGHT, margin: 16 } +/** The inner area, inside the frame's tonal edge. */ +const INSET = 6 +const INNER = { width: WIDTH - INSET * 2, height: HEIGHT - INSET * 2 } + +export interface MinimapProps { + /** The cards as drawn (a dragged card where the pointer has it). */ + cards: readonly ProjectCanvas[] + /** The picture a card shows, if any. */ + pictureOf: (id: string) => string | null + picked: ReadonlySet + busy: ReadonlySet + /** The view on screen right now, and a way to hear of every change (a pan does not re-render the board). */ + view: () => View + subscribe: (listener: () => void) => () => void + boardSize: () => { width: number; height: number } + /** Centre the view on this board point; `done` when the pointer is let go. */ + onCenter: (point: Point, done: boolean) => void +} + +function viewportStyle(view: View, board: { width: number; height: number }, fit: MinimapFit): Partial { + const x = (-view.pan.x / view.zoom) * fit.scale + fit.offset.x + INSET + const y = (-view.pan.y / view.zoom) * fit.scale + fit.offset.y + INSET + return { + transform: `translate(${x}px, ${y}px)`, + width: `${(board.width / view.zoom) * fit.scale}px`, + height: `${(board.height / view.zoom) * fit.scale}px`, + } +} + +/** + * The whole board in the corner: every card as a small picture, the part on + * screen as a tinted area. A press or drag on it moves the view there. + */ +export function Minimap(props: MinimapProps): React.JSX.Element | null { + const bounds = boundsOf(props.cards) + const fit = bounds ? minimapFit(bounds, INNER) : null + const area = useRef(null) + const latest = useRef(props) + latest.current = props + const fitRef = useRef(fit) + fitRef.current = fit + const { subscribe } = props + + // The tinted area follows every view change, pans included, without a render. + useEffect(() => { + const draw = (): void => { + const element = area.current + const current = fitRef.current + if (!element || !current) return + Object.assign(element.style, viewportStyle(latest.current.view(), latest.current.boardSize(), current)) + } + draw() + const stop = subscribe(draw) + window.addEventListener('resize', draw) + return () => { + stop() + window.removeEventListener('resize', draw) + } + }, [subscribe, fit?.scale, fit?.offset.x, fit?.offset.y]) + + if (!fit) return null + + const boardPoint = (event: React.PointerEvent): Point => { + const rect = event.currentTarget.getBoundingClientRect() + return { + x: (event.clientX - rect.left - INSET - fit.offset.x) / fit.scale, + y: (event.clientY - rect.top - INSET - fit.offset.y) / fit.scale, + } + } + + return ( +
{ + if (event.button !== 0) return + event.preventDefault() + event.currentTarget.setPointerCapture(event.pointerId) + props.onCenter(boardPoint(event), false) + }} + onPointerMove={(event) => { + if (event.currentTarget.hasPointerCapture(event.pointerId)) props.onCenter(boardPoint(event), false) + }} + onPointerUp={(event) => { + if (!event.currentTarget.hasPointerCapture(event.pointerId)) return + event.currentTarget.releasePointerCapture(event.pointerId) + props.onCenter(boardPoint(event), true) + }} + onContextMenu={(event) => event.preventDefault()} + > +
+ {props.cards.map((card) => { + const rect = cardRect(card) + const picture = props.picked.has(card.id) ? null : props.pictureOf(card.id) + return ( +
+ {picture ? : null} +
+ ) + })} + {props.cards + .filter((card) => props.busy.has(card.id)) + .map((card) => { + const rect = cardRect(card) + return ( + + ) + })} +
+
+ ) +} diff --git a/apps/desktop/src/renderer/src/pages/studio/StudioPage.tsx b/apps/desktop/src/renderer/src/pages/studio/StudioPage.tsx index e6f4df9d..9e6b8fc7 100644 --- a/apps/desktop/src/renderer/src/pages/studio/StudioPage.tsx +++ b/apps/desktop/src/renderer/src/pages/studio/StudioPage.tsx @@ -160,6 +160,8 @@ export function StudioPage(): React.JSX.Element { const [widths, setWidths] = useState(initialWidths) const [viewer, setViewer] = useState<{ canvasId: string; index: number } | null>(null) const [deleting, setDeleting] = useState(null) + /** Several picked cards waiting for 删除 to be confirmed. */ + const [deletingMany, setDeletingMany] = useState(null) const [adapterPickerOpen, setAdapterPickerOpen] = useState(false) // 从作品选择: the input a picture from 作品 goes into. // 从作品选择: the input a picture from 作品 goes into, or (right-click on the board) where it goes on the board. @@ -806,6 +808,13 @@ export function StudioPage(): React.JSX.Element { return () => window.removeEventListener('keydown', onKey) }) + /** A card that cannot be deleted yet: its job, or (a 画面) any run in its filmstrip, is still going. */ + const stillRunning = (canvas: ProjectCanvas): boolean => { + const job = canvas.jobId ? jobs.find((item) => item.id === canvas.jobId) : undefined + const running = canvas.kind === 'layout' && runsOf(canvas, project.canvasesRef.current).some((run) => isActiveRun(run, jobs)) + return ACTIVE_JOB_STATES.has(job?.state ?? '') || running + } + const deletingRuns = deleting ? removedWith(project.canvases, deleting.id).length - 1 : 0 const viewerCanvas = viewer ? (project.canvases.find((canvas) => canvas.id === viewer.canvasId) ?? null) : null const viewerJob = viewerCanvas?.jobId ? jobs.find((job) => job.id === viewerCanvas.jobId) : undefined @@ -931,15 +940,20 @@ export function StudioPage(): React.JSX.Element { requestAnimationFrame(() => board.current?.reveal(created.id)) }} onDelete={(canvas) => { - const job = canvas.jobId ? jobs.find((item) => item.id === canvas.jobId) : undefined - // A 画面 is busy while any run in its filmstrip is. - const running = canvas.kind === 'layout' && runsOf(canvas, project.canvasesRef.current).some((run) => isActiveRun(run, jobs)) - if (ACTIVE_JOB_STATES.has(job?.state ?? '') || running) { + if (stillRunning(canvas)) { toast.push('这个画面还在生成,先取消再删除', 'info') return } setDeleting(canvas) }} + onDeleteMany={(ids) => { + const list = project.canvasesRef.current.filter((canvas) => ids.includes(canvas.id)) + if (list.some(stillRunning)) { + toast.push('有画面还在生成,先取消再删除', 'info') + return + } + setDeletingMany(list.map((canvas) => canvas.id)) + }} engineFeatures={boardFeatures} controlAdapters={controlAdapters} onLink={linkInto} @@ -1075,6 +1089,22 @@ export function StudioPage(): React.JSX.Element { }} /> + setDeletingMany(null)} + title={`删除这 ${deletingMany?.length ?? 0} 张卡片?`} + description="它们的区域和生成记录会从项目里删除。已经生成的图片仍保留在作品里。" + confirmLabel="删除" + onConfirm={() => { + let removedLinks = 0 + for (const id of deletingMany ?? []) { + if (project.canvasesRef.current.some((canvas) => canvas.id === id)) removedLinks += project.removeCanvas(id) + } + if (removedLinks) toast.push(`已删除,并移除了 ${removedLinks} 条连到它们的连线`, 'info') + setDeletingMany(null) + }} + /> + void zoom: number onZoom: (factor: number) => void onFit: () => void + minimap: boolean + onMinimap: (on: boolean) => void }): React.JSX.Element { return (
onZoom(1.25)} className="text-ink"> - + + onMinimap(!minimap)}> + +
) } diff --git a/apps/desktop/src/renderer/src/pages/studio/selection.ts b/apps/desktop/src/renderer/src/pages/studio/selection.ts new file mode 100644 index 00000000..a4e4e305 --- /dev/null +++ b/apps/desktop/src/renderer/src/pages/studio/selection.ts @@ -0,0 +1,86 @@ +import { BOARD_GAP, cardRect, overlaps, type Rect } from './boardLayout' +import type { ProjectCanvas } from '@shared/types' + +/** + * Several cards picked at once (a box drawn on the empty board, Shift or Ctrl + * clicks, Ctrl+A), what 排整齐 does with them, and how the minimap scales + * the board. Pure, no React. + */ + +type Placed = Pick + +/** The box between two board points, whichever way it was drawn. */ +export function boxBetween(a: { x: number; y: number }, b: { x: number; y: number }): Rect { + return { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), w: Math.abs(a.x - b.x), h: Math.abs(a.y - b.y) } +} + +/** Cards the box touches: any overlap picks a card, it need not be inside. */ +export function cardsInBox(cards: readonly Placed[], box: Rect): string[] { + return cards.filter((card) => overlaps(cardRect(card), box)).map((card) => card.id) +} + +/** Adds the card, or takes it out when it is already picked. */ +export function toggled(ids: readonly string[], id: string): string[] { + return ids.includes(id) ? ids.filter((item) => item !== id) : [...ids, id] +} + +/** Reading order: top to bottom, then left to right. */ +export function readingOrder(cards: readonly T[]): T[] { + return [...cards].sort((a, b) => a.y - b.y || a.x - b.x) +} + +/** The rectangle around some cards. */ +export function boundsOf(cards: readonly Placed[]): Rect | null { + if (!cards.length) return null + const rects = cards.map(cardRect) + const x = Math.min(...rects.map((rect) => rect.x)) + const y = Math.min(...rects.map((rect) => rect.y)) + const right = Math.max(...rects.map((rect) => rect.x + rect.w)) + const bottom = Math.max(...rects.map((rect) => rect.y + rect.h)) + return { x, y, w: right - x, h: bottom - y } +} + +/** + * 排整齐: the cards in reading order, in rows of about √n, starting where + * the group's top-left corner was. Each row is as tall as its tallest card; + * cards keep their size. + */ +export function tidyPlaces(cards: readonly Placed[]): { id: string; x: number; y: number }[] { + const bounds = boundsOf(cards) + if (!bounds) return [] + const columns = Math.ceil(Math.sqrt(cards.length)) + const places: { id: string; x: number; y: number }[] = [] + let y = bounds.y + const ordered = readingOrder(cards) + for (let start = 0; start < ordered.length; start += columns) { + const row = ordered.slice(start, start + columns) + let x = bounds.x + for (const card of row) { + places.push({ id: card.id, x: Math.round(x), y: Math.round(y) }) + x += cardRect(card).w + BOARD_GAP + } + y += Math.max(...row.map((card) => cardRect(card).h)) + BOARD_GAP + } + return places +} + +/** How a board rectangle maps onto the minimap: board point × scale + offset. */ +export interface MinimapFit { + scale: number + offset: { x: number; y: number } +} + +/** Fits the cards' rectangle, with a margin, centred into the minimap's inner size. */ +export function minimapFit(bounds: Rect, size: { width: number; height: number }): MinimapFit { + const margin = Math.max(bounds.w, bounds.h) * 0.08 + BOARD_GAP + const w = bounds.w + margin * 2 + const h = bounds.h + margin * 2 + const scale = Math.min(size.width / w, size.height / h) + return { + scale, + offset: { + x: (size.width - bounds.w * scale) / 2 - bounds.x * scale, + y: (size.height - bounds.h * scale) / 2 - bounds.y * scale, + }, + } +} diff --git a/apps/desktop/test/renderer/studio/selection.test.ts b/apps/desktop/test/renderer/studio/selection.test.ts new file mode 100644 index 00000000..d151d500 --- /dev/null +++ b/apps/desktop/test/renderer/studio/selection.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, it } from 'vitest' +import { BOARD_GAP, cardRect } from '@renderer/pages/studio/boardLayout' +import { boundsOf, boxBetween, cardsInBox, minimapFit, readingOrder, tidyPlaces, toggled } from '@renderer/pages/studio/selection' + +// A 1024 px picture is a 256 px card (BOARD_UNIT 0.25). +const card = (id: string, x: number, y: number, width = 1024, height = 1024) => ({ id, x, y, width, height }) + +describe('studio/selection', () => { + it('draws the box whichever way the pointer went', () => { + expect(boxBetween({ x: 50, y: 80 }, { x: 10, y: 20 })).toEqual({ x: 10, y: 20, w: 40, h: 60 }) + }) + + it('picks every card the box touches, not only those inside it', () => { + const cards = [card('a', 0, 0), card('b', 400, 0), card('c', 0, 600)] + expect(cardsInBox(cards, { x: 200, y: 100, w: 250, h: 50 })).toEqual(['a', 'b']) + expect(cardsInBox(cards, { x: 300, y: 400, w: 50, h: 50 })).toEqual([]) + }) + + it('toggles a card in and out of the picked ones', () => { + expect(toggled(['a'], 'b')).toEqual(['a', 'b']) + expect(toggled(['a', 'b'], 'a')).toEqual(['b']) + }) + + it('reads cards top to bottom, then left to right', () => { + expect(readingOrder([card('c', 0, 300), card('b', 300, 0), card('a', 0, 0)]).map((item) => item.id)).toEqual(['a', 'b', 'c']) + }) + + it('lays cards out in rows of about √n from the group corner, rows as tall as their tallest card', () => { + const cards = [ + card('a', 500, 900), + card('b', 100, 100), + card('c', 900, 100, 1024, 2048), + card('d', 100, 700, 512, 512), + card('e', 300, 300), + ] + const places = tidyPlaces(cards) + const corner = boundsOf(cards)! + const at = Object.fromEntries(places.map((place) => [place.id, place])) + const w = cardRect(card('x', 0, 0)).w + // Reading order b, c, e, d, a; three columns. + expect(at['b']).toEqual({ id: 'b', x: corner.x, y: corner.y }) + expect(at['c']).toEqual({ id: 'c', x: corner.x + w + BOARD_GAP, y: corner.y }) + expect(at['e']).toEqual({ id: 'e', x: corner.x + 2 * (w + BOARD_GAP), y: corner.y }) + const secondRow = corner.y + cardRect(card('c', 0, 0, 1024, 2048)).h + BOARD_GAP + expect(at['d']).toEqual({ id: 'd', x: corner.x, y: secondRow }) + expect(at['a']!.y).toBe(secondRow) + }) + + it('fits the cards into the minimap, centred, with a margin', () => { + const bounds = { x: 1000, y: 0, w: 2000, h: 1000 } + const fit = minimapFit(bounds, { width: 196, height: 128 }) + const left = bounds.x * fit.scale + fit.offset.x + const right = (bounds.x + bounds.w) * fit.scale + fit.offset.x + expect(left).toBeGreaterThan(0) + expect(right).toBeLessThan(196) + expect(left + right).toBeCloseTo(196) + const top = bounds.y * fit.scale + fit.offset.y + const bottom = (bounds.y + bounds.h) * fit.scale + fit.offset.y + expect(top + bottom).toBeCloseTo(128) + }) +}) diff --git a/docs/reference/studio-node-board.md b/docs/reference/studio-node-board.md index 51ff6305..2e90e483 100644 --- a/docs/reference/studio-node-board.md +++ b/docs/reference/studio-node-board.md @@ -16,14 +16,16 @@ Paths are relative to `apps/desktop/src/`; `studio/` stands for `renderer/src/pa | --- | --- | --- | | Title bar | The project switcher and the 助手 toggle | `studio/ProjectSwitcher.tsx`, `studio/StudioPage.tsx` (`TitlebarPortal`) | | Left sidebar 生成设置 | The selected card's settings: description, one line for 模型, 尺寸 and LoRA, 质量, 高级 (种子, 运行位置, 显卡, sampling), 输入, the regions list, and the 生成 button with its blockers | `studio/ParamsPanel.tsx`, `studio/InputsSection.tsx`, `studio/SamplingSection.tsx`, `studio/InspectorPanel.tsx` | -| Board 画布 | Cards, wires, the floating toolbar (选择 V, 平移 H, zoom 25 to 300 %, 适应窗口) and the right-click menus | `studio/Board.tsx`, `studio/Toolbar.tsx`, `studio/BoardMenu.tsx` | +| Board 画布 | Cards, wires, the floating toolbar (选择 V, 平移 H, zoom 25 to 300 %, 适应窗口 F, 小地图), the minimap, the bar over picked cards and the right-click menus | `studio/Board.tsx`, `studio/Toolbar.tsx`, `studio/Minimap.tsx`, `studio/BoardMenu.tsx` | | Right sidebar 助手 | The assistant, hidden until toggled and kept mounted so a running chat survives; see [studio-assistant.md](/docs/reference/studio-assistant.md) | `studio/assistant/Assistant.tsx` | -`studio/useStudioProject.ts` loads and saves the project; edits autosave after 1.2 s (`AUTOSAVE_DELAY_MS`). Keys: Ctrl+Enter generates, V / H / M pick a tool, Delete removes the selected region or wire. +`studio/useStudioProject.ts` loads and saves the project; edits autosave after 1.2 s (`AUTOSAVE_DELAY_MS`). Keys: Ctrl+Enter generates, V / H / M pick a tool, Space held pans, F fits every card, Ctrl+A picks every card, Esc keeps only the selected card, and Delete removes the selected region or wire, else the picked cards (asking first). ## 2. Board and cards -- A card is drawn from its `x`, `y`, `width` and `height` in board pixels (`studio/boardLayout.ts`): `BOARD_UNIT` 0.25, so a 1024 px picture is a 256 px card; `CARD_HEADER` 28 px holds the title. Cards move by their title row or their picture (`studio/useCardDrag.ts`); a drag on empty board pans; the wheel zooms around the pointer. Gestures stay off React and off the project until they end: a pan sets the board's transform directly and commits the view on release, wheel ticks render at most once a frame, and a dragged card is drawn from `Board`'s own state and written to the project once, when it is let go. `CanvasCard` is memoized and compares its props by value (`sameCardProps`), so a new card prop must be plain data or a callback bound through `studio/useStableCallbacks.ts`; an inline closure would make every card re-render on every board render. +- A card is drawn from its `x`, `y`, `width` and `height` in board pixels (`studio/boardLayout.ts`): `BOARD_UNIT` 0.25, so a 1024 px picture is a 256 px card; `CARD_HEADER` 28 px holds the title. Cards move by their title row or their picture (`studio/useCardDrag.ts`). A left drag on empty board draws a box that picks every card it touches; the right or middle button, Space held, or the 平移 tool pans, and a right-click that does not move still opens the menu; the wheel zooms around the pointer. +- Several cards can be picked at once (`studio/selection.ts`): a box, Shift, Ctrl or ⌘ clicks, or Ctrl+A. The picked cards are a `Board` state that counts only while it holds the selected card (`selectedCanvasId`, which the left panel shows), so selecting a card any other way picks it alone; nothing about picking is saved. Picked cards move together and get a bar with 已选 N 张, 排整齐 (`tidyPlaces`: rows of about √n from the group's corner) and 删除, which asks once; Delete does the same, Esc keeps only the selected card. The minimap (`studio/Minimap.tsx`) draws every card, the picked ones in the accent and running ones with a dot, and the part on screen as a tint; a press or drag on it centres the view there. It follows pans through `Board`'s view listeners rather than renders, and cards placed near the corner keep clear of it (`keepInView`). +- Gestures stay off React and off the project until they end: a pan sets the board's transform directly and commits the view on release, wheel ticks render at most once a frame, and a dragged card is drawn from `Board`'s own state and written to the project once, when it is let go. `CanvasCard` is memoized and compares its props by value (`sameCardProps`), so a new card prop must be plain data or a callback bound through `studio/useStableCallbacks.ts`; an inline closure would make every card re-render on every board render. - Picture files (PNG, JPEG, WebP, BMP; `studio/imageNodes.ts`) dropped or pasted on the board become picture cards. They are copied into the project's assets through `Ipc.studioImportImage` (`studio/useBoardAssets.ts`), so an unsaved project is saved first. - Deleting a card removes the wires that point at it and reports how many (`studio/links.ts` `dropLinksTo`); a 画面 takes its folded runs with it (`studio/canvases.ts` `removedWith`). The dialog 删除画面? says that generated pictures stay in 作品 (`/library`).