From 4643e37599078b266549791244660500beab0b0f Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Wed, 19 Jun 2024 20:18:08 +0100 Subject: [PATCH] Various fixes --- web/scripts/app.js | 8 +-- web/scripts/changeTracker.js | 2 +- web/scripts/ui.js | 4 +- web/scripts/ui/menu/index.js | 87 +++++++++++++++++++----------- web/scripts/ui/menu/queueButton.js | 5 +- 5 files changed, 67 insertions(+), 39 deletions(-) diff --git a/web/scripts/app.js b/web/scripts/app.js index 4a80bb37f..d40f4845b 100644 --- a/web/scripts/app.js +++ b/web/scripts/app.js @@ -1539,8 +1539,6 @@ export class ComfyApp { */ async setup() { await this.#setUser(); - await Promise.all([this.workflowManager.loadWorkflows(), this.ui.settings.load()]); - await this.#loadExtensions(); // Create and mount the LiteGraph in the DOM const mainCanvas = document.createElement("canvas") @@ -1548,6 +1546,10 @@ export class ComfyApp { const canvasEl = (this.canvasEl = Object.assign(mainCanvas, { id: "graph-canvas" })); canvasEl.tabIndex = "1"; document.body.append(canvasEl); + this.resizeCanvas(); + + await Promise.all([this.workflowManager.loadWorkflows(), this.ui.settings.load()]); + await this.#loadExtensions(); addDomClippingSetting(); this.#addProcessMouseHandler(); @@ -1634,7 +1636,7 @@ export class ComfyApp { this.canvasEl.width = Math.round(width * scale); this.canvasEl.height = Math.round(height * scale); this.canvasEl.getContext("2d").scale(scale, scale); - this.canvas.draw(true, true); + this.canvas?.draw(true, true); } /** diff --git a/web/scripts/changeTracker.js b/web/scripts/changeTracker.js index 6642acab5..4b50bbff9 100644 --- a/web/scripts/changeTracker.js +++ b/web/scripts/changeTracker.js @@ -173,7 +173,7 @@ export class ChangeTracker { // Store node outputs api.addEventListener("executed", ({ detail }) => { const prompt = app.workflowManager.queuedPrompts[detail.prompt_id]; - if (!prompt.workflow) return; + if (!prompt?.workflow) return; const nodeOutputs = (prompt.workflow.changeTracker.nodeOutputs ??= {}); const output = nodeOutputs[detail.node]; if (detail.merge && output) { diff --git a/web/scripts/ui.js b/web/scripts/ui.js index 283f01a01..6709d7a5b 100644 --- a/web/scripts/ui.js +++ b/web/scripts/ui.js @@ -196,6 +196,8 @@ function dragElement(dragEl, settings) { document.onmouseup = null; document.onmousemove = null; } + + return restorePos; } class ComfyList { @@ -632,7 +634,7 @@ export class ComfyUI { onChange: function(value) { document.getElementById("comfy-dev-save-api-button").style.display = value ? "flex" : "none"}, }); - dragElement(this.menuContainer, this.settings); + this.restoreMenuPosition = dragElement(this.menuContainer, this.settings); this.setStatus({exec_info: {queue_remaining: "X"}}); } diff --git a/web/scripts/ui/menu/index.js b/web/scripts/ui/menu/index.js index adc807e6e..1e00b3d22 100644 --- a/web/scripts/ui/menu/index.js +++ b/web/scripts/ui/menu/index.js @@ -21,6 +21,17 @@ const showOnMobile = (t) => { }; export class ComfyAppMenu { + #sizeBreak = "lg"; + #lastSizeBreaks = { + lg: null, + md: null, + sm: null, + xs: null, + }; + #sizeBreaks = Object.keys(this.#lastSizeBreaks); + #cachedInnerSize = null; + #cacheTimeout = null; + /** * @param { import("../../app.js").ComfyApp } app */ @@ -41,7 +52,7 @@ export class ComfyAppMenu { { primary: getSaveButton(), mode: "hover", - position: "absolute" + position: "absolute", }, getSaveButton("Save"), new ComfyButton({ @@ -54,7 +65,7 @@ export class ComfyAppMenu { icon: "download", content: "Export", tooltip: "Export the current workflow as JSON", - action: () => this.exportWorkflow( "workflow", "workflow"), + action: () => this.exportWorkflow("workflow", "workflow"), }), new ComfyButton({ icon: "api", @@ -82,7 +93,7 @@ export class ComfyAppMenu { icon: "fit-to-page-outline", content: "Reset View", tooltip: "Reset the canvas view", - action: () => app.resetView() + action: () => app.resetView(), }), new ComfyButton({ icon: "cancel", @@ -134,47 +145,49 @@ export class ComfyAppMenu { showOnMobile(this.mobileMenuButton).element, ]); - app.ui.settings.addSetting({ + let resizeHandler; + this.menuPositionSetting = app.ui.settings.addSetting({ id: "Comfy.UseNewMenu", - defaultValue: false, - name: "[Beta] Use new menu and workflow management", + defaultValue: "Disabled", + name: "[Beta] Use new menu and workflow management. Note: On small screens the menu will always be at the top.", type: "combo", options: ["Disabled", "Top", "Bottom"], onChange: async (v) => { - if (v) { - document.body.style.display = "grid"; - app.ui.menuContainer.style.display = "none"; - this.element.style.removeProperty("display"); - if (v === "Bottom") { - app.bodyBottom.append(this.element); - } else { - app.bodyTop.prepend(this.element); + if (v && v !== "Disabled") { + if (!resizeHandler) { + resizeHandler = () => { + this.calculateSizeBreak(); + }; + window.addEventListener("resize", resizeHandler); } - this.calculateSizeBreak(); + this.updatePosition(v); } else { + if (resizeHandler) { + window.removeEventListener("resize", resizeHandler); + resizeHandler = null; + } document.body.style.removeProperty("display"); app.ui.menuContainer.style.removeProperty("display"); this.element.style.display = "none"; + app.ui.restoreMenuPosition(); } window.dispatchEvent(new Event("resize")); }, }); - - window.addEventListener("resize", () => { - this.calculateSizeBreak(); - }); } - #sizeBreak = "lg"; - #lastSizeBreaks = { - lg: null, - md: null, - sm: null, - xs: null, - }; - #sizeBreaks = Object.keys(this.#lastSizeBreaks); - #cachedInnerSize = null; - #cacheTimeout = null; + updatePosition(v) { + document.body.style.display = "grid"; + this.app.ui.menuContainer.style.display = "none"; + this.element.style.removeProperty("display"); + this.position = v; + if (v === "Bottom") { + this.app.bodyBottom.append(this.element); + } else { + this.app.bodyTop.prepend(this.element); + } + this.calculateSizeBreak(); + } updateSizeBreak(idx, prevIdx, direction) { const newSize = this.#sizeBreaks[idx]; @@ -197,7 +210,17 @@ export class ComfyAppMenu { } } - // Allow multiple updates, but prevent bouncing + if (idx) { + // We're on a small screen, force the menu at the top + if (this.position !== "Top") { + this.updatePosition("Top"); + } + } else if (this.position != this.menuPositionSetting.value) { + // Restore user position + this.updatePosition(this.menuPositionSetting.value); + } + + // Allow multiple updates, but prevent bouncing if (!direction) { direction = prevIdx - idx; } else if (direction != prevIdx - idx) { @@ -266,14 +289,14 @@ export class ComfyAppMenu { * @param { "workflow" | "output" } [promptProperty] */ async exportWorkflow(filename, promptProperty) { - if(this.app.workflowManager.activeWorkflow?.path) { + if (this.app.workflowManager.activeWorkflow?.path) { filename = this.app.workflowManager.activeWorkflow.name; } const p = await this.app.graphToPrompt(); const json = JSON.stringify(p[promptProperty], null, 2); const blob = new Blob([json], { type: "application/json" }); const file = this.getFilename(filename); - if(!file) return; + if (!file) return; downloadBlob(file, blob); } } diff --git a/web/scripts/ui/menu/queueButton.js b/web/scripts/ui/menu/queueButton.js index 11bd14900..3c29ab090 100644 --- a/web/scripts/ui/menu/queueButton.js +++ b/web/scripts/ui/menu/queueButton.js @@ -11,9 +11,10 @@ export class ComfyQueueButton { element = $el("div.comfyui-queue-button"); #internalQueueSize = 0; - queuePrompt = async () => { + queuePrompt = async (e) => { this.#internalQueueSize += this.queueOptions.batchCount; - await this.app.queuePrompt(-1, this.queueOptions.batchCount); + // Hold shift to queue front + await this.app.queuePrompt(-e.shiftKey, this.queueOptions.batchCount); }; constructor(app) {