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.

10.8 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 { useCallback, useEffect, useMemo, useRef, useSyncExternalStore } from "react";
import {
  COMPOSER_SURFACE,
  FIND_SCOPE_SURFACE,
  SIDEBAR_SURFACE,
  trackedSurfaces,
} from "@/lib/surface-registry";
import { zoomTypedOffZoomKey } from "../../interface-zoom/lib/zoom-chords";
import {
  SHORTCUT_SLOTS,
  type ShortcutBinding,
  type ShortcutId,
  activationBelongsToFocus,
  formatBindingLabel,
  isMacPlatform,
  matchesBinding,
  parseBinding,
} from "../lib/keyboard-shortcuts";
import {
  resolveBinding,
  shortcutOwningBinding,
  useKeyboardShortcutsStore,
} from "../stores/keyboard-shortcuts-store";

export const COMPOSER_INPUT_SELECTOR = COMPOSER_SURFACE;

// Every element these can match registers itself (useSurfaceRef), so a check reads a handful of
// elements, not the whole document; any other selector is still a document query.
const TRACKED_SURFACE_SELECTORS: ReadonlySet<string> = new Set([
  COMPOSER_SURFACE,
  SIDEBAR_SURFACE,
  FIND_SCOPE_SURFACE,
]);

/** What `document.querySelectorAll(selector)` matches, in no particular order. */
export function surfaceElements(selector: string): Element[] {
  const tracked = TRACKED_SURFACE_SELECTORS.has(selector)
    ? trackedSurfaces(selector, selector)
    : null;
  return tracked ?? [...document.querySelectorAll(selector)];
}

export function isTextEntryFocused(exceptFor?: string): boolean {
  if (typeof document === "undefined") return false;
  const el = document.activeElement as HTMLElement | null;
  const tag = el?.tagName;
  const typing =
    tag === "INPUT" || tag === "TEXTAREA" || Boolean(el?.isContentEditable);
  if (!typing) return false;
  // A named field does not type this chord, so it does not shield it either.
  return !(exceptFor && el?.matches(exceptFor) === true);
}

/** Escape and Enter belong to the IME while composing; isComposing on WebKit, 229 on Chromium. */
export function isImeComposing(event: KeyboardEvent): boolean {
  return event.isComposing || event.keyCode === 229;
}

// Set only while the listeners re-read their snapshots. Nothing mutates the DOM between two
// listeners (React schedules their re-renders), so each id and each document query is answered
// once per pass instead of once per subscriber; a long chat makes every miss a whole-tree walk.
let passAnswers: Map<string, boolean> | null = null;

function answerOncePerPass(key: string, compute: () => boolean): boolean {
  if (!passAnswers) return compute();
  const known = passAnswers.get(key);
  if (known !== undefined) return known;
  const answer = compute();
  passAnswers.set(key, answer);
  return answer;
}

/** Checked at press time: a dialog leaves the route mounted and need not re-render `enabled`. */
export function isSurfaceInForeground(selector: string): boolean {
  if (typeof document === "undefined") return false;
  // Every match: Compare keeps an inert base composer mounted; Radix aria-hides the page under a modal.
  return answerOncePerPass(`foreground\0${selector}`, () =>
    surfaceElements(selector).some(
      (el) => !el.closest('[aria-hidden="true"], [inert]'),
    ),
  );
}

/** Not the complement of the above: an unrendered surface (closed mobile sidebar) is not
 * backgrounded. */
export function isSurfaceBackgrounded(selector: string): boolean {
  if (typeof document === "undefined") return false;
  return answerOncePerPass(`backgrounded\0${selector}`, () => {
    const found = surfaceElements(selector);
    return (
      found.length > 0 &&
      found.every((el) => el.closest('[aria-hidden="true"], [inert]') !== null)
    );
  });
}

/** Only Escape, function keys, and non-Shift modifier chords; caret keys count as typing. */
export function typesInTextField(binding: ShortcutBinding): boolean {
  if (binding.mod || binding.ctrl || binding.alt) return false;
  if (binding.code === "Escape") return false;
  return !/^F([1-9]|1[0-9]|2[0-4])$/.test(binding.code);
}

export interface UseShortcutOptions {
  enabled?: boolean;
  skipInTextFields?: boolean;
  /** Fields exempt from skipInTextFields for a chord that types nothing there (Escape in the
   * composer must still decline a tool request). */
  textFieldException?: string;
  /** Run on auto-repeat; one-shot otherwise, since a held toggle lands wherever released. */
  repeats?: boolean;
  /** Asked at press time before the event is prevented; false leaves the key to the browser. */
  claims?: () => boolean;
}

export interface ShortcutTrigger {
  claims: () => boolean;
  run: () => void;
}

const triggers = new Map<ShortcutId, ShortcutTrigger[]>();
const triggerListeners = new Set<() => void>();
let notifyQueued = false;
function flushTriggerListeners(): void {
  notifyQueued = false;
  passAnswers = new Map();
  try {
    triggerListeners.forEach((listener) => listener());
  } finally {
    passAnswers = null;
  }
}
// One pass per microtask: a dialog mounting or unmounting registers or drops a trigger per
// shortcut and lands as several observer batches, and each pass re-asks every subscriber.
const notifyTriggerListeners = () => {
  if (notifyQueued) return;
  notifyQueued = true;
  queueMicrotask(flushTriggerListeners);
};

/** Exported for the test. Returns the unregister. */
export function registerShortcutTrigger(
  id: ShortcutId,
  trigger: ShortcutTrigger,
): () => void {
  triggers.set(id, [...(triggers.get(id) ?? []), trigger]);
  notifyTriggerListeners();
  return () => {
    triggers.set(id, (triggers.get(id) ?? []).filter((t) => t !== trigger));
    notifyTriggerListeners();
  };
}

/** Runs id as if its chord were pressed, minus key checks; false when nothing took it. */
export function triggerShortcut(id: ShortcutId): boolean {
  const trigger = [...(triggers.get(id) ?? [])].reverse().find((t) => t.claims());
  trigger?.run();
  return trigger !== undefined;
}

// Modals show as aria-hidden, inert, or only a body-portaled backdrop when an aria-live region exists.
let modalObserver: MutationObserver | null = null;
/** Exported for the test. Returns the unsubscribe. */
export function subscribeTriggers(listener: () => void, watchModals: boolean): () => void {
  triggerListeners.add(listener);
  if (watchModals && !modalObserver && typeof MutationObserver !== "undefined") {
    modalObserver = new MutationObserver(notifyTriggerListeners);
    modalObserver.observe(document.documentElement, {
      subtree: true,
      attributes: true,
      attributeFilter: ["aria-hidden", "inert"],
    });
    modalObserver.observe(document.body, { childList: true });
  }
  return () => {
    triggerListeners.delete(listener);
    if (triggerListeners.size === 0) {
      modalObserver?.disconnect();
      modalObserver = null;
    }
  };
}

/** `watchModals` also re-asks when a modal opens or closes (desktop menu only). */
export function useShortcutAvailable(id: ShortcutId, watchModals: boolean): boolean {
  const subscribe = useCallback(
    (listener: () => void) => subscribeTriggers(listener, watchModals),
    [watchModals],
  );
  return useSyncExternalStore(subscribe, () =>
    answerOncePerPass(`available\0${id}`, () =>
      (triggers.get(id) ?? []).some((t) => t.claims()),
    ),
  );
}

/** Joined so the effect re-runs only on a real change. */
function useBindingValues(id: ShortcutId): string {
  return useKeyboardShortcutsStore((s) =>
    SHORTCUT_SLOTS.map(
      (slot) => resolveBinding(s.overrides, id, slot) ?? "",
    ).join("\0"),
  );
}

/** Bindings come from the store, so Settings edits apply without reload. */
export function useShortcut(
  id: ShortcutId,
  handler: (event: KeyboardEvent) => void,
  options: UseShortcutOptions = {},
): void {
  const {
    enabled = true,
    skipInTextFields = false,
    textFieldException,
    repeats = false,
    claims,
  } = options;
  const values = useBindingValues(id);
  // A chord claimed by two actions registers only for its owner, not by mount order.
  const ownedFlags = useKeyboardShortcutsStore((s) =>
    SHORTCUT_SLOTS.map((slot) => {
      const value = resolveBinding(s.overrides, id, slot);
      return value && shortcutOwningBinding(s.overrides, value) === id
        ? "1"
        : "0";
    }).join(""),
  );
  const bindings = useMemo(() => {
    const out: ShortcutBinding[] = [];
    values.split("\0").forEach((value, index) => {
      if (ownedFlags[index] !== "1") return;
      const parsed = parseBinding(value);
      if (parsed) out.push(parsed);
    });
    return out;
  }, [values, ownedFlags]);
  const latestRef = useRef({ handler, claims });
  latestRef.current = { handler, claims };

  // Registered even with no chord bound: the menu still reaches the action.
  useEffect(() => {
    if (!enabled) return;
    return registerShortcutTrigger(id, {
      claims: () => latestRef.current.claims?.() !== false,
      run: () => latestRef.current.handler(new KeyboardEvent("keydown")),
    });
  }, [id, enabled]);

  useEffect(() => {
    if (bindings.length === 0 || !enabled) return;
    const onKeyDown = (event: KeyboardEvent) => {
      if (event.defaultPrevented) return;
      if (isImeComposing(event)) return;
      const hit = bindings.find((binding) => matchesBinding(binding, event));
      if (!hit) return;
      if (zoomTypedOffZoomKey(event, hit.code, isMacPlatform())) return;
      // The exception is for a chord that types nothing there. Decline ships on Escape; rebound to
      // Enter or a letter, the same pass would deny the request as the user edits the prompt.
      const exception = typesInTextField(hit) ? undefined : textFieldException;
      if (skipInTextFields && isTextEntryFocused(exception)) return;
      if (
        typeof document !== "undefined" &&
        activationBelongsToFocus(hit, document.activeElement)
      ) {
        return;
      }
      // Before preventDefault: an unclaimed chord must reach the browser untouched.
      if (latestRef.current.claims?.() === false) return;
      event.preventDefault();
      if (event.repeat && !repeats) return;
      latestRef.current.handler(event);
    };
    window.addEventListener("keydown", onKeyDown);
    return () => window.removeEventListener("keydown", onKeyDown);
  }, [bindings, enabled, skipInTextFields, textFieldException, repeats]);
}

/** Null when unassigned; never the shipped default, which may have been rebound. */
export function useShortcutLabel(id: ShortcutId): string | null {
  const value = useKeyboardShortcutsStore((s) =>
    resolveBinding(s.overrides, id),
  );
  return useMemo(() => {
    const binding = parseBinding(value);
    return binding ? formatBindingLabel(binding) : null;
  }, [value]);
}

export function useShortcutLabels(id: ShortcutId): string[] {
  const values = useBindingValues(id);
  return useMemo(
    () =>
      values
        .split("\0")
        .map((value) => parseBinding(value))
        .filter((binding): binding is ShortcutBinding => binding !== null)
        .map((binding) => formatBindingLabel(binding)),
    [values],
  );
}