Line data Source code
1 : <template>
2 136 : <div class="relative">
3 : <Button
4 : :tooltip="localizedCurrentColorName ?? t('color.noColor')"
5 : variant="muted-textonly"
6 : :aria-label="t('g.color')"
7 : @click="() => (showColorPicker = !showColorPicker)"
8 : >
9 : <div class="flex items-center gap-1 px-0">
10 : <i
11 : class="pi pi-circle-fill"
12 : data-testid="color-picker-current-color"
13 : :style="{ color: currentColor ?? '' }"
14 : />
15 : <i class="icon-[lucide--chevron-down]" />
16 : </div>
17 : </Button>
18 : <div
19 : v-if="showColorPicker"
20 : class="absolute -top-10 left-1/2 -translate-x-1/2"
21 : >
22 : <ToggleGroup
23 : :model-value="selectedColorOption?.name"
24 : type="single"
25 : @update:model-value="applyColorName"
26 : >
27 240 : <TextTooltip
28 : v-for="option in colorOptions"
29 : :key="option.name"
30 : :text="option.localizedName"
31 : >
32 : <ToggleGroupItem
33 : :value="option.name"
34 : :aria-label="option.localizedName"
35 : class="px-1 py-2 aria-pressed:bg-secondary aria-pressed:text-foreground"
36 : >
37 : <i
38 : class="pi pi-circle-fill"
39 : :style="{
40 : color: isLightTheme ? option.value.light : option.value.dark
41 : }"
42 : :data-testid="option.name"
43 : />
44 : </ToggleGroupItem>
45 : </TextTooltip>
46 : </ToggleGroup>
47 : </div>
48 : </div>
49 : </template>
50 :
51 : <script setup lang="ts">
52 : import { toLightThemeNodeColor } from '@/renderer/extensions/vueNodes/utils/nodeStyleUtils'
53 : import { useEventListener } from '@vueuse/core'
54 : import type { Raw } from 'vue'
55 : import { computed, ref, watch } from 'vue'
56 : import { useI18n } from 'vue-i18n'
57 :
58 : import Button from '@comfyorg/design-system/button/Button'
59 : import ToggleGroup from '@comfyorg/design-system/toggle-group/ToggleGroup'
60 : import ToggleGroupItem from '@comfyorg/design-system/toggle-group/ToggleGroupItem'
61 : import TextTooltip from '@comfyorg/design-system/tooltip/TextTooltip'
62 : import type {
63 : ColorOption as CanvasColorOption,
64 : LiteGraphCanvasEvent,
65 : Positionable
66 : } from '@/lib/litegraph/src/litegraph'
67 : import {
68 : LGraphCanvas,
69 : LiteGraph,
70 : isColorable
71 : } from '@/lib/litegraph/src/litegraph'
72 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
73 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
74 : import { useThemeStore } from '@/platform/theme/themeStore'
75 : import { getItemsColorOption } from '@/utils/litegraphUtil'
76 :
77 112 : const { t } = useI18n()
78 112 : const canvasStore = useCanvasStore()
79 112 : const themeStore = useThemeStore()
80 112 : const workflowStore = useWorkflowStore()
81 112 : const isLightTheme = computed(() => themeStore.colorScheme === 'light')
82 :
83 112 : const showColorPicker = ref(false)
84 :
85 : type ColorOption = {
86 : name: string
87 : localizedName: string
88 : value: {
89 : dark: string
90 : light: string
91 : }
92 : }
93 :
94 112 : const NO_COLOR_OPTION: ColorOption = {
95 : name: 'noColor',
96 : localizedName: t('color.noColor'),
97 : value: {
98 : dark: LiteGraph.NODE_DEFAULT_BGCOLOR,
99 : light: toLightThemeNodeColor(LiteGraph.NODE_DEFAULT_BGCOLOR)
100 : }
101 : }
102 112 : const colorOptions: ColorOption[] = [
103 : NO_COLOR_OPTION,
104 : ...Object.entries(LGraphCanvas.node_colors).map(([name, color]) => ({
105 : name,
106 : localizedName: t(`color.${name}`),
107 : value: {
108 : dark: color.bgcolor,
109 : light: toLightThemeNodeColor(color.bgcolor)
110 : }
111 : }))
112 : ]
113 :
114 112 : const selectedColorOption = ref<ColorOption | null>(null)
115 112 : const applyColorName = (value: unknown) => {
116 8 : if (typeof value === 'string') {
117 8 : applyColor(colorOptions.find((option) => option.name === value) ?? null)
118 : } else {
119 0 : applyColor(null)
120 : }
121 : }
122 112 : const applyColor = (colorOption: ColorOption | null) => {
123 8 : const colorName = colorOption?.name ?? NO_COLOR_OPTION.name
124 8 : const canvasColorOption =
125 : colorName === NO_COLOR_OPTION.name
126 : ? null
127 : : LGraphCanvas.node_colors[colorName]
128 :
129 8 : for (const item of canvasStore.selectedItems) {
130 11 : if (isColorable(item)) {
131 11 : item.setColorOption(canvasColorOption)
132 : }
133 : }
134 :
135 8 : canvasStore.canvas?.setDirty(true, true)
136 8 : selectedColorOption.value = colorOption
137 8 : currentColorOption.value = canvasColorOption
138 8 : showColorPicker.value = false
139 8 : workflowStore.activeWorkflow?.changeTracker?.captureCanvasState()
140 : }
141 :
142 112 : const currentColorOption = ref<CanvasColorOption | null>(null)
143 112 : const currentColor = computed(() =>
144 : currentColorOption.value
145 : ? isLightTheme.value
146 : ? toLightThemeNodeColor(currentColorOption.value?.bgcolor)
147 : : currentColorOption.value?.bgcolor
148 : : null
149 : )
150 :
151 112 : const localizedCurrentColorName = computed(() => {
152 128 : if (!currentColorOption.value?.bgcolor) return null
153 : const colorOption = colorOptions.find(
154 : (option) =>
155 : option.value.dark === currentColorOption.value?.bgcolor ||
156 : option.value.light === currentColorOption.value?.bgcolor
157 : )
158 128 : return colorOption?.localizedName ?? NO_COLOR_OPTION.localizedName
159 : })
160 112 : const updateColorSelectionFromNode = (
161 : newSelectedItems: Raw<Positionable[]>
162 : ) => {
163 193 : showColorPicker.value = false
164 193 : selectedColorOption.value = null
165 193 : currentColorOption.value = getItemsColorOption(newSelectedItems)
166 : }
167 112 : useEventListener(
168 : document,
169 : 'litegraph:canvas',
170 : (event: LiteGraphCanvasEvent) => {
171 44 : if (event.detail.subType === 'after-change') {
172 22 : updateColorSelectionFromNode(canvasStore.selectedItems)
173 : }
174 : }
175 : )
176 112 : watch(
177 : () => canvasStore.selectedItems,
178 : (newSelectedItems) => {
179 171 : updateColorSelectionFromNode(newSelectedItems)
180 : },
181 : { immediate: true }
182 : )
183 : </script>
|