Spaces:
Paused

assistant / ui /web /src /components /DownloadProgressPopup.svelte
localailb's picture
Deploy Full LocalAiLab Application (FastAPI + Svelte + Agents)
bc9f3e6 verified
Raw History Blame Contribute Delete
8.01 kB
<script>
import { app, t, cancelModelDownload } from "../lib/stores.svelte.js";
// Svelte 5 fine-grained reactivity: reading app.modelDl.progress inside
// an {#if} block doesn't create a separate reactive dependency. Using
// $derived ensures each property is tracked independently and triggers
// re-renders when it changes.
const running = $derived(app.modelDl.running);
const done = $derived(app.modelDl.done);
const error = $derived(app.modelDl.error);
const cancelled = $derived(app.modelDl.cancelled);
const progress = $derived(app.modelDl.progress);
const modelLabel = $derived(app.modelDl.modelLabel);
const label = $derived(app.modelDl.label);
const dlMb = $derived(app.modelDl.dlMb);
const totalMb = $derived(app.modelDl.totalMb);
const speedMb = $derived(app.modelDl.speedMb);
const statusLine = $derived(app.modelDl.statusLine);
// ── Draggable ──────────────────────────────────────────────────
let boxEl = $state(null);
let dragOffsetX = 0;
let dragOffsetY = 0;
/** Dynamic left/top in px (null = use CSS bottom/right default). */
let posX = $state(null);
let posY = $state(null);
/** True while the user is actively dragging. */
let dragging = $state(false);
function onDragStart(e) {
if (!boxEl) return;
// Only left-click starts a drag.
if (e.button !== 0) return;
e.preventDefault();
const rect = boxEl.getBoundingClientRect();
dragOffsetX = e.clientX - rect.left;
dragOffsetY = e.clientY - rect.top;
// Switch from CSS bottom/right to explicit left/top so we can move.
if (posX === null) posX = rect.left;
if (posY === null) posY = rect.top;
dragging = true;
}
function onDragMove(e) {
if (!dragging) return;
const w = window.innerWidth;
const h = window.innerHeight;
let x = e.clientX - dragOffsetX;
let y = e.clientY - dragOffsetY;
// Clamp so the box stays fully visible.
const bw = boxEl ? boxEl.offsetWidth : 400;
const bh = boxEl ? boxEl.offsetHeight : 200;
x = Math.max(0, Math.min(x, w - bw));
y = Math.max(0, Math.min(y, h - bh));
posX = x;
posY = y;
}
function onDragEnd() {
dragging = false;
}
// Reset position when the popup is dismissed (auto-hide timers clear it).
$effect(() => {
if (!running && !done && !error && !cancelled) {
posX = null;
posY = null;
}
});
</script>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<svelte:window
onmousemove={onDragMove}
onmouseup={onDragEnd}
/>
{#if running || done || error || cancelled}
<div
class="dl-popup-overlay"
style="{posX !== null ? `left:${posX}px;right:auto` : ''}; {posY !== null ? `top:${posY}px;bottom:auto` : ''}"
>
<div
class="dl-popup-box"
class:dragging
role="dialog"
aria-modal="true"
bind:this={boxEl}
>
<!-- Header (drag handle) -->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="dl-popup-header dl-popup-drag-handle"
role="none"
onmousedown={onDragStart}
>
<span class="dl-popup-icon">
{#if running}⏳{:else if done}βœ…{:else if cancelled}β›”{:else if error}❌{:else}πŸ“₯{/if}
</span>
<span class="dl-popup-title">
{#if running}
{t("label_model_download_progress")}
{:else if done}
{t("web_model_download_done")}
{:else if cancelled}
{t("web_model_download_cancelled")}
{:else if error}
{t("web_model_download_error")}
{/if}
</span>
{#if running}
<button class="dl-popup-cancel" type="button" onclick={() => cancelModelDownload()} data-tooltip={t("web_model_download_cancel")}>
{t("web_cancel")}
</button>
{/if}
</div>
<!-- Model name -->
<div class="dl-popup-model">
{label || modelLabel || "β€”"}
</div>
<!-- Progress bar -->
{#if running}
<div class="dl-popup-track">
<div class="dl-popup-fill" style="width: {progress ?? 0}%"></div>
</div>
<div class="dl-popup-stats">
<span class="dl-popup-pct">{progress ?? 0}%</span>
{#if dlMb && totalMb}
<span class="dl-popup-mb">{dlMb} / {totalMb} MB</span>
{/if}
{#if speedMb}
<span class="dl-popup-speed">({speedMb} MB/s)</span>
{/if}
</div>
{#if statusLine}
<div class="dl-popup-status">{statusLine}</div>
{/if}
{:else if done}
<div class="dl-popup-complete">
{statusLine || t("web_model_download_done")}
</div>
{:else if cancelled}
<div class="dl-popup-cancelled">
{statusLine || t("web_model_download_cancelled")}
</div>
{:else if error}
<div class="dl-popup-error">
{error}
</div>
{/if}
</div>
</div>
{/if}
<style>
.dl-popup-overlay {
position: fixed;
bottom: 24px;
right: 24px;
z-index: 2000;
pointer-events: none;
}
.dl-popup-box {
pointer-events: auto;
min-width: 320px;
max-width: 440px;
user-select: none;
padding: 16px 18px;
border-radius: 14px;
background: var(--card-bg, #f5f0e8);
border: 1px solid var(--primary-300, #f0b49e);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18), 0 2px 8px rgba(224, 138, 106, 0.15);
animation: dl-slide-in 0.3s ease-out;
}
@keyframes dl-slide-in {
from { opacity: 0; transform: translateY(16px); }
to { opacity: 1; transform: translateY(0); }
}
.dl-popup-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
}
.dl-popup-icon {
font-size: 1.2rem;
}
.dl-popup-title {
font-size: 0.85rem;
font-weight: 700;
color: var(--text-strong, #3f332b);
}
.dl-popup-model {
font-size: 0.8rem;
color: var(--text-muted, #8a7e72);
margin-bottom: 10px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dl-popup-track {
height: 8px;
background: var(--card-border, #e7e0d3);
border-radius: 4px;
overflow: hidden;
margin-bottom: 8px;
}
.dl-popup-fill {
height: 100%;
background: linear-gradient(90deg, var(--primary-500, #e08a6a), var(--primary-600, #d97757));
border-radius: 4px;
transition: width 0.3s ease;
}
.dl-popup-stats {
display: flex;
align-items: center;
gap: 8px;
font-size: 0.78rem;
}
.dl-popup-pct {
font-weight: 700;
color: var(--primary-600, #d97757);
}
.dl-popup-mb {
color: var(--text-strong, #222);
font-weight: 600;
}
.dl-popup-speed {
color: var(--primary-600, #d97757);
font-weight: 600;
}
.dl-popup-status {
font-size: 0.75rem;
color: var(--text-muted, #888);
margin-top: 4px;
}
.dl-popup-complete {
font-size: 0.82rem;
font-weight: 600;
color: #2d8a4e;
padding: 4px 0;
}
.dl-popup-error {
font-size: 0.82rem;
font-weight: 600;
color: #b4453a;
padding: 4px 0;
}
.dl-popup-drag-handle {
cursor: grab;
}
.dl-popup-drag-handle:active {
cursor: grabbing;
}
.dl-popup-cancel {
margin-left: auto;
user-select: auto;
padding: 4px 12px;
border-radius: 8px;
border: 1px solid var(--primary-300, #f0b49e);
background: var(--primary-500, #e08a6a);
color: #fff;
font-size: 0.75rem;
font-weight: 700;
cursor: pointer;
transition: background 0.2s ease, transform 0.1s ease;
}
.dl-popup-cancel:hover {
background: var(--primary-600, #d97757);
}
.dl-popup-cancel:active {
transform: scale(0.96);
}
.dl-popup-box.dragging .dl-popup-header {
cursor: grabbing;
}
.dl-popup-cancelled {
font-size: 0.82rem;
font-weight: 600;
color: #8a6d3b;
padding: 4px 0;
}
</style>