From bb5605d43a4b98a1db340c4dbeb9a7650798b131 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:37:59 +0000 Subject: [PATCH] progress on progress --- web/extensions/core/groupNode.js | 2 +- web/scripts/app.js | 28 +++-- web/scripts/changeTracker.js | 16 ++- web/scripts/domWidget.js | 2 +- web/scripts/ui/menu/index.js | 4 +- web/scripts/ui/menu/menu.css | 71 +++++++++---- web/scripts/ui/menu/workflows.js | 171 +++++++++++++++++++++++-------- web/scripts/workflows.js | 88 +++++++++++++++- web/style.css | 2 + 9 files changed, 302 insertions(+), 82 deletions(-) diff --git a/web/extensions/core/groupNode.js b/web/extensions/core/groupNode.js index 5d11ea6c5..a4bb2e163 100644 --- a/web/extensions/core/groupNode.js +++ b/web/extensions/core/groupNode.js @@ -1172,7 +1172,7 @@ export class GroupNodeHandler { } static isGroupNode(node) { - return !!node.constructor?.nodeData?.[GROUP]; + return !!node?.constructor?.nodeData?.[GROUP]; } static async fromNodes(nodes) { diff --git a/web/scripts/app.js b/web/scripts/app.js index 7b22b08ee..de59e4e4f 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -1247,11 +1247,15 @@ export class ComfyApp { }); api.addEventListener("progress", ({ detail }) => { + if (this.workflowManager.activePrompt?.workflow + && this.workflowManager.activePrompt.workflow !== this.workflowManager.activeWorkflow) return console.log(this.workflowManager.activePrompt?.workflow, this.workflowManager.activeWorkflow); this.progress = detail; this.graph.setDirtyCanvas(true, false); }); api.addEventListener("executing", ({ detail }) => { + if (this.workflowManager.activePrompt ?.workflow + && this.workflowManager.activePrompt.workflow !== this.workflowManager.activeWorkflow) return console.log(this.workflowManager.activePrompt?.workflow, this.workflowManager.activeWorkflow); this.progress = null; this.runningNodeId = detail; this.graph.setDirtyCanvas(true, false); @@ -1259,6 +1263,8 @@ export class ComfyApp { }); api.addEventListener("executed", ({ detail }) => { + if (this.workflowManager.activePrompt ?.workflow + && this.workflowManager.activePrompt.workflow !== this.workflowManager.activeWorkflow) return console.log(this.workflowManager.activePrompt?.workflow, this.workflowManager.activeWorkflow); const output = this.nodeOutputs[detail.node]; if (detail.merge && output) { for (const k in detail.output ?? {}) { @@ -1462,7 +1468,7 @@ export class ComfyApp { mainCanvas.style.touchAction = "none" const canvasEl = (this.canvasEl = Object.assign(mainCanvas, { id: "graph-canvas" })); canvasEl.tabIndex = "1"; - document.body.prepend(canvasEl); + document.body.append(canvasEl); addDomClippingSetting(); this.#addProcessMouseHandler(); @@ -1736,6 +1742,11 @@ export class ComfyApp { * @param { import("./workflows.js").ComfyWorkflowInstance | null } name The workflow */ async loadGraphData(graphData, clean = true, workflow = null) { + try { + this.workflowManager.activeWorkflow?.changeTracker?.store() + } catch (error) { + } + if (clean !== false) { this.clean(); } @@ -1753,12 +1764,7 @@ export class ComfyApp { { graphData = structuredClone(graphData); } - - try { - this.workflowManager.activeWorkflow?.changeTracker?.storeViewport() - } catch (error) { - } - + try { this.workflowManager.setWorkflow(workflow); } catch (error) { @@ -2071,6 +2077,14 @@ export class ComfyApp { this.lastNodeErrors = res.node_errors; if (this.lastNodeErrors.length > 0) { this.canvas.draw(true, true); + } else { + try { + this.workflowManager.storePrompt({ + id: res.prompt_id, + nodes: Object.keys(p.output) + }); + } catch (error) { + } } } catch (error) { const formattedError = this.#formatPromptError(error) diff --git a/web/scripts/changeTracker.js b/web/scripts/changeTracker.js index 87925eb21..fcfb1a80c 100644 --- a/web/scripts/changeTracker.js +++ b/web/scripts/changeTracker.js @@ -14,6 +14,7 @@ export class ChangeTracker { workflow; ds; + nodeOutputs; get app() { return this.workflow.manager.app; @@ -23,14 +24,19 @@ export class ChangeTracker { this.workflow = workflow; } - storeViewport() { + store() { + this.nodeOutputs = clone(this.app.nodeOutputs); this.ds = { scale: this.app.canvas.ds.scale, offset: [...this.app.canvas.ds.offset] }; } - restoreViewport() { - if (!this.ds) return; - this.app.canvas.ds.scale = this.ds.scale; - this.app.canvas.ds.offset = this.ds.offset; + restore() { + if (this.ds) { + this.app.canvas.ds.scale = this.ds.scale; + this.app.canvas.ds.offset = this.ds.offset; + } + if (this.nodeOutputs) { + this.app.nodeOutputs = this.nodeOutputs; + } } checkState() { diff --git a/web/scripts/domWidget.js b/web/scripts/domWidget.js index d5eeebdbd..ef6cf2b31 100644 --- a/web/scripts/domWidget.js +++ b/web/scripts/domWidget.js @@ -253,7 +253,7 @@ LGraphNode.prototype.addDOMWidget = function (name, type, element, options) { const transform = new DOMMatrix() .scaleSelf(elRect.width / ctx.canvas.width, elRect.height / ctx.canvas.height) .multiplySelf(ctx.getTransform()) - .translateSelf(margin, margin + y); + .translateSelf(margin, margin + y + elRect.top); const scale = new DOMMatrix().scaleSelf(transform.a, transform.d); diff --git a/web/scripts/ui/menu/index.js b/web/scripts/ui/menu/index.js index 2f352bfee..870b53217 100644 --- a/web/scripts/ui/menu/index.js +++ b/web/scripts/ui/menu/index.js @@ -37,7 +37,7 @@ export class ComfyAppMenu { }); this.logo = $el("h1.comfyui-logo.sm-hide", { title: "ComfyUI" }, "ComfyUI"); - this.element = $el("nav.comfyui-menu", { style: { display: "none" }, parent: document.body }, [ + this.element = $el("nav.comfyui-menu", { style: { display: "none" } }, [ this.logo, this.workflows.element, new ComfySplitButton( @@ -126,6 +126,8 @@ export class ComfyAppMenu { ).element, ]); + document.body.prepend(this.element); + app.ui.settings.addSetting({ id: "Comfy.UseNewMenu", defaultValue: false, diff --git a/web/scripts/ui/menu/menu.css b/web/scripts/ui/menu/menu.css index b2aaf1141..0743b648c 100644 --- a/web/scripts/ui/menu/menu.css +++ b/web/scripts/ui/menu/menu.css @@ -37,12 +37,14 @@ opacity: 0.5; cursor: not-allowed; } -.primary .comfyui-button, .primary.comfyui-button { +.primary .comfyui-button, +.primary.comfyui-button { background-color: var(--primary-bg) !important; color: var(--primary-fg) !important; } -.primary .comfyui-button:not(:disabled):hover, .primary.comfyui-button:not(:disabled):hover { +.primary .comfyui-button:not(:disabled):hover, +.primary.comfyui-button:not(:disabled):hover { background-color: var(--primary-hover-bg) !important; color: var(--primary-hover-fg) !important; } @@ -126,9 +128,6 @@ /* Menu */ .comfyui-menu { - position: absolute; - top: 0; - left: 0; width: 100vw; background: var(--comfy-menu-bg); color: var(--fg-color); @@ -273,6 +272,8 @@ .comfyui-workflows-button { flex-direction: row-reverse; max-width: 200px; + position: relative; + z-index: 0; } .comfyui-workflows-button.popup-open { @@ -282,8 +283,17 @@ .comfyui-workflows-button.unsaved { font-style: italic; } +.comfyui-workflows-button-progress { + position: absolute; + top: 0; + left: 0; + background-color: green; + height: 100%; + border-radius: 4px; + z-index: -1; +} -.comfyui-workflows-button>span { +.comfyui-workflows-button > span { flex: auto; text-align: left; overflow: hidden; @@ -292,7 +302,7 @@ display: flex; align-items: center; gap: 7px; - width: 130px; + width: 150px; } .comfyui-workflows-label { overflow: hidden; @@ -318,7 +328,7 @@ .comfyui-workflows-popup { font-family: Arial, Helvetica, sans-serif; - font-size: 0.8em; + font-size: 0.8em; padding: 10px; overflow: auto; background-color: var(--border-color); @@ -337,6 +347,13 @@ left: 50%; top: 75px; } + +.comfyui-workflows-panel h3 { + margin: 10px 0 10px 0; + font-size: 11px; + opacity: 0.8; +} + .comfyui-workflows-actions { display: flex; gap: 10px; @@ -353,7 +370,8 @@ color: var(--primary-fg); } -.comfyui-workflows-favorites, .comfyui-workflows-open { +.comfyui-workflows-favorites, +.comfyui-workflows-open { border-bottom: 1px solid var(--comfy-input-bg); padding-bottom: 5px; margin-bottom: 5px; @@ -373,22 +391,22 @@ display: block; text-align: center; } -.comfyui-workflows-tree>ul { +.comfyui-workflows-tree > ul { padding: 0; } -.comfyui-workflows-tree>ul ul { +.comfyui-workflows-tree > ul ul { margin: 0; padding: 0 0 0 25px; } -.comfyui-workflows-tree .closed>ul { +.comfyui-workflows-tree .closed > ul { display: none; } .comfyui-workflows-tree li, .comfyui-workflows-tree-file { - --item-height: 31px; + --item-height: 32px; list-style-type: none; height: var(--item-height); display: flex; @@ -409,30 +427,43 @@ z-index: -1; } +.comfyui-workflows-tree-file.running:not(:hover)::before { + content: ""; + position: absolute; + width: var(--progress, 0); + left: 0; + height: var(--item-height); + background-color: green; + z-index: -1; +} + +.comfyui-workflows-tree-file.unsaved span { + font-style: italic; +} + .comfyui-workflows-tree-file span { flex: auto; } -.comfyui-workflows-tree-file span+.comfyui-workflows-file-action { +.comfyui-workflows-tree-file span + .comfyui-workflows-file-action { margin-left: 10px; } .comfyui-workflows-tree-file .comfyui-workflows-file-action { - opacity: 0; background-color: transparent; color: var(--fg-color); padding: 2px 4px; } +.comfyui-workflows-tree-file:not(:hover) .comfyui-workflows-file-action { + opacity: 0; +} + .comfyui-workflows-tree-file .comfyui-workflows-file-action:hover { background-color: var(--primary-bg); color: var(--primary-fg); } -.comfyui-workflows-tree-file:hover .comfyui-workflows-file-action { - opacity: 1; -} - .comfyui-workflows-tree-file .comfyui-workflows-file-action-primary { background-color: transparent; color: var(--fg-color); @@ -441,7 +472,7 @@ } .comfyui-workflows-file-action-favorite .mdi-star { - color: orange + color: orange; } /* View List */ diff --git a/web/scripts/ui/menu/workflows.js b/web/scripts/ui/menu/workflows.js index 44f5bfc9a..f36d1c9df 100644 --- a/web/scripts/ui/menu/workflows.js +++ b/web/scripts/ui/menu/workflows.js @@ -9,6 +9,7 @@ import { createSpinner } from "../spinner.js"; import { ComfyWorkflow } from "../../workflows.js"; export class ComfyWorkflowsMenu { + #first = true; element = $el("div.comfyui-workflows"); get open() { @@ -24,34 +25,18 @@ export class ComfyWorkflowsMenu { */ constructor(app) { this.app = app; - - const updateActive = () => { - const active = app.workflowManager.activeWorkflow; - this.button.tooltip = active.path; - this.workflowLabel.textContent = active.name; - this.unsaved = active.unsaved; - }; - - app.workflowManager.addEventListener("changeWorkflow", updateActive); - app.workflowManager.addEventListener("rename", updateActive); - app.workflowManager.addEventListener("delete", updateActive); - - app.workflowManager.addEventListener("save", () => { - this.unsaved = app.workflowManager.activeWorkflow.unsaved; - }); - - api.addEventListener("graphChanged", () => { - this.unsaved = true; - }); + this.#bindEvents(); const classList = { "comfyui-workflows-button": true, "comfyui-button": true, unsaved: getStorageValue("Comfy.PreviousWorkflowUnsaved") === "true", + running: false, }; + this.buttonProgress = $el("div.comfyui-workflows-button-progress"); this.workflowLabel = $el("span.comfyui-workflows-label", ""); this.button = new ComfyButton({ - content: $el("div.comfyui-workflows-button-inner", [$el("i.mdi.mdi-graph"), this.workflowLabel]), + content: $el("div.comfyui-workflows-button-inner", [$el("i.mdi.mdi-graph"), this.workflowLabel, this.buttonProgress]), icon: "chevron-down", classList, }); @@ -72,12 +57,59 @@ export class ComfyWorkflowsMenu { setStorageValue("Comfy.PreviousWorkflowUnsaved", v); }); } + + #updateProgress = () => { + const prompt = this.app.workflowManager.activePrompt; + let percent = 0; + if (this.app.workflowManager.activeWorkflow === prompt?.workflow) { + const total = Object.values(prompt.nodes); + const done = total.filter(Boolean); + percent = (done.length / total.length) * 100; + } + this.buttonProgress.style.width = percent + "%"; + }; + + #updateActive = () => { + const active = this.app.workflowManager.activeWorkflow; + this.button.tooltip = active.path; + this.workflowLabel.textContent = active.name; + this.unsaved = active.unsaved; + + if (this.#first) { + this.#first = false; + this.content.load(); + } + + this.#updateProgress(); + }; + + #bindEvents() { + this.app.workflowManager.addEventListener("changeWorkflow", this.#updateActive); + this.app.workflowManager.addEventListener("rename", this.#updateActive); + this.app.workflowManager.addEventListener("delete", this.#updateActive); + + this.app.workflowManager.addEventListener("save", () => { + this.unsaved = this.app.workflowManager.activeWorkflow.unsaved; + }); + + this.app.workflowManager.addEventListener("execute", (e) => { + this.#updateProgress(); + }); + + api.addEventListener("graphChanged", () => { + this.unsaved = true; + }); + } } export class ComfyWorkflowsContent { element = $el("div.comfyui-workflows-panel"); treeState = {}; treeFiles = {}; + /** @type { Map } */ + openFiles = new Map(); + /** @type {WorkflowElement} */ + activeElement = null; /** * @param {import("../../app.js").ComfyApp} app @@ -128,22 +160,19 @@ export class ComfyWorkflowsContent { this.app.workflowManager.addEventListener("favorite", (e) => { const workflow = e["detail"]; - const button = this.treeFiles[workflow.path]?.primaryButton; + const button = this.treeFiles[workflow.path]?.primary; if (!button) return; // Can happen when a workflow is renamed button.icon = this.#getFavoriteIcon(workflow); button.overIcon = this.#getFavoriteOverIcon(workflow); this.updateFavorites(); }); - this.app.workflowManager.addEventListener("open", (e) => { - this.updateOpen(); - }); - this.app.workflowManager.addEventListener("close", (e) => { - this.updateOpen(); - }); - this.app.workflowManager.addEventListener("rename", (e) => { - this.load(); - }); + for (const e of ["save", "open", "close", "changeWorkflow"]) { + // TODO: dont be lazy and just update the specific element + app.workflowManager.addEventListener(e, () => this.updateOpen()); + } + this.app.workflowManager.addEventListener("rename", () => this.load()); + this.app.workflowManager.addEventListener("execute", (e) => this.#updateActive()); } async load() { @@ -156,18 +185,45 @@ export class ComfyWorkflowsContent { updateOpen() { const current = this.openElement; + this.openFiles.clear(); this.openElement = $el("div.comfyui-workflows-open", [ - $el("h3", "Open Workflows"), + $el("h3", "Open"), ...this.app.workflowManager.openWorkflows.map((w) => { const wrapper = new WorkflowElement(this, w, { - buttons: [], + primary: { element: $el("i.mdi.mdi-18px.mdi-circle-small") }, + buttons: [ + new ComfyButton({ + icon: "content-save", + iconSize: 18, + classList: "comfyui-button comfyui-workflows-file-action", + enabled: w.unsaved, + action: (e) => { + e.stopImmediatePropagation(); + w.save(); + }, + }), + new ComfyButton({ + icon: "close", + iconSize: 18, + classList: "comfyui-button comfyui-workflows-file-action", + action: (e) => { + e.stopImmediatePropagation(); + this.app.workflowManager.closeWorkflow(w); + }, + }), + ], }); + if (w.unsaved) { + wrapper.element.classList.add("unsaved"); + } + this.openFiles.set(w, wrapper); return wrapper.element; }), ]); + this.#updateActive(); current?.replaceWith(this.openElement); } @@ -175,14 +231,14 @@ export class ComfyWorkflowsContent { const current = this.favoritesElement; const favorites = [...this.app.workflowManager.workflows.filter((w) => w.isFavorite)]; - this.favoritesElement = $el( - "div.comfyui-workflows-favorites", - favorites + this.favoritesElement = $el("div.comfyui-workflows-favorites", [ + $el("h3", "Favorites"), + ...favorites .map((w) => { return this.#getWorkflowElement(w).element; }) - .filter(Boolean) - ); + .filter(Boolean), + ]); current?.replaceWith(this.favoritesElement); } @@ -192,13 +248,14 @@ export class ComfyWorkflowsContent { const nodes = {}; this.treeFiles = {}; - this.treeElement = $el("ul.comfyui-workflows-tree"); + const tree = $el("ul.comfyui-workflows-tree"); + this.treeElement = $el("section", [$el("h3", "Browse"), tree]); for (const workflow of this.app.workflowManager.workflows) { if (!workflow.pathParts) continue; let currentPath = ""; - let currentRoot = this.treeElement; + let currentRoot = tree; for (let i = 0; i < workflow.pathParts.length; i++) { currentPath += (currentPath ? "\\" : "") + workflow.pathParts[i]; @@ -230,6 +287,34 @@ export class ComfyWorkflowsContent { } } + #updateActive() { + this.#removeActive(); + + const active = this.app.workflowManager.activePrompt; + if (!active?.workflow) return; + + const open = this.openFiles.get(active.workflow); + if (!open) return; + + this.activeElement = open; + + const total = Object.values(active.nodes); + const done = total.filter(Boolean); + const percent = done.length / total.length; + open.element.classList.add("running"); + open.element.style.setProperty("--progress", percent * 100 + "%"); + open.primary.element.classList.remove("mdi-circle-small"); + open.primary.element.classList.add("mdi-play"); + } + + #removeActive() { + if (!this.activeElement) return; + this.activeElement.element.classList.remove("running"); + this.activeElement.element.style.removeProperty("--progress"); + this.activeElement.primary.element.classList.add("mdi-circle-small"); + this.activeElement.primary.element.classList.remove("mdi-play"); + } + /** @param {ComfyWorkflow} workflow */ #getFavoriteIcon(workflow) { return workflow.isFavorite ? "star" : "file-outline"; @@ -321,7 +406,7 @@ export class ComfyWorkflowsContent { /** @param {ComfyWorkflow} workflow */ #getWorkflowElement(workflow) { return new WorkflowElement(this, workflow, { - primaryButton: this.#getFavoriteButton(workflow, true), + primary: this.#getFavoriteButton(workflow, true), buttons: [this.#getInsertButton(workflow), this.#getRenameButton(workflow), this.#getDeleteButton(workflow)], }); } @@ -364,10 +449,10 @@ class WorkflowElement { * @param { ComfyWorkflowsContent } parent * @param { ComfyWorkflow } workflow */ - constructor(parent, workflow, { tagName = "li", primaryButton, buttons }) { + constructor(parent, workflow, { tagName = "li", primary, buttons }) { this.parent = parent; this.workflow = workflow; - this.primaryButton = primaryButton; + this.primary = primary; this.buttons = buttons; this.element = $el( @@ -379,7 +464,7 @@ class WorkflowElement { }, title: this.workflow.path, }, - [this.primaryButton?.element, $el("span", workflow.name), ...buttons.map((b) => b.element)] + [this.primary?.element, $el("span", workflow.name), ...buttons.map((b) => b.element)] ); } } diff --git a/web/scripts/workflows.js b/web/scripts/workflows.js index d95016c8f..f429b3851 100644 --- a/web/scripts/workflows.js +++ b/web/scripts/workflows.js @@ -12,20 +12,30 @@ function appendJson(path) { } export class ComfyWorkflowManager extends EventTarget { + /** @type {string | null} */ + #activePromptId = null; #unsavedCount = 0; - /** @type {Record} */ workflowLookup = {}; - /** @type {Array} */ workflows = []; /** @type {Array} */ openWorkflows = []; + /** @type {Record}>} */ + queuedPrompts = {}; get activeWorkflow() { return this.openWorkflows[0]; } + get activePromptId() { + return this.#activePromptId; + } + + get activePrompt() { + return this.queuedPrompts[this.#activePromptId]; + } + /** * @param {import("./app.js").ComfyApp} app */ @@ -33,6 +43,48 @@ export class ComfyWorkflowManager extends EventTarget { super(); this.app = app; ChangeTracker.init(app); + + this.#bindExecutionEvents(); + } + + #bindExecutionEvents() { + // TODO: on reload, set active prompt based on the latest ws message + + const emit = () => this.dispatchEvent(new CustomEvent("execute", { detail: this.activePrompt })); + let executing = null; + api.addEventListener("execution_start", (e) => { + this.#activePromptId = e.detail.prompt_id; + + // This event can fire before the event is stored, so put a placeholder + this.queuedPrompts[this.#activePromptId] ??= { nodes: {} }; + emit(); + }); + api.addEventListener("execution_cached", (e) => { + if (!this.activePrompt) return; + for (const n of e.detail.nodes) { + this.activePrompt.nodes[n] = true; + } + emit(); + }); + api.addEventListener("executed", (e) => { + if (!this.activePrompt) return; + this.activePrompt.nodes[e.detail.node] = true; + emit(); + }); + api.addEventListener("executing", (e) => { + if (!this.activePrompt) return; + + if (executing) { + // Seems sometimes nodes that are cached fire executing but not executed + this.activePrompt.nodes[executing] = true; + } + executing = e.detail; + if (!executing) { + delete this.queuedPrompts[this.#activePromptId]; + this.#activePromptId = null; + } + emit(); + }); } async loadWorkflows() { @@ -99,6 +151,35 @@ export class ComfyWorkflowManager extends EventTarget { setStorageValue("Comfy.PreviousWorkflow", this.activeWorkflow.path ?? ""); this.dispatchEvent(new CustomEvent("changeWorkflow")); } + + storePrompt({ nodes, id }) { + this.queuedPrompts[id] ??= {}; + this.queuedPrompts[id].nodes = { + ...nodes.reduce((p, n) => { + p[n] = false; + return p; + }, {}), + ...this.queuedPrompts[id].nodes, + }; + this.queuedPrompts[id].workflow = this.activeWorkflow; + } + + /** + * @param {ComfyWorkflow} workflow + */ + async closeWorkflow(workflow) { + if (!workflow.isOpen) { + return; + } + workflow.changeTracker = null; + this.openWorkflows.splice(this.openWorkflows.indexOf(workflow), 1); + if (this.openWorkflows.length) { + await this.openWorkflows[0].load(); + } else { + // Load default + await this.app.loadGraphData(); + } + } } export class ComfyWorkflow { @@ -188,7 +269,6 @@ export class ComfyWorkflow { } load = async () => { - debugger; if (this.isOpen) { await this.manager.app.loadGraphData(this.changeTracker.activeState, true, this); } else { @@ -282,7 +362,7 @@ export class ComfyWorkflow { track() { if (this.changeTracker) { - this.changeTracker.restoreViewport(); + this.changeTracker.restore(); } else { this.changeTracker = new ChangeTracker(this); } diff --git a/web/style.css b/web/style.css index e0e5890b0..a85dfb752 100644 --- a/web/style.css +++ b/web/style.css @@ -32,6 +32,8 @@ body { overflow: hidden; background-color: var(--bg-color); color: var(--fg-color); + display: flex; + flex-direction: column; } #graph-canvas {