Файли
openidea/src/ui/Menu.tsx
T
Mansur Akhmadiтаmansur-akhmadi 9b6e5a5a62 feat(vcs): copy a file's link on its Git remote from Copy Path/Reference… (#3553)
Copy Path/Reference… gains a <hosting> Link item (GitHub Link, GitLab Link, Bitbucket Link) that copies the page Open In › <hosting> opens; in the editor the link covers the selected lines (#L2-L3 / #L2-3 / #lines-2:3). A copyPath menu id lets features add items to the chooser, so editor/core and project-tree stay free of Git code.

Closes #3552

Co-authored-by: mansur-akhmadi <[email protected]>
2026-11-15 23:29:54 +08:00

580 рядки
18 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/**
* Menus and context menus. Items are either ad-hoc (`{label, run}`) or bound
* to registered actions (`{kind: "action", action: "editor.closeTab"}`), which
* supplies title, shortcut and enablement from the action registry.
*/
import {
Fragment,
useEffect,
useLayoutEffect,
useRef,
useState,
type KeyboardEvent as ReactKeyboardEvent,
} from "react";
import clsx from "clsx";
import { create } from "zustand";
import { getAction, isActionEnabled, executeActionFrom, actionToggled } from "@/platform/actions";
import { useContextKeys } from "@/platform/context";
import { shortcutLabel } from "@/platform/keymap";
import { Icon, type IconName } from "./icons";
import { Popup, type PopupAnchor } from "./Popup";
import type { Placement } from "@floating-ui/react-dom";
import styles from "./Menu.module.css";
import { tx } from "@/platform/i18n";
import { isSubmenuAim, type MenuPointer } from "./menuAim";
export type MenuItem =
| { kind: "separator" }
| { kind: "header"; label: string }
| {
kind: "action";
action: string;
arg?: unknown;
label?: string;
/** A check mark in this state instead of the action's own `toggled` (one row per argument). */
checked?: boolean;
icon?: IconName | (string & {});
disabled?: boolean;
}
| {
kind?: "item";
id?: string;
label: string;
/** User text (e.g. a past query): shown as is, never looked up in the catalog. */
verbatim?: boolean;
icon?: IconName | (string & {});
shortcut?: string;
disabled?: boolean;
checked?: boolean;
danger?: boolean;
run?: () => void;
/** The pointer or the keyboard made it the active item (not on opening the menu). */
onActive?: () => void;
submenu?: MenuItem[];
/** The text the item copies, for choosers that show it (Copy Path/Reference…). */
preview?: string;
};
interface Resolved {
kind: "item" | "separator" | "header";
key: string;
label: string;
icon?: string;
shortcut?: string;
disabled?: boolean;
checked?: boolean;
danger?: boolean;
run?: () => void;
onActive?: () => void;
submenu?: MenuItem[];
}
export function resolveMenuItems(items: MenuItem[]): Resolved[] {
const out: Resolved[] = [];
items.forEach((item, i) => {
if (item.kind === "separator") {
if (out.length && out[out.length - 1]!.kind !== "separator")
out.push({ kind: "separator", key: `sep${i}`, label: "" });
return;
}
if (item.kind === "header") {
out.push({ kind: "header", key: `h${i}`, label: tx(item.label) });
return;
}
if (item.kind === "action") {
const a = getAction(item.action);
if (!a) return;
out.push({
kind: "item",
key: typeof item.arg === "string" ? `${item.action}|${item.arg}` : item.action,
label: tx(item.label) ?? a.title,
icon: item.icon ?? a.icon,
shortcut: shortcutLabel(item.action),
disabled: !!item.disabled || !isActionEnabled(a, undefined, item.arg),
checked: item.checked ?? actionToggled(a),
run: () => void executeActionFrom("menu", item.action, item.arg),
});
return;
}
out.push({
...item,
label: item.verbatim ? item.label : tx(item.label),
kind: "item",
key: item.id ?? `${i}:${item.label}`,
});
});
while (out.length && out[out.length - 1]!.kind === "separator") out.pop();
return out;
}
const selectable = (r: Resolved | undefined) => !!r && r.kind === "item" && !r.disabled;
interface MenuListProps {
items: MenuItem[];
/** Reserve space for item icons even when an item has none. */
iconColumn?: boolean;
/** Called after an item ran (closes the whole menu chain). */
onDone: () => void;
/** Close just this level (submenus). */
onCloseLevel?: () => void;
"aria-label"?: string;
/** Focus the first item on mount. */
autoFocusFirst?: boolean;
/**
* Numeric mnemonics, like IDEA's Select In popup: label the first nine
* enabled rows 1–9 and activate one on that digit. Submenus never inherit it.
*/
numbers?: boolean;
}
export function MenuList({
items,
iconColumn = true,
onDone,
onCloseLevel,
autoFocusFirst = true,
numbers = false,
...aria
}: MenuListProps) {
// Action enablement follows the context keys: opening the menu moves focus
// (e.g. editorTextFocus → popupOpen) right after the first render.
useContextKeys();
const resolved = resolveMenuItems(items);
// IDEA numeric mnemonics: the first nine selectable rows, in display order.
const numbersOf: Array<number | undefined> = [];
if (numbers) {
let n = 0;
resolved.forEach((r, i) => {
if (n < 9 && selectable(r)) numbersOf[i] = ++n;
});
}
const [active, setActive] = useState(() => (autoFocusFirst ? resolved.findIndex(selectable) : -1));
const [openSub, setOpenSubIndex] = useState<number | null>(null);
const [subAnchor, setSubAnchor] = useState<HTMLElement | null>(null);
const itemRefs = useRef<Array<HTMLButtonElement | null>>([]);
const hoverTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
const submenuRef = useRef<HTMLDivElement | null>(null);
const pointer = useRef<MenuPointer | null>(null);
const previousPointer = useRef<MenuPointer | null>(null);
const pendingAim = useRef<number | null>(null);
// Pointer movement highlights a row without focusing it. Focusing on every
// enter drops the hover paint in the Windows webview, so the menu looks frozen.
const pointerHover = useRef(false);
// Row the pointer last entered: pointerenter and mouseenter both report it.
const hovered = useRef(-1);
const setOpenSub = (i: number | null) => {
setSubAnchor(i === null ? null : (itemRefs.current[i] ?? null));
setOpenSubIndex(i);
};
useLayoutEffect(() => {
if (pointerHover.current) {
pointerHover.current = false;
return;
}
if (active >= 0 && openSub === null) itemRefs.current[active]?.focus({ preventScroll: true });
}, [active, openSub]);
useEffect(() => () => clearTimeout(hoverTimer.current), []);
const focusItem = (i: number) => {
setActive(i);
resolved[i]?.onActive?.();
};
const move = (delta: number, from = active) => {
const n = resolved.length;
if (!n) return;
let i = from;
for (let step = 0; step < n; step++) {
i = (i + delta + n) % n;
if (selectable(resolved[i])) {
focusItem(i);
return;
}
}
};
const activate = (i: number) => {
cancelHover();
const item = resolved[i];
if (!selectable(item)) return;
if (item!.submenu) {
setActive(i);
setOpenSub(i);
return;
}
onDone();
item!.run?.();
};
const cancelHover = () => {
clearTimeout(hoverTimer.current);
pendingAim.current = null;
};
const applyHover = (i: number, immediate = false) => {
cancelHover();
if (i !== active) {
pointerHover.current = true;
focusItem(i);
}
const next = resolved[i]?.submenu ? i : null;
if (immediate) setOpenSub(next);
else hoverTimer.current = setTimeout(() => setOpenSub(next), next === null ? 200 : 120);
};
const recordPointer = (point: MenuPointer) => {
if (pointer.current?.x === point.x && pointer.current.y === point.y) return false;
previousPointer.current = pointer.current;
pointer.current = point;
return true;
};
const aiming = () => {
const rect = submenuRef.current?.parentElement?.getBoundingClientRect();
return !!rect && !!pointer.current && isSubmenuAim(previousPointer.current, pointer.current, rect);
};
const deferHover = (i: number) => {
cancelHover();
pendingAim.current = i;
hoverTimer.current = setTimeout(() => applyHover(i, true), 300);
};
const pointerMove = (point: MenuPointer) => {
if (!recordPointer(point) || pendingAim.current === null) return;
const i = pendingAim.current;
if (aiming()) deferHover(i);
else applyHover(i);
};
const enterSubmenu = () => {
cancelHover();
hovered.current = -1;
pointer.current = previousPointer.current = null;
if (openSub !== null && active !== openSub) {
pointerHover.current = true;
setActive(openSub);
}
};
// Track movement across the gap too; submenu portals are outside this level's DOM.
useEffect(() => {
if (openSub === null) return;
const onMove = (e: PointerEvent) => {
if (submenuRef.current?.parentElement?.contains(e.target as Node)) enterSubmenu();
else pointerMove({ x: e.clientX, y: e.clientY });
};
document.addEventListener("pointermove", onMove);
return () => document.removeEventListener("pointermove", onMove);
});
const hover = (i: number, point: MenuPointer) => {
const item = resolved[i];
if (!selectable(item) || hovered.current === i) return;
hovered.current = i;
recordPointer(point);
if (openSub !== null && openSub !== i && aiming()) deferHover(i);
else applyHover(i);
};
const leave = (i: number) => {
// A compatibility mouseleave can follow pointerenter on the next row.
if (hovered.current === i) hovered.current = -1;
};
const onKeyDown = (e: ReactKeyboardEvent) => {
cancelHover();
pointer.current = previousPointer.current = null;
switch (e.key) {
case "ArrowDown":
move(1);
break;
case "ArrowUp":
move(-1, active < 0 ? 0 : active);
break;
case "Home":
move(1, -1);
break;
case "End":
move(-1, 0);
break;
case "ArrowRight":
if (resolved[active]?.submenu) setOpenSub(active);
else return;
break;
case "ArrowLeft":
if (onCloseLevel) onCloseLevel();
else return;
break;
case "Enter":
case " ":
activate(active);
break;
default:
if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) {
if (numbers && /^[1-9]$/.test(e.key)) {
const n = Number(e.key);
for (let i = 0; i < resolved.length; i++)
if (numbersOf[i] === n) {
activate(i);
break;
}
return;
}
const ch = e.key.toLowerCase();
const n = resolved.length;
for (let s = 1; s <= n; s++) {
const i = (active + s + n) % n;
const r = resolved[i]!;
if (selectable(r) && r.label.toLowerCase().startsWith(ch)) {
focusItem(i);
break;
}
}
break;
}
return;
}
e.preventDefault();
e.stopPropagation();
};
return (
<div
role="menu"
aria-label={aria["aria-label"]}
className={styles.menu}
onKeyDown={onKeyDown}
// A right-click in an open menu does nothing; without this it reaches whatever is under the
// menu in the component tree, e.g. the title bar's toolbar menu behind File › Open Recent.
onContextMenu={(e) => e.preventDefault()}
onPointerMove={(e) => {
if (e.currentTarget.contains(e.target as Node)) pointerMove({ x: e.clientX, y: e.clientY });
}}
>
{resolved.map((r, i) => {
if (r.kind === "separator") return <div key={r.key} role="separator" className={styles.separator} />;
if (r.kind === "header")
return (
<div key={r.key} role="presentation" className={styles.header}>
{r.label}
</div>
);
const hasSub = !!r.submenu;
return (
<Fragment key={r.key}>
<button
ref={(el) => {
itemRefs.current[i] = el;
}}
type="button"
role={r.checked !== undefined ? "menuitemcheckbox" : "menuitem"}
aria-checked={r.checked}
aria-disabled={r.disabled || undefined}
aria-haspopup={hasSub ? "menu" : undefined}
aria-expanded={hasSub ? openSub === i : undefined}
tabIndex={i === active ? 0 : -1}
className={clsx(
styles.item,
i === active && styles.active,
r.disabled && styles.disabled,
r.danger && styles.danger,
)}
onPointerEnter={(e) => hover(i, { x: e.clientX, y: e.clientY })}
onMouseEnter={(e) => hover(i, { x: e.clientX, y: e.clientY })}
onPointerLeave={() => leave(i)}
onMouseLeave={() => leave(i)}
onClick={() => activate(i)}
>
{numbers && (
<span className={styles.mnemonic} data-menu-number={numbersOf[i]} aria-hidden={true}>
{numbersOf[i] ?? ""}
</span>
)}
{iconColumn && (
<span className={styles.icon} data-menu-icon={!r.checked && r.icon ? "" : undefined}>
{r.checked ? <Icon name="check" size={13} /> : r.icon ? <Icon name={r.icon} size={14} /> : null}
</span>
)}
<span className={styles.label} title={r.label}>
{r.label}
</span>
{r.shortcut && <span className={styles.shortcut}>{r.shortcut}</span>}
{hasSub && <Icon name="chevron-right" size={14} className={styles.subArrow} />}
</button>
{hasSub && openSub === i && subAnchor && (
<Popup
open
anchor={subAnchor}
placement="right-start"
offset={2}
role="presentation"
autoFocus={false}
onClose={() => setOpenSub(null)}
className={styles.popup}
>
<div ref={submenuRef} onPointerEnter={enterSubmenu}>
<MenuList
items={r.submenu!}
onDone={onDone}
onCloseLevel={() => {
setOpenSub(null);
itemRefs.current[i]?.focus();
}}
/>
</div>
</Popup>
)}
</Fragment>
);
})}
</div>
);
}
export interface MenuProps {
open: boolean;
onClose: () => void;
anchor: PopupAnchor;
items: MenuItem[];
iconColumn?: boolean;
placement?: Placement;
"aria-label"?: string;
/** Numeric mnemonics on the first nine rows; see `MenuList`. */
numbers?: boolean;
}
/** A dropdown menu anchored to an element or point. */
export function Menu({
open,
onClose,
anchor,
items,
placement = "bottom-start",
iconColumn,
numbers,
...aria
}: MenuProps) {
return (
<Popup
open={open}
onClose={onClose}
anchor={anchor}
placement={placement}
role="presentation"
autoFocus={false}
className={styles.popup}
>
<MenuList
items={items}
onDone={onClose}
iconColumn={iconColumn}
numbers={numbers}
aria-label={aria["aria-label"]}
/>
</Popup>
);
}
// ---------------------------------------------------------------------------
// Context menus
interface ContextMenuState {
menu: {
x: number;
y: number;
items: MenuItem[];
numbers: boolean;
id: number;
/** The text selection the menu was opened on (see `restoreSelection`). */
selection: Range[] | null;
owner: Element | null;
} | null;
}
export const useContextMenuStore = create<ContextMenuState>(() => ({ menu: null }));
let menuSeq = 0;
export function showContextMenu(
at: { clientX: number; clientY: number } | { x: number; y: number },
items: MenuItem[],
options?: { numbers?: boolean; owner?: Element | null },
) {
const x = "clientX" in at ? at.clientX : at.x;
const y = "clientY" in at ? at.clientY : at.y;
useContextMenuStore.setState({
menu: {
x,
y,
items,
numbers: !!options?.numbers,
id: ++menuSeq,
selection: textSelection(),
owner: options?.owner ?? null,
},
});
}
/** The page's text selection, unless it is in an editable element (editors keep their own). */
function textSelection(): Range[] | null {
const sel = document.getSelection();
if (!sel || sel.isCollapsed || !sel.rangeCount) return null;
const node = sel.anchorNode;
const el = node instanceof Element ? node : node?.parentElement;
if (!el || el.closest("input, textarea, [contenteditable]:not([contenteditable='false'])")) return null;
return Array.from({ length: sel.rangeCount }, (_, i) => sel.getRangeAt(i).cloneRange());
}
/**
* WebKit drops a page's text selection when focus moves to another element, as when the menu
* focuses its first row and when it gives focus back: put the selection the menu was opened on
* back, so it stays highlighted while its Copy / Quote apply to it.
*/
function restoreSelection(ranges: readonly Range[]) {
const sel = document.getSelection();
if (!sel || !sel.isCollapsed) return;
if (!ranges.every((r) => r.startContainer.isConnected && r.endContainer.isConnected)) return;
sel.removeAllRanges();
for (const r of ranges) sel.addRange(r);
}
export function hideContextMenu() {
useContextMenuStore.setState({ menu: null });
}
/** Renders the active context menu; mount once (the workbench does). */
export function ContextMenuHost() {
const menu = useContextMenuStore((s) => s.menu);
const selection = menu?.selection ?? null;
const owner = menu?.owner ?? null;
useLayoutEffect(() => {
if (!owner) return;
owner.setAttribute("data-context-menu-open", "");
return () => owner.removeAttribute("data-context-menu-open");
}, [owner]);
// After the rows' layout effects: the first row has taken focus.
useLayoutEffect(() => {
if (!selection) return;
restoreSelection(selection);
// After the popup has given focus back; a click elsewhere selects afresh after this.
return () => queueMicrotask(() => restoreSelection(selection));
}, [selection]);
if (!menu) return null;
return (
<Popup
key={menu.id}
open
anchor={{ x: menu.x, y: menu.y }}
placement="bottom-start"
offset={2}
role="presentation"
autoFocus={false}
onClose={hideContextMenu}
className={styles.popup}
>
<MenuList items={menu.items} numbers={menu.numbers} onDone={hideContextMenu} aria-label="Context menu" />
</Popup>
);
}