Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: drag an image from one slot to another (#15876)
Nilay2be82500304f
diff --git a/studio/frontend/src/components/image-dropzone.tsx b/studio/frontend/src/components/image-dropzone.tsxindex d054f89ee3b..779ca1d88e6 100644--- a/studio/frontend/src/components/image-dropzone.tsx+++ b/studio/frontend/src/components/image-dropzone.tsx@@ -18,13 +18,18 @@ import {useNativeDropTarget,} from "@/features/native-intents";import { useImageCrop } from "@/hooks/use-image-crop";+import {+ IMAGE_SLOT_DRAG_TYPE,+ classifyNativeImageDrop,+ draggedImageSlot,+ endImageSlotDrag,+ startImageSlotDrag,+} from "@/components/image-slot-drag";import { GALLERY_ITEM_DRAG_TYPE } from "@/hooks/use-strip-reorder";import { MAX_UPLOAD_IMAGE_SIDE, fitImageDataUrl } from "@/lib/image-fit";import { cn } from "@/lib/utils";import { toast } from "@/lib/toast";-const NATIVE_IMAGE_EXTS = ["jpg", "jpeg", "png", "webp", "gif"];-type GalleryImageSource = (id: string) => Promise<Blob | null>;let gallerySource: GalleryImageSource | null = null;@@ -42,6 +47,10 @@ function isGalleryDrag(event: React.DragEvent): boolean {return gallerySource !== null && event.dataTransfer.types.includes(GALLERY_ITEM_DRAG_TYPE);}+function isSlotDrag(event: React.DragEvent): boolean {+ return draggedImageSlot() !== null && event.dataTransfer.types.includes(IMAGE_SLOT_DRAG_TYPE);+}+/** Shared image picker that returns a data URL. */export function ImageDropzone({value,@@ -129,10 +138,23 @@ export function ImageDropzone({// that the native side registers and reads for this picker.const readNativePath = useCallback(async (path: string | undefined) => {+ const kind = classifyNativeImageDrop(path);+ const slotImage = kind === "none" || kind === "in-app" ? draggedImageSlot() : null;+ if (slotImage !== null) {+ selection.current += 1;+ onChange(slotImage);+ return;+ }if (!path) return;- if (!NATIVE_IMAGE_EXTS.includes(path.split(".").pop()?.toLowerCase() ?? "")) {- toast.error("Drop a JPEG, PNG, WebP or GIF image", {- description: "Other image formats can still be chosen with the picker.",+ if (kind === "in-app") {+ toast.error("Can't drop this image here", {+ description: "Click to pick the image file instead.",+ });+ return;+ }+ if (kind === "unsupported") {+ toast.error("This image type can't be dropped here", {+ description: "Drop a JPEG, PNG, WebP or GIF, or click to choose other formats.",});return;}@@ -173,11 +195,21 @@ export function ImageDropzone({return (<divdata-reload-snapshot-sensitive+ draggable={true}+ onDragStart={(e) => {+ if (!e.currentTarget.contains(e.target as Node)) return;+ startImageSlotDrag(value);+ e.dataTransfer.effectAllowed = "copy";+ e.dataTransfer.setData(IMAGE_SLOT_DRAG_TYPE, "");+ }}+ onDragEnd={() => endImageSlotDrag()}className={cn("relative overflow-hidden rounded-lg border border-border", className)}>- {/* Checkerboard behind the preview, so a transparent upload shows its alpha. */}+ {/* Checkerboard behind the preview, so a transparent upload shows its alpha.+ Not draggable itself, or the OS would read the drag as a dropped file. */}<imgsrc={value}+ draggable={false}alt="Source"className="max-h-44 w-full object-contain bg-muted/30"style={{@@ -237,13 +269,19 @@ export function ImageDropzone({onDragOver={(e) => {e.preventDefault();// The strip offers copy or move; without this the cursor reads as a move.- if (isGalleryDrag(e)) e.dataTransfer.dropEffect = "copy";+ if (isGalleryDrag(e) || isSlotDrag(e)) e.dataTransfer.dropEffect = "copy";setDragging(true);}}onDragLeave={() => setDragging(false)}onDrop={(e) => {e.preventDefault();setDragging(false);+ const slotImage = isSlotDrag(e) ? draggedImageSlot() : null;+ if (slotImage) {+ selection.current += 1;+ onChange(slotImage);+ return;+ }const galleryId = isGalleryDrag(e) ? e.dataTransfer.getData(GALLERY_ITEM_DRAG_TYPE) : "";if (galleryId) void readGalleryItem(galleryId);else readFile(e.dataTransfer.files?.[0]);diff --git a/studio/frontend/src/components/image-slot-drag.ts b/studio/frontend/src/components/image-slot-drag.tsnew file mode 100644index 00000000000..d09ebaa4867--- /dev/null+++ b/studio/frontend/src/components/image-slot-drag.ts@@ -0,0 +1,53 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++export const IMAGE_SLOT_DRAG_TYPE = "application/x-unsloth-image-slot";++const NATIVE_IMAGE_EXTS = ["jpg", "jpeg", "png", "webp", "gif"];++// The desktop shell reports the drop natively, after dragend.+const NATIVE_DROP_GRACE_MS = 1000;++let draggedSlotImage: string | null = null;+let endedAt: number | null = null;++// Capture runs before the slot's own onDragStart re-arms it.+if (typeof document !== "undefined") {+ document.addEventListener(+ "dragstart",+ () => {+ draggedSlotImage = null;+ endedAt = null;+ },+ true,+ );+}++export function startImageSlotDrag(dataUrl: string): void {+ draggedSlotImage = dataUrl;+ endedAt = null;+}++export function endImageSlotDrag(now = Date.now()): void {+ endedAt = now;+}++export function draggedImageSlot(now = Date.now()): string | null {+ if (endedAt !== null && now - endedAt > NATIVE_DROP_GRACE_MS) {+ draggedSlotImage = null;+ endedAt = null;+ }+ return draggedSlotImage;+}++export type NativeImageDrop = "none" | "image" | "in-app" | "unsupported";++export function classifyNativeImageDrop(+ path: string | undefined,+): NativeImageDrop {+ if (!path) return "none";+ // An image dragged out of the page reaches the native handler as its URL, not a file.+ if (/^(data|blob|https?):/i.test(path)) return "in-app";+ const ext = path.split(".").pop()?.toLowerCase() ?? "";+ return NATIVE_IMAGE_EXTS.includes(ext) ? "image" : "unsupported";+}diff --git a/studio/frontend/tests/image-slot-drag.test.ts b/studio/frontend/tests/image-slot-drag.test.tsnew file mode 100644index 00000000000..c9d3759b75a--- /dev/null+++ b/studio/frontend/tests/image-slot-drag.test.ts@@ -0,0 +1,100 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";++import {+ classifyNativeImageDrop,+ draggedImageSlot,+ endImageSlotDrag,+ startImageSlotDrag,+} from "../src/components/image-slot-drag.ts";+import { readSrc } from "./helpers/kit.ts";++const IMAGE_DROPZONE = readSrc("components/image-dropzone.tsx");+const SLOT_DRAG = "../src/components/image-slot-drag.ts";++test("an image dragged out of the page is not reported as a bad file format", () => {+ for (const path of [+ "data:image/png;base64,iVBORw0KGgo=",+ "blob:http://127.0.0.1:8888/1f2e",+ "http://127.0.0.1:8888/api/images/out.png",+ ]) {+ assert.equal(classifyNativeImageDrop(path), "in-app", path);+ }+});++test("real files are classified by extension", () => {+ assert.equal(classifyNativeImageDrop("/home/me/cat.PNG"), "image");+ assert.equal(classifyNativeImageDrop("C:\\Users\\me\\cat.webp"), "image");+ assert.equal(classifyNativeImageDrop("/home/me/cat.heic"), "unsupported");+ assert.equal(classifyNativeImageDrop("/home/me/notes"), "unsupported");+ assert.equal(classifyNativeImageDrop(undefined), "none");+});++test("a slot image outlives dragend just long enough for the native drop", () => {+ startImageSlotDrag("data:image/png;base64,AAAA");+ assert.equal(draggedImageSlot(), "data:image/png;base64,AAAA");+ endImageSlotDrag(1_000);+ assert.equal(draggedImageSlot(1_500), "data:image/png;base64,AAAA");+ assert.equal(draggedImageSlot(2_500), null);+ assert.equal(draggedImageSlot(1_500), null);+});++test("a new drag replaces the previous slot image", () => {+ startImageSlotDrag("data:image/png;base64,AAAA");+ endImageSlotDrag(1_000);+ startImageSlotDrag("data:image/png;base64,BBBB");+ assert.equal(draggedImageSlot(9_000), "data:image/png;base64,BBBB");+ endImageSlotDrag(0);+ draggedImageSlot(10_000);+});++test("a filled slot drags its image and an empty slot copies it in", () => {+ assert.match(+ IMAGE_DROPZONE,+ /draggable=\{true\}[\s\S]*?startImageSlotDrag\(value\)/,+ );+ assert.match(IMAGE_DROPZONE, /onDragEnd=\{\(\) => endImageSlotDrag\(\)\}/);+ assert.match(IMAGE_DROPZONE, /<img\s+src=\{value\}\s+draggable=\{false\}/);+ assert.match(+ IMAGE_DROPZONE,+ /onDrop=\{\(e\) => \{[\s\S]*?isSlotDrag\(e\) \? draggedImageSlot\(\) : null;[\s\S]*?onChange\(slotImage\)/,+ );+});++test("a native drop with no path or an in-app URL takes the dragged slot image", () => {+ assert.match(+ IMAGE_DROPZONE,+ /const kind = classifyNativeImageDrop\(path\);\s*const slotImage =\s*kind === "none" \|\| kind === "in-app" \? draggedImageSlot\(\) : null;[\s\S]*?onChange\(slotImage\);\s*return;\s*\}\s*if \(!path\) return;/,+ );+});++test("an in-app native drop never shows the file format error", () => {+ assert.doesNotMatch(+ IMAGE_DROPZONE,+ /Other image formats can still be chosen with the picker/,+ );+ assert.match(+ IMAGE_DROPZONE,+ /if \(kind === "in-app"\) \{\s*toast\.error\("Can't drop this image here"/,+ );+});++// macOS drops arrive pathless, so a stale slot image must not claim the next drag.+test("any other drag drops the slot image", async () => {+ const doc = new EventTarget();+ Object.assign(globalThis, { document: doc });+ const slot = (await import(+ `${SLOT_DRAG}?other-drag`+ )) as typeof import("../src/components/image-slot-drag.ts");+ slot.startImageSlotDrag("data:image/png;base64,AAAA");+ slot.endImageSlotDrag(1_000);+ doc.dispatchEvent(new Event("dragstart"));+ assert.equal(slot.draggedImageSlot(1_200), null);+ doc.dispatchEvent(new Event("dragstart"));+ slot.startImageSlotDrag("data:image/png;base64,BBBB");+ assert.equal(slot.draggedImageSlot(), "data:image/png;base64,BBBB");+ Reflect.deleteProperty(globalThis, "document");+});diff --git a/studio/frontend/tests/native-drop-targets.test.ts b/studio/frontend/tests/native-drop-targets.test.tsindex 01f74e1001b..44c95921682 100644--- a/studio/frontend/tests/native-drop-targets.test.ts+++ b/studio/frontend/tests/native-drop-targets.test.ts@@ -35,6 +35,7 @@ const { setAppliedInterfaceZoom } = await import();const IMAGE_DROPZONE = readSrc("components/image-dropzone.tsx");+const IMAGE_SLOT_DRAG = readSrc("components/image-slot-drag.ts");const USE_NATIVE_DROP = readSrc("features/native-intents/use-native-drop.ts");const asElement = (value: FakeElement) => value as unknown as HTMLElement;@@ -195,7 +196,8 @@ test("the shared image picker owns native drops and ignores stale reads", asyncassert.match(IMAGE_DROPZONE, /readNativeAttachmentFile\(intent\.path\.token\)/);// A read outliving the picker would land on whoever holds onChange now.assert.match(IMAGE_DROPZONE, /if \(!mounted\.current \|\| claimed !== selection\.current\) return;/);- assert.match(IMAGE_DROPZONE, /NATIVE_IMAGE_EXTS\.includes\(/);+ assert.match(IMAGE_DROPZONE, /classifyNativeImageDrop\(path\)/);+ assert.match(IMAGE_SLOT_DRAG, /NATIVE_IMAGE_EXTS\.includes\(/);// Index-keyed reference slots keep the picker mounted when one is removed.assert.match(IMAGE_DROPZONE, /if \(seen\.current === value\) return;\s*seen\.current = value;\s*selection\.current \+= 1;/);});@@ -208,7 +210,7 @@ test("the picker's droppable formats are ones the native path policy admits", as.sort();const admitted = listed(rust, /IMAGE_ATTACHMENT_EXTS:\s*&\[&str\]\s*=\s*&\[([^\]]+)\]/);- const droppable = listed(IMAGE_DROPZONE, /NATIVE_IMAGE_EXTS\s*=\s*\[([^\]]+)\]/);+ const droppable = listed(IMAGE_SLOT_DRAG, /NATIVE_IMAGE_EXTS\s*=\s*\[([^\]]+)\]/);assert.ok(droppable.length > 0);assert.deepEqual(droppable.filter((ext) => !admitted.includes(ext)), []);});