Line data Source code
1 1 : <template>
2 : <div
3 : class="text-xs"
4 : :class="
5 : cn(
6 : WidgetInputBaseClass,
7 : 'flex w-full items-center justify-center gap-2 px-2',
8 : useWidgetHeight()
9 : )
10 : "
11 : :data-widget-name="widget.name"
12 : >
13 : <template v-if="resolution">
14 : <span data-testid="resolution-preview-value">
15 : {{ resolution.width }} × {{ resolution.height }}
16 : </span>
17 : <span class="text-muted-foreground">
18 : {{ resolution.mpLabel }}
19 : </span>
20 : </template>
21 : <span v-else class="text-muted-foreground"> — </span>
22 : </div>
23 : </template>
24 :
25 : <script setup lang="ts">
26 : import { computed } from 'vue'
27 : import { useI18n } from 'vue-i18n'
28 :
29 : import { useLiveGraphNode } from '@/composables/useLiveGraphNode'
30 : import type { IWidgetResolutionPreviewOptions } from '@comfyorg/graph-model/widgets'
31 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
32 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
33 : import type { SimplifiedWidget } from '@/types/simplifiedWidget'
34 : import { widgetId } from '@comfyorg/graph-model/widgetId'
35 : import { useWidgetHeight } from '@/types/widgetTypes'
36 : import { resolveNode } from '@/utils/litegraphUtil'
37 : import { cn } from '@comfyorg/tailwind-utils'
38 :
39 : import { WidgetInputBaseClass } from './layout'
40 :
41 : const { widget, nodeId } = defineProps<{
42 : widget: SimplifiedWidget<null, IWidgetResolutionPreviewOptions>
43 : nodeId?: NodeId
44 : }>()
45 :
46 2 : const { t } = useI18n()
47 2 : const widgetValueStore = useWidgetValueStore()
48 :
49 2 : const hostNode = useLiveGraphNode(() =>
50 : nodeId === undefined ? undefined : resolveNode(nodeId)
51 : )
52 :
53 12 : function siblingValue(name: string): unknown {
54 12 : const node = hostNode.value
55 12 : const graphId = node?.graph?.rootGraph.id
56 12 : if (!node || !graphId) return undefined
57 12 : return widgetValueStore.getWidget(widgetId(graphId, node.id, name))?.value
58 : }
59 :
60 : // Python round() ties to even; Math.round ties up.
61 8 : function roundHalfToEven(value: number): number {
62 8 : const floor = Math.floor(value)
63 8 : if (value - floor !== 0.5) return Math.round(value)
64 8 : return floor % 2 === 0 ? floor : floor + 1
65 : }
66 :
67 : // Mirrors ResolutionSelector.execute in comfy_extras/nodes_resolution.py —
68 : // keep the math in sync with the backend.
69 2 : const resolution = computed(() => {
70 4 : const ratioRaw = siblingValue(widget.options?.ratio_widget ?? 'aspect_ratio')
71 4 : const mpRaw = siblingValue(widget.options?.megapixels_widget ?? 'megapixels')
72 4 : const multipleRaw = siblingValue(
73 : widget.options?.multiple_widget ?? 'multiple'
74 : )
75 :
76 4 : const match =
77 : typeof ratioRaw === 'string'
78 : ? /^(\d+(?:\.\d+)?)\s*:\s*(\d+(?:\.\d+)?)/.exec(ratioRaw)
79 : : null
80 4 : const megapixels = typeof mpRaw === 'number' ? mpRaw : NaN
81 4 : const multiple =
82 : typeof multipleRaw === 'number' && multipleRaw > 0 ? multipleRaw : null
83 4 : if (
84 : !match ||
85 : !Number.isFinite(megapixels) ||
86 : megapixels <= 0 ||
87 : multiple === null
88 : ) {
89 0 : return null
90 : }
91 :
92 4 : const wRatio = Number(match[1])
93 4 : const hRatio = Number(match[2])
94 4 : if (!(wRatio > 0) || !(hRatio > 0)) return null
95 4 : const scale = Math.sqrt((megapixels * 1024 * 1024) / (wRatio * hRatio))
96 4 : const width = roundHalfToEven((wRatio * scale) / multiple) * multiple
97 4 : const height = roundHalfToEven((hRatio * scale) / multiple) * multiple
98 4 : if (!width || !height) return null
99 :
100 4 : return {
101 : width,
102 : height,
103 : mpLabel: t('g.megapixelsValue', {
104 : count: ((width * height) / (1024 * 1024)).toFixed(2)
105 : })
106 : }
107 : })
108 : </script>
|