Skip to content

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.

Studio: keep the composer still when switching chats in WebKit (#16076)

Michael Han0d5d40cd83d9
diff --git a/studio/frontend/src/components/assistant-ui/thread-gutter.ts b/studio/frontend/src/components/assistant-ui/thread-gutter.ts
index e0e7ebdd6f5..09e20dcb72a 100644
--- a/studio/frontend/src/components/assistant-ui/thread-gutter.ts
+++ b/studio/frontend/src/components/assistant-ui/thread-gutter.ts
@@ -41,6 +41,12 @@ export function bothEdgesGutterMisplaced(doc: Document = document): boolean {
return misplacedCache ?? false;
}
+const gutterSyncs = new WeakMap<HTMLElement, () => void>();
+
+export function syncThreadGutter(viewport: HTMLElement): void {
+ gutterSyncs.get(viewport)?.();
+}
+
export function watchThreadGutter(viewport: HTMLElement): () => void {
if (typeof ResizeObserver === "undefined") {
return () => undefined;
@@ -74,9 +80,11 @@ export function watchThreadGutter(viewport: HTMLElement): () => void {
};
const observer = new ResizeObserver(sync);
observer.observe(viewport);
+ gutterSyncs.set(viewport, sync);
sync();
return () => {
observer.disconnect();
+ gutterSyncs.delete(viewport);
viewport.removeAttribute(THREAD_GUTTER_SHIFT_ATTRIBUTE);
viewport.style.removeProperty(THREAD_GUTTER_SHIFT_PROPERTY);
};
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 2dd637977e4..bdd165b9593 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -68,6 +68,7 @@ import { attachWheelHoverSuppression } from "@/components/assistant-ui/thread-wh
import { threadHasResearchMessage } from "@/components/assistant-ui/thread-research-presence";
import {
THREAD_GUTTER_CHANGE_EVENT,
+ syncThreadGutter,
watchThreadGutter,
} from "@/components/assistant-ui/thread-gutter";
import { observeThreadWidthSteps } from "@/components/assistant-ui/thread-width-steps";
@@ -2129,7 +2130,8 @@ export const Thread: FC<{
return attachWheelHoverSuppression(viewportEl);
}, [viewportEl]);
- useEffect(() => {
+ // Layout effect: a remounted viewport must be shifted before its first paint.
+ useLayoutEffect(() => {
if (!viewportEl) return;
return watchThreadGutter(viewportEl);
}, [viewportEl]);
@@ -2596,6 +2598,8 @@ const ThreadComposerDock: FC<{
const resizeObserver = new ResizeObserver(() => apply());
let observedFooter: Element | null = null;
const apply = () => {
+ // Can fire before the gutter's observer; unshifted, the composer lands a scrollbar off-centre.
+ syncThreadGutter(viewport);
// The footer is the real column box: WebKit's both-edges gutter reserves 2x the scrollbar on the right, clientWidth 1x.
const footer = childWithClass(viewport, "aui-thread-viewport-footer");
if (footer && footer !== observedFooter) {
diff --git a/studio/frontend/tests/thread-gutter.test.ts b/studio/frontend/tests/thread-gutter.test.ts
index 564d68fb081..8ca2f82fef6 100644
--- a/studio/frontend/tests/thread-gutter.test.ts
+++ b/studio/frontend/tests/thread-gutter.test.ts
@@ -6,7 +6,10 @@ import test from "node:test";
import {
THREAD_GUTTER_CHANGE_EVENT,
+ THREAD_GUTTER_SHIFT_ATTRIBUTE,
+ syncThreadGutter,
threadGutterShift,
+ watchThreadGutter,
} from "../src/components/assistant-ui/thread-gutter.ts";
import { readSrc } from "./helpers/kit.ts";
@@ -40,3 +43,78 @@ test("the composer dock re-measures when the shift moves the column", () => {
assert.match(thread, DOCK_LISTENER);
assert.equal(THREAD_GUTTER_CHANGE_EVENT, "unsloth:thread-gutter-change");
});
+
+const LAYOUT_WATCH =
+ /useLayoutEffect\(\(\) => \{\s*if \(!viewportEl\) return;\s*return watchThreadGutter\(viewportEl\);/;
+const DOCK_SYNCS_FIRST =
+ /const apply = \(\) => \{[^}]*?syncThreadGutter\(viewport\);[\s\S]*?const footer = childWithClass/;
+
+function fakeViewport() {
+ const attrs = new Set<string>();
+ const props = new Map<string, string>();
+ const events: string[] = [];
+ const box = { left: 0, width: 100 };
+ const doc = {
+ createElement: () => ({
+ setAttribute: () => undefined,
+ style: {},
+ append: () => undefined,
+ remove: () => undefined,
+ offsetWidth: 100,
+ clientWidth: 85,
+ getBoundingClientRect: () => box,
+ }),
+ body: { append: () => undefined },
+ };
+ const viewport = {
+ offsetWidth: 600,
+ clientWidth: 600,
+ ownerDocument: doc,
+ style: {
+ setProperty: (k: string, v: string) => props.set(k, v),
+ removeProperty: (k: string) => props.delete(k),
+ },
+ toggleAttribute: (name: string, on: boolean) =>
+ on ? attrs.add(name) : attrs.delete(name),
+ removeAttribute: (name: string) => attrs.delete(name),
+ hasAttribute: (name: string) => attrs.has(name),
+ dispatchEvent: (event: Event) => events.push(event.type),
+ };
+ return { viewport, attrs, events };
+}
+
+test("a caller can bring the shift up to date before its own observer would", () => {
+ const g = globalThis as Record<string, unknown>;
+ const saved = [g.ResizeObserver, g.getComputedStyle];
+ g.ResizeObserver = class {
+ observe = () => undefined;
+ disconnect = () => undefined;
+ };
+ g.getComputedStyle = () => ({
+ borderLeftWidth: "0px",
+ borderRightWidth: "0px",
+ direction: "ltr",
+ });
+ try {
+ const { viewport, attrs, events } = fakeViewport();
+ const el = viewport as unknown as HTMLElement;
+ const stop = watchThreadGutter(el);
+ assert.equal(attrs.has(THREAD_GUTTER_SHIFT_ATTRIBUTE), false);
+ viewport.clientWidth = 585;
+ syncThreadGutter(el);
+ assert.equal(attrs.has(THREAD_GUTTER_SHIFT_ATTRIBUTE), true);
+ assert.deepEqual(events, [THREAD_GUTTER_CHANGE_EVENT]);
+ stop();
+ viewport.clientWidth = 600;
+ syncThreadGutter(el);
+ assert.equal(attrs.has(THREAD_GUTTER_SHIFT_ATTRIBUTE), false);
+ } finally {
+ [g.ResizeObserver, g.getComputedStyle] = saved;
+ }
+});
+
+test("a remounted viewport is shifted before paint, and the dock syncs the gutter before it measures", () => {
+ const thread = readSrc("components/assistant-ui/thread.tsx");
+ assert.match(thread, LAYOUT_WATCH);
+ assert.match(thread, DOCK_SYNCS_FIRST);
+});