From 35d3ecbdffdab570a9c8fbb181c37b03327e65d5 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Tue, 12 Mar 2024 21:41:14 +0000 Subject: [PATCH] various fixes add close unsaved warning --- web/scripts/changeTracker.js | 2 + web/scripts/ui/components/asyncDialog.js | 64 ++++++++++++++++ web/scripts/ui/dialog.js | 21 +++-- web/scripts/ui/menu/workflows.js | 97 ++++++++++++------------ web/scripts/workflows.js | 37 ++++++++- web/style.css | 26 +++++++ 6 files changed, 187 insertions(+), 60 deletions(-) create mode 100644 web/scripts/ui/components/asyncDialog.js diff --git a/web/scripts/changeTracker.js b/web/scripts/changeTracker.js index 83f8d9e65..0cae8959f 100644 --- a/web/scripts/changeTracker.js +++ b/web/scripts/changeTracker.js @@ -44,6 +44,8 @@ export class ChangeTracker { } checkState() { + if(!this.app.graph) return; + const currentState = this.app.graph.serialize(); if (!this.activeState) { this.activeState = clone(currentState); diff --git a/web/scripts/ui/components/asyncDialog.js b/web/scripts/ui/components/asyncDialog.js new file mode 100644 index 000000000..ed800cc8a --- /dev/null +++ b/web/scripts/ui/components/asyncDialog.js @@ -0,0 +1,64 @@ +import { ComfyDialog } from "../dialog.js"; +import { $el } from "../../ui.js"; + +export class ComfyAsyncDialog extends ComfyDialog { + #resolve; + + constructor(actions) { + super( + "dialog.comfy-dialog", + actions.map((opt) => { + if (typeof opt === "string") { + opt = { text: opt }; + } + return $el("button.comfyui-button", { + type: "button", + textContent: opt.text, + onclick: () => this.close(opt.value ?? opt.text), + }); + }) + ); + } + + show(html) { + this.element.addEventListener("close", () => { + this.close(); + }); + + super.show(html); + + return new Promise((resolve) => { + this.#resolve = resolve; + }); + } + + showModal(html) { + this.element.addEventListener("close", () => { + this.close(); + }); + + super.show(html); + this.element.showModal(); + + return new Promise((resolve) => { + this.#resolve = resolve; + }); + } + + close(result = null) { + this.#resolve(result); + this.element.close(); + super.close(); + } + + static async prompt({ title = null, message, actions }) { + const dialog = new ComfyAsyncDialog(actions); + const content = [$el("span", message)]; + if (title) { + content.unshift($el("h3", title)); + } + const res = await dialog.showModal(content); + dialog.element.remove(); + return res; + } +} diff --git a/web/scripts/ui/dialog.js b/web/scripts/ui/dialog.js index 16c12b406..803a97a2b 100644 --- a/web/scripts/ui/dialog.js +++ b/web/scripts/ui/dialog.js @@ -1,21 +1,26 @@ import { $el } from "../ui.js"; export class ComfyDialog extends EventTarget { - constructor(type = "div") { + #buttons; + + constructor(type = "div", buttons = null) { super(); + this.#buttons = buttons; this.element = $el(type + ".comfy-modal", { parent: document.body }, [ $el("div.comfy-modal-content", [$el("p", { $: (p) => (this.textElement = p) }), ...this.createButtons()]), ]); } createButtons() { - return [ - $el("button", { - type: "button", - textContent: "Close", - onclick: () => this.close(), - }), - ]; + return ( + this.#buttons ?? [ + $el("button", { + type: "button", + textContent: "Close", + onclick: () => this.close(), + }), + ] + ); } close() { diff --git a/web/scripts/ui/menu/workflows.js b/web/scripts/ui/menu/workflows.js index 95415a35d..0686933c3 100644 --- a/web/scripts/ui/menu/workflows.js +++ b/web/scripts/ui/menu/workflows.js @@ -7,7 +7,7 @@ import { api } from "../../api.js"; import { ComfyPopup } from "../components/popup.js"; import { createSpinner } from "../spinner.js"; import { ComfyWorkflow, trimJsonExt } from "../../workflows.js"; -import { ComfyDialog } from "../dialog.js"; +import { ComfyAsyncDialog } from "../components/asyncDialog.js"; export class ComfyWorkflowsMenu { #first = true; @@ -213,39 +213,42 @@ export class ComfyWorkflowsMenu { const getExtraMenuOptions = nodeType.prototype["getExtraMenuOptions"]; nodeType.prototype["getExtraMenuOptions"] = function (_, options) { const r = getExtraMenuOptions?.apply?.(this, arguments); - const t = /** @type { {imageIndex?: number, overIndex?: number, imgs: string[]} } */ /** @type {any} */ (this); - let img; - if (t.imageIndex != null) { - // An image is selected so select that - img = t.imgs?.[t.imageIndex]; - } else if (t.overIndex != null) { - // No image is selected but one is hovered - img = t.img?.s[t.overIndex]; - } - if (img) { - let pos = options.findIndex((o) => o.content === "Save Image"); - if (pos === -1) { - pos = 0; - } else { - pos++; + if (app.ui.settings.getSettingValue("Comfy.UseNewMenu", false) === true) { + const t = /** @type { {imageIndex?: number, overIndex?: number, imgs: string[]} } */ /** @type {any} */ (this); + let img; + if (t.imageIndex != null) { + // An image is selected so select that + img = t.imgs?.[t.imageIndex]; + } else if (t.overIndex != null) { + // No image is selected but one is hovered + img = t.img?.s[t.overIndex]; } - options.splice(pos, 0, { - content: "Send to workflow", - has_submenu: true, - submenu: { - options: [ - { - callback: () => sendToWorkflow(img, app.workflowManager.activeWorkflow), - title: "[Current workflow]", - }, - ...self.#getFavoriteMenuOptions(sendToWorkflow.bind(null, img)), - null, - ...self.#getMenuOptions(sendToWorkflow.bind(null, img)), - ], - }, - }); + if (img) { + let pos = options.findIndex((o) => o.content === "Save Image"); + if (pos === -1) { + pos = 0; + } else { + pos++; + } + + options.splice(pos, 0, { + content: "Send to workflow", + has_submenu: true, + submenu: { + options: [ + { + callback: () => sendToWorkflow(img, app.workflowManager.activeWorkflow), + title: "[Current workflow]", + }, + ...self.#getFavoriteMenuOptions(sendToWorkflow.bind(null, img)), + null, + ...self.#getMenuOptions(sendToWorkflow.bind(null, img)), + ], + }, + }); + } } return r; @@ -277,6 +280,7 @@ export class ComfyWorkflowsContent { icon: "file-code", iconSize: 18, classList: "comfyui-button primary", + tooltip: "Load default workflow", action: () => { popup.open = false; app.loadGraphData(); @@ -286,6 +290,7 @@ export class ComfyWorkflowsContent { content: "Browse", icon: "folder", iconSize: 18, + tooltip: "Browse for an image or exported workflow", action: () => { popup.open = false; app.ui.loadFile(); @@ -295,6 +300,7 @@ export class ComfyWorkflowsContent { content: "Blank", icon: "plus-thick", iconSize: 18, + tooltip: "Create a new blank workflow", action: () => { app.workflowManager.setWorkflow(null); app.clean(); @@ -351,6 +357,7 @@ export class ComfyWorkflowsContent { icon: "close", iconSize: 18, classList: "comfyui-button comfyui-workflows-file-action", + tooltip: "Close workflow", action: (e) => { e.stopImmediatePropagation(); this.app.workflowManager.closeWorkflow(w); @@ -469,6 +476,11 @@ export class ComfyWorkflowsContent { return workflow.isFavorite ? "star-off" : "star-outline"; } + /** @param {ComfyWorkflow} workflow */ + #getFavoriteTooltip(workflow) { + return workflow.isFavorite ? "Remove this workflow from your favorites" : "Add this workflow to your favorites"; + } + /** @param {ComfyWorkflow} workflow */ #getFavoriteButton(workflow, primary) { return new ComfyButton({ @@ -476,6 +488,7 @@ export class ComfyWorkflowsContent { overIcon: this.#getFavoriteOverIcon(workflow), iconSize: 18, classList: "comfyui-button comfyui-workflows-file-action-favorite" + (primary ? " comfyui-workflows-file-action-primary" : ""), + tooltip: this.#getFavoriteTooltip(workflow), action: (e) => { e.stopImmediatePropagation(); workflow.favorite(!workflow.isFavorite); @@ -613,24 +626,20 @@ class WorkflowElement { } } -class WidgetSelectionDialog extends ComfyDialog { +class WidgetSelectionDialog extends ComfyAsyncDialog { #options; - #resolve; /** * @param {Array<{widget: {name: string}, node: {pos: [number, number], title: string, id: string, type: string}}>} options */ constructor(options) { - super("dialog"); + super(); this.#options = options; } show(app) { - this.element.addEventListener("close", () => { - this.close(); - }); this.element.classList.add("comfy-widget-selection-dialog"); - super.show( + return super.show( $el("div", [ $el("h2", "Select image target"), $el( @@ -649,6 +658,7 @@ class WidgetSelectionDialog extends ComfyDialog { app.canvas.ds.offset[0] = -opt.node.pos[0] + 50; app.canvas.ds.offset[1] = -opt.node.pos[1] + 50; app.canvas.selectNode(opt.node); + app.graph.setDirtyCanvas(true, true); }, }, "Show" @@ -657,8 +667,7 @@ class WidgetSelectionDialog extends ComfyDialog { "button.comfyui-button.primary", { onclick: () => { - this.#resolve(opt); - this.close(); + this.close(opt); }, }, "Select" @@ -668,13 +677,5 @@ class WidgetSelectionDialog extends ComfyDialog { ), ]) ); - return new Promise((resolve) => { - this.#resolve = resolve; - }); - } - - close() { - this.#resolve(); - super.close(); } } \ No newline at end of file diff --git a/web/scripts/workflows.js b/web/scripts/workflows.js index 38e9ebfeb..f95647df3 100644 --- a/web/scripts/workflows.js +++ b/web/scripts/workflows.js @@ -2,6 +2,7 @@ import { api } from "./api.js"; import { ChangeTracker } from "./changeTracker.js"; +import { ComfyAsyncDialog } from "./ui/components/asyncDialog.js"; import { getStorageValue, setStorageValue } from "./utils.js"; function appendJsonExt(path) { @@ -171,9 +172,33 @@ export class ComfyWorkflowManager extends EventTarget { /** * @param {ComfyWorkflow} workflow */ - async closeWorkflow(workflow) { + async closeWorkflow(workflow, warnIfUnsaved = true) { if (!workflow.isOpen) { - return; + return true; + } + if (workflow.unsaved && warnIfUnsaved) { + const res = await ComfyAsyncDialog.prompt({ + title: "Save Changes?", + message: `Do you want to save changes to "${workflow.path ?? workflow.name}" before closing?`, + actions: ["Yes", "No", "Cancel"], + }); + if (res === "Yes") { + const active = this.activeWorkflow; + if (active !== workflow) { + // We need to switch to the workflow to save it + await workflow.load(); + } + + if (!(await workflow.save())) { + // Save was canceled, restore the previous workflow + if (active !== workflow) { + await active.load(); + } + return; + } + } else if (res === "Cancel") { + return; + } } workflow.changeTracker = null; this.openWorkflows.splice(this.openWorkflows.indexOf(workflow), 1); @@ -284,9 +309,9 @@ export class ComfyWorkflow { async save(saveAs = false) { if (!this.path || saveAs) { - await this.#save(null, false); + return !!(await this.#save(null, false)); } else { - await this.#save(this.path, true); + return !!(await this.#save(this.path, true)); } } @@ -331,6 +356,7 @@ export class ComfyWorkflow { await this.favorite(true); } this.manager.dispatchEvent(new CustomEvent("rename", { detail: this })); + setStorageValue("Comfy.PreviousWorkflow", this.path ?? ""); } async insert() { @@ -405,6 +431,7 @@ export class ComfyWorkflow { await this.manager.loadWorkflows(); this.unsaved = false; this.manager.dispatchEvent(new CustomEvent("rename", { detail: this })); + setStorageValue("Comfy.PreviousWorkflow", this.path ?? ""); } else if (path !== this.path) { // Saved as, open the new copy await this.manager.loadWorkflows(); @@ -415,5 +442,7 @@ export class ComfyWorkflow { this.unsaved = false; this.manager.dispatchEvent(new CustomEvent("save", { detail: this })); } + + return true; } } diff --git a/web/style.css b/web/style.css index a85dfb752..2876c670f 100644 --- a/web/style.css +++ b/web/style.css @@ -372,6 +372,32 @@ dialog::backdrop { background: rgba(0, 0, 0, 0.5); } +.comfy-dialog.comfy-modal { + top: 0; + font-family: Arial, sans-serif; + border-color: var(--bg-color); +} + +.comfy-dialog .comfy-modal-content { + flex-direction: row; + flex-wrap: wrap; + gap: 10px; + color: var(--fg-color); +} + +.comfy-dialog .comfy-modal-content h3 { + margin-top: 0; +} + +.comfy-dialog .comfy-modal-content > p { + width: 100%; +} + +.comfy-dialog .comfy-modal-content > .comfyui-button { + flex: 1; + justify-content: center; +} + #comfy-settings-dialog { padding: 0; width: 41rem;