Skip to content

Commit c81a2b2

Browse files
AustinMrozchristian-byrne
authored andcommitted
Do not delay fit to view on graph restore (#7645)
Fixes a bug where swapping to a different workflow from the inside of a subgraph would cause nodes to be in an incorrect position after swapping back. in vue mode Prior to an unknown-but-recent PR, all nodes would would stack on the origin. This PR instead solves the remaining issue where having `ComfyEnableWorkflowViewRestore` would cause incorrect node positions. This is done by not delaying the fitView by a frame (which causes it to occur after the graph is no longer in the configuring state). In order to accomplish this, the code in LGraphNode has been updated to allow measuring node bounds without requiring a ctx argument. This arg is only used to ensure sufficient width for a node's title and is irrelevant when loading an existing graph. | Before | After | | ------ | ----- | | <img width="360" alt="before" src="https://github.com/user-attachments/assets/7f73817b-36e9-4400-8342-9e660cb36628"/> | <img width="360" alt="after" src="https://github.com/user-attachments/assets/c7ab4b99-2797-4276-9703-58d489cc3eaf" />| See also #7591, which solves similar issues, but does not resolve this bug. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7645-Do-not-delay-fit-to-view-on-graph-restore-2ce6d73d36508153972cc7b5948ce375) by [Unito](https://www.unito.io)
1 parent d2281f6 commit c81a2b2

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/lib/litegraph/src/LGraphNode.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ export class LGraphNode
20002000
* @param out `x, y, width, height` are written to this array.
20012001
* @param ctx The canvas context to use for measuring text.
20022002
*/
2003-
measure(out: Rect, ctx: CanvasRenderingContext2D): void {
2003+
measure(out: Rect, ctx?: CanvasRenderingContext2D): void {
20042004
const titleMode = this.title_mode
20052005
const renderTitle =
20062006
titleMode != TitleMode.TRANSPARENT_TITLE &&
@@ -2013,11 +2013,13 @@ export class LGraphNode
20132013
out[2] = this.size[0]
20142014
out[3] = this.size[1] + titleHeight
20152015
} else {
2016-
ctx.font = this.innerFontStyle
2016+
if (ctx) ctx.font = this.innerFontStyle
20172017
this._collapsed_width = Math.min(
20182018
this.size[0],
2019-
ctx.measureText(this.getTitle() ?? '').width +
2020-
LiteGraph.NODE_TITLE_HEIGHT * 2
2019+
ctx
2020+
? ctx.measureText(this.getTitle() ?? '').width +
2021+
LiteGraph.NODE_TITLE_HEIGHT * 2
2022+
: 0
20212023
)
20222024
out[2] = this._collapsed_width || LiteGraph.NODE_COLLAPSED_WIDTH
20232025
out[3] = LiteGraph.NODE_TITLE_HEIGHT
@@ -2047,7 +2049,7 @@ export class LGraphNode
20472049
* Calculates the render area of this node, populating both {@link boundingRect} and {@link renderArea}.
20482050
* Called automatically at the start of every frame.
20492051
*/
2050-
updateArea(ctx: CanvasRenderingContext2D): void {
2052+
updateArea(ctx?: CanvasRenderingContext2D): void {
20512053
const bounds = this.#boundingRect
20522054
this.measure(bounds, ctx)
20532055
this.onBounding?.(bounds)

src/scripts/app.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,12 +1224,7 @@ export class ComfyApp {
12241224
this.canvas.ds.offset = graphData.extra.ds.offset
12251225
this.canvas.ds.scale = graphData.extra.ds.scale
12261226
} else {
1227-
// @note: Set view after the graph has been rendered once. fitView uses
1228-
// boundingRect on nodes to calculate the view bounds, which only become
1229-
// available after the first render.
1230-
requestAnimationFrame(() => {
1231-
useLitegraphService().fitView()
1232-
})
1227+
useLitegraphService().fitView()
12331228
}
12341229
}
12351230
} catch (error) {

src/services/litegraphService.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,13 @@ export const useLitegraphService = () => {
868868
app.canvas.animateToBounds(graphNode.boundingRect)
869869
}
870870

871+
function ensureBounds(nodes: LGraphNode[]) {
872+
for (const node of nodes) {
873+
if (!node.boundingRect.every((i) => i === 0)) continue
874+
node.updateArea()
875+
}
876+
}
877+
871878
/**
872879
* Resets the canvas view to the default
873880
*/
@@ -881,11 +888,10 @@ export const useLitegraphService = () => {
881888
}
882889

883890
function fitView() {
884-
const canvas = canvasStore.canvas
885-
if (!canvas) return
886-
891+
const canvas = canvasStore.getCanvas()
887892
const nodes = canvas.graph?.nodes
888893
if (!nodes) return
894+
ensureBounds(nodes)
889895
const bounds = createBounds(nodes)
890896
if (!bounds) return
891897

0 commit comments

Comments
 (0)