diff --git a/browser_tests/fixtures/components/SidebarTab.ts b/browser_tests/fixtures/components/SidebarTab.ts index 824d0dff175..9d02c577274 100644 --- a/browser_tests/fixtures/components/SidebarTab.ts +++ b/browser_tests/fixtures/components/SidebarTab.ts @@ -89,6 +89,8 @@ export class NodeLibrarySidebarTabV2 extends SidebarTab { public readonly essentialsTab: Locator public readonly sortButton: Locator public readonly nodePreview: Locator + public readonly nodePreviewInputs: Locator + public readonly nodePreviewBody: Locator constructor(public override readonly page: Page) { super(page, 'node-library') @@ -98,6 +100,12 @@ export class NodeLibrarySidebarTabV2 extends SidebarTab { this.essentialsTab = this.getTab('Essentials') this.sortButton = this.sidebarContent.getByRole('button', { name: 'Sort' }) this.nodePreview = page.getByTestId(TestIds.sidebar.nodePreviewCard) + this.nodePreviewInputs = this.nodePreview.getByTestId( + TestIds.sidebar.nodePreviewInputs + ) + this.nodePreviewBody = this.nodePreview.getByTestId( + TestIds.sidebar.nodePreviewBody + ) } getTab(name: string) { diff --git a/browser_tests/fixtures/helpers/CanvasHelper.ts b/browser_tests/fixtures/helpers/CanvasHelper.ts index 871e8b430dd..f3c7d2074c6 100644 --- a/browser_tests/fixtures/helpers/CanvasHelper.ts +++ b/browser_tests/fixtures/helpers/CanvasHelper.ts @@ -202,6 +202,21 @@ export class CanvasHelper { ) } + async getElementWidth(): Promise { + return this.page.evaluate(() => window.app!.canvasEl.width) + } + + async getVisibleNodeCount(): Promise { + return this.page.evaluate(() => { + const { canvas } = window.app! + if (!canvas.graph) return 0 + canvas.ds.computeVisibleArea(canvas.viewport) + return canvas.graph.nodes.filter((node) => + canvas.ds.visible_area.overlaps(node.boundingRect) + ).length + }) + } + async waitForViewToSettle(): Promise { await this.page.waitForFunction( () => diff --git a/browser_tests/fixtures/helpers/TemplateHelper.ts b/browser_tests/fixtures/helpers/TemplateHelper.ts index a1703eb89b3..9dbacbdfb62 100644 --- a/browser_tests/fixtures/helpers/TemplateHelper.ts +++ b/browser_tests/fixtures/helpers/TemplateHelper.ts @@ -137,6 +137,20 @@ export class TemplateHelper { ) } + /** Serves in-memory workflow data as the named template's graph. */ + async mockWorkflowData(name: string, workflow: unknown): Promise { + await this.page.route(`**/templates/${name}.json`, (route) => + route.fulfill({ + status: 200, + body: JSON.stringify(workflow), + headers: { + 'Content-Type': 'application/json', + 'Cache-Control': 'no-store' + } + }) + ) + } + /** Opens the template browser and loads the named template. */ async load(name: string): Promise { await this.page.evaluate(() => { diff --git a/browser_tests/fixtures/selectors.ts b/browser_tests/fixtures/selectors.ts index 44d52996a93..335e593a288 100644 --- a/browser_tests/fixtures/selectors.ts +++ b/browser_tests/fixtures/selectors.ts @@ -12,6 +12,8 @@ export const TestIds = { nodeLibrary: 'node-library-tree', nodeLibrarySearch: 'node-library-search', nodePreviewCard: 'node-preview-card', + nodePreviewInputs: 'node-preview-inputs', + nodePreviewBody: 'node-preview-body', workflows: 'workflows-sidebar', workflowsRefreshButton: 'workflows-refresh-button', modeToggle: 'mode-toggle', diff --git a/browser_tests/fixtures/utils/appModeTemplate.ts b/browser_tests/fixtures/utils/appModeTemplate.ts new file mode 100644 index 00000000000..df9bf7a01b6 --- /dev/null +++ b/browser_tests/fixtures/utils/appModeTemplate.ts @@ -0,0 +1,22 @@ +import { makeTemplate } from '@e2e/fixtures/data/templateFixtures' +import { withTemplates } from '@e2e/fixtures/helpers/TemplateHelper' +import type { TemplateHelper } from '@e2e/fixtures/helpers/TemplateHelper' +import linearBasicApp from '@e2e/assets/linear-basic-app-1.json' with { type: 'json' } + +export const APP_MODE_TEMPLATE = 'viewport-app-template' + +export async function mockAppModeTemplate( + templates: TemplateHelper +): Promise { + templates.configure( + withTemplates([ + makeTemplate({ name: APP_MODE_TEMPLATE, title: 'App Template' }) + ]) + ) + await templates.mock() + const appModeWorkflow = { + ...structuredClone(linearBasicApp), + extra: { ...linearBasicApp.extra, linearMode: true } + } + await templates.mockWorkflowData(APP_MODE_TEMPLATE, appModeWorkflow) +} diff --git a/browser_tests/tests/appModeTemplateViewport.product.spec.ts b/browser_tests/tests/appModeTemplateViewport.product.spec.ts new file mode 100644 index 00000000000..0471aceae6c --- /dev/null +++ b/browser_tests/tests/appModeTemplateViewport.product.spec.ts @@ -0,0 +1,46 @@ +import { mergeTests } from '@playwright/test' + +import { + comfyExpect as expect, + comfyPageFixture +} from '@e2e/fixtures/ComfyPage' +import { templateApiFixture } from '@e2e/fixtures/templateApiFixture' +import { + APP_MODE_TEMPLATE, + mockAppModeTemplate +} from '@e2e/fixtures/utils/appModeTemplate' + +const test = mergeTests(comfyPageFixture, templateApiFixture) + +test.describe('App mode template viewport', { tag: ['@canvas'] }, () => { + test('frames a template after the hidden canvas becomes visible', async ({ + comfyPage, + templateApi + }) => { + const { appMode, canvasOps, workflow } = comfyPage + await mockAppModeTemplate(templateApi) + + await test.step('hide the canvas in app mode', async () => { + await appMode.enterAppModeWithInputs([['3', 'seed']]) + await expect(appMode.centerPanel).toBeVisible() + await expect.poll(() => canvasOps.getElementWidth()).toBe(0) + }) + + await test.step('load the template through the template browser path', async () => { + await templateApi.load(APP_MODE_TEMPLATE) + await expect + .poll(() => workflow.getActiveWorkflowPath()) + .toContain(APP_MODE_TEMPLATE) + await workflow.waitForWorkflowIdle() + await expect.poll(() => canvasOps.getElementWidth()).toBe(0) + }) + + await test.step('show the canvas and apply the queued camera', async () => { + await appMode.toggleAppMode() + await expect.poll(() => canvasOps.getElementWidth()).toBeGreaterThan(0) + await canvasOps.waitForViewToSettle() + + expect(await canvasOps.getVisibleNodeCount()).toBe(7) + }) + }) +}) diff --git a/browser_tests/tests/interaction.spec.ts b/browser_tests/tests/interaction.spec.ts index a69947481ba..9f885684654 100644 --- a/browser_tests/tests/interaction.spec.ts +++ b/browser_tests/tests/interaction.spec.ts @@ -1,4 +1,3 @@ -import type { Locator } from '@playwright/test' import { expect } from '@playwright/test' import type { Position } from '@vueuse/core' @@ -1083,58 +1082,56 @@ test.describe('Viewport settings', () => { offset: await comfyPage.canvasOps.getOffset() }) - const changeTab = async (tab: Locator) => { - await tab.click() - await comfyPage.nextFrame() + const changeTab = async (workflowName: string) => { + await comfyPage.menu.topbar.getWorkflowTab(workflowName).click() + await expect + .poll(() => comfyPage.workflow.getActiveWorkflowPath()) + .toBe(`workflows/${workflowName}.json`) + await comfyPage.canvasOps.waitForViewToSettle() await comfyMouse.move(DefaultGraphPositions.emptySpace) - // If tooltip is visible, wait for it to hide await expect( comfyPage.page.locator('.workflow-popover-fade') ).toHaveCount(0) } - // Screenshot the canvas element - await comfyPage.settings.setSetting('Comfy.Graph.CanvasMenu', true) - - const toggleButton = comfyPage.page.getByTestId( - TestIds.canvas.toggleMinimapButton - ) - await toggleButton.click() - await comfyPage.settings.setSetting('Comfy.Graph.CanvasMenu', false) - - await comfyPage.menu.topbar.saveWorkflow('Workflow A') - await comfyPage.nextFrame() - - // Save workflow as a new file, then zoom out before screen shot - await comfyPage.menu.topbar.saveWorkflowAs('Workflow B') - - await comfyPage.nextFrame() - const tabA = comfyPage.menu.topbar.getWorkflowTab('Workflow A') - await changeTab(tabA) - - const viewportA = await getViewport() + await test.step('Save two workflow tabs', async () => { + await comfyPage.settings.setSetting('Comfy.Graph.CanvasMenu', true) + await comfyPage.page + .getByTestId(TestIds.canvas.toggleMinimapButton) + .click() + await comfyPage.settings.setSetting('Comfy.Graph.CanvasMenu', false) - const tabB = comfyPage.menu.topbar.getWorkflowTab('Workflow B') - await changeTab(tabB) + await comfyPage.menu.topbar.saveWorkflow('Workflow A') + await comfyPage.nextFrame() + await comfyPage.menu.topbar.saveWorkflowAs('Workflow B') + await comfyPage.nextFrame() + }) - await comfyMouse.move(DefaultGraphPositions.emptySpace) - for (let i = 0; i < 4; i++) { - await comfyMouse.wheel(0, 60) - } + const { viewportA, viewportB } = + await test.step('Give each workflow a distinct viewport', async () => { + await changeTab('Workflow A') + const viewportA = await getViewport() - await comfyPage.nextFrame() - const viewportB = await getViewport() + await changeTab('Workflow B') + await comfyMouse.wheel(0, 60) + await expect.poll(getViewport).not.toEqual(viewportA) + await comfyPage.canvasOps.waitForViewToSettle() - expect(viewportB).not.toEqual(viewportA) + const viewportB = await getViewport() + expect(viewportB).not.toEqual(viewportA) + return { viewportA, viewportB } + }) - // Go back to Workflow A - await changeTab(tabA) - await expect.poll(getViewport).toEqual(viewportA) + await test.step('Restore Workflow A viewport', async () => { + await changeTab('Workflow A') + await expect.poll(getViewport).toEqual(viewportA) + }) - // And back to Workflow B - await changeTab(tabB) - await expect.poll(getViewport).toEqual(viewportB) + await test.step('Restore Workflow B viewport', async () => { + await changeTab('Workflow B') + await expect.poll(getViewport).toEqual(viewportB) + }) }) }) diff --git a/browser_tests/tests/rerouteNode.spec.ts-snapshots/native-reroute-alt-click-chromium-0-5x-linux.png b/browser_tests/tests/rerouteNode.spec.ts-snapshots/native-reroute-alt-click-chromium-0-5x-linux.png index 5247263450a..e34f9072d14 100644 Binary files a/browser_tests/tests/rerouteNode.spec.ts-snapshots/native-reroute-alt-click-chromium-0-5x-linux.png and b/browser_tests/tests/rerouteNode.spec.ts-snapshots/native-reroute-alt-click-chromium-0-5x-linux.png differ diff --git a/browser_tests/tests/sidebar/blueprintDynamicCombo.spec.ts b/browser_tests/tests/sidebar/blueprintDynamicCombo.spec.ts index 3e65e884d54..7053f4aaccc 100644 --- a/browser_tests/tests/sidebar/blueprintDynamicCombo.spec.ts +++ b/browser_tests/tests/sidebar/blueprintDynamicCombo.spec.ts @@ -20,11 +20,16 @@ test.describe('Blueprint dynamic combo preview', { tag: '@ui' }, () => { await tab.getNode('Dynamic combo blueprint').hover() await expect(tab.nodePreview).toBeVisible() await expect( - tab.nodePreview.getByText('boundary_model', { exact: true }) + tab.nodePreviewInputs.getByText('boundary_model', { exact: true }) ).toBeVisible() await expect( tab.nodePreview.getByText('COMFY_DYNAMICCOMBO_V3', { exact: true }) ).toBeVisible() + await expect(tab.nodePreviewBody).toBeVisible() + // The preview body renders a COMBO widget for a COMFY_DYNAMICCOMBO_V3 + // input only where `dynamicComboOptionKeys` exists, which arrived on main + // after this release line branched and is not part of #11420. Asserting + // that widget here would pin a capability 1.54 does not ship. expect(await comfyPage.page.pageErrors()).toEqual([]) }) }) diff --git a/docs/adr/CANVAS-VIEWPORT-0009-canvas-viewport-system.md b/docs/adr/CANVAS-VIEWPORT-0009-canvas-viewport-system.md new file mode 100644 index 00000000000..e8534775577 --- /dev/null +++ b/docs/adr/CANVAS-VIEWPORT-0009-canvas-viewport-system.md @@ -0,0 +1,82 @@ +# ADR-CANVAS-VIEWPORT-0009: Canvas Viewport System + +Date: 2026-09-28 + +## Status + +Proposed + +## Context + +LGraphCanvas uses a dual-canvas architecture: a foreground canvas (the DOM element) renders nodes, and a background canvas (offscreen) renders the grid, links, and groups. `drawFrontCanvas()` composites the background onto the foreground by dividing the background canvas dimensions by `devicePixelRatio` — assuming both canvases were DPR-scaled. `drawBackCanvas()` reinforces this assumption by applying `ctx.setTransform(scale, 0, 0, scale, 0, 0)` using DPR. Both canvases must have identical physical (DPR-scaled) dimensions for compositing to produce correct results. + +Two independent resize paths exist today: + +- **`resizeCanvas()` in app.ts** is DPR-aware for the foreground canvas: it multiplies CSS pixels by `devicePixelRatio` to set that canvas's physical dimensions and scales its context. The background canvas is sized and transformed later by `drawBackCanvas()`. +- **`LGraphCanvas.resize()`** is DPR-unaware: it sets both canvases to CSS pixel dimensions directly, producing canvases at 1× regardless of display density. + +Neither path documents that it depends on the other, creating implicit temporal coupling. Code that calls one without the other produces a background/foreground size mismatch. + +The original bug: when switching from app mode (canvas hidden via `v-show`) to graph mode, `resize()` was called to force dimensions onto the newly-visible canvas. Because `resize()` is DPR-unaware, the background canvas received CSS pixel dimensions while `drawFrontCanvas()` divided those dimensions by DPR (expecting physical pixels), producing a scaled-down composite. This change introduces a canvas scheduler (`useCanvasScheduler`) to solve the hidden-canvas lifecycle problem by deferring work until the canvas is visible, together with the viewport system to remove the DPR mismatch when that work runs. + +`window.devicePixelRatio` is read at 6+ call sites across LGraphCanvas (`drawFrontCanvas`, `drawBackCanvas`, `centerOnNode`, `renderInfo`, `processMouseDown` hit testing, font scaling) and 3+ call sites in app.ts/renderer code. Each reads independently with no shared source of truth, so any change to DPR handling requires auditing every call site. + +## Decision + +Introduce a `CanvasViewport` — a plain, frozen snapshot of the dimensions and DPR used for one canvas-sizing operation: + +```ts +interface CanvasViewport { + readonly cssWidth: number + readonly cssHeight: number + readonly dpr: number + readonly physicalWidth: number // cssWidth * dpr + readonly physicalHeight: number // cssHeight * dpr +} +``` + +Viewport measurement normalizes every finite positive DPR to at least `1`, preserving at least one backing pixel per CSS pixel so browser zoom values below `1` do not blur the canvas. Physical dimensions use this normalized DPR rather than the raw browser value. + +Two functions operate on this type: + +- **`measureViewport(cssWidth, cssHeight, rawDpr)`** — a pure function that produces a new `CanvasViewport` from numeric dimensions and DPR. `measureViewportFromElement(element, rawDpr?)` is the DOM adapter used by canvas lifecycle code. +- **`applyViewport(viewport, fgCanvas, bgCanvas, consumer?)`** — a side-effecting function that atomically sizes both foreground and background canvases to the viewport's physical dimensions, scales their 2D contexts, and updates an optional viewport consumer such as `LGraphCanvas` with both the applied DPR and CSS dimensions. Both canvases and the consumer are updated in a single call, eliminating the possibility of a partial resize or a later layout read to recover the CSS size. + +The existing `LGraphCanvas.resize()` method and `resizeCanvas()` in app.ts both delegate their sizing work to the viewport system. Both paths follow the same sequence: measure → apply → draw. + +`LGraphCanvas` caches the active DPR in its `dpr` property. `applyViewport()` updates the consumer's DPR and CSS viewport size atomically; the three viewport callers pass `LGraphCanvas` as that consumer. Most internal consumers (`drawFrontCanvas`, `drawBackCanvas`, `centerOnNode`, `renderInfo`, `processMouseDown` hit testing, and LOD threshold calculation) read the cache. Direct browser-DPR readers remain in `LGraphCanvas.setCanvas()`, `LGraphCanvas.resize()` during measurement, `DragAndScale.getViewportSize()`, and `useBoundingBoxes`; viewport measurement functions also read the browser value, while `layoutStore` retains a browser fallback for legacy callers. The viewport system therefore coordinates canvas sizing but does not yet own a single DPR read boundary. + +The new `CanvasScheduler` and viewport system have separate responsibilities: the scheduler handles **when** by queuing work while the canvas is hidden and flushing it from `resizeCanvas()` once the canvas ResizeObserver applies a visible viewport, while the viewport handles **what** by applying correct DPR-scaled dimensions atomically to both canvases. + +### Design Principles + +Following the principles established in [ADR-ECS-0008](ECS-0008-entity-component-system.md): + +- `CanvasViewport` is a **plain data component** — no methods, no back-references, frozen after creation. +- `measureViewport` is a **pure system function** — testable without DOM (accepts dimension inputs). +- `applyViewport` is a **side-effecting system** — testable with mock canvas objects. +- No methods are added to `LGraphCanvas` or any other entity class. + +### Alternatives Considered + +1. **Reactive derivation (Vue `computed`)** — rejected because it would require Vue reactivity inside litegraph internals, crossing a hard architectural boundary between the Vue application layer and the litegraph rendering layer. +2. **Transaction/batch-commit pattern** — rejected as overkill for a single async boundary (the hidden canvas becoming visible). The measure/apply split achieves the same atomicity guarantee with less machinery. +3. **Just fixing `resizeCanvas()` to also update bgcanvas** — rejected because it doesn't address the scattered DPR reads or prevent future divergence. A point fix solves today's bug but leaves the same class of bug latent at every other DPR read site. + +## Consequences + +### Positive + +- Applying one viewport snapshot to both canvases eliminates an entire class of sizing bugs where foreground and background canvases diverge. +- Phase separation (measure vs apply) makes the resize lifecycle explicit and testable. +- Pure functions (`measureViewport`) are trivially testable without DOM fixtures. +- Separates hidden-canvas scheduling from the dimensions and transforms applied when scheduled work runs. + +### Negative + +- Adds a new abstraction layer that all canvas-sizing code must flow through. +- `LGraphCanvas.setCanvas()`, `LGraphCanvas.resize()`, `DragAndScale.getViewportSize()`, and `useBoundingBoxes` still read `window.devicePixelRatio` directly. `layoutStore` accepts a caller-supplied `dpr` and falls back to the browser value for legacy callers. A future refactor could consolidate these reads behind one boundary. + +## Notes + +- References [ADR-ECS-0008](ECS-0008-entity-component-system.md) for the design principles (plain data components, pure system functions, no methods on entities). diff --git a/docs/adr/README.md b/docs/adr/README.md index 026e10284d7..51f72fc3b05 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -17,6 +17,7 @@ An Architecture Decision Record captures an important architectural decision mad | [AUTH-BILLING-0014](AUTH-BILLING-0014-billing-attempt-context-and-workspace-scope.md) | Billing Attempt Context and Workspace Scope | Proposed | 2026-07-28 | | [AUTH-CREDENTIALS-0011](AUTH-CREDENTIALS-0011-cloud-credential-lifecycle-invariants.md) | Cloud Credential Lifecycle Invariants | Proposed | 2026-07-09 | | [BILLING-CHECKOUT-0031](BILLING-CHECKOUT-0031-checkout-journey-instrumentation-contract.md) | Checkout Journey Instrumentation Contract | Proposed | 2026-09-09 | +| [CANVAS-VIEWPORT-0009](CANVAS-VIEWPORT-0009-canvas-viewport-system.md) | Canvas Viewport System | Proposed | 2026-09-28 | | [CRDT-AUTHORITY-0035](CRDT-AUTHORITY-0035-human-canvas-authority-and-draft-reconciliation.md) | Human Canvas Authority and Draft Reconciliation | Proposed | 2026-09-19 | | [CRDT-FOLLOWER-0025](CRDT-FOLLOWER-0025-in-app-agent-crdt-follower-and-distribution-resolved-boundaries.md) | In-App Agent CRDT Follower and Distribution-Resolved Boundaries | Proposed | 2026-08-21 | | [CRDT-INPUTS-0030](CRDT-INPUTS-0030-preserve-named-input-targets.md) | Project Document Inputs onto Reordered Live Nodes by Name | Proposed | 2026-09-09 | diff --git a/src/components/node/NodePreviewCard.vue b/src/components/node/NodePreviewCard.vue index 33880c47cc6..230a6e01a7d 100644 --- a/src/components/node/NodePreviewCard.vue +++ b/src/components/node/NodePreviewCard.vue @@ -53,6 +53,7 @@

{ diff --git a/src/components/sidebar/SideToolbar.vue b/src/components/sidebar/SideToolbar.vue index 22322677746..19728ea47c1 100644 --- a/src/components/sidebar/SideToolbar.vue +++ b/src/components/sidebar/SideToolbar.vue @@ -269,11 +269,10 @@ onMounted(() => { await nextTick() const sidebarRight = sideToolbarRef.value?.getBoundingClientRect()?.right - // The release line's renderInfo keeps a 5px text inset that main removed; drop the - 5 when that litegraph change lands here. canvasStore.canvas.fpsInfoLocation = [ sidebarRight === undefined ? undefined - : sidebarRight + canvasGutter() - 5, + : sidebarRight + canvasGutter(), null ] } else { diff --git a/src/composables/canvas/visibleCanvasViewport.test.ts b/src/composables/canvas/visibleCanvasViewport.test.ts index a78fcaec6df..f6a72c68866 100644 --- a/src/composables/canvas/visibleCanvasViewport.test.ts +++ b/src/composables/canvas/visibleCanvasViewport.test.ts @@ -1,9 +1,8 @@ import { createPinia, setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi } from 'vitest' -import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph' +import { createTestDragAndScale } from '@/utils/__tests__/canvasTestUtils' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' - import { visibleCanvasViewport } from './visibleCanvasViewport' vi.mock('@/platform/telemetry', () => ({ useTelemetry: () => undefined })) @@ -12,27 +11,23 @@ describe('visibleCanvasViewport', () => { beforeEach(() => { localStorage.clear() setActivePinia(createPinia()) - vi.stubGlobal('devicePixelRatio', 2) }) - it('uses the full CSS-pixel canvas while the Agent panel is closed', () => { - const canvas = { - canvas: { width: 1600, height: 900 } - } as LGraphCanvas - - expect(visibleCanvasViewport(canvas)).toEqual([0, 0, 800, 450]) + it('uses the applied CSS viewport while the agent panel is closed', () => { + expect( + visibleCanvasViewport({ ds: createTestDragAndScale(640, 360) }) + ).toEqual([0, 0, 640, 360]) }) - it('T-06 / PM-669 / FE-1633 excludes the docked Agent panel width from Fit View', () => { + it('subtracts the visible agent panel from the CSS viewport', () => { const panel = useAgentPanelStore() panel.enabled = true panel.consentAccepted = true panel.isOpen = true panel.setWidth(500) - const canvas = { - canvas: { width: 1600, height: 900 } - } as LGraphCanvas - expect(visibleCanvasViewport(canvas)).toEqual([0, 0, 300, 450]) + expect( + visibleCanvasViewport({ ds: createTestDragAndScale(800, 450) }) + ).toEqual([0, 0, 300, 450]) }) }) diff --git a/src/composables/canvas/visibleCanvasViewport.ts b/src/composables/canvas/visibleCanvasViewport.ts index b8b0ce44f39..bcc7a8162eb 100644 --- a/src/composables/canvas/visibleCanvasViewport.ts +++ b/src/composables/canvas/visibleCanvasViewport.ts @@ -2,10 +2,11 @@ import type { ReadOnlyRect } from '@/lib/litegraph/src/interfaces' import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' -export function visibleCanvasViewport(canvas: LGraphCanvas): ReadOnlyRect { +export function visibleCanvasViewport( + canvas: Pick +): ReadOnlyRect { const panel = useAgentPanelStore() - const width = canvas.canvas.width / window.devicePixelRatio - const height = canvas.canvas.height / window.devicePixelRatio + const [width, height] = canvas.ds.getViewportSize() const coveredWidth = panel.isVisible ? panel.width : 0 return [0, 0, Math.max(width - coveredWidth, 0), height] } diff --git a/src/composables/useLoad3d.test.ts b/src/composables/useLoad3d.test.ts index 747cbb6c660..88ef17e6680 100644 --- a/src/composables/useLoad3d.test.ts +++ b/src/composables/useLoad3d.test.ts @@ -22,10 +22,8 @@ import type { IWidget } from '@/lib/litegraph/src/types/widgets' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { useToastStore } from '@/platform/updates/common/toastStore' import { api } from '@/scripts/api' -import { - createMockCanvasPointerEvent, - createMockLGraphNode -} from '@/utils/__tests__/litegraphTestUtils' +import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasPointerEvent } from '@/utils/__tests__/canvasTestUtils' vi.mock('@/extensions/core/load3d/Load3d', () => ({ default: vi.fn() diff --git a/src/composables/useViewErrorsInGraph.test.ts b/src/composables/useViewErrorsInGraph.test.ts index e7ceb775652..402c0a1098a 100644 --- a/src/composables/useViewErrorsInGraph.test.ts +++ b/src/composables/useViewErrorsInGraph.test.ts @@ -4,7 +4,7 @@ import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { useExecutionErrorStore } from '@/stores/executionErrorStore' import { useRightSidePanelStore } from '@/stores/workspace/rightSidePanelStore' import { LGraph, LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { useViewErrorsInGraph } from './useViewErrorsInGraph' diff --git a/src/extensions/core/contextMenuFilter.test.ts b/src/extensions/core/contextMenuFilter.test.ts index d1f6ff676b8..f15f9c51a69 100644 --- a/src/extensions/core/contextMenuFilter.test.ts +++ b/src/extensions/core/contextMenuFilter.test.ts @@ -5,10 +5,8 @@ import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' import { useExtensionService } from '@/services/extensionService' import { useExtensionStore } from '@/stores/extensionStore' import type { ComfyExtension } from '@/types/comfy' -import { - createMockCanvas, - createMockLGraphNode -} from '@/utils/__tests__/litegraphTestUtils' +import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvas } from '@/utils/__tests__/canvasTestUtils' describe('Context Menu Extension API', () => { let mockCanvas: LGraphCanvas diff --git a/src/lib/litegraph/src/DragAndScale.animateToBounds.test.ts b/src/lib/litegraph/src/DragAndScale.animateToBounds.test.ts index 4ebac673e83..b275e20a664 100644 --- a/src/lib/litegraph/src/DragAndScale.animateToBounds.test.ts +++ b/src/lib/litegraph/src/DragAndScale.animateToBounds.test.ts @@ -1,13 +1,14 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { DragAndScale } from '@/lib/litegraph/src/DragAndScale' +import { createTestCanvasElement } from '@/utils/__tests__/canvasTestUtils' type Bounds = [number, number, number, number] let pendingFrame: FrameRequestCallback | undefined function createDragAndScale() { - return new DragAndScale({ width: 1600, height: 900 } as HTMLCanvasElement) + return new DragAndScale(createTestCanvasElement({ width: 1600, height: 900 })) } function settle() { diff --git a/src/lib/litegraph/src/DragAndScale.ts b/src/lib/litegraph/src/DragAndScale.ts index 3e9aa6a591d..8583eb9f471 100644 --- a/src/lib/litegraph/src/DragAndScale.ts +++ b/src/lib/litegraph/src/DragAndScale.ts @@ -1,3 +1,5 @@ +import { readBrowserDpr } from '@/renderer/core/canvas/canvasViewport' + import type { Point, ReadOnlyRect, Rect } from './interfaces' import { EaseFunction, Rectangle } from './litegraph' @@ -44,6 +46,7 @@ export class DragAndScale { visible_area: Rectangle dragging?: boolean viewport?: Rect + private viewportSize?: [number, number] onredraw?(das: DragAndScale): void onChanged?(scale: number, offset: Point): void @@ -102,7 +105,7 @@ export class DragAndScale { copyState(this.state, this.lastState) } - let { width, height } = this.element + let [width, height] = this.getViewportSize() let startx = -offset[0] let starty = -offset[1] if (viewport) { @@ -118,6 +121,31 @@ export class DragAndScale { visible_area.resizeBottomRight(endx, endy) } + setViewportSize(width: number, height: number): void { + this.viewportSize = [width, height] + } + + clearViewportSize(): void { + this.viewportSize = undefined + } + + getViewportSize(): [number, number] { + const viewportSize = this.viewportSize + if (viewportSize && viewportSize[0] > 0 && viewportSize[1] > 0) { + return [...viewportSize] + } + + const rect = this.element.getBoundingClientRect() + if (rect.width > 0 && rect.height > 0) { + return [rect.width, rect.height] + } + + if (viewportSize) return [...viewportSize] + + const dpr = readBrowserDpr() + return [this.element.width / dpr, this.element.height / dpr] + } + toCanvasContext(ctx: CanvasRenderingContext2D): void { ctx.scale(this.scale, this.scale) ctx.translate(this.offset[0], this.offset[1]) @@ -186,33 +214,32 @@ export class DragAndScale { */ fitToBounds( bounds: ReadOnlyRect, - { zoom = 0.75 }: { zoom?: number } = {} + { zoom = 0.75, viewport }: { zoom?: number; viewport?: ReadOnlyRect } = {} ): void { //If element hasn't initialized (browser tab is in background) //it has a size of 300x150 and a more reasonable default is used instead. const [width, height] = this.element.width === 300 && this.element.height === 150 ? [1920, 1080] - : [this.element.width, this.element.height] - const cw = width / window.devicePixelRatio - const ch = height / window.devicePixelRatio + : this.getViewportSize() + const [vx, vy, vw, vh] = viewport ?? [0, 0, width, height] + if (!(vw > 0) || !(vh > 0)) return let targetScale = this.scale if (zoom > 0) { - const targetScaleX = (zoom * cw) / Math.max(bounds[2], 300) - const targetScaleY = (zoom * ch) / Math.max(bounds[3], 300) + const targetScaleX = (zoom * vw) / Math.max(bounds[2], 300) + const targetScaleY = (zoom * vh) / Math.max(bounds[3], 300) // Choose the smaller scale to ensure the node fits into the viewport // Ensure we don't go over the max scale - targetScale = Math.min(targetScaleX, targetScaleY, this.max_scale) + targetScale = Math.max( + Math.min(targetScaleX, targetScaleY, this.max_scale), + this.min_scale + ) } - const scaledWidth = cw / targetScale - const scaledHeight = ch / targetScale - - // Calculate the target position to center the bounds in the viewport - const targetX = -bounds[0] - bounds[2] * 0.5 + scaledWidth * 0.5 - const targetY = -bounds[1] - bounds[3] * 0.5 + scaledHeight * 0.5 + const targetX = (vx + vw * 0.5) / targetScale - bounds[0] - bounds[2] * 0.5 + const targetY = (vy + vh * 0.5) / targetScale - bounds[1] - bounds[3] * 0.5 // Apply the changes immediately this.offset[0] = targetX @@ -245,10 +272,9 @@ export class DragAndScale { const easeFunction = easeFunctions[easing] const startTimestamp = performance.now() - const cw = this.element.width / window.devicePixelRatio - const ch = this.element.height / window.devicePixelRatio + const [cw, ch] = this.getViewportSize() const [vx, vy, vw, vh] = viewport ?? [0, 0, cw, ch] - if (vw <= 0 || vh <= 0) return + if (!(vw > 0) || !(vh > 0)) return const startX = this.offset[0] const startY = this.offset[1] const startX2 = startX - cw / this.scale diff --git a/src/lib/litegraph/src/LGraph.tabSwitchSnapshot.test.ts b/src/lib/litegraph/src/LGraph.tabSwitchSnapshot.test.ts index 9398313dbc8..3fa2443c714 100644 --- a/src/lib/litegraph/src/LGraph.tabSwitchSnapshot.test.ts +++ b/src/lib/litegraph/src/LGraph.tabSwitchSnapshot.test.ts @@ -19,10 +19,8 @@ import type { ExportedSubgraph, ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' -import { - createMockCanvasRenderingContext2D, - reloadSerializedGraph -} from '@/utils/__tests__/litegraphTestUtils' +import { reloadSerializedGraph } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' const INTERIOR_TYPE = 'Fixture/BlueprintInterior' diff --git a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts index 6b8b768afef..b5e581f348c 100644 --- a/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.clipboard.test.ts @@ -41,7 +41,7 @@ import { useRerouteStore } from '@/stores/rerouteStore' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { graphScopeOf } from '@/types/graphScopeId' import { toRerouteId } from '@/types/rerouteId' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' vi.mock('@/renderer/core/canvas/canvasStore', () => ({ useCanvasStore: () => ({}) diff --git a/src/lib/litegraph/src/LGraphCanvas.ghost.test.ts b/src/lib/litegraph/src/LGraphCanvas.ghost.test.ts index ad2efa82a03..46aa2cc4244 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ghost.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ghost.test.ts @@ -4,7 +4,7 @@ import { setActivePinia } from 'pinia' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { LGraph, LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' vi.mock('@/renderer/core/layout/store/layoutStore') diff --git a/src/lib/litegraph/src/LGraphCanvas.invalidation.test.ts b/src/lib/litegraph/src/LGraphCanvas.invalidation.test.ts index f854ebf6cb8..f81bd412220 100644 --- a/src/lib/litegraph/src/LGraphCanvas.invalidation.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.invalidation.test.ts @@ -3,7 +3,7 @@ import { setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi } from 'vitest' import { LGraph, LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' vi.mock('@/renderer/core/layout/store/layoutStore') diff --git a/src/lib/litegraph/src/LGraphCanvas.linkDragAutoPan.test.ts b/src/lib/litegraph/src/LGraphCanvas.linkDragAutoPan.test.ts index 748d9e336e4..de07b8dd0c6 100644 --- a/src/lib/litegraph/src/LGraphCanvas.linkDragAutoPan.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.linkDragAutoPan.test.ts @@ -3,7 +3,7 @@ import { setActivePinia } from 'pinia' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { LGraph, LGraphCanvas } from '@/lib/litegraph/src/litegraph' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' vi.mock('@/renderer/core/layout/store/layoutStore') diff --git a/src/lib/litegraph/src/LGraphCanvas.prompt.test.ts b/src/lib/litegraph/src/LGraphCanvas.prompt.test.ts index 3c24c262a7c..b102779bfa0 100644 --- a/src/lib/litegraph/src/LGraphCanvas.prompt.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.prompt.test.ts @@ -4,7 +4,7 @@ import { LGraph, LGraphCanvas } from '@/lib/litegraph/src/litegraph' import { createMockCanvasPointerEvent, createMockCanvasRenderingContext2D -} from '@/utils/__tests__/litegraphTestUtils' +} from '@/utils/__tests__/canvasTestUtils' function createCanvas( rect: Pick = { diff --git a/src/lib/litegraph/src/LGraphCanvas.renderInfo.test.ts b/src/lib/litegraph/src/LGraphCanvas.renderInfo.test.ts index f0be6694ae8..defbf83ab7b 100644 --- a/src/lib/litegraph/src/LGraphCanvas.renderInfo.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.renderInfo.test.ts @@ -38,27 +38,25 @@ describe('LGraphCanvas.renderInfo', () => { expect(spy).not.toHaveBeenCalled() }) - it('uses canvas.height divided by devicePixelRatio as y fallback', () => { + it('draws every line at the x it was given', () => { + lgCanvas.renderInfo(ctx, 100, 500) + + expect(ctx.translate).toHaveBeenCalledWith(100, 500) + + const drawnAt = vi.mocked(ctx.fillText).mock.calls + expect(drawnAt.length).toBeGreaterThan(0) + expect(drawnAt.map(([, x]) => x)).toEqual(drawnAt.map(() => 0)) + }) + + it('uses canvas.height divided by dpr as y fallback', () => { lgCanvas.canvas.width = 1920 lgCanvas.canvas.height = 2160 + lgCanvas.dpr = 2 - const originalDPR = window.devicePixelRatio - Object.defineProperty(window, 'devicePixelRatio', { - value: 2, - configurable: true - }) - - try { - lgCanvas.renderInfo(ctx, 10, 0) + lgCanvas.renderInfo(ctx, 10, 0) - // lineCount = 5 (graph present, no info_text), lineHeight = 13 - // y = canvas.height / DPR - (lineCount + 1) * lineHeight - expect(ctx.translate).toHaveBeenCalledWith(10, 2160 / 2 - 6 * 13) - } finally { - Object.defineProperty(window, 'devicePixelRatio', { - value: originalDPR, - configurable: true - }) - } + // lineCount = 5 (graph present, no info_text), lineHeight = 13 + // y = canvas.height / DPR - (lineCount + 1) * lineHeight + expect(ctx.translate).toHaveBeenCalledWith(10, 2160 / 2 - 6 * 13) }) }) diff --git a/src/lib/litegraph/src/LGraphCanvas.resize.test.ts b/src/lib/litegraph/src/LGraphCanvas.resize.test.ts new file mode 100644 index 00000000000..ea31511c8ec --- /dev/null +++ b/src/lib/litegraph/src/LGraphCanvas.resize.test.ts @@ -0,0 +1,73 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { LGraph, LGraphCanvas } from '@/lib/litegraph/src/litegraph' +import { createTestCanvasElement } from '@/utils/__tests__/canvasTestUtils' + +describe('LGraphCanvas.resize', () => { + beforeEach(() => { + vi.stubGlobal('devicePixelRatio', 2) + }) + + function createParentSizedCanvas() { + const parentSize = { width: 800, height: 600 } + const parent = document.createElement('div') + Object.defineProperties(parent, { + offsetWidth: { get: () => parentSize.width }, + offsetHeight: { get: () => parentSize.height } + }) + const element = createTestCanvasElement() + parent.append(element) + const canvas = new LGraphCanvas(element, new LGraph(), { + skip_render: true, + skip_events: true + }) + return { canvas, parentSize } + } + + it('follows parent growth with logical CSS size and DPR-scaled backing size', () => { + const { canvas, parentSize } = createParentSizedCanvas() + canvas.resize() + + parentSize.width = 1000 + parentSize.height = 700 + canvas.resize() + + const { style, width, height } = canvas.canvas + expect([style.width, style.height, width, height]).toEqual([ + '1000px', + '700px', + 2000, + 1400 + ]) + }) + + it('sizes the backing store without overriding caller CSS dimensions', () => { + const { canvas } = createParentSizedCanvas() + canvas.canvas.style.width = '75%' + canvas.canvas.style.height = '50vh' + + canvas.resize() + + const { style, width, height } = canvas.canvas + expect([style.width, style.height, width, height]).toEqual([ + '75%', + '50vh', + 1600, + 1200 + ]) + }) + + it('applies explicit dimensions as CSS pixels and backing-store pixels', () => { + const { canvas } = createParentSizedCanvas() + + canvas.resize(640, 480) + + const { style, width, height } = canvas.canvas + expect([style.width, style.height, width, height]).toEqual([ + '640px', + '480px', + 1280, + 960 + ]) + }) +}) diff --git a/src/lib/litegraph/src/LGraphCanvas.selectOnly.test.ts b/src/lib/litegraph/src/LGraphCanvas.selectOnly.test.ts index fceb1821bb9..c8ba1cbe2d1 100644 --- a/src/lib/litegraph/src/LGraphCanvas.selectOnly.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.selectOnly.test.ts @@ -8,6 +8,7 @@ import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' vi.mock('@/renderer/core/layout/store/layoutStore', () => ({ @@ -133,6 +134,25 @@ describe('LGraphCanvas selectOnly', () => { expect(firstNode.selected).toBeFalsy() }) + it('uses the applied canvas DPR for link hit testing', () => { + const { canvas } = createHarness() + canvas.dpr = 1.5 + const event = new MouseEvent('pointerdown', { + button: 0, + clientX: 700, + clientY: 500 + }) + Object.defineProperty(event, 'isPrimary', { value: true }) + + canvas.processMouseDown(event) + + expect(layoutStore.queryLinkSegmentAtPoint).toHaveBeenCalledWith( + { x: 700, y: 500 }, + canvas.ctx, + 1.5 + ) + }) + it('does not open a context menu on double click', () => { const { canvas, firstNode } = createHarness() const event = new MouseEvent('pointerdown', { diff --git a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts index 82b0bade2de..67f1ca8c588 100644 --- a/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.subgraphPasteDelete.test.ts @@ -13,7 +13,7 @@ import { createUuidv4 } from '@/lib/litegraph/src/litegraph' import { useWidgetValueStore } from '@/stores/widgetValueStore' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' import { createTestSubgraphData, diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 7af79b265f1..b6080ba711f 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -16,6 +16,12 @@ import { import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { LayoutSource } from '@/renderer/core/layout/types' +import { + applyParentSizedCanvasStyle, + applyViewport, + measureViewport, + readBrowserDpr +} from '@/renderer/core/canvas/canvasViewport' import { useLinkStore } from '@/stores/linkStore' import { graphScopeOf } from '@/types/graphScopeId' import { toLinkId } from '@/types/linkId' @@ -534,7 +540,7 @@ export class LGraphCanvas implements CustomEventDispatcher } const baseFontSize = LiteGraph.NODE_TEXT_SIZE // 14px - const dprAdjustment = Math.sqrt(window.devicePixelRatio || 1) //Using sqrt here because higher DPR monitors do not linearily scale the readability of the font, instead they increase the font by some heurisitc, and to approximate we use sqrt to say basically a DPR of 2 increases the readability by 40%, 3 by 70% + const dprAdjustment = Math.sqrt(this.dpr) //Using sqrt here because higher DPR monitors do not linearily scale the readability of the font, instead they increase the font by some heurisitc, and to approximate we use sqrt to say basically a DPR of 2 increases the readability by 40%, 3 by 70% // Calculate the zoom level where text becomes unreadable this._lowQualityZoomThreshold = @@ -792,6 +798,9 @@ export class LGraphCanvas implements CustomEventDispatcher /** Link rendering adapter for litegraph-to-canvas integration */ linkRenderer: LitegraphLinkAdapter | null = null + /** Device pixel ratio applied with the current viewport dimensions. */ + dpr: number = 1 + /** If true, enable drag zoom. Ctrl+Shift+Drag Up/Down: zoom canvas. */ dragZoomEnabled: boolean = false /** The start position of the drag zoom and original read-only state. */ @@ -1938,6 +1947,7 @@ export class LGraphCanvas implements CustomEventDispatcher // maybe detach events from old_canvas this.canvas = element this.ds.element = element + this.ds.clearViewportSize() this.pointer.element = element this._setCursor = createCursorCache(element) @@ -1955,6 +1965,7 @@ export class LGraphCanvas implements CustomEventDispatcher this.bgcanvas = document.createElement('canvas') this.bgcanvas.width = this.canvas.width this.bgcanvas.height = this.canvas.height + this.dpr = readBrowserDpr() const ctx = element.getContext('2d') if (ctx == null) { @@ -2528,10 +2539,14 @@ export class LGraphCanvas implements CustomEventDispatcher // Set the width of the line for isPointInStroke checks const { lineWidth } = this.ctx this.ctx.lineWidth = this.connections_width + 7 - const dpi = Math.max(window.devicePixelRatio, 1) + const dpi = this.dpr // Try layout store for segment hit testing first (more precise) - const hitSegment = layoutStore.queryLinkSegmentAtPoint({ x, y }, this.ctx) + const hitSegment = layoutStore.queryLinkSegmentAtPoint( + { x, y }, + this.ctx, + dpi + ) for (const linkSegment of this.renderedPaths) { const centre = linkSegment._pos @@ -4850,15 +4865,15 @@ export class LGraphCanvas implements CustomEventDispatcher * centers the camera on a given node */ centerOnNode(node: LGraphNode): void { - const dpi = window.devicePixelRatio || 1 + const { dpr } = this this.ds.offset[0] = -node.pos[0] - node.size[0] * 0.5 + - (this.canvas.width * 0.5) / (this.ds.scale * dpi) + (this.canvas.width * 0.5) / (this.ds.scale * dpr) this.ds.offset[1] = -node.pos[1] - node.size[1] * 0.5 + - (this.canvas.height * 0.5) / (this.ds.scale * dpi) + (this.canvas.height * 0.5) / (this.ds.scale * dpr) this.setDirty(true, true) } @@ -5116,13 +5131,13 @@ export class LGraphCanvas implements CustomEventDispatcher : undefined this.drawBackCanvas(false, nodesInFrameOrder, nodesGraph) } else { - const scale = window.devicePixelRatio + const { dpr } = this ctx.drawImage( this.bgcanvas, 0, 0, - this.bgcanvas.width / scale, - this.bgcanvas.height / scale + this.bgcanvas.width / dpr, + this.bgcanvas.height / dpr ) } const graphAfterBackground = this.graph @@ -5454,13 +5469,8 @@ export class LGraphCanvas implements CustomEventDispatcher renderInfo(ctx: CanvasRenderingContext2D, x: number, y: number): void { const lineHeight = 13 const lineCount = (this.graph ? 5 : 1) + (this.info_text ? 1 : 0) - x = x || 10 - y = - y || - this.canvas.height / - ((this.canvas.ownerDocument.defaultView ?? window).devicePixelRatio || - 1) - - (lineCount + 1) * lineHeight + x = x || 15 + y = y || this.canvas.height / this.dpr - (lineCount + 1) * lineHeight ctx.save() ctx.translate(x, y) @@ -5472,22 +5482,22 @@ export class LGraphCanvas implements CustomEventDispatcher if (this.graph) { ctx.fillText( `T: ${this.graph.globaltime.toFixed(2)}s`, - 5, + 0, lineHeight * line++ ) - ctx.fillText(`I: ${this.graph.iteration}`, 5, lineHeight * line++) + ctx.fillText(`I: ${this.graph.iteration}`, 0, lineHeight * line++) ctx.fillText( `N: ${this.graph._nodes.length} [${this.visible_nodes.length}]`, - 5, + 0, lineHeight * line++ ) - ctx.fillText(`V: ${this.graph._version}`, 5, lineHeight * line++) - ctx.fillText(`FPS:${this.fps.toFixed(2)}`, 5, lineHeight * line++) + ctx.fillText(`V: ${this.graph._version}`, 0, lineHeight * line++) + ctx.fillText(`FPS:${this.fps.toFixed(2)}`, 0, lineHeight * line++) } else { - ctx.fillText('No graph selected', 5, lineHeight * line++) + ctx.fillText('No graph selected', 0, lineHeight * line++) } if (this.info_text) { - ctx.fillText(this.info_text, 5, lineHeight * line) + ctx.fillText(this.info_text, 0, lineHeight * line) } ctx.restore() } @@ -5535,9 +5545,9 @@ export class LGraphCanvas implements CustomEventDispatcher // reset in case of error if (!this.viewport) { - const scale = window.devicePixelRatio + const { dpr } = this ctx.restore() - ctx.setTransform(scale, 0, 0, scale, 0, 0) + ctx.setTransform(dpr, 0, 0, dpr, 0, 0) } if (this.graph) { @@ -6617,10 +6627,11 @@ export class LGraphCanvas implements CustomEventDispatcher } /** - * resizes the canvas to a given size, if no size is passed, then it tries to fill the parentNode - * @todo Remove or rewrite + * @deprecated Use {@link measureViewport} + {@link applyViewport} from `canvasViewport.ts` instead. + * This method remains for legacy callers that rely on parent-element fallback sizing. */ resize(width?: number, height?: number): void { + const usesParentSize = !width && !height if (!width && !height) { const parent = this.canvas.parentElement if (!parent) @@ -6631,12 +6642,29 @@ export class LGraphCanvas implements CustomEventDispatcher height = parent.offsetHeight } - if (this.canvas.width == width && this.canvas.height == height) return + if (usesParentSize) { + applyParentSizedCanvasStyle(this.canvas, width ?? 0, height ?? 0) + } else { + if (!this.canvas.style.width) this.canvas.style.width = `${width}px` + if (!this.canvas.style.height) this.canvas.style.height = `${height}px` + } - this.canvas.width = width ?? 0 - this.canvas.height = height ?? 0 - this.bgcanvas.width = this.canvas.width - this.bgcanvas.height = this.canvas.height + const viewport = measureViewport( + width ?? 0, + height ?? 0, + window.devicePixelRatio + ) + + if ( + this.canvas.width === viewport.physicalWidth && + this.canvas.height === viewport.physicalHeight && + this.bgcanvas.width === viewport.physicalWidth && + this.bgcanvas.height === viewport.physicalHeight && + this.dpr === viewport.dpr + ) + return + + applyViewport(viewport, this.canvas, this.bgcanvas, this) this.setDirty(true, true) } diff --git a/src/lib/litegraph/src/LGraphNode.test.ts b/src/lib/litegraph/src/LGraphNode.test.ts index 40cd93a1dae..faba40832e5 100644 --- a/src/lib/litegraph/src/LGraphNode.test.ts +++ b/src/lib/litegraph/src/LGraphNode.test.ts @@ -22,10 +22,8 @@ import { } from '@/lib/litegraph/src/litegraph' import { test } from './__fixtures__/testExtensions' -import { - createMockCanvasRenderingContext2D, - createMockLGraphNodeWithArrayBoundingRect -} from '@/utils/__tests__/litegraphTestUtils' +import { createMockLGraphNodeWithArrayBoundingRect } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' import { toLinkId } from '@/types/linkId' import { UNASSIGNED_NODE_ID, toNodeId } from '@/types/nodeId' diff --git a/src/lib/litegraph/src/LGraphNode.widgetLabelRename.regression.test.ts b/src/lib/litegraph/src/LGraphNode.widgetLabelRename.regression.test.ts index 1f0aea0e81b..78ff75c343a 100644 --- a/src/lib/litegraph/src/LGraphNode.widgetLabelRename.regression.test.ts +++ b/src/lib/litegraph/src/LGraphNode.widgetLabelRename.regression.test.ts @@ -6,10 +6,8 @@ import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' -import { - createMockCanvasRenderingContext2D, - reloadSerializedGraph -} from '@/utils/__tests__/litegraphTestUtils' +import { reloadSerializedGraph } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' import { renameWidget } from '@/utils/widgetUtil' /** diff --git a/src/lib/litegraph/src/__tests__/DragAndScale.fitToBounds.test.ts b/src/lib/litegraph/src/__tests__/DragAndScale.fitToBounds.test.ts new file mode 100644 index 00000000000..96361819d8f --- /dev/null +++ b/src/lib/litegraph/src/__tests__/DragAndScale.fitToBounds.test.ts @@ -0,0 +1,151 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import type { ReadOnlyRect } from '@/lib/litegraph/src/interfaces' +import { DragAndScale } from '@/lib/litegraph/src/litegraph' +import { createTestCanvasElement } from '@/utils/__tests__/canvasTestUtils' + +describe('DragAndScale.fitToBounds', () => { + beforeEach(() => { + vi.stubGlobal('devicePixelRatio', 1) + }) + + it.for<{ name: string; size: [number, number]; viewport?: ReadOnlyRect }>([ + { name: 'the canvas has no width', size: [0, 400] }, + { name: 'the canvas has no height', size: [400, 0] }, + { + name: 'the supplied viewport is empty', + size: [1000, 500], + viewport: [200, 0, 0, 500] + } + ])( + 'leaves the camera unchanged when $name', + ({ size: [width, height], viewport }) => { + const dragAndScale = new DragAndScale( + createTestCanvasElement({ width, height }) + ) + dragAndScale.offset = [13, 29] + dragAndScale.scale = 2 + + dragAndScale.fitToBounds([0, 0, 500, 500], { viewport }) + + expect(dragAndScale.state).toEqual({ offset: [13, 29], scale: 2 }) + } + ) + + it('uses fallback 1920x1080 when canvas is 300x150', () => { + const dragAndScale = new DragAndScale( + createTestCanvasElement({ width: 300, height: 150 }) + ) + + dragAndScale.fitToBounds([0, 0, 600, 600]) + + expect(dragAndScale.scale).toBeCloseTo(1.35) + }) + + it('centers and scales bounds in the full canvas', () => { + const dragAndScale = new DragAndScale( + createTestCanvasElement({ width: 1000, height: 500 }) + ) + + dragAndScale.fitToBounds([0, 0, 500, 250]) + + expect(dragAndScale.scale).toBeCloseTo(1.25) + expect(dragAndScale.offset[0]).toBeCloseTo(150) + expect(dragAndScale.offset[1]).toBeCloseTo(75) + }) + + it('centers and scales bounds inside the supplied viewport', () => { + const dragAndScale = new DragAndScale( + createTestCanvasElement({ width: 1000, height: 500 }) + ) + + dragAndScale.fitToBounds([0, 0, 500, 250], { + viewport: [200, 0, 600, 500] + }) + + expect(dragAndScale.scale).toBeCloseTo(0.9) + expect((250 + dragAndScale.offset[0]) * dragAndScale.scale).toBeCloseTo(500) + expect((125 + dragAndScale.offset[1]) * dragAndScale.scale).toBeCloseTo(250) + }) + + it('does not fit below the minimum scale in a narrow viewport', () => { + const dragAndScale = new DragAndScale( + createTestCanvasElement({ width: 1000, height: 500 }) + ) + + dragAndScale.fitToBounds([0, 0, 1000, 500], { + viewport: [0, 0, 10, 500] + }) + + expect(dragAndScale.scale).toBe(dragAndScale.min_scale) + }) +}) + +describe('DragAndScale.computeVisibleArea', () => { + it('refreshes a hidden cached viewport from visible layout', () => { + const canvas = createTestCanvasElement({ + width: 1600, + height: 1200, + cssSize: [800, 600] + }) + const dragAndScale = new DragAndScale(canvas) + dragAndScale.setViewportSize(0, 0) + + expect(dragAndScale.getViewportSize()).toEqual([800, 600]) + }) + + it('does not expose its cached viewport tuple by reference', () => { + const dragAndScale = new DragAndScale( + createTestCanvasElement({ width: 1600, height: 1200 }) + ) + dragAndScale.setViewportSize(800, 600) + + dragAndScale.getViewportSize()[0] = 1 + + expect(dragAndScale.getViewportSize()).toEqual([800, 600]) + }) + + it('uses the applied viewport size without reading layout', () => { + const canvas = createTestCanvasElement({ width: 1600, height: 1200 }) + const rectSpy = vi.spyOn(canvas, 'getBoundingClientRect') + const dragAndScale = new DragAndScale(canvas) + dragAndScale.setViewportSize(800, 600) + + dragAndScale.computeVisibleArea(undefined) + + expect(rectSpy).not.toHaveBeenCalled() + expect([ + dragAndScale.visible_area.width, + dragAndScale.visible_area.height + ]).toEqual([800, 600]) + }) + + it.for<{ + name: string + cssSize: [number, number] + expected: [number, number] + }>([ + { + name: 'the CSS size when layout reports one', + cssSize: [800, 500], + expected: [800, 500] + }, + { + name: 'the backing size at DPR 1 when layout reports none', + cssSize: [0, 0], + expected: [400, 250] + } + ])('falls back to $name at a sub-1 browser DPR', ({ cssSize, expected }) => { + vi.stubGlobal('devicePixelRatio', 0.5) + const dragAndScale = new DragAndScale( + createTestCanvasElement({ width: 400, height: 250, cssSize }) + ) + + dragAndScale.computeVisibleArea(undefined) + + expect([ + dragAndScale.visible_area.width, + dragAndScale.visible_area.height + ]).toEqual(expected) + }) +}) diff --git a/src/lib/litegraph/src/canvas/LinkConnector.dropOnNode.test.ts b/src/lib/litegraph/src/canvas/LinkConnector.dropOnNode.test.ts index ef2bf1b4545..d10fa4eafd5 100644 --- a/src/lib/litegraph/src/canvas/LinkConnector.dropOnNode.test.ts +++ b/src/lib/litegraph/src/canvas/LinkConnector.dropOnNode.test.ts @@ -6,7 +6,7 @@ import { LinkConnector, ToInputRenderLink } from '@/lib/litegraph/src/litegraph' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' /** * Multi-group autogrow nodes (e.g. the ByteDance Seedance reference node) stack diff --git a/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts b/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts index feff1bdd2f7..cc254e72492 100644 --- a/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts +++ b/src/lib/litegraph/src/canvas/LinkConnector.integration.test.ts @@ -23,7 +23,7 @@ import { toRerouteId } from '@/types/rerouteId' import { createMockCanvasPointerEvent, createMockCanvasRenderingContext2D -} from '@/utils/__tests__/litegraphTestUtils' +} from '@/utils/__tests__/canvasTestUtils' interface TestContext { pinia: undefined diff --git a/src/lib/litegraph/src/canvas/LinkConnectorSubgraphInputValidation.test.ts b/src/lib/litegraph/src/canvas/LinkConnectorSubgraphInputValidation.test.ts index b7af64c1daa..5bd00e7ef7c 100644 --- a/src/lib/litegraph/src/canvas/LinkConnectorSubgraphInputValidation.test.ts +++ b/src/lib/litegraph/src/canvas/LinkConnectorSubgraphInputValidation.test.ts @@ -20,10 +20,8 @@ import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' import { createTestSubgraph } from '../subgraph/__fixtures__/subgraphHelpers' -import { - createMockCanvasPointerEvent, - createMockNodeInputSlot -} from '@/utils/__tests__/litegraphTestUtils' +import { createMockNodeInputSlot } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasPointerEvent } from '@/utils/__tests__/canvasTestUtils' type MockPointerEvent = CanvasPointerEvent type MockRenderLink = ToOutputRenderLink diff --git a/src/lib/litegraph/src/contextMenuCompat.test.ts b/src/lib/litegraph/src/contextMenuCompat.test.ts index 74ed902e119..0ed4f71546a 100644 --- a/src/lib/litegraph/src/contextMenuCompat.test.ts +++ b/src/lib/litegraph/src/contextMenuCompat.test.ts @@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { legacyMenuCompat } from '@/lib/litegraph/src/contextMenuCompat' import type { IContextMenuValue } from '@/lib/litegraph/src/litegraph' import { LGraphCanvas } from '@/lib/litegraph/src/litegraph' -import { createMockCanvas } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvas } from '@/utils/__tests__/canvasTestUtils' describe('contextMenuCompat', () => { let originalGetCanvasMenuOptions: typeof LGraphCanvas.prototype.getCanvasMenuOptions diff --git a/src/lib/litegraph/src/widgets/NumericWidgetRendering.test.ts b/src/lib/litegraph/src/widgets/NumericWidgetRendering.test.ts index e26c78993bd..ddb97189655 100644 --- a/src/lib/litegraph/src/widgets/NumericWidgetRendering.test.ts +++ b/src/lib/litegraph/src/widgets/NumericWidgetRendering.test.ts @@ -13,7 +13,7 @@ import { createMockCanvas, createMockCanvasPointerEvent, createMockCanvasRenderingContext2D -} from '@/utils/__tests__/litegraphTestUtils' +} from '@/utils/__tests__/canvasTestUtils' function createKnob(node: LGraphNode) { return new KnobWidget( diff --git a/src/platform/workflow/core/services/workflowService.insertWorkflow.test.ts b/src/platform/workflow/core/services/workflowService.insertWorkflow.test.ts index 65911e325b0..38d6bdca692 100644 --- a/src/platform/workflow/core/services/workflowService.insertWorkflow.test.ts +++ b/src/platform/workflow/core/services/workflowService.insertWorkflow.test.ts @@ -13,7 +13,7 @@ import { app } from '@/scripts/app' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { toNodeId } from '@/types/nodeId' import { widgetId } from '@/types/widgetId' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' import { createUuidv4 } from '@/utils/uuid' import type { UUID } from '@/utils/uuid' diff --git a/src/platform/workflow/core/services/workflowService.test.ts b/src/platform/workflow/core/services/workflowService.test.ts index 9f0453560f5..3fb6e358487 100644 --- a/src/platform/workflow/core/services/workflowService.test.ts +++ b/src/platform/workflow/core/services/workflowService.test.ts @@ -655,6 +655,28 @@ describe('useWorkflowService', () => { expect(workflowStore.activeWorkflow.path).toBe('workflows/retained.json') }) + it('does not repaint the retained workflow for a superseded load', async () => { + const workflowStore = useWorkflowStore() + const retained = createWorkflow(null, { + loadable: true, + path: 'workflows/retained.json' + }) + const superseded = createWorkflow(null, { + loadable: true, + path: 'workflows/superseded.json' + }) + workflowStore.attachWorkflow(retained, 0) + workflowStore.attachWorkflow(superseded, 1) + workflowStore.activeWorkflow = retained as LoadedComfyWorkflow + vi.mocked(app.loadGraphData).mockResolvedValueOnce(undefined) + + await expect(useWorkflowService().openWorkflow(superseded)).resolves.toBe( + true + ) + + expect(app.loadGraphData).toHaveBeenCalledOnce() + }) + it('serializes rapid workflow opens so the final selection stays active', async () => { const workflowStore = useWorkflowStore() const first = createWorkflow(null, { diff --git a/src/platform/workflow/core/utils/workflowToClipboardItems.integration.test.ts b/src/platform/workflow/core/utils/workflowToClipboardItems.integration.test.ts index 676c9035835..8ebed606f17 100644 --- a/src/platform/workflow/core/utils/workflowToClipboardItems.integration.test.ts +++ b/src/platform/workflow/core/utils/workflowToClipboardItems.integration.test.ts @@ -16,7 +16,7 @@ import type { SerialisableGraph } from '@/lib/litegraph/src/types/serialisation' import { toRerouteId } from '@/types/rerouteId' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' import { createUuidv4 } from '@/utils/uuid' import { workflowToClipboardItems } from './workflowToClipboardItems' diff --git a/src/platform/workflow/management/stores/workflowStore.test.ts b/src/platform/workflow/management/stores/workflowStore.test.ts index 5422efcdb9f..867ed3fac82 100644 --- a/src/platform/workflow/management/stores/workflowStore.test.ts +++ b/src/platform/workflow/management/stores/workflowStore.test.ts @@ -24,10 +24,10 @@ import { isValidUuid } from '@/utils/formatUtil' import { syncEntities } from '@/utils/syncUtil' import { isSubgraph } from '@/utils/typeGuardUtil' import { - createMockCanvas, createMockChangeTracker, createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvas } from '@/utils/__tests__/canvasTestUtils' // Add mock for api at the top of the file vi.mock('@/scripts/api', () => ({ diff --git a/src/renderer/core/canvas/__tests__/cameraState.test.ts b/src/renderer/core/canvas/__tests__/cameraState.test.ts new file mode 100644 index 00000000000..92c7c1ca1d0 --- /dev/null +++ b/src/renderer/core/canvas/__tests__/cameraState.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from 'vitest' + +import { normalizeCameraState } from '@/renderer/core/canvas/cameraState' + +describe('normalizeCameraState', () => { + it.for([ + { name: 'missing state', value: undefined }, + { name: 'zero scale', value: { offset: [0, 0], scale: 0 } }, + { name: 'negative scale', value: { offset: [0, 0], scale: -1 } }, + { name: 'non-finite scale', value: { offset: [0, 0], scale: Infinity } }, + { name: 'non-finite x', value: { offset: [NaN, 0], scale: 1 } }, + { name: 'non-finite y', value: { offset: [0, null], scale: 1 } } + ])('rejects $name', ({ value }) => { + expect(normalizeCameraState(value)).toBeNull() + }) + + it.for([ + { name: 'array offsets', offset: [-120, 42] }, + { name: 'legacy object-shaped offsets', offset: { 0: -120, 1: 42 } } + ])('accepts $name with a positive scale', ({ offset }) => { + expect(normalizeCameraState({ offset, scale: 0.5 })).toEqual({ + offset: [-120, 42], + scale: 0.5 + }) + }) + + it.for([ + { savedScale: 5e-324, expectedScale: 0.01 }, + { savedScale: 5, expectedScale: 4 } + ])( + 'clamps saved scale $savedScale to the active canvas range', + ({ savedScale, expectedScale }) => { + const state = { offset: [-120, 42], scale: savedScale } + + expect( + normalizeCameraState(state, { minScale: 0.01, maxScale: 4 }) + ).toEqual({ offset: [-120, 42], scale: expectedScale }) + } + ) +}) diff --git a/src/renderer/core/canvas/__tests__/canvasViewport.test.ts b/src/renderer/core/canvas/__tests__/canvasViewport.test.ts new file mode 100644 index 00000000000..3139088a507 --- /dev/null +++ b/src/renderer/core/canvas/__tests__/canvasViewport.test.ts @@ -0,0 +1,208 @@ +import { describe, expect, it, vi } from 'vitest' + +import { + applyParentSizedCanvasStyle, + applyViewport, + measureViewport, + measureViewportFromElement +} from '@/renderer/core/canvas/canvasViewport' +import { + createMockCanvasRenderingContext2D, + createTestCanvasElement +} from '@/utils/__tests__/canvasTestUtils' + +describe('measureViewport', () => { + it('computes physical dimensions from CSS dimensions and DPR', () => { + expect(measureViewport(800, 600, 2)).toEqual({ + cssWidth: 800, + cssHeight: 600, + dpr: 2, + physicalWidth: 1600, + physicalHeight: 1200 + }) + }) + + it.for([ + { rawDpr: 0.5, dpr: 1 }, + { rawDpr: -1, dpr: 1 }, + { rawDpr: Number.POSITIVE_INFINITY, dpr: 1 }, + { rawDpr: Number.NaN, dpr: 1 }, + { rawDpr: 1.5, dpr: 1.5 } + ])('normalizes a raw DPR of $rawDpr to $dpr', ({ rawDpr, dpr }) => { + expect(measureViewport(800, 600, rawDpr).dpr).toBe(dpr) + }) + + it('rounds physical dimensions', () => { + const viewport = measureViewport(801, 601, 1.5) + + expect([viewport.physicalWidth, viewport.physicalHeight]).toEqual([ + 1202, 902 + ]) + }) + + it('returns a frozen object', () => { + expect(Object.isFrozen(measureViewport(800, 600, 2))).toBe(true) + }) +}) + +describe('measureViewportFromElement', () => { + it.for([ + { name: 'a visible canvas', cssSize: [800, 600], expected: [800, 600] }, + { name: 'a hidden canvas', cssSize: [0, 0], expected: [0, 0] } + ] as const)( + 'measures CSS dimensions independently of the backing store for $name', + ({ cssSize, expected }) => { + const canvas = createTestCanvasElement({ + width: 1600, + height: 1200, + cssSize: [...cssSize] + }) + + const viewport = measureViewportFromElement(canvas, 2) + + expect([viewport.cssWidth, viewport.cssHeight]).toEqual(expected) + } + ) + + it('does not multiply an intrinsically sized canvas across measurements', () => { + const canvas = createTestCanvasElement() + vi.spyOn(canvas, 'getBoundingClientRect').mockImplementation( + () => new DOMRect(0, 0, canvas.width, canvas.height) + ) + + applyViewport(measureViewportFromElement(canvas, 2), canvas, canvas) + const second = measureViewportFromElement(canvas, 2) + + expect(second).toMatchObject({ + cssWidth: 800, + cssHeight: 600, + physicalWidth: 1600, + physicalHeight: 1200 + }) + }) + + it('reuses the applied CSS size without resetting a stable backing store', () => { + const canvas = createTestCanvasElement({ cssSize: [800, 600] }) + applyViewport(measureViewport(800, 600, 2), canvas, canvas) + const widthWrite = vi.spyOn(canvas, 'width', 'set') + const heightWrite = vi.spyOn(canvas, 'height', 'set') + + const viewport = measureViewportFromElement(canvas, 1.5) + + expect(viewport).toEqual({ + cssWidth: 800, + cssHeight: 600, + dpr: 1.5, + physicalWidth: 1200, + physicalHeight: 900 + }) + expect(widthWrite).not.toHaveBeenCalled() + expect(heightWrite).not.toHaveBeenCalled() + }) + + it('restores DPR scaling after measuring resets the backing store', () => { + const ctx = createMockCanvasRenderingContext2D() + const canvas = createTestCanvasElement({ ctx }) + const viewport = measureViewport(800, 600, 2) + applyViewport(viewport, canvas, canvas) + vi.spyOn(canvas, 'getBoundingClientRect') + .mockReturnValueOnce(new DOMRect(0, 0, 800.25, 600.25)) + .mockReturnValueOnce(new DOMRect(0, 0, 0, 0)) + + applyViewport(measureViewportFromElement(canvas, 2), canvas, canvas) + + expect(ctx.scale).toHaveBeenCalledTimes(2) + expect(ctx.scale).toHaveBeenLastCalledWith(2, 2) + }) +}) + +describe('applyViewport', () => { + it('sizes and scales both canvases to the physical dimensions', () => { + const fgContext = createMockCanvasRenderingContext2D() + const bgContext = createMockCanvasRenderingContext2D() + const fg = createTestCanvasElement({ ctx: fgContext }) + const bg = createTestCanvasElement({ ctx: bgContext }) + + applyViewport(measureViewport(800, 600, 2), fg, bg) + + expect([fg.width, fg.height, bg.width, bg.height]).toEqual([ + 1600, 1200, 1600, 1200 + ]) + expect(fgContext.scale).toHaveBeenCalledExactlyOnceWith(2, 2) + expect(bgContext.scale).toHaveBeenCalledExactlyOnceWith(2, 2) + }) + + it('scales a shared foreground/background context only once', () => { + const ctx = createMockCanvasRenderingContext2D() + const canvas = createTestCanvasElement({ ctx }) + + applyViewport(measureViewport(800, 600, 2), canvas, canvas) + + expect(ctx.scale).toHaveBeenCalledExactlyOnceWith(2, 2) + }) + + it('hands the applied DPR and CSS dimensions to the consumer', () => { + const consumer = { dpr: 1, ds: { setViewportSize: vi.fn() } } + + applyViewport( + measureViewport(800, 600, 2), + createTestCanvasElement(), + createTestCanvasElement(), + consumer + ) + + expect(consumer.dpr).toBe(2) + expect(consumer.ds.setViewportSize).toHaveBeenCalledWith(800, 600) + }) + + it('does not reset backing stores that already match', () => { + const viewport = measureViewport(800, 600, 2) + const fg = createTestCanvasElement() + const bg = createTestCanvasElement() + applyViewport(viewport, fg, bg) + const sizeWrites = [ + vi.spyOn(fg, 'width', 'set'), + vi.spyOn(fg, 'height', 'set'), + vi.spyOn(bg, 'width', 'set'), + vi.spyOn(bg, 'height', 'set') + ] + + applyViewport(viewport, fg, bg) + + expect(sizeWrites.map((write) => write.mock.calls.length)).toEqual([ + 0, 0, 0, 0 + ]) + }) +}) + +describe('applyParentSizedCanvasStyle', () => { + it('preserves stylesheet-driven dimensions that already match the parent', () => { + const canvas = createTestCanvasElement({ cssSize: [800, 600] }) + + applyParentSizedCanvasStyle(canvas, 800, 600) + + expect([canvas.style.width, canvas.style.height]).toEqual(['', '']) + }) + + it('follows the parent while the canvas style is its own', () => { + const canvas = createTestCanvasElement() + + applyParentSizedCanvasStyle(canvas, 800, 600) + applyParentSizedCanvasStyle(canvas, 1000, 700) + + expect([canvas.style.width, canvas.style.height]).toEqual([ + '1000px', + '700px' + ]) + }) + + it('keeps CSS dimensions supplied by the caller', () => { + const canvas = createTestCanvasElement() + canvas.style.width = '75%' + canvas.style.height = '50vh' + + applyParentSizedCanvasStyle(canvas, 800, 600) + + expect([canvas.style.width, canvas.style.height]).toEqual(['75%', '50vh']) + }) +}) diff --git a/src/renderer/core/canvas/__tests__/useCanvasScheduler.test.ts b/src/renderer/core/canvas/__tests__/useCanvasScheduler.test.ts new file mode 100644 index 00000000000..59335974abc --- /dev/null +++ b/src/renderer/core/canvas/__tests__/useCanvasScheduler.test.ts @@ -0,0 +1,203 @@ +import { fromPartial } from '@total-typescript/shoehorn' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph' +import { reportError } from '@/platform/telemetry/reportError' +import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { createCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler' +import { + createTestCanvasElement, + setCanvasVisible +} from '@/utils/__tests__/canvasTestUtils' + +vi.mock(import('@/platform/telemetry/reportError')) + +describe('createCanvasScheduler', () => { + let storeCanvas: HTMLCanvasElement + + beforeEach(() => { + storeCanvas = createTestCanvasElement({ visible: true }) + document.body.append(storeCanvas) + useCanvasStore().canvas = fromPartial({ canvas: storeCanvas }) + }) + + it('runs an operation immediately when its canvas is visible', () => { + const run = vi.fn() + + createCanvasScheduler().schedule({ run }) + + expect(run).toHaveBeenCalledOnce() + }) + + it('treats a fixed-position canvas as visible without an offset parent', () => { + const run = vi.fn() + Object.defineProperty(storeCanvas, 'offsetParent', { + configurable: true, + value: null + }) + + createCanvasScheduler().schedule({ run }) + + expect(run).toHaveBeenCalledOnce() + }) + + it('checks the operation canvas rather than the store canvas', () => { + const run = vi.fn() + setCanvasVisible(storeCanvas, false) + const element = createTestCanvasElement({ visible: true }) + document.body.append(element) + + createCanvasScheduler().schedule({ element, run }) + + expect(run).toHaveBeenCalledOnce() + }) + + it('queues work while the canvas is hidden and runs it in order on flush', () => { + const scheduler = createCanvasScheduler() + const calls: string[] = [] + setCanvasVisible(storeCanvas, false) + + scheduler.schedule({ run: () => calls.push('first') }) + scheduler.schedule({ run: () => calls.push('second') }) + scheduler.flush() + expect(calls).toEqual([]) + + setCanvasVisible(storeCanvas, true) + scheduler.flush() + expect(calls).toEqual(['first', 'second']) + }) + + it('reports a failing operation and still runs the rest', () => { + const scheduler = createCanvasScheduler() + const error = new Error('op failed') + const after = vi.fn() + setCanvasVisible(storeCanvas, false) + scheduler.schedule({ + run: () => { + throw error + } + }) + scheduler.schedule({ run: after }) + + setCanvasVisible(storeCanvas, true) + scheduler.flush() + + expect(after).toHaveBeenCalledOnce() + expect(reportError).toHaveBeenCalledWith(error, { + errorType: 'canvas_scheduled_operation_failed' + }) + }) + + it('replaces pending keyed work when an operation schedules during flush', () => { + const scheduler = createCanvasScheduler() + const calls: string[] = [] + const second = createTestCanvasElement({ visible: false }) + document.body.append(second) + setCanvasVisible(storeCanvas, false) + + scheduler.schedule({ + run: () => { + calls.push('first') + scheduler.schedule({ + key: 'camera', + isCurrent: () => true, + run: () => calls.push('replacement') + }) + } + }) + scheduler.schedule({ + key: 'camera', + element: second, + isCurrent: () => true, + run: () => calls.push('original') + }) + + setCanvasVisible(storeCanvas, true) + scheduler.flush() + + expect(calls).toEqual(['first', 'replacement']) + }) + + it('discards queued work whose canvas was detached', () => { + const scheduler = createCanvasScheduler() + const run = vi.fn() + const element = createTestCanvasElement({ visible: false }) + document.body.append(element) + scheduler.schedule({ element, run }) + + element.remove() + setCanvasVisible(element, true) + scheduler.flush() + + expect(run).not.toHaveBeenCalled() + }) + + describe('keyed operations', () => { + beforeEach(() => { + setCanvasVisible(storeCanvas, false) + }) + + it.for([ + { + name: 'a current replacement', + replacementIsCurrent: true, + runCounts: { original: 0, replacement: 1 } + }, + { + name: 'a stale replacement', + replacementIsCurrent: false, + runCounts: { original: 1, replacement: 0 } + } + ])( + 'keeps one operation per key when $name is scheduled', + ({ replacementIsCurrent, runCounts }) => { + const scheduler = createCanvasScheduler() + const original = vi.fn() + const replacement = vi.fn() + scheduler.schedule({ + key: 'camera', + isCurrent: () => true, + run: original + }) + scheduler.schedule({ + key: 'camera', + isCurrent: () => replacementIsCurrent, + run: replacement + }) + + setCanvasVisible(storeCanvas, true) + scheduler.flush() + + expect({ + original: original.mock.calls.length, + replacement: replacement.mock.calls.length + }).toEqual(runCounts) + } + ) + + it('drops an operation that becomes stale before it runs', () => { + const scheduler = createCanvasScheduler() + const run = vi.fn() + let current = true + scheduler.schedule({ key: 'camera', isCurrent: () => current, run }) + + current = false + setCanvasVisible(storeCanvas, true) + scheduler.flush() + + expect(run).not.toHaveBeenCalled() + }) + + it('cancels a queued operation by key', () => { + const scheduler = createCanvasScheduler() + const run = vi.fn() + scheduler.schedule({ key: 'camera', isCurrent: () => true, run }) + + scheduler.cancel('camera') + setCanvasVisible(storeCanvas, true) + scheduler.flush() + + expect(run).not.toHaveBeenCalled() + }) + }) +}) diff --git a/src/renderer/core/canvas/cameraState.ts b/src/renderer/core/canvas/cameraState.ts new file mode 100644 index 00000000000..aa0dbf81c83 --- /dev/null +++ b/src/renderer/core/canvas/cameraState.ts @@ -0,0 +1,44 @@ +import type { DragAndScaleState } from '@/lib/litegraph/src/DragAndScale' +import { clamp } from 'es-toolkit' + +interface CameraScaleRange { + readonly minScale: number + readonly maxScale: number +} + +const defaultScaleRange: CameraScaleRange = { minScale: 0.1, maxScale: 10 } + +export function normalizeCameraState( + value: unknown, + { minScale, maxScale }: CameraScaleRange = defaultScaleRange +): DragAndScaleState | null { + if ( + !value || + typeof value !== 'object' || + !('offset' in value) || + !('scale' in value) || + typeof value.offset !== 'object' || + value.offset === null + ) { + return null + } + + if ( + '0' in value.offset && + '1' in value.offset && + typeof value.offset[0] === 'number' && + Number.isFinite(value.offset[0]) && + typeof value.offset[1] === 'number' && + Number.isFinite(value.offset[1]) && + typeof value.scale === 'number' && + Number.isFinite(value.scale) && + value.scale > 0 + ) { + return { + offset: [value.offset[0], value.offset[1]], + scale: clamp(value.scale, minScale, maxScale) + } + } + + return null +} diff --git a/src/renderer/core/canvas/canvasRedrawBudget.test.ts b/src/renderer/core/canvas/canvasRedrawBudget.test.ts index febbab17a26..0586101b0f8 100644 --- a/src/renderer/core/canvas/canvasRedrawBudget.test.ts +++ b/src/renderer/core/canvas/canvasRedrawBudget.test.ts @@ -12,7 +12,7 @@ import { import { useLitegraphSettings } from '@/platform/settings/composables/useLitegraphSettings' import { useSettingStore } from '@/platform/settings/settingStore' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' -import { createMockCanvas2DContext } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' const PROGRESS_EVENTS = 100 @@ -28,7 +28,7 @@ async function drawUntilNoLongerDirty(canvas: LGraphCanvas, maxFrames = 5) { } function createMockCtx(owner: HTMLCanvasElement): CanvasRenderingContext2D { - return createMockCanvas2DContext({ + return createMockCanvasRenderingContext2D({ canvas: owner, translate: vi.fn(), scale: vi.fn(), diff --git a/src/renderer/core/canvas/canvasViewport.ts b/src/renderer/core/canvas/canvasViewport.ts new file mode 100644 index 00000000000..5623241a28d --- /dev/null +++ b/src/renderer/core/canvas/canvasViewport.ts @@ -0,0 +1,169 @@ +interface CanvasViewport { + readonly cssWidth: number + readonly cssHeight: number + readonly dpr: number + readonly physicalWidth: number + readonly physicalHeight: number +} + +interface CanvasViewportConsumer { + dpr: number + ds: { setViewportSize(width: number, height: number): void } +} + +const appliedViewportByCanvas = new WeakMap() +const backingStoreResetByMeasurement = new WeakSet() +const autoSizedStyleByCanvas = new WeakMap< + HTMLCanvasElement, + { width?: string; height?: string } +>() + +function applyParentSizedCanvasStyle( + canvas: HTMLCanvasElement, + width: number, + height: number +): void { + const { style } = canvas + const renderedSize = canvas.getBoundingClientRect() + const previousStyle = autoSizedStyleByCanvas.get(canvas) ?? {} + const nextStyle: { width?: string; height?: string } = {} + if ( + (!style.width && renderedSize.width !== width) || + style.width === previousStyle.width + ) { + style.width = `${width}px` + nextStyle.width = style.width + } + if ( + (!style.height && renderedSize.height !== height) || + style.height === previousStyle.height + ) { + style.height = `${height}px` + nextStyle.height = style.height + } + autoSizedStyleByCanvas.set(canvas, nextStyle) +} + +function normalizeDpr(rawDpr: number): number { + return rawDpr > 0 && Number.isFinite(rawDpr) ? Math.max(rawDpr, 1) : 1 +} + +function readBrowserDpr(): number { + return typeof window === 'undefined' + ? 1 + : normalizeDpr(window.devicePixelRatio) +} + +function measureViewport( + cssWidth: number, + cssHeight: number, + rawDpr: number +): CanvasViewport { + // Keep at least one backing pixel per CSS pixel. DPR values below one are + // valid browser inputs, but using them directly makes the canvas blurry. + const dpr = normalizeDpr(rawDpr) + return Object.freeze({ + cssWidth, + cssHeight, + dpr, + physicalWidth: Math.round(cssWidth * dpr), + physicalHeight: Math.round(cssHeight * dpr) + }) +} + +function measureViewportFromElement( + element: HTMLCanvasElement, + rawDpr = window.devicePixelRatio +): CanvasViewport { + const initialRect = element.getBoundingClientRect() + if (initialRect.width === 0 || initialRect.height === 0) { + return measureViewport(initialRect.width, initialRect.height, rawDpr) + } + + const previousViewport = appliedViewportByCanvas.get(element) + if ( + previousViewport && + initialRect.width === previousViewport.cssWidth && + initialRect.height === previousViewport.cssHeight + ) { + return measureViewport( + previousViewport.cssWidth, + previousViewport.cssHeight, + rawDpr + ) + } + + const savedWidth = element.width + const savedHeight = element.height + let cssRect: DOMRect + try { + element.width = 0 + element.height = 0 + cssRect = element.getBoundingClientRect() + } finally { + element.width = savedWidth + element.height = savedHeight + backingStoreResetByMeasurement.add(element) + } + const width = cssRect.width || previousViewport?.cssWidth || initialRect.width + const height = + cssRect.height || previousViewport?.cssHeight || initialRect.height + return measureViewport(width, height, rawDpr) +} + +function applyViewport( + viewport: CanvasViewport, + fg: HTMLCanvasElement, + bg: HTMLCanvasElement, + consumer?: CanvasViewportConsumer +): CanvasViewport { + const previousForegroundViewport = appliedViewportByCanvas.get(fg) + const foregroundResetByMeasurement = backingStoreResetByMeasurement.delete(fg) + const foregroundChanged = + foregroundResetByMeasurement || + fg.width !== viewport.physicalWidth || + fg.height !== viewport.physicalHeight || + previousForegroundViewport?.dpr !== viewport.dpr + if ( + fg.width !== viewport.physicalWidth || + previousForegroundViewport?.dpr !== viewport.dpr + ) + fg.width = viewport.physicalWidth + if (fg.height !== viewport.physicalHeight) fg.height = viewport.physicalHeight + if (foregroundChanged) fg.getContext('2d')?.scale(viewport.dpr, viewport.dpr) + if (bg !== fg) { + const previousBackgroundViewport = appliedViewportByCanvas.get(bg) + const backgroundResetByMeasurement = + backingStoreResetByMeasurement.delete(bg) + const backgroundChanged = + backgroundResetByMeasurement || + bg.width !== viewport.physicalWidth || + bg.height !== viewport.physicalHeight || + previousBackgroundViewport?.dpr !== viewport.dpr + if ( + bg.width !== viewport.physicalWidth || + previousBackgroundViewport?.dpr !== viewport.dpr + ) + bg.width = viewport.physicalWidth + if (bg.height !== viewport.physicalHeight) + bg.height = viewport.physicalHeight + if (backgroundChanged) + bg.getContext('2d')?.scale(viewport.dpr, viewport.dpr) + } + + appliedViewportByCanvas.set(fg, viewport) + appliedViewportByCanvas.set(bg, viewport) + if (consumer) { + consumer.dpr = viewport.dpr + consumer.ds.setViewportSize(viewport.cssWidth, viewport.cssHeight) + } + return viewport +} + +export { + readBrowserDpr, + measureViewport, + measureViewportFromElement, + applyParentSizedCanvasStyle, + applyViewport +} diff --git a/src/renderer/core/canvas/pathRenderer.test.ts b/src/renderer/core/canvas/pathRenderer.test.ts index c5d921ede1d..cf00614eba7 100644 --- a/src/renderer/core/canvas/pathRenderer.test.ts +++ b/src/renderer/core/canvas/pathRenderer.test.ts @@ -7,25 +7,6 @@ import type { } from '@/renderer/core/canvas/pathRenderer' import { CanvasPathRenderer } from '@/renderer/core/canvas/pathRenderer' -class StubPath2D { - calls: Array<{ method: string; args: unknown[] }> = [] - moveTo(...args: unknown[]) { - this.calls.push({ method: 'moveTo', args }) - } - lineTo(...args: unknown[]) { - this.calls.push({ method: 'lineTo', args }) - } - bezierCurveTo(...args: unknown[]) { - this.calls.push({ method: 'bezierCurveTo', args }) - } - quadraticCurveTo(...args: unknown[]) { - this.calls.push({ method: 'quadraticCurveTo', args }) - } - arc(...args: unknown[]) { - this.calls.push({ method: 'arc', args }) - } -} - function createMockCtx(): CanvasRenderingContext2D { return { save: vi.fn(), @@ -80,7 +61,6 @@ describe('CanvasPathRenderer', () => { let renderer: CanvasPathRenderer beforeEach(() => { - vi.stubGlobal('Path2D', StubPath2D) renderer = new CanvasPathRenderer() }) @@ -200,24 +180,19 @@ describe('CanvasPathRenderer', () => { style: { mode: 'linear', connectionWidth: 3 } }) - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D + const path = renderer.drawLink(ctx, link, context) - const moveToCall = path.calls.find((c) => c.method === 'moveTo') - const lineToCalls = path.calls.filter((c) => c.method === 'lineTo') + const lineToCalls = vi.mocked(path.lineTo).mock.calls - expect(moveToCall).toBeDefined() + expect(path.moveTo).toHaveBeenCalled() // Linear mode: start -> innerA -> innerB -> end = 3 lineTo calls expect(lineToCalls).toHaveLength(3) // innerA.x = start.x + 15 (right direction) - expect(lineToCalls[0].args).toEqual([15, 0]) + expect(lineToCalls[0]).toEqual([15, 0]) // innerB.x = end.x - 15 (left direction) - expect(lineToCalls[1].args).toEqual([185, 100]) + expect(lineToCalls[1]).toEqual([185, 100]) // end point - expect(lineToCalls[2].args).toEqual([200, 100]) + expect(lineToCalls[2]).toEqual([200, 100]) }) it('handles up/down directions', () => { @@ -232,16 +207,12 @@ describe('CanvasPathRenderer', () => { style: { mode: 'linear', connectionWidth: 3 } }) - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const lineToCalls = path.calls.filter((c) => c.method === 'lineTo') + const path = renderer.drawLink(ctx, link, context) + const lineToCalls = vi.mocked(path.lineTo).mock.calls // innerA: y + 15 (down), innerB: y - 15 (up) - expect(lineToCalls[0].args).toEqual([50, 65]) - expect(lineToCalls[1].args).toEqual([50, 185]) + expect(lineToCalls[0]).toEqual([50, 65]) + expect(lineToCalls[1]).toEqual([50, 185]) }) it('handles none direction (no offset)', () => { @@ -254,16 +225,12 @@ describe('CanvasPathRenderer', () => { style: { mode: 'linear', connectionWidth: 3 } }) - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const lineToCalls = path.calls.filter((c) => c.method === 'lineTo') + const path = renderer.drawLink(ctx, link, context) + const lineToCalls = vi.mocked(path.lineTo).mock.calls // No offset — innerA == start, innerB == end - expect(lineToCalls[0].args).toEqual([0, 0]) - expect(lineToCalls[1].args).toEqual([200, 100]) + expect(lineToCalls[0]).toEqual([0, 0]) + expect(lineToCalls[1]).toEqual([200, 100]) }) }) @@ -280,22 +247,18 @@ describe('CanvasPathRenderer', () => { style: { mode: 'straight', connectionWidth: 3 } }) - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const lineToCalls = path.calls.filter((c) => c.method === 'lineTo') + const path = renderer.drawLink(ctx, link, context) + const lineToCalls = vi.mocked(path.lineTo).mock.calls // straight: start -> innerA -> (midX, innerA.y) -> (midX, innerB.y) -> innerB -> end expect(lineToCalls).toHaveLength(5) // innerA = (10, 0), innerB = (190, 100), midX = 100 - expect(lineToCalls[0].args).toEqual([10, 0]) // innerA - expect(lineToCalls[1].args).toEqual([100, 0]) // midX, innerA.y - expect(lineToCalls[2].args).toEqual([100, 100]) // midX, innerB.y - expect(lineToCalls[3].args).toEqual([190, 100]) // innerB - expect(lineToCalls[4].args).toEqual([200, 100]) // end + expect(lineToCalls[0]).toEqual([10, 0]) // innerA + expect(lineToCalls[1]).toEqual([100, 0]) // midX, innerA.y + expect(lineToCalls[2]).toEqual([100, 100]) // midX, innerB.y + expect(lineToCalls[3]).toEqual([190, 100]) // innerB + expect(lineToCalls[4]).toEqual([200, 100]) // end }) it('handles up direction offset (l=10)', () => { @@ -310,16 +273,12 @@ describe('CanvasPathRenderer', () => { style: { mode: 'straight', connectionWidth: 3 } }) - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const lineToCalls = path.calls.filter((c) => c.method === 'lineTo') + const path = renderer.drawLink(ctx, link, context) + const lineToCalls = vi.mocked(path.lineTo).mock.calls // innerA = (100, 90), innerB = (100, 310), midX = 100 - expect(lineToCalls[0].args).toEqual([100, 90]) - expect(lineToCalls[3].args).toEqual([100, 310]) + expect(lineToCalls[0]).toEqual([100, 90]) + expect(lineToCalls[3]).toEqual([100, 310]) }) }) @@ -333,15 +292,8 @@ describe('CanvasPathRenderer', () => { const link = makeLink({ controlPoints: cp }) const context = makeContext() - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const bezier = path.calls.find((c) => c.method === 'bezierCurveTo') - - expect(bezier).toBeDefined() - expect(bezier!.args).toEqual([50, 10, 150, 90, 200, 100]) + const path = renderer.drawLink(ctx, link, context) + expect(path.bezierCurveTo).toHaveBeenCalledWith(50, 10, 150, 90, 200, 100) }) it('auto-calculates control points when none provided', () => { @@ -349,14 +301,8 @@ describe('CanvasPathRenderer', () => { const link = makeLink() const context = makeContext() - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const bezier = path.calls.find((c) => c.method === 'bezierCurveTo') - - expect(bezier).toBeDefined() + const path = renderer.drawLink(ctx, link, context) + expect(path.bezierCurveTo).toHaveBeenCalled() }) it('uses quadratic curve for single control point', () => { @@ -364,15 +310,8 @@ describe('CanvasPathRenderer', () => { const link = makeLink({ controlPoints: [{ x: 100, y: 50 }] }) const context = makeContext() - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const quad = path.calls.find((c) => c.method === 'quadraticCurveTo') - - expect(quad).toBeDefined() - expect(quad!.args).toEqual([100, 50, 200, 100]) + const path = renderer.drawLink(ctx, link, context) + expect(path.quadraticCurveTo).toHaveBeenCalledWith(100, 50, 200, 100) }) it('falls back to lineTo when no control points', () => { @@ -380,15 +319,8 @@ describe('CanvasPathRenderer', () => { const link = makeLink({ controlPoints: [] }) const context = makeContext() - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const lineToCall = path.calls.find((c) => c.method === 'lineTo') - - expect(lineToCall).toBeDefined() - expect(lineToCall!.args).toEqual([200, 100]) + const path = renderer.drawLink(ctx, link, context) + expect(path.lineTo).toHaveBeenCalledWith(200, 100) }) }) @@ -403,20 +335,16 @@ describe('CanvasPathRenderer', () => { }) const context = makeContext() - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const bezier = path.calls.find((c) => c.method === 'bezierCurveTo')! + const path = renderer.drawLink(ctx, link, context) + const bezier = vi.mocked(path.bezierCurveTo).mock.calls[0] // dist=400, controlDist = max(30, 400*0.25) = 100 // cp1 = (0+100, 0) = (100, 0) // cp2 = (400-100, 0) = (300, 0) - expect(bezier.args[0]).toBe(100) // cp1.x - expect(bezier.args[1]).toBe(0) // cp1.y - expect(bezier.args[2]).toBe(300) // cp2.x - expect(bezier.args[3]).toBe(0) // cp2.y + expect(bezier[0]).toBe(100) // cp1.x + expect(bezier[1]).toBe(0) // cp1.y + expect(bezier[2]).toBe(300) // cp2.x + expect(bezier[3]).toBe(0) // cp2.y }) it('uses minimum controlDist of 30 for short links', () => { @@ -429,16 +357,12 @@ describe('CanvasPathRenderer', () => { }) const context = makeContext() - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const bezier = path.calls.find((c) => c.method === 'bezierCurveTo')! + const path = renderer.drawLink(ctx, link, context) + const bezier = vi.mocked(path.bezierCurveTo).mock.calls[0] // dist=10, controlDist = max(30, 10*0.25) = 30 - expect(bezier.args[0]).toBe(30) // cp1.x = 0 + 30 - expect(bezier.args[2]).toBe(-20) // cp2.x = 10 - 30 + expect(bezier[0]).toBe(30) // cp1.x = 0 + 30 + expect(bezier[2]).toBe(-20) // cp2.x = 10 - 30 }) }) @@ -857,11 +781,10 @@ describe('CanvasPathRenderer', () => { fromInput: true }, context - ) as unknown as StubPath2D + ) // When fromInput, dragPoint becomes start and fixedPoint becomes end - const moveToCall = path.calls.find((c) => c.method === 'moveTo') - expect(moveToCall!.args).toEqual([0, 0]) + expect(path.moveTo).toHaveBeenCalledWith(0, 0) }) it('uses custom dragDirection when provided', () => { @@ -934,7 +857,7 @@ describe('CanvasPathRenderer', () => { const result = renderer.drawLink(ctx, link, context) - expect(result).toBeInstanceOf(StubPath2D) + expect(result).toBeInstanceOf(Path2D) }) }) @@ -959,14 +882,10 @@ describe('CanvasPathRenderer', () => { style: { mode: 'linear', connectionWidth: 3 } }) - const path = renderer.drawLink( - ctx, - link, - context - ) as unknown as StubPath2D - const lineToCalls = path.calls.filter((c) => c.method === 'lineTo') + const path = renderer.drawLink(ctx, link, context) + const lineToCalls = vi.mocked(path.lineTo).mock.calls - expect(lineToCalls[0].args).toEqual(expectedInnerA) + expect(lineToCalls[0]).toEqual(expectedInnerA) } ) }) diff --git a/src/renderer/core/canvas/useCanvasScheduler.ts b/src/renderer/core/canvas/useCanvasScheduler.ts new file mode 100644 index 00000000000..104baeaa3b0 --- /dev/null +++ b/src/renderer/core/canvas/useCanvasScheduler.ts @@ -0,0 +1,100 @@ +import { createSharedComposable } from '@vueuse/core' + +import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { reportError } from '@/platform/telemetry/reportError' + +interface CanvasOperationBase { + element?: HTMLCanvasElement + run: () => void +} + +type CanvasOperation = + | (CanvasOperationBase & { key?: never; isCurrent?: never }) + | (CanvasOperationBase & { key: string; isCurrent: () => boolean }) + +export interface CanvasScheduler { + /** Run an op now when its canvas is visible, otherwise queue it. */ + schedule(operation: CanvasOperation): void + /** Run every queued op whose canvas is now visible; keep the rest queued. */ + flush(): void + /** Discard the pending operation with this key. */ + cancel(key: string): void +} + +export function createCanvasScheduler(): CanvasScheduler { + const canvasStore = useCanvasStore() + const queue: CanvasOperation[] = [] + let isFlushing = false + + function targetOf(operation: CanvasOperation): HTMLCanvasElement | undefined { + return operation.element ?? canvasStore.canvas?.canvas + } + + function isVisible(element: HTMLCanvasElement | undefined): boolean { + return ( + element?.isConnected === true && + element.offsetWidth > 0 && + element.offsetHeight > 0 + ) + } + + function isStale(operation: CanvasOperation): boolean { + if (operation.isCurrent?.() === false) return true + const { element } = operation + return Boolean( + element && element !== canvasStore.canvas?.canvas && !element.isConnected + ) + } + + function run(operation: CanvasOperation): void { + try { + operation.run() + } catch (error) { + reportError(error, { + errorType: 'canvas_scheduled_operation_failed' + }) + } + } + + function flush(): void { + if (isFlushing) return + + isFlushing = true + try { + for (let index = 0; index < queue.length;) { + const operation = queue[index] + if (isStale(operation)) { + queue.splice(index, 1) + } else if (isVisible(targetOf(operation))) { + queue.splice(index, 1) + run(operation) + } else { + index++ + } + } + } finally { + isFlushing = false + } + } + + function schedule(operation: CanvasOperation): void { + if (operation.isCurrent?.() === false) return + + const existingIndex = operation.key + ? queue.findIndex(({ key }) => key === operation.key) + : -1 + if (existingIndex === -1) queue.push(operation) + else queue[existingIndex] = operation + + if (isVisible(targetOf(operation))) flush() + } + + function cancel(key: string): void { + const index = queue.findIndex((operation) => operation.key === key) + if (index !== -1) queue.splice(index, 1) + } + + return { schedule, flush, cancel } +} + +export const useCanvasScheduler = createSharedComposable(createCanvasScheduler) diff --git a/src/renderer/core/layout/__tests__/TransformPane.test.ts b/src/renderer/core/layout/__tests__/TransformPane.test.ts index 1e9b32eb595..32ad23f1d66 100644 --- a/src/renderer/core/layout/__tests__/TransformPane.test.ts +++ b/src/renderer/core/layout/__tests__/TransformPane.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it, vi } from 'vitest' import { computed, nextTick } from 'vue' import { useTransformState } from '@/renderer/core/layout/transform/useTransformState' -import { createMockCanvas } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvas } from '@/utils/__tests__/canvasTestUtils' import TransformPane from '../transform/TransformPane.vue' diff --git a/src/renderer/core/layout/operations/graphLayoutAttachment.perf.test.ts b/src/renderer/core/layout/operations/graphLayoutAttachment.perf.test.ts index 352d993b30d..4d5d0973816 100644 --- a/src/renderer/core/layout/operations/graphLayoutAttachment.perf.test.ts +++ b/src/renderer/core/layout/operations/graphLayoutAttachment.perf.test.ts @@ -11,7 +11,7 @@ import { } from '@/lib/litegraph/src/litegraph' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { LayoutSource } from '@/renderer/core/layout/types' -import { createMockCanvas2DContext } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' type GeometryCounts = { contentLookups: number @@ -204,7 +204,7 @@ function createRenderer(nodeCount: number): { context: CanvasRenderingContext2D nodes: LGraphNode[] } { - const context = createMockCanvas2DContext({ + const context = createMockCanvasRenderingContext2D({ bezierCurveTo: vi.fn(), clip: vi.fn(), closePath: vi.fn(), diff --git a/src/renderer/core/layout/store/hitTargetAuthority.test.ts b/src/renderer/core/layout/store/hitTargetAuthority.test.ts index 96444af04d9..19d6c3a525c 100644 --- a/src/renderer/core/layout/store/hitTargetAuthority.test.ts +++ b/src/renderer/core/layout/store/hitTargetAuthority.test.ts @@ -10,7 +10,7 @@ import { import { getNodeInputOnPos } from '@/lib/litegraph/src/canvas/measureSlots' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { toGroupId } from '@/types/groupId' -import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/litegraphTestUtils' +import { createMockCanvasRenderingContext2D } from '@/utils/__tests__/canvasTestUtils' vi.mock('@/renderer/core/canvas/canvasStore', () => ({ useCanvasStore: () => ({}) diff --git a/src/renderer/core/layout/store/layoutStore.test.ts b/src/renderer/core/layout/store/layoutStore.test.ts index d7203d56723..3ee193b44ee 100644 --- a/src/renderer/core/layout/store/layoutStore.test.ts +++ b/src/renderer/core/layout/store/layoutStore.test.ts @@ -940,3 +940,72 @@ describe('layoutStore content-size performance contract', () => { } ) }) + +describe('layoutStore queryLinkSegmentAtPoint DPR threading', () => { + beforeEach(() => { + layoutStore.resetForTests() + layoutStore.updateLinkSegmentLayout(toLinkId(1), null, { + path: new Path2D(), + bounds: { x: 0, y: 0, width: 100, height: 100 }, + centerPos: { x: 50, y: 50 } + }) + }) + + function strokeHitOnlyAt(x: number, y: number) { + return fromPartial({ + lineWidth: 17, + isPointInStroke: (_path: Path2D, hitX: number, hitY: number) => + hitX === x && hitY === y + }) + } + + it('scales the CSS-space point by the caller-supplied dpr', () => { + expect( + layoutStore.queryLinkSegmentAtPoint( + { x: 50, y: 50 }, + strokeHitOnlyAt(25, 25), + 0.5 + ) + ).toEqual({ linkId: toLinkId(1), rerouteId: null }) + }) + + it('forwards the caller-supplied dpr through the link-only query', () => { + expect( + layoutStore.queryLinkAtPoint( + { x: 50, y: 50 }, + strokeHitOnlyAt(25, 25), + 0.5 + ) + ).toBe(toLinkId(1)) + }) + + it.for([ + { + name: 'the browser DPR', + global: 'devicePixelRatio', + value: 2, + hitAt: 100 + }, + { + name: 'a sub-1 browser DPR clamped to 1', + global: 'devicePixelRatio', + value: 0.5, + hitAt: 50 + }, + { + name: 'DPR 1 without a window', + global: 'window', + value: undefined, + hitAt: 50 + } + ])('falls back to $name when dpr is omitted', ({ global, value, hitAt }) => { + vi.stubGlobal(global, value) + + expect( + layoutStore.queryLinkSegmentAtPoint( + { x: 50, y: 50 }, + strokeHitOnlyAt(hitAt, hitAt) + ) + ).toEqual({ linkId: toLinkId(1), rerouteId: null }) + }) +}) diff --git a/src/renderer/core/layout/store/layoutStore.ts b/src/renderer/core/layout/store/layoutStore.ts index 71374c51546..ef7a89d85bc 100644 --- a/src/renderer/core/layout/store/layoutStore.ts +++ b/src/renderer/core/layout/store/layoutStore.ts @@ -6,6 +6,7 @@ import * as Y from 'yjs' import { toGroupId } from '@/types/groupId' import { toNodeId } from '@/types/nodeId' import type { GroupId } from '@/types/groupId' +import { readBrowserDpr } from '@/renderer/core/canvas/canvasViewport' import { removeNodeTitleHeight } from '@/renderer/core/layout/utils/nodeSizeUtil' import { toRerouteId } from '@/types/rerouteId' import type { UUID } from '@/utils/uuid' @@ -598,10 +599,17 @@ class LayoutStoreImpl { } /** * Query link segment at point (returns structured data) + * + * @param dpr Device pixel ratio used to map the CSS-space point into the + * canvas's device-pixel-scaled stroke space. Pass the active + * `LGraphCanvas.dpr` so this hit-test agrees with `processMouseDown`'s + * `isPointInStroke` fallback. Defaults to the normalized browser DPR + * for legacy callers without a canvas reference. */ queryLinkSegmentAtPoint( point: Point, - ctx?: CanvasRenderingContext2D + ctx?: CanvasRenderingContext2D, + dpr = readBrowserDpr() ): { linkId: LinkId; rerouteId: RerouteId | null } | null { // Determine tolerance from current canvas state (if available) // - Use the caller-provided ctx.lineWidth (LGraphCanvas sets this to connections_width + padding) @@ -632,13 +640,10 @@ class LayoutStoreImpl { if (!segmentLayout) continue if (ctx) { - // Match LiteGraph behavior: hit test uses device pixel ratio for coordinates - const dpi = - (typeof window !== 'undefined' && window.devicePixelRatio) || 1 const hit = ctx.isPointInStroke( segmentLayout.path, - point.x * dpi, - point.y * dpi + point.x * dpr, + point.y * dpr ) if (hit) { @@ -669,10 +674,11 @@ class LayoutStoreImpl { */ queryLinkAtPoint( point: Point, - ctx?: CanvasRenderingContext2D + ctx?: CanvasRenderingContext2D, + dpr?: number ): LinkId | null { // Invoke segment query and return just the linkId - const segment = this.queryLinkSegmentAtPoint(point, ctx) + const segment = this.queryLinkSegmentAtPoint(point, ctx, dpr) return segment ? segment.linkId : null } diff --git a/src/renderer/extensions/minimap/composables/useMinimap.intervalLifecycle.test.ts b/src/renderer/extensions/minimap/composables/useMinimap.intervalLifecycle.test.ts index a30dc178661..e78a3fe6d80 100644 --- a/src/renderer/extensions/minimap/composables/useMinimap.intervalLifecycle.test.ts +++ b/src/renderer/extensions/minimap/composables/useMinimap.intervalLifecycle.test.ts @@ -7,11 +7,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { nextTick, shallowRef } from 'vue' +import { createMockLinks } from '@/utils/__tests__/litegraphTestUtils' import { - createMockCanvas2DContext, - createMockLinks, + createMockCanvasRenderingContext2D, createMockMinimapCanvas -} from '@/utils/__tests__/litegraphTestUtils' +} from '@/utils/__tests__/canvasTestUtils' const mockNodes = [ { @@ -140,7 +140,7 @@ describe('useMinimap change-detection interval', () => { beforeEach(() => { vi.useFakeTimers() - context = createMockCanvas2DContext() + context = createMockCanvasRenderingContext2D() mockNodes[0].pos = [0, 0] }) diff --git a/src/renderer/extensions/minimap/composables/useMinimap.test.ts b/src/renderer/extensions/minimap/composables/useMinimap.test.ts index 1540200f651..fe13cbb8112 100644 --- a/src/renderer/extensions/minimap/composables/useMinimap.test.ts +++ b/src/renderer/extensions/minimap/composables/useMinimap.test.ts @@ -11,9 +11,9 @@ import { toLinkId } from '@/types/linkId' import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import { toNodeId } from '@/types/nodeId' import { - createMockCanvas2DContext, + createMockCanvasRenderingContext2D, createMockMinimapCanvas -} from '@/utils/__tests__/litegraphTestUtils' +} from '@/utils/__tests__/canvasTestUtils' import type { UUID } from '@/utils/uuid' interface MockNode { @@ -299,7 +299,7 @@ describe('useMinimap', () => { setActivePinia(createTestingPinia({ stubActions: false })) registerMockLink(1, 'node2') - mockContext2D = createMockCanvas2DContext() + mockContext2D = createMockCanvasRenderingContext2D() moduleMockCanvasElement = createMockMinimapCanvas({ getContext: vi diff --git a/src/renderer/extensions/minimap/composables/useMinimapViewport.test.ts b/src/renderer/extensions/minimap/composables/useMinimapViewport.test.ts index 8647759df88..fbc99db9277 100644 --- a/src/renderer/extensions/minimap/composables/useMinimapViewport.test.ts +++ b/src/renderer/extensions/minimap/composables/useMinimapViewport.test.ts @@ -40,6 +40,7 @@ describe('useMinimapViewport', () => { width: 1600, height: 1200 } as HTMLCanvasElement, + dpr: 2, ds: { scale: 1, offset: [0, 0] diff --git a/src/renderer/extensions/minimap/composables/useMinimapViewport.ts b/src/renderer/extensions/minimap/composables/useMinimapViewport.ts index 61c01c190ff..ce592df6460 100644 --- a/src/renderer/extensions/minimap/composables/useMinimapViewport.ts +++ b/src/renderer/extensions/minimap/composables/useMinimapViewport.ts @@ -44,7 +44,7 @@ export function useMinimapViewport( if (!c) return const canvasEl = c.canvas - const dpr = window.devicePixelRatio || 1 + const dpr = c.dpr canvasDimensions.value = { width: canvasEl.clientWidth || canvasEl.width / dpr, diff --git a/src/renderer/extensions/minimap/types.ts b/src/renderer/extensions/minimap/types.ts index ef60a13133e..2b13827f032 100644 --- a/src/renderer/extensions/minimap/types.ts +++ b/src/renderer/extensions/minimap/types.ts @@ -9,6 +9,7 @@ import type { NodeId } from '@/types/nodeId' */ export interface MinimapCanvas { canvas: HTMLCanvasElement + dpr: number ds: { scale: number offset: [number, number] diff --git a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue index 09bb43ccca0..89b74aee0f1 100644 --- a/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue +++ b/src/renderer/extensions/vueNodes/components/LGraphNodePreview.vue @@ -15,7 +15,7 @@

diff --git a/src/scripts/app.test.ts b/src/scripts/app.test.ts index 48edbee4c14..853fc88ebdd 100644 --- a/src/scripts/app.test.ts +++ b/src/scripts/app.test.ts @@ -19,6 +19,11 @@ import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/ import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' import type { useWorkflowValidation } from '@/platform/workflow/validation/composables/useWorkflowValidation' import { createMockChangeTracker } from '@/utils/__tests__/litegraphTestUtils' +import { + createTestCanvasElement, + createTestDragAndScale, + setCanvasVisible +} from '@/utils/__tests__/canvasTestUtils' import { useNodeReplacementStore } from '@/platform/nodeReplacement/nodeReplacementStore' import type { NodeReplacement } from '@/platform/nodeReplacement/types' import type { NodeExecutionOutput } from '@/schemas/apiSchema' @@ -60,6 +65,7 @@ import { useWidgetValueStore } from '@/stores/widgetValueStore' import { extractFilesFromDragEvent } from '@/utils/eventUtils' import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema' import { reportError } from '@/platform/telemetry/reportError' +import { useCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler' import { zeroUuid } from '@/utils/uuid' import type { importA1111 } from './pnginfo' @@ -350,6 +356,35 @@ describe('ComfyApp', () => { ) }) + it('resizes both canvas layers in device pixels and redraws immediately', () => { + vi.stubGlobal('devicePixelRatio', 2) + // Both layers start off the expected result. The fixture's default backing + // store is 800x600, which is exactly 400x300 CSS at DPR 2, so leaving it + // would satisfy the dimension assertions even if a layer were never sized. + const foreground = createTestCanvasElement({ + width: 1, + height: 1, + cssSize: [400, 300] + }) + const background = createTestCanvasElement({ + width: 1, + height: 1, + cssSize: [400, 300] + }) + const ds = createTestDragAndScale() + const setViewportSize = vi.spyOn(ds, 'setViewportSize') + Reflect.set(mockCanvas, 'bgcanvas', background) + Reflect.set(mockCanvas, 'ds', ds) + + Reflect.apply(Reflect.get(app, 'resizeCanvas'), app, [foreground]) + + expect([foreground.width, foreground.height]).toEqual([800, 600]) + expect([background.width, background.height]).toEqual([800, 600]) + expect(mockCanvas.dpr).toBe(2) + expect(setViewportSize).toHaveBeenCalledWith(400, 300) + expect(mockCanvas.draw).toHaveBeenCalledExactlyOnceWith(true, true) + }) + describe('loadGraphData', () => { it('forwards clean and navigation intent to workflow navigation', async () => { app.canvasElRef.value = document.createElement('canvas') @@ -509,6 +544,221 @@ describe('ComfyApp', () => { ]) }) + it('closes every beforeLoadGraph when a newer load overtakes an older one', async () => { + app.canvasElRef.value = document.createElement('canvas') + Reflect.set(app, 'rootGraphInternal', new LGraph()) + let releaseFirstLoad!: () => void + const firstLoadBlocked = new Promise((resolve) => { + releaseFirstLoad = resolve + }) + let firstBeforeLoad = true + mockExtensionService.invokeExtensionsAsync.mockImplementation( + async (hook) => { + if (hook === 'beforeLoadGraph' && firstBeforeLoad) { + firstBeforeLoad = false + await firstLoadBlocked + } + } + ) + + const olderLoad = app.loadGraphData(createWorkflowGraphData(), false) + await app.loadGraphData(createWorkflowGraphData(), false) + releaseFirstLoad() + await expect(olderLoad).resolves.toBeUndefined() + + const hooks = mockExtensionService.invokeExtensionsAsync.mock.calls.map( + ([hook]) => hook + ) + const opened = hooks.filter((hook) => hook === 'beforeLoadGraph') + const closed = hooks.filter( + (hook) => hook === 'afterConfigureGraph' || hook === 'onGraphLoadError' + ) + expect(closed).toHaveLength(opened.length) + }) + + it('does not clean the committed graph when an older load resumes', async () => { + app.canvasElRef.value = document.createElement('canvas') + const graph = new LGraph() + Reflect.set(app, 'rootGraphInternal', graph) + const clear = vi.spyOn(graph, 'clear') + let releaseFirstLoad!: () => void + const firstLoadBlocked = new Promise((resolve) => { + releaseFirstLoad = resolve + }) + let firstBeforeLoad = true + mockExtensionService.invokeExtensionsAsync.mockImplementation( + async (hook) => { + if (hook === 'beforeLoadGraph' && firstBeforeLoad) { + firstBeforeLoad = false + await firstLoadBlocked + } + } + ) + + const olderLoad = app.loadGraphData(createWorkflowGraphData()) + await app.loadGraphData(createWorkflowGraphData()) + clear.mockClear() + releaseFirstLoad() + await olderLoad + + expect(clear).not.toHaveBeenCalled() + }) + + it('lets an older valid load commit when its newer replacement fails', async () => { + app.canvasElRef.value = document.createElement('canvas') + Reflect.set(app, 'rootGraphInternal', new LGraph()) + let releaseOlderLoad!: () => void + const olderLoadBlocked = new Promise((resolve) => { + releaseOlderLoad = resolve + }) + let firstBeforeLoad = true + let rejectNextConfigure = true + mockExtensionService.invokeExtensionsAsync.mockImplementation( + async (hook) => { + if (hook === 'beforeLoadGraph' && firstBeforeLoad) { + firstBeforeLoad = false + await olderLoadBlocked + } + if (hook === 'beforeConfigureGraph' && rejectNextConfigure) { + rejectNextConfigure = false + throw new Error('newer load failed') + } + } + ) + + const olderLoad = app.loadGraphData(createWorkflowGraphData(), false) + await expect( + app.loadGraphData(createWorkflowGraphData(), false) + ).resolves.toBe(false) + releaseOlderLoad() + + await expect(olderLoad).resolves.toBe(true) + }) + + it('stops post-configure work when a newer graph commits', async () => { + app.canvasElRef.value = document.createElement('canvas') + Reflect.set(app, 'rootGraphInternal', new LGraph()) + let releaseOlderLoad!: () => void + let olderLoadReachedHook!: () => void + const olderLoadBlocked = new Promise((resolve) => { + releaseOlderLoad = resolve + }) + const hookReached = new Promise((resolve) => { + olderLoadReachedHook = resolve + }) + let firstAfterConfigure = true + mockExtensionService.invokeExtensionsAsync.mockImplementation( + async (hook) => { + if (hook === 'afterConfigureGraph' && firstAfterConfigure) { + firstAfterConfigure = false + olderLoadReachedHook() + await olderLoadBlocked + } + } + ) + + const olderLoad = app.loadGraphData(createWorkflowGraphData(), false) + await hookReached + await app.loadGraphData(createWorkflowGraphData(), false) + releaseOlderLoad() + + await expect(olderLoad).resolves.toBeUndefined() + expect(mockWorkflowService.afterLoadNewGraph).toHaveBeenCalledOnce() + }) + + describe('pending camera restore while the canvas is hidden', () => { + let canvasElement: HTMLCanvasElement + + beforeEach(() => { + canvasElement = createTestCanvasElement({ visible: false }) + document.body.append(canvasElement) + app.canvasElRef.value = canvasElement + Reflect.set(app, 'rootGraphInternal', new LGraph()) + Reflect.set(mockCanvas, 'ds', createTestDragAndScale()) + Reflect.set(mockCanvas, 'bgcanvas', createTestCanvasElement()) + useCanvasScheduler().cancel('graph-load-camera') + }) + + function revealCanvas() { + setCanvasVisible(canvasElement, true) + useCanvasScheduler().flush() + } + + it('restores a valid saved camera after the canvas becomes visible', async () => { + const ds = createTestDragAndScale() + Reflect.set(mockCanvas, 'ds', ds) + Reflect.set(mockCanvas, 'viewport', [0, 0, 800, 600]) + Reflect.set(mockCanvas, 'visible_area', ds.visible_area) + mockSettingStore.get.mockImplementation((key: string) => + key === 'Comfy.EnableWorkflowViewRestore' ? true : undefined + ) + const graphData = createWorkflowGraphData() + graphData.extra = { ds: { offset: [-120, 42], scale: 0.5 } } + + await app.loadGraphData(graphData, true, true) + app.rootGraph._nodes.push( + createMockNode({ + pos: [0, 0], + getBounding: () => + new Float64Array([-1_000_000, -1_000_000, 2_000_000, 2_000_000]) + }) + ) + expect(ds.offset).not.toEqual([-120, 42]) + + revealCanvas() + + expect(ds.offset).toEqual([-120, 42]) + expect(ds.scale).toBe(0.5) + expect(mockCanvas.draw).toHaveBeenCalledWith(true, true) + }) + + it('survives a same-workflow undo', async () => { + const workflow = new ComfyWorkflow({ + path: 'workflows/camera.json', + modified: 0, + size: 0 + }) + + await app.loadGraphData(createWorkflowGraphData(), true, true, workflow) + await app.loadGraphData( + createWorkflowGraphData(), + false, + false, + workflow + ) + revealCanvas() + + expect(mockCanvas.draw).toHaveBeenCalledWith(true, true) + }) + + it('matches the workflow name to its workflow object on undo', async () => { + const workflow = new ComfyWorkflow({ + path: 'workflows/camera.json', + modified: 0, + size: 0 + }) + + await app.loadGraphData(createWorkflowGraphData(), true, true, 'camera') + await app.loadGraphData( + createWorkflowGraphData(), + false, + false, + workflow + ) + revealCanvas() + + expect(mockCanvas.draw).toHaveBeenCalledWith(true, true) + }) + + it('is dropped when a different anonymous graph loads', async () => { + await app.loadGraphData(createWorkflowGraphData(), true, true) + await app.loadGraphData(createWorkflowGraphData(), true, false) + revealCanvas() + + expect(mockCanvas.draw).not.toHaveBeenCalled() + }) + }) + it('brackets an API JSON import with graph-load hooks', async () => { app.canvasElRef.value = document.createElement('canvas') const graph = new LGraph() diff --git a/src/scripts/app.ts b/src/scripts/app.ts index ffc424891ea..cc29918bba7 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -6,11 +6,17 @@ import { shallowRef } from 'vue' import { partnerRunGateBlocksAutoQueue } from '@/composables/billing/usePartnerNodesRunGate' import { useCanvasPositionConversion } from '@/composables/element/useCanvasPositionConversion' +import { normalizeCameraState } from '@/renderer/core/canvas/cameraState' +import { + applyViewport, + measureViewportFromElement +} from '@/renderer/core/canvas/canvasViewport' +import { useCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler' import { promotedInputSource } from '@/core/graph/subgraph/promotedInputWidget' import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConcretePromotedWidget' import { setBackendNodeText, st, t } from '@/i18n' -import { normalizeI18nKey } from '@/utils/formatUtil' +import { appendJsonExt, normalizeI18nKey } from '@/utils/formatUtil' import { ChangeTracker } from '@/scripts/changeTracker' import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' import { createMutationView } from '@/lib/litegraph/src/infrastructure/createMutationView' @@ -27,6 +33,7 @@ import type { IBaseWidget, TWidgetValue } from '@/lib/litegraph/src/types/widgets' +import type { ISerialisedGraph } from '@/lib/litegraph/src/types/serialisation' import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums' import { useFreeTierQuota } from '@/platform/cloud/subscription/composables/useFreeTierQuota' import { isCloud } from '@/platform/distribution/types' @@ -374,6 +381,11 @@ export class ComfyApp { } private configuringGraphLevel: number = 0 + private graphLoadSequence = 0 + private committedGraphLoadSequence = 0 + private pendingCamera: + | { id: number; workflow: string | null | ComfyWorkflow } + | undefined get configuringGraph() { return this.configuringGraphLevel > 0 } @@ -1103,16 +1115,10 @@ export class ComfyApp { } private resizeCanvas(canvas: HTMLCanvasElement) { - // Limit minimal scale to 1, see https://github.com/comfyanonymous/ComfyUI/pull/845 - const scale = Math.max(window.devicePixelRatio, 1) - - // Clear fixed width and height while calculating rect so it uses 100% instead - canvas.height = canvas.width = NaN - const { width, height } = canvas.getBoundingClientRect() - canvas.width = Math.round(width * scale) - canvas.height = Math.round(height * scale) - canvas.getContext('2d')?.scale(scale, scale) - this.canvas?.draw(true, true) + const viewport = measureViewportFromElement(canvas) + applyViewport(viewport, canvas, this.canvas.bgcanvas, this.canvas) + useCanvasScheduler().flush() + this.canvas.draw(true, true) } private updateVueAppNodeDefs(defs: Record) { @@ -1302,7 +1308,18 @@ export class ComfyApp { silentAssetErrors?: boolean workflowNavigationId?: number } = {} - ): Promise { + ): Promise { + const canvasScheduler = useCanvasScheduler() + const loadId = ++this.graphLoadSequence + const abortIfSuperseded = async () => { + if (loadId >= this.committedGraphLoadSequence) return false + await useExtensionService().invokeExtensionsAsync( + 'onGraphLoadError', + new DOMException('Graph load superseded by a newer load', 'AbortError') + ) + return true + } + const { checkForRerouteMigration = false, openSource, @@ -1315,6 +1332,8 @@ export class ComfyApp { useWorkflowService().beforeLoadNewGraph(clean !== false) await useExtensionService().invokeExtensionsAsync('beforeLoadGraph') + if (await abortIfSuperseded()) return undefined + let reset_invalid_values = false const missingNodeTypes: MissingNodeType[] = [] try { @@ -1358,6 +1377,7 @@ export class ComfyApp { if (useSettingStore().get('Comfy.Validation.Workflows')) { const { graphData: validatedGraphData } = await useWorkflowValidation().validateWorkflow(graphData) + if (await abortIfSuperseded()) return undefined // If the validation failed, use the original graph data. // Ideally we should not block users from loading the workflow. @@ -1385,9 +1405,11 @@ export class ComfyApp { graphData, missingNodeTypes ) + if (await abortIfSuperseded()) return undefined const nodeReplacementStore = useNodeReplacementStore() await nodeReplacementStore.load() + if (await abortIfSuperseded()) return undefined // Collect missing node types from all nodes (root + subgraphs) const collectMissingNodes = ( @@ -1462,8 +1484,11 @@ export class ComfyApp { return false } - const canvasVisible = !!(this.canvasEl.width && this.canvasEl.height) const fitView = () => { + const savedCameraState = normalizeCameraState(graphData.extra?.ds, { + minScale: this.canvas.ds.min_scale, + maxScale: this.canvas.ds.max_scale + }) if ( restore_view && useSettingStore().get('Comfy.EnableWorkflowViewRestore') @@ -1471,9 +1496,9 @@ export class ComfyApp { // Always fit view for templates to ensure they're visible on load if (openSource === 'template') { useLitegraphService().fitView() - } else if (graphData.extra?.ds) { - this.canvas.ds.offset = graphData.extra.ds.offset - this.canvas.ds.scale = graphData.extra.ds.scale + } else if (savedCameraState) { + this.canvas.ds.offset = savedCameraState.offset + this.canvas.ds.scale = savedCameraState.scale // Fit view if no nodes visible in restored viewport this.canvas.ds.computeVisibleArea(this.canvas.viewport) @@ -1485,7 +1510,7 @@ export class ComfyApp { this.canvas.visible_area ) ) { - requestAnimationFrame(() => useLitegraphService().fitView()) + useLitegraphService().fitView() } } else { useLitegraphService().fitView() @@ -1497,8 +1522,7 @@ export class ComfyApp { let activatedWorkflow: LoadedComfyWorkflow | undefined try { try { - // @ts-expect-error Discrepancies between zod and litegraph - in progress - this.rootGraph.configure(graphData) + this.rootGraph.configure(graphData as ISerialisedGraph) // Save original renderer version before scaling (it gets modified during scaling) const originalMainGraphRenderer = @@ -1516,7 +1540,50 @@ export class ComfyApp { ) } - if (canvasVisible) fitView() + this.committedGraphLoadSequence = Math.max( + loadId, + this.committedGraphLoadSequence + ) + const workflowPath = + typeof workflow === 'string' + ? ComfyWorkflow.basePath + appendJsonExt(workflow) + : workflow?.path + const pendingWorkflow = this.pendingCamera?.workflow + const pendingWorkflowPath = + typeof pendingWorkflow === 'string' + ? ComfyWorkflow.basePath + appendJsonExt(pendingWorkflow) + : pendingWorkflow?.path + const preservesPendingCamera = + !restore_view && + workflowPath !== undefined && + workflowPath === pendingWorkflowPath + if (!preservesPendingCamera) { + canvasScheduler.cancel('graph-load-camera') + this.pendingCamera = restore_view + ? { id: loadId, workflow } + : undefined + if (restore_view) { + canvasScheduler.schedule({ + key: 'graph-load-camera', + element: this.canvasEl, + isCurrent: () => this.pendingCamera?.id === loadId, + run: () => { + const viewport = measureViewportFromElement(this.canvasEl) + applyViewport( + viewport, + this.canvasEl, + this.canvas.bgcanvas, + this.canvas + ) + fitView() + this.canvas.draw(true, true) + if (this.pendingCamera?.id === loadId) { + this.pendingCamera = undefined + } + } + }) + } + } } catch (error) { await this.reportGraphLoadFailure(error) // Resolves rather than throws: the close/replacement guards read this outcome. @@ -1584,6 +1651,7 @@ export class ComfyApp { 'afterConfigureGraph', missingNodeTypes ) + if (await abortIfSuperseded()) return undefined const effectiveShareId = shareId ?? @@ -1604,18 +1672,13 @@ export class ComfyApp { this.rootGraph.serialize() as unknown as ComfyWorkflowJSON, effectiveShareId ) + if (await abortIfSuperseded()) return undefined await useExtensionService().invokeExtensionsAsync('afterLoadGraph') + if (await abortIfSuperseded()) return undefined // Capture the workflow this load activated before the asset-scan awaits // below can hand control back and let the user switch to another one. activatedWorkflow = useWorkflowStore().activeWorkflow ?? undefined - // If the canvas was not visible and we're a fresh load, resize the canvas and fit the view - // This fixes switching from app mode to a new graph mode workflow (e.g. load template) - if (!canvasVisible && (!workflow || typeof workflow === 'string')) { - this.canvas.resize() - requestAnimationFrame(() => fitView()) - } - // Drop missing-node entries whose enclosing subgraph is // muted/bypassed. The initial JSON scan only checks each node's // own mode; the cascade from an inactive container is applied here @@ -1635,11 +1698,13 @@ export class ComfyApp { missingNodeTypes: activeMissingNodeTypes, silent: silentAssetErrors }) + if (await abortIfSuperseded()) return undefined await runMissingMediaPipeline({ rootGraph: this.rootGraph, silent: silentAssetErrors }) + if (await abortIfSuperseded()) return undefined } if (!deferWarnings) { diff --git a/src/scripts/changeTracker.test.ts b/src/scripts/changeTracker.test.ts index 150054e5ff4..6b635611521 100644 --- a/src/scripts/changeTracker.test.ts +++ b/src/scripts/changeTracker.test.ts @@ -49,7 +49,7 @@ vi.mock('@/scripts/app', () => ({ last_link_id: 0 })) }, - loadGraphData: vi.fn(() => Promise.resolve()), + loadGraphData: vi.fn(() => Promise.resolve(true)), canvas: { ds: { scale: 1, offset: [0, 0] } }, @@ -1205,6 +1205,20 @@ describe('ChangeTracker', () => { }) describe('undo and redo', () => { + it('preserves history when an undo load is superseded', async () => { + const initial = createState(1) + const changed = createState(2) + const tracker = createTracker(changed) + tracker.undoQueue.push(initial) + vi.mocked(app.loadGraphData).mockResolvedValueOnce(undefined) + + await tracker.undo() + + expect(tracker.activeState).toEqual(changed) + expect(tracker.undoQueue).toEqual([initial]) + expect(tracker.redoQueue).toEqual([]) + }) + it('dispatches autoQueueGraphChanged for a data change in both directions', async () => { const initial = createState(1) const changed = structuredClone(initial) diff --git a/src/scripts/changeTracker.ts b/src/scripts/changeTracker.ts index 538bffa4406..2f7e6399331 100644 --- a/src/scripts/changeTracker.ts +++ b/src/scripts/changeTracker.ts @@ -464,13 +464,23 @@ export class ChangeTracker { const prevState = source.pop() if (prevState) { const previousState = this.activeState - target.push(previousState) this._restoringState = true try { - await app.loadGraphData(prevState, false, false, this.workflow, { - checkForRerouteMigration: false, - silentAssetErrors: true - }) + const result = await app.loadGraphData( + prevState, + false, + false, + this.workflow, + { + checkForRerouteMigration: false, + silentAssetErrors: true + } + ) + if (result === undefined) { + source.push(prevState) + return + } + target.push(previousState) this.activeState = prevState this.updateModified(previousState) } finally { diff --git a/src/services/litegraphService.test.ts b/src/services/litegraphService.test.ts index 93827d8f79f..e9a6350dcfc 100644 --- a/src/services/litegraphService.test.ts +++ b/src/services/litegraphService.test.ts @@ -29,16 +29,25 @@ describe('useLitegraphService().getCanvasCenter', () => { expect(center).toEqual([0, 0]) }) - it('returns the visible-area centre once the canvas is ready', () => { - Reflect.set(app, 'isGraphReady', true) - Reflect.set(app, 'canvas', { - ds: { visible_area: [10, 20, 200, 100] } - }) - - const center = useLitegraphService().getCanvasCenter() - - expect(center).toEqual([110, 70]) - }) + it.for<{ + dpr: number + visibleArea: [number, number, number, number] + center: [number, number] + }>([ + { dpr: 1, visibleArea: [10, 20, 200, 100], center: [110, 70] }, + { dpr: 2, visibleArea: [0, 0, 800, 600], center: [400, 300] } + ])( + 'returns the CSS-pixel visible-area centre at DPR $dpr', + ({ dpr, visibleArea, center }) => { + Reflect.set(app, 'isGraphReady', true) + Reflect.set(app, 'canvas', { + dpr, + ds: { visible_area: visibleArea } + }) + + expect(useLitegraphService().getCanvasCenter()).toEqual(center) + } + ) }) describe('useLitegraphService().registerNodeDef slot text', () => { diff --git a/src/services/litegraphService.ts b/src/services/litegraphService.ts index 1c204ae3f69..02dfc3e0a56 100644 --- a/src/services/litegraphService.ts +++ b/src/services/litegraphService.ts @@ -1,6 +1,7 @@ import { pick, zip } from 'es-toolkit/compat' import { downloadFile, openFileInNewTab } from '@/base/common/downloadUtil' +import { visibleCanvasViewport } from '@/composables/canvas/visibleCanvasViewport' import { useSelectedLiteGraphItems } from '@/composables/canvas/useSelectedLiteGraphItems' import { useSubgraphOperations } from '@/composables/graph/useSubgraphOperations' import { useNodeAnimatedImage } from '@/composables/node/useNodeAnimatedImage' @@ -957,11 +958,10 @@ export const useLitegraphService = () => { } function getCanvasCenter(): Point { - const dpi = Math.max(window.devicePixelRatio || 1, 1) if (!app.isGraphReady) return [0, 0] const visibleArea = app.canvas.ds.visible_area const [x, y, w, h] = visibleArea - return [x + w / dpi / 2, y + h / dpi / 2] + return [x + w / 2, y + h / 2] } function goToNode(nodeId: SerializedNodeId) { @@ -999,7 +999,9 @@ export const useLitegraphService = () => { const bounds = createBounds(nodes) if (!bounds) return - canvas.ds.fitToBounds(bounds) + canvas.ds.fitToBounds(bounds, { + viewport: visibleCanvasViewport(canvas) + }) canvas.setDirty(true, true) } diff --git a/src/stores/agentNodeSelectionStore.test.ts b/src/stores/agentNodeSelectionStore.test.ts index 0c240ae6770..5a1bae34bde 100644 --- a/src/stores/agentNodeSelectionStore.test.ts +++ b/src/stores/agentNodeSelectionStore.test.ts @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { nextTick } from 'vue' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { createTestDragAndScale } from '@/utils/__tests__/canvasTestUtils' import { useAgentNodeSelectionStore } from '@/stores/agentNodeSelectionStore' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' @@ -56,7 +57,11 @@ function stubCanvas(nodes: unknown[], selected: unknown[] = []) { selectedItems, deselectAll, animateToBounds, - canvas: element + canvas: element, + // Framing reads the applied CSS viewport off `ds`, so the stub needs a + // `DragAndScale` with the same 1600x900 viewport the element reports. + dpr: 1, + ds: createTestDragAndScale(1600, 900) } as never return { animateToBounds, deselectAll, selectedItems } } diff --git a/src/stores/queueStore.loadWorkflow.test.ts b/src/stores/queueStore.loadWorkflow.test.ts index 62c2a92257b..d73022f03c8 100644 --- a/src/stores/queueStore.loadWorkflow.test.ts +++ b/src/stores/queueStore.loadWorkflow.test.ts @@ -8,6 +8,7 @@ import type { import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' import type { ComfyApp } from '@/scripts/app' import * as jobOutputCache from '@/services/jobOutputCache' +import { useNodeOutputStore } from '@/stores/nodeOutputStore' import { TaskItemImpl } from '@/stores/queueStore' vi.mock('@/services/extensionService', () => ({ @@ -101,6 +102,23 @@ describe('TaskItemImpl.loadWorkflow - workflow fetching', () => { expect(mockApp.loadGraphData).toHaveBeenCalledWith(mockWorkflow) }) + it('does not install outputs when the workflow load is superseded', async () => { + const job = createHistoryJob('test-job-id') + const task = new TaskItemImpl(job) + vi.spyOn(jobOutputCache, 'getJobDetail').mockResolvedValue( + mockJobDetail as JobDetail + ) + vi.mocked(mockApp.loadGraphData).mockResolvedValue(undefined) + const setNodeOutputs = vi.spyOn( + useNodeOutputStore(), + 'setNodeOutputsByExecutionId' + ) + + await task.loadWorkflow(mockApp) + + expect(setNodeOutputs).not.toHaveBeenCalled() + }) + it('should not load workflow when fetch returns undefined', async () => { const job = createHistoryJob('test-job-id') const task = new TaskItemImpl(job) diff --git a/src/stores/queueStore.ts b/src/stores/queueStore.ts index 1b2402849b4..76610fd8a10 100644 --- a/src/stores/queueStore.ts +++ b/src/stores/queueStore.ts @@ -440,7 +440,8 @@ export class TaskItemImpl { return } - await app.loadGraphData(toRaw(workflowData)) + const loadResult = await app.loadGraphData(toRaw(workflowData)) + if (loadResult === undefined) return // Use full outputs from job detail, or fall back to existing outputs const outputsToLoad = jobDetail?.outputs ?? this.outputs diff --git a/src/stores/subgraphNavigationStore.ts b/src/stores/subgraphNavigationStore.ts index 1ed8b4edcb1..21b6d0bf2ae 100644 --- a/src/stores/subgraphNavigationStore.ts +++ b/src/stores/subgraphNavigationStore.ts @@ -14,6 +14,7 @@ import { useWorkflowStore } from '@/platform/workflow/management/stores/workflow import { reportError } from '@/platform/telemetry/reportError' import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { useCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler' import { isUuidShapedSubgraphId } from '@/schemas/subgraphIdSchema' import { app } from '@/scripts/app' import { useLitegraphService } from '@/services/litegraphService' @@ -46,6 +47,7 @@ export const useSubgraphNavigationStore = defineStore( () => { const workflowStore = useWorkflowStore() const canvasStore = useCanvasStore() + const canvasScheduler = useCanvasScheduler() const router = useRouter() const routeHash = useRouteHash() @@ -75,6 +77,7 @@ export const useSubgraphNavigationStore = defineStore( * flag resets even when the tab is backgrounded. */ let isWorkflowSwitching = false + let viewportRestoreSequence = 0 // ── Helpers ────────────────────────────────────────────────────── /** Build a workflow-scoped cache key. */ @@ -150,6 +153,9 @@ export const useSubgraphNavigationStore = defineStore( } function restoreViewport(graphId: string): void { + const restoreSequence = ++viewportRestoreSequence + canvasScheduler.cancel('subgraph-navigation-fit') + const canvas = currentCanvas() if (!canvas) return @@ -160,11 +166,27 @@ export const useSubgraphNavigationStore = defineStore( return } - // First visit — fit to content so subgraph nodes are visible - requestAnimationFrame(() => { - if (getActiveGraphId() !== graphId) return - if (!canvas.graph?.nodes.length) return - useLitegraphService().fitView() + // First visit — fit to content so subgraph nodes are visible. + // The scheduler guarantees the canvas is laid out before `run` fires; + // the frame hop then lets this release's node layout settle before the + // fit measures it, which a synchronous fit would read too early. + canvasScheduler.schedule({ + key: 'subgraph-navigation-fit', + isCurrent: () => + restoreSequence === viewportRestoreSequence && + getActiveGraphId() === graphId, + run: () => { + requestAnimationFrame(() => { + if (restoreSequence !== viewportRestoreSequence) return + if (getActiveGraphId() !== graphId) return + if (!canvas.graph?.nodes.length) return + canvas.ds.setViewportSize( + canvas.canvas.offsetWidth, + canvas.canvas.offsetHeight + ) + useLitegraphService().fitView() + }) + } }) } diff --git a/src/stores/subgraphNavigationStore.viewport.test.ts b/src/stores/subgraphNavigationStore.viewport.test.ts index a23b97b9d8a..cd875f00b79 100644 --- a/src/stores/subgraphNavigationStore.viewport.test.ts +++ b/src/stores/subgraphNavigationStore.viewport.test.ts @@ -2,27 +2,37 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { nextTick } from 'vue' import type { LGraph, Subgraph } from '@/lib/litegraph/src/litegraph' +import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { useLitegraphService } from '@/services/litegraphService' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore' +import { useCanvasScheduler } from '@/renderer/core/canvas/useCanvasScheduler' import { app } from '@/scripts/app' import { useSubgraphNavigationStore, VIEWPORT_CACHE_MAX_SIZE } from '@/stores/subgraphNavigationStore' +import { + createTestCanvasElement, + setCanvasVisible +} from '@/utils/__tests__/canvasTestUtils' -const { mockSetDirty, mockFitView } = vi.hoisted(() => ({ +const { mockSetDirty, mockSetViewportSize, mockFitView } = vi.hoisted(() => ({ mockSetDirty: vi.fn(), + mockSetViewportSize: vi.fn(), mockFitView: vi.fn() })) vi.mock('@/scripts/app', () => { const mockCanvas = { + canvas: undefined as unknown, subgraph: undefined as unknown, graph: undefined as unknown, ds: { scale: 1, offset: [0, 0], state: { scale: 1, offset: [0, 0] }, + setViewportSize: mockSetViewportSize, fitToBounds: vi.fn(), visible_area: [0, 0, 1000, 1000], computeVisibleArea: vi.fn() @@ -53,11 +63,6 @@ vi.mock('@/scripts/app', () => { } }) -vi.mock('@/renderer/core/canvas/canvasStore', () => ({ - useCanvasStore: () => ({ - getCanvas: () => app.canvas - }) -})) vi.mock('@vueuse/router', () => ({ useRouteHash: vi.fn() })) vi.mock('@/services/litegraphService', () => ({ @@ -66,7 +71,18 @@ vi.mock('@/services/litegraphService', () => ({ const mockCanvas = app.canvas +/** + * The scheduled first-visit fit hops one frame before it measures, so this + * release's node layout has settled by the time `fitView` reads it. Capturing + * the callbacks rather than running them inline keeps that hop visible to the + * tests instead of hiding it behind a synchronous stub. + */ let rafCallbacks: FrameRequestCallback[] = [] +function flushAnimationFrames(): void { + const pending = rafCallbacks + rafCallbacks = [] + for (const callback of pending) callback(0) +} describe('useSubgraphNavigationStore - Viewport Persistence', () => { beforeEach(() => { @@ -75,6 +91,10 @@ describe('useSubgraphNavigationStore - Viewport Persistence', () => { rafCallbacks.push(cb) return rafCallbacks.length }) + useCanvasStore().canvas = app.canvas + vi.mocked(useCanvasStore().getCanvas).mockImplementation(() => app.canvas) + mockCanvas.canvas = createTestCanvasElement({ visible: true }) + document.body.append(mockCanvas.canvas) mockCanvas.subgraph = undefined mockCanvas.graph = app.graph mockCanvas.ds.scale = 1 @@ -155,20 +175,38 @@ describe('useSubgraphNavigationStore - Viewport Persistence', () => { expect(mockSetDirty).toHaveBeenCalledWith(true, true) }) - it('does not mutate canvas synchronously on cache miss', () => { + it('queues a cache-miss fit while the canvas is hidden', () => { const store = useSubgraphNavigationStore() + setCanvasVisible(mockCanvas.canvas, false) mockCanvas.ds.scale = 1 mockCanvas.ds.offset = [0, 0] mockSetDirty.mockClear() store.restoreViewport('non-existent') - // Should not change canvas synchronously expect(mockCanvas.ds.scale).toBe(1) expect(mockCanvas.ds.offset).toEqual([0, 0]) - expect(mockSetDirty).not.toHaveBeenCalled() - // But should have scheduled a rAF - expect(rafCallbacks).toHaveLength(1) + expect(mockCanvas.setDirty).not.toHaveBeenCalled() + expect(useLitegraphService().fitView).not.toHaveBeenCalled() + }) + + it('refreshes the viewport before fitting after the canvas becomes visible', () => { + const store = useSubgraphNavigationStore() + const mockGraph = app.graph as { nodes: unknown[]; _nodes: unknown[] } + mockGraph.nodes = [{ pos: [0, 0], size: [100, 100] }] + mockGraph._nodes = mockGraph.nodes + setCanvasVisible(mockCanvas.canvas, false) + + store.restoreViewport('root') + setCanvasVisible(mockCanvas.canvas, true) + useCanvasScheduler().flush() + flushAnimationFrames() + + expect(mockSetViewportSize).toHaveBeenCalledWith(800, 600) + expect(mockSetViewportSize).toHaveBeenCalledBefore(mockFitView) + + mockGraph.nodes = [] + mockGraph._nodes = [] }) it('calls fitView on cache miss when graph has nodes', () => { @@ -180,13 +218,9 @@ describe('useSubgraphNavigationStore - Viewport Persistence', () => { mockGraph._nodes = mockGraph.nodes store.restoreViewport('root') + flushAnimationFrames() - expect(mockFitView).not.toHaveBeenCalled() - expect(rafCallbacks).toHaveLength(1) - - rafCallbacks[0](performance.now()) - - expect(mockFitView).toHaveBeenCalledOnce() + expect(useLitegraphService().fitView).toHaveBeenCalledOnce() mockGraph.nodes = [] mockGraph._nodes = [] @@ -202,13 +236,30 @@ describe('useSubgraphNavigationStore - Viewport Persistence', () => { store.restoreViewport('root') - expect(rafCallbacks).toHaveLength(1) - rafCallbacks[0](performance.now()) + expect(useLitegraphService().fitView).not.toHaveBeenCalled() + }) - expect(mockFitView).not.toHaveBeenCalled() + it('fits the first visit on the next frame when the canvas is ready', () => { + const store = useSubgraphNavigationStore() + store.viewportCache.delete(':root') + + const mockGraph = app.graph as { nodes: unknown[]; _nodes: unknown[] } + mockGraph.nodes = [{ pos: [0, 0], size: [100, 100] }] + mockGraph._nodes = mockGraph.nodes + + store.restoreViewport('root') + + // The scheduler accepted the op immediately — no second visibility wait. + expect(useLitegraphService().fitView).not.toHaveBeenCalled() + flushAnimationFrames() + + expect(useLitegraphService().fitView).toHaveBeenCalledOnce() + + mockGraph.nodes = [] + mockGraph._nodes = [] }) - it('fits the first visit on the next frame', () => { + it('does not let an earlier first-visit frame overwrite a later cache restore', () => { const store = useSubgraphNavigationStore() store.viewportCache.delete(':root') @@ -217,27 +268,34 @@ describe('useSubgraphNavigationStore - Viewport Persistence', () => { mockGraph._nodes = mockGraph.nodes store.restoreViewport('root') - expect(rafCallbacks).toHaveLength(1) - rafCallbacks[0](performance.now()) - expect(mockFitView).toHaveBeenCalledOnce() - expect(rafCallbacks).toHaveLength(1) + mockCanvas.subgraph = { id: 'sub-b' } as never + store.viewportCache.set(':sub-b', { scale: 2, offset: [20, 30] }) + store.restoreViewport('sub-b') + + mockCanvas.subgraph = undefined + store.viewportCache.set(':root', { scale: 3, offset: [40, 50] }) + store.restoreViewport('root') + flushAnimationFrames() + + expect(mockFitView).not.toHaveBeenCalled() + expect(mockCanvas.ds.scale).toBe(3) + expect(mockCanvas.ds.offset).toEqual([40, 50]) mockGraph.nodes = [] mockGraph._nodes = [] }) - it('skips fitView if active graph changed before rAF fires', () => { + it('skips a queued fit if the active graph changes while hidden', () => { const store = useSubgraphNavigationStore() store.viewportCache.delete(':root') + setCanvasVisible(mockCanvas.canvas, false) store.restoreViewport('root') - expect(rafCallbacks).toHaveLength(1) - // Simulate graph switching away before rAF fires mockCanvas.subgraph = { id: 'different-graph' } as never - - rafCallbacks[0](performance.now()) + setCanvasVisible(mockCanvas.canvas, true) + useCanvasScheduler().flush() expect(mockFitView).not.toHaveBeenCalled() }) diff --git a/src/utils/__tests__/canvasTestUtils.ts b/src/utils/__tests__/canvasTestUtils.ts new file mode 100644 index 00000000000..249076dacca --- /dev/null +++ b/src/utils/__tests__/canvasTestUtils.ts @@ -0,0 +1,150 @@ +import { vi } from 'vitest' + +import type { + CanvasPointerEvent, + LGraphCanvas +} from '@/lib/litegraph/src/litegraph' +import { DragAndScale } from '@/lib/litegraph/src/litegraph' + +/** + * Creates a mock LGraphCanvas with minimal required properties for testing + */ +export function createMockCanvas( + overrides: Partial | Record = {} +): LGraphCanvas { + return { + setDirty: vi.fn(), + state: {}, + ...(overrides as Partial) + } as LGraphCanvas +} + +/** + * Creates a mock CanvasPointerEvent + */ +export function createMockCanvasPointerEvent( + canvasX: number, + canvasY: number, + overrides: Partial = {} +): CanvasPointerEvent { + return { + canvasX, + canvasY, + ...overrides + } as CanvasPointerEvent +} + +/** + * Creates a mock CanvasRenderingContext2D + */ +export function createMockCanvasRenderingContext2D( + overrides: Partial = {} +): CanvasRenderingContext2D { + const partial: Partial = { + save: vi.fn(), + restore: vi.fn(), + translate: vi.fn(), + scale: vi.fn(), + fillRect: vi.fn(), + strokeRect: vi.fn(), + fillText: vi.fn(), + measureText: vi.fn(() => ({ width: 10 }) as TextMetrics), + beginPath: vi.fn(), + moveTo: vi.fn(), + lineTo: vi.fn(), + stroke: vi.fn(), + fill: vi.fn(), + closePath: vi.fn(), + arc: vi.fn(), + rect: vi.fn(), + clip: vi.fn(), + clearRect: vi.fn(), + setTransform: vi.fn(), + roundRect: vi.fn(), + getTransform: vi.fn( + () => ({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 }) as DOMMatrix + ), + createPattern: vi.fn(() => null), + font: '', + fillStyle: '', + strokeStyle: '', + lineWidth: 1, + globalAlpha: 1, + textAlign: 'left', + textBaseline: 'alphabetic', + ...overrides + } + return partial as CanvasRenderingContext2D +} + +/** + * Creates a mock MinimapCanvas for minimap testing + */ +export function createMockMinimapCanvas( + overrides: Partial = {} +): HTMLCanvasElement { + const mockGetContext = vi.fn() + mockGetContext.mockImplementation((contextId: string) => + contextId === '2d' ? createMockCanvasRenderingContext2D() : null + ) + + const partial: Partial = { + width: 200, + height: 200, + clientWidth: 200, + clientHeight: 200, + getContext: mockGetContext as HTMLCanvasElement['getContext'], + ...overrides + } + return partial as HTMLCanvasElement +} + +interface TestCanvasElementOptions { + /** Backing-store dimensions. */ + width?: number + height?: number + /** Layout dimensions reported by `getBoundingClientRect`. */ + cssSize?: [width: number, height: number] + /** Whether layout reports the canvas as rendered; see {@link setCanvasVisible}. */ + visible?: boolean + ctx?: CanvasRenderingContext2D +} + +export function createTestCanvasElement({ + width = 800, + height = 600, + cssSize, + visible, + ctx = createMockCanvasRenderingContext2D() +}: TestCanvasElementOptions = {}): HTMLCanvasElement { + const element = document.createElement('canvas') + element.width = width + element.height = height + element.getContext = vi.fn().mockReturnValue(ctx) + if (cssSize) { + vi.spyOn(element, 'getBoundingClientRect').mockReturnValue( + new DOMRect(0, 0, ...cssSize) + ) + } + if (visible !== undefined) setCanvasVisible(element, visible) + return element +} + +/** happy-dom has no layout, so report the offset geometry a rendered or hidden canvas has. */ +export function setCanvasVisible(element: HTMLElement, visible: boolean): void { + Object.defineProperties(element, { + offsetParent: { configurable: true, value: visible ? document.body : null }, + offsetWidth: { configurable: true, value: visible ? 800 : 0 }, + offsetHeight: { configurable: true, value: visible ? 600 : 0 } + }) +} + +/** A `DragAndScale` with an applied CSS viewport, as `applyViewport` leaves it. */ +export function createTestDragAndScale( + width = 800, + height = 600 +): DragAndScale { + const ds = new DragAndScale(createTestCanvasElement({ width, height })) + ds.setViewportSize(width, height) + return ds +} diff --git a/src/utils/__tests__/litegraphTestUtils.ts b/src/utils/__tests__/litegraphTestUtils.ts index 717e04c2b18..d296ee01cd9 100644 --- a/src/utils/__tests__/litegraphTestUtils.ts +++ b/src/utils/__tests__/litegraphTestUtils.ts @@ -7,7 +7,6 @@ import { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEven import type { LGraphEventMap } from '@/lib/litegraph/src/infrastructure/LGraphEventMap' import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' import type { - CanvasPointerEvent, ISerialisedGraph, LGraph, LGraphCanvas, @@ -146,21 +145,6 @@ export function createMockLGraph(overrides: Partial = {}): LGraph { return Object.assign(graph, { rootGraph: overrides.rootGraph ?? graph }) } -/** - * Creates a mock CanvasPointerEvent - */ -export function createMockCanvasPointerEvent( - canvasX: number, - canvasY: number, - overrides: Partial = {} -): CanvasPointerEvent { - return { - canvasX, - canvasY, - ...overrides - } as CanvasPointerEvent -} - /** * Creates a mock CanvasRenderingContext2D */ @@ -340,28 +324,6 @@ export function createMockLoadedWorkflow( }) } -/** - * Creates a mock MinimapCanvas for minimap testing - */ -export function createMockMinimapCanvas( - overrides: Partial = {} -): HTMLCanvasElement { - const mockGetContext = vi.fn() - mockGetContext.mockImplementation((contextId: string) => - contextId === '2d' ? createMockCanvas2DContext() : null - ) - - const partial: Partial = { - width: 200, - height: 200, - clientWidth: 200, - clientHeight: 200, - getContext: mockGetContext as HTMLCanvasElement['getContext'], - ...overrides - } - return partial as HTMLCanvasElement -} - /** * Creates a mock CanvasRenderingContext2D for canvas testing */ diff --git a/src/utils/__tests__/stubPath2D.ts b/src/utils/__tests__/stubPath2D.ts new file mode 100644 index 00000000000..9d4a5ca7cdd --- /dev/null +++ b/src/utils/__tests__/stubPath2D.ts @@ -0,0 +1,15 @@ +import { vi } from 'vitest' + +class RecordedPath2D implements Pick< + Path2D, + 'moveTo' | 'lineTo' | 'bezierCurveTo' | 'quadraticCurveTo' | 'arc' +> { + moveTo(): void {} + lineTo(): void {} + bezierCurveTo(): void {} + quadraticCurveTo(): void {} + arc(): void {} +} + +/** Spied stand-in for Path2D, which happy-dom does not implement. */ +export const StubPath2D = vi.mockObject(RecordedPath2D, { spy: true }) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 59f377501b6..1098399f461 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -57,6 +57,7 @@ import { useWorkflowTabActivityStore } from '@/stores/workflowTabActivityStore' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' import { useToastStore } from '@/platform/updates/common/toastStore' import { useAssetsStore } from '@/stores/assetsStore' +import { createTestDragAndScale } from '@/utils/__tests__/canvasTestUtils' import { getFilenameDetails } from '@/utils/formatUtil' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' @@ -1192,7 +1193,10 @@ function setupNodeSelectionCanvas() { multi_select: false, allow_dragnodes: true, selectOnly: false, - canvas: canvasElement + canvas: canvasElement, + // Entering node-selection mode frames the graph, and framing reads the + // applied CSS viewport off `ds` rather than the canvas backing store. + ds: createTestDragAndScale(900, 700) } appMock.canvas = canvas canvasStore.canvas = fromPartial(canvas) diff --git a/src/workbench/extensions/agent/stores/agent/agentPanelStoreId.test.ts b/src/workbench/extensions/agent/stores/agent/agentPanelStoreId.test.ts index b937328177c..40487fccd80 100644 --- a/src/workbench/extensions/agent/stores/agent/agentPanelStoreId.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentPanelStoreId.test.ts @@ -5,7 +5,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { visibleCanvasViewport } from '@/composables/canvas/visibleCanvasViewport' import { useBillingContext } from '@/composables/billing/useBillingContext' -import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph' +import { createTestDragAndScale } from '@/utils/__tests__/canvasTestUtils' import { useAgentDockMount } from '@/workbench/extensions/agent/composables/useAgentDockMount' import { useAgentPanelStore } from './agentPanelStore' @@ -75,7 +75,10 @@ describe('the agentPanel store id', () => { store.isOpen = true expect(docked.value).toBe(true) - const canvas = { canvas: { width: 1600, height: 900 } } as LGraphCanvas + // `visibleCanvasViewport` reads the applied CSS viewport off `ds`, not the + // backing store off `canvas.canvas`, so the stub supplies a `DragAndScale` + // whose viewport has been applied at 1600x900. + const canvas = { ds: createTestDragAndScale(1600, 900) } const viewport = visibleCanvasViewport(canvas) expect(viewport.every((value) => Number.isFinite(value))).toBe(true) diff --git a/vitest.setup.ts b/vitest.setup.ts index 70eaa6bf0c5..cfa96a9c61c 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -6,10 +6,15 @@ import 'vue' import DOMPurify from 'dompurify' import { clearRegisteredLiteGraphTypes } from '@/lib/litegraph/src/litegraphInstance' +import { remoteConfigState } from '@/platform/remoteConfig/remoteConfig' +import { StubPath2D } from '@/utils/__tests__/stubPath2D' beforeEach(() => { vi.stubGlobal('__VUE_DEVTOOLS_GLOBAL_HOOK__', { emit: vi.fn() }) - setActivePinia(createTestingPinia({ stubActions: false })) + vi.stubGlobal('Path2D', StubPath2D) + const pinia = createTestingPinia({ stubActions: false }) + setActivePinia(pinia) + remoteConfigState.value = 'anonymous' }) afterEach(() => {