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.

3.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

/** Classic scrollbars take layout space, so their gutter stays unset. */

export const OVERLAY_SCROLLBAR_GUTTER_VAR = "--overlay-scrollbar-gutter";
/** For a padding-reserved gutter; unset for overlay scrollbars. */
export const CLASSIC_SCROLLBAR_WIDTH_VAR = "--classic-scrollbar-width";

const MAX_GUTTER_PX = 48;

/** Re-measuring forces layout, so resize bursts are coalesced. */
const RESIZE_SETTLE_MS = 200;

type Measurement = { gutter: number; classic: number; reliable: boolean };

function measure(doc: Document): Measurement {
  const body = doc.body;
  if (!body) {
    return { gutter: 0, classic: 0, reliable: false };
  }

  const probe = doc.createElement("div");
  // Above dialogs; pointer-events:auto beats a Radix modal's body "none".
  probe.style.cssText =
    "position:fixed;top:0;left:0;width:60px;height:60px;margin:0;border:0;padding:0;opacity:0;overflow-y:scroll;pointer-events:auto;z-index:2147483647";
  const content = doc.createElement("div");
  content.style.cssText = "width:100%;height:300px";
  probe.appendChild(content);
  body.appendChild(probe);

  try {
    const classic = probe.offsetWidth - probe.clientWidth;
    if (classic > 0) {
      return { gutter: 0, classic, reliable: true };
    }

    // Reveal scrollbars configured to appear only while scrolling.
    probe.scrollTop = 1;

    const rect = probe.getBoundingClientRect();
    const right = Math.round(rect.right);
    const y = Math.round(rect.top + rect.height / 2);

    let gutter = 0;
    for (let offset = 1; offset <= MAX_GUTTER_PX; offset++) {
      if (doc.elementFromPoint(right - offset, y) === content) {
        return { gutter, classic: 0, reliable: true };
      }
      gutter = offset;
    }
    return { gutter: 0, classic: 0, reliable: false };
  } finally {
    body.removeChild(probe);
  }
}

export function measureOverlayScrollbarGutter(doc: Document): number {
  return measure(doc).gutter;
}

export function applyOverlayScrollbarGutter(doc: Document): number {
  const { gutter, classic, reliable } = measure(doc);
  const root = doc.documentElement;
  if (!reliable) {
    // Keep the last good gutter: one unreadable sweep must not shift rows.
    return (
      Number.parseFloat(
        root.style.getPropertyValue(OVERLAY_SCROLLBAR_GUTTER_VAR),
      ) || 0
    );
  }
  if (gutter > 0) {
    root.style.setProperty(OVERLAY_SCROLLBAR_GUTTER_VAR, `${gutter}px`);
  } else {
    root.style.removeProperty(OVERLAY_SCROLLBAR_GUTTER_VAR);
  }
  if (classic > 0) {
    root.style.setProperty(CLASSIC_SCROLLBAR_WIDTH_VAR, `${classic}px`);
  } else {
    root.style.removeProperty(CLASSIC_SCROLLBAR_WIDTH_VAR);
  }
  return gutter;
}

export function watchOverlayScrollbarGutter(win: Window): () => void {
  const doc = win.document;
  let resizeTimer: ReturnType<typeof setTimeout> | undefined;

  // Hit testing a hidden page reads nothing, so skip the forced layout.
  const remeasure = () =>
    doc.visibilityState === "hidden" ? 0 : applyOverlayScrollbarGutter(doc);
  const onResize = () => {
    if (resizeTimer !== undefined) {
      clearTimeout(resizeTimer);
    }
    resizeTimer = setTimeout(remeasure, RESIZE_SETTLE_MS);
  };

  remeasure();
  win.addEventListener("resize", onResize);
  win.addEventListener("focus", remeasure);
  doc.addEventListener("visibilitychange", remeasure);

  return () => {
    if (resizeTimer !== undefined) {
      clearTimeout(resizeTimer);
    }
    win.removeEventListener("resize", onResize);
    win.removeEventListener("focus", remeasure);
    doc.removeEventListener("visibilitychange", remeasure);
  };
}