Line data Source code
1 : import _ from 'es-toolkit/compat'
2 : import { once } from 'es-toolkit'
3 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
4 :
5 : import { app, ComfyApp } from '@/scripts/app'
6 : import { useMaskEditorStore } from '@comfyorg/mask-editor/maskEditorStore'
7 : import { useDialogStore } from '@/stores/dialogStore'
8 : import { useMaskEditor } from '@/renderer/extensions/maskEditor/composables/useMaskEditor'
9 : import { useCanvasTransform } from '@comfyorg/mask-editor/useCanvasTransform'
10 : import { markCoreMediaMenuCallback } from '@/utils/coreMediaMenuActionUtils'
11 : import { isImageNode } from '@/utils/litegraphUtil'
12 : import { selectedCanvasNodes } from '@/renderer/core/canvas/litegraph/selectionAdapter'
13 : import type { ComfyExtension } from '@/types/comfy'
14 :
15 2 : function openMaskEditor(node: LGraphNode): void {
16 2 : if (!node.imgs?.length && node.previewMediaType !== 'image') {
17 0 : console.error('[MaskEditor] Node has no images')
18 0 : return
19 : }
20 :
21 2 : useMaskEditor().openMaskEditor(node)
22 : }
23 :
24 : // Open mask editor from clipspace (for plugin compatibility)
25 : // This is called when ComfyApp.open_maskeditor() is invoked without arguments
26 0 : function openMaskEditorFromClipspace(): void {
27 0 : const node = ComfyApp.clipspace_return_node
28 0 : if (!node) {
29 0 : console.error('[MaskEditor] No clipspace_return_node found')
30 0 : return
31 : }
32 :
33 0 : openMaskEditor(node)
34 : }
35 :
36 2355 : const warnOpenMaskEditorDeprecated = once(() =>
37 : console.warn(
38 : '[MaskEditor] ComfyApp.open_maskeditor is deprecated. ' +
39 : 'Plugins should migrate to using the command system or direct node context menu integration.'
40 : )
41 : )
42 :
43 : // Check if the dialog is already opened
44 0 : function isOpened(): boolean {
45 0 : return useDialogStore().isDialogOpen('global-mask-editor')
46 : }
47 :
48 2355 : const changeBrushSize = async (sizeChanger: (oldSize: number) => number) => {
49 0 : if (!isOpened()) return
50 :
51 0 : const store = useMaskEditorStore()
52 0 : const oldBrushSize = store.brushSettings.size
53 0 : const newBrushSize = sizeChanger(oldBrushSize)
54 0 : store.setBrushSize(newBrushSize)
55 : }
56 :
57 2355 : export const maskEditorExtension: ComfyExtension = {
58 : name: 'Comfy.MaskEditor',
59 : settings: [
60 : {
61 : id: 'Comfy.MaskEditor.BrushAdjustmentSpeed',
62 : category: ['Mask Editor', 'BrushAdjustment', 'Sensitivity'],
63 : name: 'Brush adjustment speed multiplier',
64 : tooltip:
65 : 'Controls how quickly the brush size and hardness change when adjusting. Higher values mean faster changes.',
66 : type: 'slider',
67 : attrs: {
68 : min: 0.1,
69 : max: 2.0,
70 : step: 0.1
71 : },
72 : defaultValue: 1.0,
73 : versionAdded: '1.0.0'
74 : },
75 : {
76 : id: 'Comfy.MaskEditor.UseDominantAxis',
77 : category: ['Mask Editor', 'BrushAdjustment', 'UseDominantAxis'],
78 : name: 'Lock brush adjustment to dominant axis',
79 : tooltip:
80 : 'When enabled, brush adjustments will only affect size OR hardness based on which direction you move more',
81 : type: 'boolean',
82 : defaultValue: true
83 : }
84 : ],
85 : commands: [
86 : {
87 : id: 'Comfy.MaskEditor.OpenMaskEditor',
88 : icon: 'icon-[lucide--pencil]',
89 : label: 'Open Mask Editor for Selected Node',
90 : function: () => {
91 2 : const selectedNodes = selectedCanvasNodes(app.canvas)
92 2 : if (selectedNodes.length !== 1) return
93 :
94 2 : const [selectedNode] = selectedNodes
95 2 : openMaskEditor(selectedNode)
96 : }
97 : },
98 : {
99 : id: 'Comfy.MaskEditor.BrushSize.Increase',
100 : icon: 'icon-[lucide--circle-plus]',
101 : label: 'Increase Brush Size in MaskEditor',
102 : function: () => changeBrushSize((old) => _.clamp(old + 2, 1, 250))
103 : },
104 : {
105 : id: 'Comfy.MaskEditor.BrushSize.Decrease',
106 : icon: 'icon-[lucide--circle-minus]',
107 : label: 'Decrease Brush Size in MaskEditor',
108 : function: () => changeBrushSize((old) => _.clamp(old - 2, 1, 250))
109 : },
110 : {
111 : id: 'Comfy.MaskEditor.ColorPicker',
112 : icon: 'icon-[lucide--palette]',
113 : label: 'Open Color Picker in MaskEditor',
114 : function: () => {
115 0 : if (!isOpened()) return
116 :
117 : const store = useMaskEditorStore()
118 0 : store.colorInput?.click()
119 : }
120 : },
121 : {
122 : id: 'Comfy.MaskEditor.Rotate.Right',
123 : icon: 'icon-[lucide--rotate-cw]',
124 : label: 'Rotate Right in MaskEditor',
125 : function: async () => {
126 0 : if (!isOpened()) return
127 0 : await useCanvasTransform().rotateClockwise()
128 : }
129 : },
130 : {
131 : id: 'Comfy.MaskEditor.Rotate.Left',
132 : icon: 'icon-[lucide--undo-2]',
133 : label: 'Rotate Left in MaskEditor',
134 : function: async () => {
135 0 : if (!isOpened()) return
136 0 : await useCanvasTransform().rotateCounterclockwise()
137 : }
138 : },
139 : {
140 : id: 'Comfy.MaskEditor.Mirror.Horizontal',
141 : icon: 'icon-[lucide--move-horizontal]',
142 : label: 'Mirror Horizontal in MaskEditor',
143 : function: async () => {
144 0 : if (!isOpened()) return
145 0 : await useCanvasTransform().mirrorHorizontal()
146 : }
147 : },
148 : {
149 : id: 'Comfy.MaskEditor.Mirror.Vertical',
150 : icon: 'icon-[lucide--move-vertical]',
151 : label: 'Mirror Vertical in MaskEditor',
152 : function: async () => {
153 0 : if (!isOpened()) return
154 0 : await useCanvasTransform().mirrorVertical()
155 : }
156 : }
157 : ],
158 : beforeRegisterNodeDef(nodeType: typeof LGraphNode) {
159 2439372 : const original = nodeType.prototype.getExtraMenuOptions
160 2439372 : nodeType.prototype.getExtraMenuOptions = function (canvas, options) {
161 3774 : const extra = original?.call(this, canvas, options) ?? []
162 3774 : if (ComfyApp.clipspace_return_node || !isImageNode(this)) return extra
163 :
164 314 : options.push({
165 : content: 'Open in MaskEditor | Image Canvas',
166 : callback: markCoreMediaMenuCallback(() => {
167 0 : useMaskEditor().openMaskEditor(this)
168 : }, 'preview')
169 : })
170 314 : return extra
171 : }
172 : },
173 : init() {
174 2355 : ComfyApp.open_maskeditor = () => {
175 0 : warnOpenMaskEditorDeprecated()
176 0 : openMaskEditorFromClipspace()
177 : }
178 : }
179 : }
|