Line data Source code
1 : <script setup lang="ts">
2 : import { nextTick, ref, watch } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { cn } from '@comfyorg/tailwind-utils'
6 :
7 : import WorkflowTemplateDownloadFailure from '@/platform/workflow/templates/components/WorkflowTemplateDownloadFailure.vue'
8 : import Badge from '@comfyorg/design-system/badge/Badge'
9 : import Button from '@comfyorg/design-system/button/Button'
10 : import type { TemplateDetailRow } from '@/platform/workflow/templates/types/templateDetail'
11 : import type { TemplateModelDownloadState } from '@/platform/workflow/templates/utils/templateModelDownloadState'
12 : import { formatSize } from '@/utils/formatUtil'
13 :
14 : type DownloadableStatus = Extract<
15 : TemplateDetailRow['status'],
16 : { kind: 'downloadable' }
17 : >
18 :
19 : const { status, rowName } = defineProps<{
20 : status: DownloadableStatus
21 : rowName: string
22 : }>()
23 2355 : const emit = defineEmits<{ download: [] }>()
24 0 : const { t } = useI18n()
25 :
26 : /**
27 : * The control unmounts on request, dropping focus to the body. Hand it to what
28 : * replaces it, but only when the user acted here.
29 : */
30 0 : const statusRegion = ref<HTMLElement | null>(null)
31 0 : const restoreFocus = ref(false)
32 :
33 0 : function requestDownload() {
34 0 : restoreFocus.value = true
35 0 : emit('download')
36 : }
37 :
38 0 : watch(
39 : () => status.downloadState?.status,
40 : async () => {
41 0 : if (!restoreFocus.value) return
42 0 : restoreFocus.value = false
43 0 : await nextTick()
44 0 : statusRegion.value?.focus()
45 : }
46 : )
47 :
48 : type DownloadingState = Extract<
49 : TemplateModelDownloadState,
50 : { status: 'downloading' }
51 : >
52 :
53 0 : function getPassiveDownloadLabel(
54 : state: TemplateModelDownloadState | undefined
55 : ): string | undefined {
56 0 : if (state?.status === 'queued') {
57 0 : return t('templateWorkflows.detail.downloadQueued')
58 : }
59 0 : if (state?.status === 'starting') {
60 0 : return t('templateWorkflows.detail.downloadStarting')
61 : }
62 : }
63 :
64 0 : function getProgressPercent(state: DownloadingState): number | undefined {
65 0 : if (state.fraction === null) return undefined
66 0 : return Math.round(Math.min(1, Math.max(0, state.fraction)) * 100)
67 : }
68 :
69 0 : function getKnownProgressText(state: DownloadingState): string | undefined {
70 0 : if (state.receivedBytes !== null && state.totalBytes !== null) {
71 0 : return `${formatSize(state.receivedBytes)} / ${formatSize(state.totalBytes)}`
72 : }
73 0 : if (state.receivedBytes !== null) return formatSize(state.receivedBytes)
74 0 : if (state.totalBytes !== null) return formatSize(state.totalBytes)
75 : }
76 :
77 0 : function getProgressText(state: DownloadingState): string {
78 0 : const knownProgress = getKnownProgressText(state)
79 0 : if (state.activity !== 'paused') {
80 0 : return knownProgress ?? t('templateWorkflows.detail.downloading')
81 : }
82 0 : return knownProgress
83 : ? t(
84 : 'templateWorkflows.detail.downloadPausedProgress',
85 : { progress: knownProgress },
86 : // Byte counts composed with a separator; escaping turns the slash
87 : // into an entity in both the label and aria-valuetext.
88 : { escapeParameter: false }
89 : )
90 : : t('templateWorkflows.detail.downloadPaused')
91 : }
92 :
93 0 : function namedLabel(key: string): string {
94 0 : return t(key, { model: rowName }, { escapeParameter: false })
95 : }
96 0 : </script>
97 :
98 : <template>
99 : <Button
100 0 : v-if="!status.downloadState || status.downloadState.status === 'idle'"
101 : :aria-label="namedLabel('templateWorkflows.detail.downloadModelNamed')"
102 : :title="status.label"
103 : variant="textonly"
104 : size="unset"
105 : class="col-start-3 row-start-1 size-8 shrink-0 p-1.5"
106 : @click="requestDownload()"
107 : >
108 : <i aria-hidden="true" class="icon-[tabler--download]" />
109 : </Button>
110 : <span
111 : v-else-if="
112 : status.downloadState.status === 'queued' ||
113 : status.downloadState.status === 'starting'
114 : "
115 : ref="statusRegion"
116 : role="status"
117 : tabindex="-1"
118 : class="col-[2/-1] row-start-2 flex min-w-0 items-center gap-3 text-xs text-muted-foreground focus-visible:outline-none"
119 : >
120 : <span
121 : aria-hidden="true"
122 : class="h-1 min-w-0 flex-1 overflow-hidden rounded-full bg-secondary"
123 : >
124 : <span
125 : class="indeterminate-stripe indeterminate-stripe--drifting block h-full w-full rounded-full text-primary"
126 : />
127 : </span>
128 : <span class="shrink-0">
129 : {{ getPassiveDownloadLabel(status.downloadState) }}
130 : </span>
131 : </span>
132 : <span
133 : v-else-if="status.downloadState.status === 'downloading'"
134 : ref="statusRegion"
135 : tabindex="-1"
136 : class="col-[2/-1] row-start-2 flex min-w-0 items-center gap-3 focus-visible:outline-none"
137 : >
138 : <span
139 : role="progressbar"
140 : :aria-label="
141 : namedLabel(
142 : status.downloadState.activity === 'paused'
143 : ? 'templateWorkflows.detail.downloadPausedModel'
144 : : 'templateWorkflows.detail.downloadingModel'
145 : )
146 : "
147 : aria-valuemin="0"
148 : aria-valuemax="100"
149 : :aria-valuenow="getProgressPercent(status.downloadState)"
150 : :aria-valuetext="getProgressText(status.downloadState)"
151 : class="block h-1 min-w-0 flex-1 overflow-hidden rounded-full bg-secondary"
152 : >
153 : <span
154 : :class="
155 : cn(
156 : 'block h-full rounded-full',
157 : status.downloadState.fraction === null
158 : ? 'indeterminate-stripe w-full text-primary'
159 : : 'bg-primary',
160 : status.downloadState.fraction === null &&
161 : status.downloadState.activity === 'active' &&
162 : 'indeterminate-stripe--drifting'
163 : )
164 : "
165 : :style="
166 : getProgressPercent(status.downloadState) === undefined
167 : ? undefined
168 : : {
169 : width: `${getProgressPercent(status.downloadState)}%`
170 : }
171 : "
172 : />
173 : </span>
174 : <span class="shrink-0 text-xs text-muted-foreground">
175 : {{ getProgressText(status.downloadState) }}
176 : </span>
177 : </span>
178 : <Badge
179 : v-else-if="status.downloadState.status === 'done'"
180 : role="status"
181 : :aria-label="t('templateWorkflows.detail.downloaded')"
182 : variant="compact"
183 : severity="success"
184 : class="col-start-3 row-start-1"
185 : >
186 : {{ t('templateWorkflows.detail.downloaded') }}
187 : </Badge>
188 : <WorkflowTemplateDownloadFailure
189 : v-else
190 : :state="status.downloadState"
191 : :row-name="rowName"
192 : @retry="requestDownload()"
193 : />
194 : </template>
195 :
196 : <style scoped>
197 : /* Tiled, so one tile of travel loops seamlessly. */
198 : @keyframes template-download-stripe-drift {
199 : to {
200 : background-position: 0.75rem 0;
201 : }
202 : }
203 :
204 : .indeterminate-stripe {
205 : background-image: repeating-linear-gradient(
206 : 135deg,
207 : currentColor 0 25%,
208 : transparent 25% 50%
209 : );
210 : background-size: 0.75rem 0.75rem;
211 : }
212 :
213 : .indeterminate-stripe--drifting {
214 : animation: template-download-stripe-drift 0.7s linear infinite;
215 : }
216 :
217 : /*
218 : * Switched off by hand, as `agent-shimmer-outline` does: the global rule only
219 : * collapses duration, which accelerates an infinite animation rather than
220 : * stopping it. The stripe still reads as indeterminate while held still.
221 : */
222 : .disable-animations .indeterminate-stripe--drifting {
223 : animation: none;
224 : }
225 :
226 : @media (prefers-reduced-motion: reduce) {
227 : .indeterminate-stripe--drifting {
228 : animation: none;
229 : }
230 : }
231 : </style>
|