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.

9.9 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

import { Button } from "@/components/ui/button";
import {
  getAppliedInterfaceZoom,
  subscribeAppliedInterfaceZoom,
} from "@/features/settings/lib/interface-scale-runtime";
import { isImeComposing } from "@/features/settings/hooks/use-shortcut";
import {
  formatBindingValueLabel,
  isMacPlatform,
} from "@/features/settings/lib/keyboard-shortcuts";
import {
  INTERFACE_SCALE_RANGE,
  useInterfaceScaleStore,
} from "@/features/settings/stores/interface-scale-store";
import {
  shortcutOwningBinding,
  useKeyboardShortcutsStore,
} from "@/features/settings/stores/keyboard-shortcuts-store";
import { useT } from "@/i18n";
import { isTauri } from "@/lib/api-base";
import { Z_LAYER } from "@/lib/z-layers";
import { useEffect, useState, useSyncExternalStore } from "react";
import { createPortal } from "react-dom";
import {
  useZoomPopupStore,
  zoomInterface,
  zoomInterfaceFromChord,
  zoomScopeFor,
  zoomScopeFromChord,
} from "../lib/zoom-actions.ts";
import {
  ZOOM_CHORDS,
  type ZoomDirection,
  zoomChordTaken,
  zoomDirectionForKey,
} from "../lib/zoom-chords.ts";
import { createWheelZoomAccumulator, isZoomWheel } from "../lib/zoom-wheel.ts";
import { MinusSignIcon, PlusSignIcon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";

export const ZOOM_POPUP_HIDE_MS = 2500;

const ZOOM_BUTTON_CLASS =
  "hover:bg-[rgb(0_0_0_/_calc(0.06*var(--contrast-wash-gain,1)))] dark:hover:bg-[rgb(255_255_255_/_calc(0.1*var(--contrast-wash-gain,1)))]";

function keepFocus(event: { preventDefault: () => void }): void {
  event.preventDefault();
}

function withChord(label: string, direction: ZoomDirection): string {
  const chord = formatBindingValueLabel(ZOOM_CHORDS[direction]);
  return chord ? `${label} (${chord})` : label;
}

function ZoomPopup() {
  const t = useT();
  const scale = useInterfaceScaleStore((s) => s.scale);
  const token = useZoomPopupStore((s) => s.token);
  const hide = useZoomPopupStore((s) => s.hide);
  // Divided back out below, so the popup keeps one on-screen size at every zoom.
  const pageZoom = useSyncExternalStore(
    subscribeAppliedInterfaceZoom,
    getAppliedInterfaceZoom,
  );
  const [hovered, setHovered] = useState(false);
  const [focused, setFocused] = useState(false);
  const held = hovered || focused;

  // biome-ignore lint/correctness/useExhaustiveDependencies: each zoom restarts the timer.
  useEffect(() => {
    if (held) return;
    const timer = setTimeout(hide, ZOOM_POPUP_HIDE_MS);
    return () => clearTimeout(timer);
  }, [token, held, hide]);

  return (
    // pointer-events-auto survives a modal's body lock.
    <div
      className="modal-pe-live interface-zoom-position pointer-events-auto fixed right-4"
      style={{ zIndex: Z_LAYER.ZOOM_POPUP }}
      // Keeps an open modal from treating this as an outside click.
      onPointerDown={(event) => event.stopPropagation()}
    >
      <div
        // biome-ignore lint/a11y/useSemanticElements: a labelled group of zoom controls, not a form.
        role="group"
        aria-label={t("shell.zoom.label")}
        data-find-skip=""
        data-testid="interface-zoom-popup"
        onPointerEnter={() => setHovered(true)}
        onPointerLeave={() => setHovered(false)}
        onFocus={() => setFocused(true)}
        onBlur={(event) => {
          if (!event.currentTarget.contains(event.relatedTarget))
            setFocused(false);
        }}
        onKeyDown={(event) => {
          if (event.key !== "Escape") return;
          event.preventDefault();
          event.stopPropagation();
          hide();
        }}
        style={pageZoom === 1 ? undefined : { zoom: 1 / pageZoom }}
        className="find-bar-surface flex h-11 items-center gap-0.5 rounded-full pr-1.5 pl-4 duration-100 animate-in fade-in-0"
      >
        <span className="min-w-[calc(2.875rem*var(--ui-space-scale,1))] pr-1.5 font-medium text-ui-14 tabular-nums">
          {scale}%
        </span>
        <Button
          variant="ghost"
          size="icon-sm"
          className={ZOOM_BUTTON_CLASS}
          disabled={scale <= INTERFACE_SCALE_RANGE.min}
          onMouseDown={keepFocus}
          onClick={() => zoomInterface(-1)}
          aria-label={t("shell.zoom.zoomOut")}
          title={withChord(t("shell.zoom.zoomOut"), -1)}
        >
          <HugeiconsIcon icon={MinusSignIcon}
            strokeWidth={1.75}
            className="size-[calc(16px*var(--ui-space-scale,1))]"
          />
        </Button>
        <Button
          variant="ghost"
          size="icon-sm"
          className={ZOOM_BUTTON_CLASS}
          disabled={scale >= INTERFACE_SCALE_RANGE.max}
          onMouseDown={keepFocus}
          onClick={() => zoomInterface(1)}
          aria-label={t("shell.zoom.zoomIn")}
          title={withChord(t("shell.zoom.zoomIn"), 1)}
        >
          <HugeiconsIcon icon={PlusSignIcon}
            strokeWidth={1.75}
            className="size-[calc(16px*var(--ui-space-scale,1))]"
          />
        </Button>
        <span aria-hidden="true" className="mx-1 h-5 w-px bg-border" />
        <Button
          variant="ghost"
          size="sm"
          className={`rounded-full px-2.5 text-muted-foreground text-ui-14 ${ZOOM_BUTTON_CLASS}`}
          disabled={scale === INTERFACE_SCALE_RANGE.default}
          onMouseDown={keepFocus}
          onClick={() => zoomInterface(0)}
          title={withChord(t("shell.zoom.reset"), 0)}
        >
          {t("shell.zoom.reset")}
        </Button>
      </div>
    </div>
  );
}

/** Always mounted, so the first zoom is announced too. */
function ZoomAnnouncer({ open }: { open: boolean }) {
  const t = useT();
  const scale = useInterfaceScaleStore((s) => s.scale);
  return (
    <output aria-live="polite" className="sr-only">
      {open ? t("shell.zoom.announce", { percent: String(scale) }) : ""}
    </output>
  );
}

/** Cmd/Ctrl +, - and 0, plus Ctrl+wheel off macOS; a zoom scope zooms its own content. */
export function InterfaceZoom() {
  const open = useZoomPopupStore((s) => s.open);

  useEffect(() => {
    const mac = isMacPlatform();
    // Capture on document, not window, so the shortcut recorder's window listener wins.
    const onKeyDown = (event: KeyboardEvent) => {
      if (event.defaultPrevented || isImeComposing(event)) return;
      const direction = zoomDirectionForKey(event, mac);
      if (direction === null) return;
      const scope = zoomScopeFor(event.target);
      if (!(scope || isTauri)) return;
      const { overrides } = useKeyboardShortcutsStore.getState();
      const owned = (value: string) =>
        shortcutOwningBinding(overrides, value) !== null;
      if (zoomChordTaken(event, direction, mac, owned)) return;
      event.preventDefault();
      if (scope) zoomScopeFromChord(scope, direction);
      else zoomInterfaceFromChord(direction);
    };
    document.addEventListener("keydown", onKeyDown, true);
    // Bubble phase, so canvases with their own Ctrl+wheel zoom keep it.
    const wheelStep = createWheelZoomAccumulator();
    const scopeWheelStep = createWheelZoomAccumulator();
    const onWheel = (event: WheelEvent) => {
      if (modifierHeld && !event.ctrlKey && !event.metaKey) {
        modifierHeld = false;
        syncWheel();
      }
      if (event.defaultPrevented || !isZoomWheel(event)) return;
      const scope = zoomScopeFor(event.target);
      if (scope) {
        event.preventDefault();
        const direction = scopeWheelStep(event, getAppliedInterfaceZoom());
        if (direction !== null) scope.zoom(direction);
        return;
      }
      if (!isTauri || mac) return;
      event.preventDefault();
      const direction = wheelStep(event, getAppliedInterfaceZoom());
      if (direction !== null) zoomInterface(direction);
    };
    let wheelArmed = false;
    const armWheel = () => {
      if (wheelArmed) return;
      wheelArmed = true;
      window.addEventListener("wheel", onWheel, { passive: false });
    };
    const disarmWheel = () => {
      if (!wheelArmed) return;
      wheelArmed = false;
      window.removeEventListener("wheel", onWheel);
    };
    let modifierHeld = false;
    let pointerInScope = false;
    const syncWheel = () => {
      if (modifierHeld || pointerInScope) armWheel();
      else disarmWheel();
    };
    const onModifierChange = (event: KeyboardEvent) => {
      modifierHeld = event.ctrlKey || event.metaKey;
      syncWheel();
    };
    const onPointerOver = (event: PointerEvent) => {
      modifierHeld = event.ctrlKey || event.metaKey;
      pointerInScope = zoomScopeFor(event.target) !== null;
      syncWheel();
    };
    const onBlur = () => {
      modifierHeld = false;
      syncWheel();
    };
    const onPassiveWheel = (event: WheelEvent) => {
      if (wheelArmed || !isZoomWheel(event)) return;
      modifierHeld = true;
      syncWheel();
    };
    if (mac) {
      armWheel();
    } else {
      window.addEventListener("keydown", onModifierChange, true);
      window.addEventListener("keyup", onModifierChange, true);
      window.addEventListener("pointerover", onPointerOver, true);
      window.addEventListener("blur", onBlur);
      window.addEventListener("wheel", onPassiveWheel, { passive: true });
    }
    return () => {
      document.removeEventListener("keydown", onKeyDown, true);
      window.removeEventListener("keydown", onModifierChange, true);
      window.removeEventListener("keyup", onModifierChange, true);
      window.removeEventListener("pointerover", onPointerOver, true);
      window.removeEventListener("blur", onBlur);
      window.removeEventListener("wheel", onPassiveWheel);
      window.removeEventListener("wheel", onWheel);
    };
  }, []);

  if (typeof document === "undefined") return null;
  // Portaled so a modal's inert/aria-hidden does not reach it.
  return createPortal(
    <>
      {isTauri && <ZoomAnnouncer open={open} />}
      {open && <ZoomPopup />}
    </>,
    document.body,
  );
}