From 37a25c64ddd5db5d4dcb8e81205e6e9510020f79 Mon Sep 17 00:00:00 2001 From: Royce Schultz Date: Tue, 30 Apr 2024 00:02:29 -0600 Subject: [PATCH] Adds node for sending system notifications. --- py/system_notification.py | 40 +++++++++++++++++++++++++++++++ web/js/systemNotification.js | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 py/system_notification.py create mode 100644 web/js/systemNotification.js diff --git a/py/system_notification.py b/py/system_notification.py new file mode 100644 index 0000000..d20db94 --- /dev/null +++ b/py/system_notification.py @@ -0,0 +1,40 @@ +# Hack: string type that is always equal in not equal comparisons +class AnyType(str): + def __ne__(self, __value: object) -> bool: + return False + + +# Our any instance wants to be a wildcard string +any = AnyType("*") + + +class PlaySound: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "any": (any, {}), + "mode": (["always", "on empty queue"], {}), + }} + + FUNCTION = "nop" + INPUT_IS_LIST = True + OUTPUT_IS_LIST = (True,) + OUTPUT_NODE = True + RETURN_TYPES = (any,) + + CATEGORY = "utils" + + def IS_CHANGED(self, **kwargs): + return float("NaN") + + def nop(self, any, mode): + return {"ui": {"a": []}, "result": (any,)} + + +NODE_CLASS_MAPPINGS = { + "SystemNotification|pysssss": PlaySound, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "SystemNotification|pysssss": "SystemNotification 🐍", +} diff --git a/web/js/systemNotification.js b/web/js/systemNotification.js new file mode 100644 index 0000000..34499fb --- /dev/null +++ b/web/js/systemNotification.js @@ -0,0 +1,46 @@ +import { app } from "../../../scripts/app.js"; + +const notificationSetup = () => { + if (!("Notification" in window)) { + console.log("This browser does not support notifications."); + alert("This browser does not support notifications."); + return; + } + if (Notification.permission === "denied") { + console.log("Notifications are blocked. Please enable them in your browser settings."); + alert("Notifications are blocked. Please enable them in your browser settings."); + return; + } + if (Notification.permission !== "granted") { + Notification.requestPermission() + } + return true; +} + +app.registerExtension({ + name: "pysssss.SystemNotification", + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.name === "SystemNotification|pysssss") { + const onExecuted = nodeType.prototype.onExecuted; + nodeType.prototype.onExecuted = async function () { + onExecuted?.apply(this, arguments); + if (this.widgets[0].value === "on empty queue") { + if (app.ui.lastQueueSize !== 0) { + await new Promise((r) => setTimeout(r, 500)); + } + if (app.ui.lastQueueSize !== 0) { + return; + } + } + if(!notificationSetup()) return; + const notification = new Notification("ComfyUI", { body: "Your notification has triggered." }) + }; + + const onNodeCreated = nodeType.prototype.onNodeCreated; + nodeType.prototype.onNodeCreated = function () { + onNodeCreated?.apply(this, arguments); + notificationSetup(); + } + } + }, +});