Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

3.8 KB
Raw Blame
/**
 * When `?` should open the shortcut overlay — and, more importantly, when it
 * must not.
 *
 * `?` is a character people type. A global listener that opened a dialog every
 * time it appeared would eat the question mark out of a chat message, a wiki
 * note, a search box and every command typed into a terminal pane, which is a
 * far worse bug than the missing overlay it was meant to fix. So the guard
 * below is the substance of this feature, not a detail of it.
 *
 * Kept as a plain function on the event rather than inside the hook so the
 * rules are testable without mounting anything.
 */

import { appZoomChordMatches } from "@/lib/appZoom";
import { isBareChord } from "@/lib/appChords";

/** Elements that are text entry by their tag alone. */
const TEXT_TAGS = new Set(["INPUT", "TEXTAREA", "SELECT"]);

/**
 * `<input type="checkbox">` and friends are not text entry, so `?` is free
 * there. Everything not on this list — including a bare `<input>` with no type
 * — is treated as text, because the safe default is to let the character
 * through.
 */
const NON_TEXT_INPUT_TYPES = new Set([
  "button",
  "checkbox",
  "color",
  "file",
  "image",
  "radio",
  "range",
  "reset",
  "submit",
]);

export function isTextEntryTarget(target: EventTarget | null): boolean {
  if (!(target instanceof HTMLElement)) return false;
  // Read the attribute as well as the property: `isContentEditable` is
  // computed from the rendered tree, so it is false for an element that is
  // editable but not attached to the document yet — and an element that is
  // about to receive typed text is exactly the case this must catch.
  const editable = target.getAttribute("contenteditable");
  if (target.isContentEditable || editable === "" || editable === "true") {
    return true;
  }
  if (target.closest?.('[contenteditable=""], [contenteditable="true"]')) {
    return true;
  }
  const tag = target.tagName;
  if (tag === "INPUT") {
    const type = (target as HTMLInputElement).type?.toLowerCase() ?? "text";
    return !NON_TEXT_INPUT_TYPES.has(type);
  }
  if (TEXT_TAGS.has(tag)) return true;
  // xterm renders an off-screen textarea that is caught above, but a pane can
  // also mark a whole region as keyboard-owning; honour that claim.
  return Boolean(target.closest?.('[data-keyboard-capture="true"]'));
}

export interface OverlayTriggerEvent {
  key: string;
  /** Needed only for a chord the user recorded anew (letters match by position). */
  code?: string;
  shiftKey?: boolean;
  ctrlKey: boolean;
  metaKey: boolean;
  altKey: boolean;
  defaultPrevented: boolean;
  target: EventTarget | null;
}

/**
 * True when this keystroke is the overview's chord — by default a bare `?`
 * typed outside any text entry.
 *
 * Shift is allowed and expected: `?` is Shift plus `/` on a US layout, and Shift
 * plus the key right of `0` on a German one. Reading `event.key` rather than a
 * physical code is what makes both work without a per-layout table.
 * Ctrl/Cmd/Alt disqualify: those chords belong to whatever else claims them.
 *
 * `combo` is the chord chosen under Settings → Keyboard shortcuts ("" = off).
 * A chord without Ctrl, Alt or ⌘ keeps the text-entry guard; one with them
 * works inside a field too, like every other app chord.
 */
export function shouldOpenShortcutOverlay(event: OverlayTriggerEvent, combo = "question"): boolean {
  if (event.defaultPrevented) return false;
  if (!combo) return false;
  if (combo !== "question") {
    const matches = appZoomChordMatches(
      { ...event, code: event.code ?? "", shiftKey: event.shiftKey ?? false },
      combo,
    );
    if (!matches) return false;
    return !isBareChord(combo) || !isTextEntryTarget(event.target);
  }
  if (event.key !== "?") return false;
  if (event.ctrlKey || event.metaKey || event.altKey) return false;
  return !isTextEntryTarget(event.target);
}