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: bake the blurred chat wallpaper once instead of re-running the blur on every scroll frame (#17253)
Daniel Han81b7720e1602
diff --git a/studio/frontend/src/features/chat-background/artwork-geometry.ts b/studio/frontend/src/features/chat-background/artwork-geometry.tsnew file mode 100644index 00000000000..af2bcd7d824--- /dev/null+++ b/studio/frontend/src/features/chat-background/artwork-geometry.ts@@ -0,0 +1,37 @@+// 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 { type ImageSizing, parsePosition } from "./model.ts";++/** Where CSS paints a no-repeat background of an iw x ih image in a w x h box (CSS px). */+export function imageBox(+ w: number,+ h: number,+ iw: number,+ ih: number,+ sizing: ImageSizing,+ position: string,+): { x: number; y: number; w: number; h: number } {+ let dw = iw,+ dh = ih;+ if (sizing === "cover" || sizing === "contain") {+ const s = (sizing === "cover" ? Math.max : Math.min)(w / iw, h / ih);+ dw = iw * s;+ dh = ih * s;+ } else if (sizing === "width") {+ dw = w;+ dh = (ih * w) / iw;+ } else if (sizing === "height") {+ dh = h;+ dw = (iw * h) / ih;+ } else if (sizing === "stretch") {+ dw = w;+ dh = h;+ }+ const [px, py] = parsePosition(position) ?? [50, 50];+ return { x: ((w - dw) * px) / 100, y: ((h - dh) * py) / 100, w: dw, h: dh };+}++/** Canvas px per CSS px: a wide blur keeps no fine detail, so it is baked at a lower resolution. */+export function bakeScale(blur: number, dpr: number): number {+ return Math.min(Math.max(dpr, 1), Math.max(0.25, 8 / blur));+}diff --git a/studio/frontend/src/features/chat-background/artwork.tsx b/studio/frontend/src/features/chat-background/artwork.tsxnew file mode 100644index 00000000000..d43fc0725e2--- /dev/null+++ b/studio/frontend/src/features/chat-background/artwork.tsx@@ -0,0 +1,166 @@+// 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 { useEffect, useRef, useState, type CSSProperties } from "react";+import type { ImageSizing } from "./model";+import { bakeScale, imageBox } from "./artwork-geometry";++const BACKGROUND_SIZE: Record<ImageSizing, string> = {+ cover: "cover",+ contain: "contain",+ width: "100% auto",+ height: "auto 100%",+ stretch: "100% 100%",+ original: "auto",+};+// A resize or a slider drag keeps the live filter until the inputs hold this long.+const SETTLE_MS = 160;++const canvasFilter =+ typeof CanvasRenderingContext2D !== "undefined" &&+ "filter" in CanvasRenderingContext2D.prototype;++/** The box exactly as CSS paints it (clipped to the box), then blurred the way `filter: blur()` blurs it. */+async function bake(+ url: string,+ box: { w: number; h: number },+ sizing: ImageSizing,+ position: string,+ blur: number,+): Promise<Blob | null> {+ const image = new Image();+ image.src = url;+ await image.decode();+ const k = bakeScale(blur, window.devicePixelRatio || 1);+ const cw = Math.max(1, Math.round(box.w * k)),+ ch = Math.max(1, Math.round(box.h * k));+ const at = imageBox(box.w, box.h, image.naturalWidth, image.naturalHeight, sizing, position);+ const plain = document.createElement("canvas");+ plain.width = cw;+ plain.height = ch;+ const pctx = plain.getContext("2d");+ if (!pctx) return null;+ pctx.imageSmoothingQuality = "high";+ pctx.drawImage(image, at.x * k, at.y * k, at.w * k, at.h * k);+ const out = document.createElement("canvas");+ out.width = cw;+ out.height = ch;+ const octx = out.getContext("2d");+ if (!octx) return null;+ octx.imageSmoothingQuality = "high";+ if (canvasFilter) {+ octx.filter = `blur(${blur * k}px)`;+ octx.drawImage(plain, 0, 0);+ } else {+ // Engines without a canvas filter (older WebKit): a downscale and smooth upscale reads as the same blur.+ const f = Math.min(1, 1.5 / (blur * k));+ const small = document.createElement("canvas");+ small.width = Math.max(1, Math.round(cw * f));+ small.height = Math.max(1, Math.round(ch * f));+ const sctx = small.getContext("2d");+ if (!sctx) return null;+ sctx.imageSmoothingQuality = "high";+ sctx.drawImage(plain, 0, 0, small.width, small.height);+ octx.drawImage(small, 0, 0, cw, ch);+ }+ return new Promise((resolve) => out.toBlob(resolve, "image/png"));+}++type Props = {+ url: string;+ sizing: ImageSizing;+ position: string;+ zoom: number;+ blur: number;+ /** Departing frames only fade out: they keep the live filter rather than bake for 180 ms. */+ bakeable: boolean;+};++/**+ * The wallpaper artwork. A live `filter: blur()` behind a scrolling, see-through thread is re-run on+ * every scroll frame (about 4-6x the scroll cost of no wallpaper); a blurred bitmap baked once costs+ * nothing per frame. The live filter stays only while a bake for the current inputs is pending.+ */+export function WallpaperArtwork({ url, sizing, position, zoom, blur, bakeable }: Props) {+ const el = useRef<HTMLDivElement>(null);+ const [size, setSize] = useState<{ w: number; h: number } | null>(null);+ const [baked, setBaked] = useState<{ key: string; url: string } | null>(null);+ const bakedUrl = useRef<string | null>(null);+ const want = bakeable && blur > 0;+ useEffect(() => {+ const node = el.current;+ if (!node || !want || typeof ResizeObserver === "undefined") return;+ let timer: ReturnType<typeof setTimeout> | undefined;+ const read = () => {+ // The box before transform: scale(zoom), which CSS applies after the filter as well.+ const w = node.offsetWidth,+ h = node.offsetHeight;+ if (w > 0 && h > 0)+ setSize((s) => (s && s.w === w && s.h === h ? s : { w, h }));+ };+ const observer = new ResizeObserver(() => {+ clearTimeout(timer);+ timer = setTimeout(read, SETTLE_MS);+ });+ observer.observe(node);+ read();+ return () => {+ clearTimeout(timer);+ observer.disconnect();+ };+ }, [want]);+ const key =+ want && size+ ? `${url}|${sizing}|${position}|${blur}|${size.w}x${size.h}|${window.devicePixelRatio || 1}`+ : null;+ useEffect(() => {+ if (!key || !size) return;+ let cancelled = false;+ const timer = setTimeout(() => {+ bake(url, size, sizing, position, blur)+ .then((blob) => {+ if (cancelled || !blob) return;+ const next = URL.createObjectURL(blob);+ if (bakedUrl.current) URL.revokeObjectURL(bakedUrl.current);+ bakedUrl.current = next;+ setBaked({ key, url: next });+ })+ .catch(() => undefined);+ }, SETTLE_MS);+ return () => {+ cancelled = true;+ clearTimeout(timer);+ };+ }, [key, size, url, sizing, position, blur]);+ useEffect(+ () => () => {+ if (bakedUrl.current) URL.revokeObjectURL(bakedUrl.current);+ bakedUrl.current = null;+ },+ [],+ );+ const ready = baked !== null && baked.key === key;+ const style: CSSProperties = ready+ ? {+ backgroundImage: `url("${baked.url}")`,+ backgroundPosition: "0 0",+ backgroundSize: "100% 100%",+ transform: `scale(${zoom / 100})`,+ transformOrigin: position,+ }+ : {+ backgroundImage: `url("${url}")`,+ backgroundPosition: position,+ backgroundSize: BACKGROUND_SIZE[sizing],+ transform: `scale(${zoom / 100})`,+ transformOrigin: position,+ filter: blur ? `blur(${blur}px)` : undefined,+ };+ return (+ <div+ ref={el}+ className="chat-wallpaper-artwork"+ data-baked={ready || undefined}+ style={style}+ />+ );+}diff --git a/studio/frontend/src/features/chat-background/background.tsx b/studio/frontend/src/features/chat-background/background.tsxindex 9670ee9516b..0015ebf123a 100644--- a/studio/frontend/src/features/chat-background/background.tsx+++ b/studio/frontend/src/features/chat-background/background.tsx@@ -12,24 +12,16 @@ import { acquireRender } from "./render";import { safeArtworkOpacity, type RGB } from "./contrast";import {type ChatBackgroundValue,- type ImageSizing,fadeKey,visibilityKey,} from "./model";import { useAppliedMaterial } from "./material";import { ComposerGlassLenses } from "./lens";+import { WallpaperArtwork } from "./artwork";import { useWallpaperHostFlags } from "./host-flags";import { type ImageTone, glassReadability, measureTone } from "./tone";type Frame = { url: string; value: ChatBackgroundValue };-const BACKGROUND_SIZE: Record<ImageSizing, string> = {- cover: "cover",- contain: "contain",- width: "100% auto",- height: "auto 100%",- stretch: "100% 100%",- original: "auto",-};// The window titlebar sits outside the wallpaper's subtree, so a ready wallpaper marks the root// rather than having CSS ask html:has(), which re-matches on every DOM change.export const ROOT_READY = "data-chat-wallpaper-ready";@@ -317,18 +309,13 @@ export function ChatBackground({ active = true }: { active?: boolean }) {className="chat-wallpaper-image"data-fade={adjustment[fadeKey(glass)]}>- <div- className="chat-wallpaper-artwork"- style={{- backgroundImage: `url("${frame.url}")`,- backgroundPosition: adjustment.position,- backgroundSize: BACKGROUND_SIZE[adjustment.sizing],- transform: `scale(${adjustment.zoom / 100})`,- transformOrigin: adjustment.position,- filter: adjustment.blur- ? `blur(${adjustment.blur}px)`- : undefined,- }}+ <WallpaperArtwork+ url={frame.url}+ sizing={adjustment.sizing}+ position={adjustment.position}+ zoom={adjustment.zoom}+ blur={adjustment.blur}+ bakeable={i === 1}/></div></div>diff --git a/studio/frontend/tests/wallpaper-artwork.test.ts b/studio/frontend/tests/wallpaper-artwork.test.tsnew file mode 100644index 00000000000..986571f7c6f--- /dev/null+++ b/studio/frontend/tests/wallpaper-artwork.test.ts@@ -0,0 +1,47 @@+// 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 { bakeScale, imageBox } from "../src/features/chat-background/artwork-geometry.ts";+import { readSrc } from "./helpers/kit.ts";++const ARTWORK = readSrc("features/chat-background/artwork.tsx");+const BACKGROUND = readSrc("features/chat-background/background.tsx");++test("the baked box is where CSS paints a no-repeat background for every sizing", () => {+ // A 400 x 200 image in a 1000 x 1000 box.+ assert.deepEqual(imageBox(1000, 1000, 400, 200, "cover", "50% 50%"), { x: -500, y: 0, w: 2000, h: 1000 });+ assert.deepEqual(imageBox(1000, 1000, 400, 200, "contain", "50% 50%"), { x: 0, y: 250, w: 1000, h: 500 });+ assert.deepEqual(imageBox(1000, 1000, 400, 200, "width", "0% 100%"), { x: 0, y: 500, w: 1000, h: 500 });+ assert.deepEqual(imageBox(1000, 1000, 400, 200, "height", "100% 0%"), { x: -1000, y: 0, w: 2000, h: 1000 });+ assert.deepEqual(imageBox(1000, 1000, 400, 200, "stretch", "30% 70%"), { x: 0, y: 0, w: 1000, h: 1000 });+ assert.deepEqual(imageBox(1000, 1000, 400, 200, "original", "50% 50%"), { x: 300, y: 400, w: 400, h: 200 });+ assert.deepEqual(imageBox(1000, 1000, 400, 200, "original", "bad"), { x: 300, y: 400, w: 400, h: 200 }, "an unparsable position centres, as the model does");+});++test("a wide blur bakes at a lower resolution, a narrow one at the device pixel ratio", () => {+ assert.equal(bakeScale(20, 2), 0.4);+ assert.equal(bakeScale(2, 2), 2);+ assert.equal(bakeScale(4, 1), 1);+ assert.equal(bakeScale(100, 3), 0.25, "never below a quarter of a CSS pixel");+});++test("the live blur filter shows only until the bitmap for the current inputs is baked", () => {+ assert.match(ARTWORK, /const ready = baked !== null && baked\.key === key;/);+ assert.match(ARTWORK, /backgroundSize: "100% 100%",[\s\S]{0,120}transform: `scale\(\$\{zoom \/ 100\}\)`/,+ "the baked bitmap covers the box and keeps the zoom transform, which CSS applies after the filter");+ assert.match(ARTWORK, /filter: blur \? `blur\(\$\{blur\}px\)` : undefined,/, "the pending path is the old live filter");+ assert.match(ARTWORK, /`\$\{url\}\|\$\{sizing\}\|\$\{position\}\|\$\{blur\}\|\$\{size\.w\}x\$\{size\.h\}\|/,+ "a new image, sizing, position, blur or box size bakes again");+ assert.match(ARTWORK, /octx\.filter = `blur\(\$\{blur \* k\}px\)`;\s*octx\.drawImage\(plain, 0, 0\);/,+ "the box is drawn clipped first, then blurred, as filter: blur() blurs the painted box");+ assert.match(ARTWORK, /"filter" in CanvasRenderingContext2D\.prototype/, "older WebKit without a canvas filter gets the downscale path");+ assert.match(ARTWORK, /URL\.revokeObjectURL\(bakedUrl\.current\)/, "baked URLs are released");+});++test("only the current frame bakes; a departing frame fades out with the live filter", () => {+ assert.match(BACKGROUND, /<WallpaperArtwork\s+url=\{frame\.url\}[\s\S]{0,200}bakeable=\{i === 1\}/);+ assert.equal(BACKGROUND.includes("filter: adjustment.blur"), false);+});