Update "AlwaysSnapToGrid" to work with groups.

When https://github.com/comfyanonymous/ComfyUI/pull/3393 is applied, this would be required for always snapping groups as well.
This commit is contained in:
Regis Gaughan, III
2024-05-02 21:25:39 -04:00
committed by GitHub
parent 3f2c021e50
commit 7679b57440
+32 -16
View File
@@ -1,7 +1,21 @@
import { app } from "../../../scripts/app.js";
let setting;
const id = "pysssss.SnapToGrid";
/** Wraps the provided function call to set/reset shiftDown when setting is enabled. */
function wrapCallInSettingCheck(fn) {
if (setting?.value) {
const shift = app.shiftDown;
app.shiftDown = true;
const r = fn();
app.shiftDown = shift;
return r;
}
return fn();
}
const ext = {
name: id,
init() {
@@ -22,14 +36,7 @@ const ext = {
// Override drawNode to draw the drop position
const drawNode = LGraphCanvas.prototype.drawNode;
LGraphCanvas.prototype.drawNode = function () {
if (setting?.value) {
const shift = app.shiftDown;
app.shiftDown = true;
const r = drawNode.apply(this, arguments);
app.shiftDown = shift;
return r;
}
return drawNode.apply(this, arguments);
wrapCallInSettingCheck(() => drawNode.apply(this, arguments));
};
// Override node added to add a resize handler to force grid alignment
@@ -38,18 +45,27 @@ const ext = {
const r = onNodeAdded?.apply(this, arguments);
const onResize = node.onResize;
node.onResize = function () {
if (setting?.value) {
const shift = app.shiftDown;
app.shiftDown = true;
const r = onResize?.apply(this, arguments);
app.shiftDown = shift;
return r;
}
return onResize?.apply(this, arguments);
wrapCallInSettingCheck(() => onResize?.apply(this, arguments));
};
return r;
};
const groupMove = LGraphGroup.prototype.move;
LGraphGroup.prototype.move = function(deltax, deltay, ignore_nodes) {
wrapCallInSettingCheck(() => groupMove.apply(this, arguments));
}
const canvasDrawGroups = LGraphCanvas.prototype.drawGroups;
LGraphCanvas.prototype.drawGroups = function (canvas, ctx) {
wrapCallInSettingCheck(() => canvasDrawGroups.apply(this, arguments));
}
const canvasOnGroupAdd = LGraphCanvas.onGroupAdd;
LGraphCanvas.onGroupAdd = function() {
wrapCallInSettingCheck(() => canvasOnGroupAdd.apply(this, arguments));
}
return configure.apply(this, arguments);
};
},