Line data Source code
1 : import { toLightThemeNodeColor } from '@/renderer/extensions/vueNodes/utils/nodeStyleUtils'
2 : import { computed } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import {
6 : LGraphCanvas,
7 : LGraphNode,
8 : LiteGraph,
9 : RenderShape,
10 : isColorable
11 : } from '@/lib/litegraph/src/litegraph'
12 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
13 : import { useThemeStore } from '@/platform/theme/themeStore'
14 :
15 : import { useCanvasRefresh } from './useCanvasRefresh'
16 :
17 : interface ColorOption {
18 : name: string
19 : localizedName: string
20 : value: {
21 : dark: string
22 : light: string
23 : }
24 : }
25 :
26 : interface ShapeOption {
27 : name: string
28 : localizedName: string
29 : value: RenderShape
30 : }
31 :
32 : /**
33 : * Composable for handling node color and shape customization
34 : */
35 7065 : export function useNodeCustomization() {
36 7065 : const { t } = useI18n()
37 7065 : const canvasStore = useCanvasStore()
38 7065 : const themeStore = useThemeStore()
39 7065 : const canvasRefresh = useCanvasRefresh()
40 7065 : const isLightTheme = computed(() => themeStore.colorScheme === 'light')
41 :
42 : // Color options
43 7065 : const NO_COLOR_OPTION: ColorOption = {
44 : name: 'noColor',
45 : localizedName: t('color.noColor'),
46 : value: {
47 : dark: LiteGraph.NODE_DEFAULT_BGCOLOR,
48 : light: toLightThemeNodeColor(LiteGraph.NODE_DEFAULT_BGCOLOR)
49 : }
50 : }
51 :
52 7065 : const colorOptions: ColorOption[] = [
53 : NO_COLOR_OPTION,
54 : ...Object.entries(LGraphCanvas.node_colors).map(([name, color]) => ({
55 : name,
56 : localizedName: t(`color.${name}`),
57 : value: {
58 : dark: color.bgcolor,
59 : light: toLightThemeNodeColor(color.bgcolor)
60 : }
61 : }))
62 : ]
63 :
64 : // Shape options
65 7065 : const shapeOptions: ShapeOption[] = [
66 : {
67 : name: 'default',
68 : localizedName: t('shape.default'),
69 : value: RenderShape.ROUND
70 : },
71 : {
72 : name: 'box',
73 : localizedName: t('shape.box'),
74 : value: RenderShape.BOX
75 : },
76 : {
77 : name: 'card',
78 : localizedName: t('shape.CARD'),
79 : value: RenderShape.CARD
80 : }
81 : ]
82 :
83 7065 : const applyColor = (colorOption: ColorOption | null) => {
84 4 : const colorName = colorOption?.name ?? NO_COLOR_OPTION.name
85 4 : const canvasColorOption =
86 : colorName === NO_COLOR_OPTION.name
87 : ? null
88 : : LGraphCanvas.node_colors[colorName]
89 :
90 4 : for (const item of canvasStore.selectedItems) {
91 4 : if (isColorable(item)) {
92 4 : item.setColorOption(canvasColorOption)
93 : }
94 : }
95 :
96 4 : canvasRefresh.refreshCanvas()
97 : }
98 :
99 7065 : const applyShape = (shapeOption: ShapeOption) => {
100 3 : const selectedNodes = Array.from(canvasStore.selectedItems).filter(
101 : (item): item is LGraphNode => item instanceof LGraphNode
102 : )
103 :
104 3 : if (selectedNodes.length === 0) {
105 0 : return
106 : }
107 :
108 3 : selectedNodes.forEach((node) => {
109 3 : node.shape = shapeOption.value
110 : })
111 :
112 3 : canvasRefresh.refreshCanvas()
113 : }
114 :
115 7065 : const getCurrentColor = (): ColorOption | null => {
116 0 : const selectedItems = Array.from(canvasStore.selectedItems)
117 0 : if (selectedItems.length === 0) return null
118 :
119 : // Get color from first colorable item
120 0 : const firstColorableItem = selectedItems.find((item) => isColorable(item))
121 0 : if (!firstColorableItem || !isColorable(firstColorableItem)) return null
122 :
123 : // Get the current color option from the colorable item
124 : const currentColorOption = firstColorableItem.getColorOption()
125 0 : const currentBgColor = currentColorOption?.bgcolor ?? null
126 :
127 : // Find matching color option
128 0 : return (
129 : colorOptions.find(
130 : (option) =>
131 : option.value.dark === currentBgColor ||
132 : option.value.light === currentBgColor
133 : ) ?? NO_COLOR_OPTION
134 : )
135 : }
136 :
137 7065 : const getCurrentShape = (): ShapeOption | null => {
138 13334 : const selectedNodes = Array.from(canvasStore.selectedItems).filter(
139 : (item): item is LGraphNode => item instanceof LGraphNode
140 : )
141 :
142 13334 : if (selectedNodes.length === 0) return null
143 :
144 : const firstNode = selectedNodes[0]
145 13334 : const currentShape = firstNode.shape ?? RenderShape.ROUND
146 :
147 13334 : return (
148 : shapeOptions.find((option) => option.value === currentShape) ??
149 : shapeOptions[0]
150 : )
151 : }
152 :
153 7065 : return {
154 : colorOptions,
155 : shapeOptions,
156 : applyColor,
157 : applyShape,
158 : getCurrentColor,
159 : getCurrentShape,
160 : isLightTheme
161 : }
162 : }
|