Line data Source code
1 16 : <template>
2 : <WidgetLayoutField
3 : v-slot="{ borderStyle }"
4 : :widget
5 : :input-id
6 : :no-border="!hasLabels"
7 : :root-class="cn(hasLabels && 'h-auto')"
8 : >
9 : <ToggleGroup
10 : v-if="hasLabels"
11 : type="single"
12 : :model-value="modelValue ? 'on' : 'off'"
13 : :disabled="Boolean(widget.options?.read_only)"
14 : :class="cn(WidgetInputBaseClass, 'w-full min-w-0 p-1')"
15 : @update:model-value="(v) => handleOptionChange(v as string)"
16 : >
17 : <ToggleGroupItem
18 : :id="modelValue ? inputId : undefined"
19 : value="off"
20 : :aria-label="widget.options?.off ?? t('widgets.boolean.false')"
21 : size="sm"
22 : class="flex-1 truncate"
23 : >
24 : {{ widget.options?.off ?? t('widgets.boolean.false') }}
25 : </ToggleGroupItem>
26 : <ToggleGroupItem
27 : :id="modelValue ? undefined : inputId"
28 : value="on"
29 : :aria-label="widget.options?.on ?? t('widgets.boolean.true')"
30 : size="sm"
31 : class="flex-1 truncate"
32 : >
33 : {{ widget.options?.on ?? t('widgets.boolean.true') }}
34 : </ToggleGroupItem>
35 : </ToggleGroup>
36 :
37 : <div
38 : v-else
39 : :class="
40 : cn(
41 : '-m-1 flex w-fit items-center gap-2 rounded-full p-1',
42 : hideLayoutField || 'ml-auto',
43 : borderStyle
44 : )
45 : "
46 : >
47 : <Switch
48 : :id="inputId"
49 : :model-value
50 : :disabled="Boolean(widget.options?.disabled)"
51 : :readonly="Boolean(widget.options?.read_only)"
52 : :aria-label="widget.label || widget.name"
53 : @update:model-value="updateValue"
54 : />
55 : </div>
56 : </WidgetLayoutField>
57 : </template>
58 :
59 : <script setup lang="ts">
60 : import { computed, useId } from 'vue'
61 : import { useI18n } from 'vue-i18n'
62 :
63 : import Switch from '@comfyorg/design-system/switch/Switch'
64 : import ToggleGroup from '@comfyorg/design-system/toggle-group/ToggleGroup'
65 : import ToggleGroupItem from '@comfyorg/design-system/toggle-group/ToggleGroupItem'
66 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
67 : import type { IWidgetOptions } from '@comfyorg/graph-model/widgets'
68 : import type { SimplifiedWidget } from '@/types/simplifiedWidget'
69 : import { useHideLayoutField } from '@/types/widgetTypes'
70 : import { cn } from '@comfyorg/tailwind-utils'
71 :
72 : import { WidgetInputBaseClass } from './layout'
73 : import WidgetLayoutField from './layout/WidgetLayoutField.vue'
74 :
75 : const { widget } = defineProps<{
76 : widget: SimplifiedWidget<boolean, IWidgetOptions>
77 : }>()
78 :
79 187 : const modelValue = defineModel<boolean>()
80 187 : const inputId = useId()
81 :
82 187 : const hideLayoutField = useHideLayoutField()
83 187 : const { t } = useI18n()
84 :
85 187 : const hasLabels = computed(() => {
86 410 : return widget.options?.on != null || widget.options?.off != null
87 : })
88 :
89 1 : function handleOptionChange(value: string | undefined) {
90 1 : if (value) {
91 1 : updateValue(value === 'on')
92 : }
93 : }
94 :
95 11 : function updateValue(value: boolean) {
96 11 : modelValue.value = value
97 11 : useWorkflowStore().activeWorkflow?.changeTracker.captureCanvasState()
98 : }
99 : </script>
|