Файли
openidea/src/platform/navigationModifier.ts
T
wangfeiтаhapput 607e4e095d fix(editor): show navigation link for bound click modifiers on hover (#7446)
Cmd+Alt-click (Ctrl+Alt elsewhere) is bound to Go to Implementation in the IntelliJ keymap, but the hover affordance only recognised Cmd/Ctrl alone, so the symbol kept plain text and the I-beam cursor. The navigation modifier tracker now also holds for any modifiers the active keymap binds to a navigation left-click (declaration, implementation, type declaration, super method); the LSP link hover and the plain hover popups follow it, and rebinding or removing the mouse binding removes the affordance.

Co-authored-by: happut <[email protected]>
2026-12-02 17:18:31 +08:00

107 рядки
3.8 KiB
TypeScript

/**
* The navigation hover gesture (⌘/Ctrl, or the modifiers of a bound navigation
* click such as ⌘⌥/Ctrl+Alt for implementations) as a live, window-wide state:
* it shows the navigation link and its brief info, and every plain hover popup
* (quick documentation, problems, debugger values) yields to it. Tracked on the
* window so an editor sees the key even when it isn't focused.
*/
import { bindingsFor, hasPrimaryModifier, isMacHost, mouseStrokeOf, normalizeBinding } from "./keymap";
import { notifyEach } from "./listeners";
type ModifierEvent = Pick<MouseEvent, "metaKey" | "ctrlKey" | "altKey" | "shiftKey">;
/** ⌘/Ctrl alone: no ⌥/⇧ (other gestures) and not the other platform's Ctrl/⌘. */
export function isNavigationGesture(e: ModifierEvent): boolean {
return hasPrimaryModifier(e) && !e.altKey && !e.shiftKey && !(isMacHost() ? e.ctrlKey : e.metaKey);
}
/** Left-click actions that navigate: their modifiers carry the link affordance too. */
const NAVIGATION_MOUSE_ACTIONS = [
"navigate.declaration",
"navigate.implementation",
"navigate.typeDeclaration",
"navigate.superMethod",
];
function isBoundNavigationClick(e: ModifierEvent): boolean {
const stroke = mouseStrokeOf({
button: 0,
metaKey: e.metaKey,
ctrlKey: e.ctrlKey,
altKey: e.altKey,
shiftKey: e.shiftKey,
});
return (
stroke !== null &&
NAVIGATION_MOUSE_ACTIONS.some((id) => bindingsFor(id).some((binding) => normalizeBinding(binding) === stroke))
);
}
/** The declaration gesture, or the modifiers of a navigation click the keymap binds (e.g. ⌘⌥). */
export function isNavigationHoverGesture(e: ModifierEvent): boolean {
return isNavigationGesture(e) || isBoundNavigationClick(e);
}
let held = false;
let pointer: Element | null = null;
const listeners = new Set<(held: boolean) => void>();
let installed: (() => void) | null = null;
function setHeld(value: boolean) {
if (held === value) return;
held = value;
notifyEach([...listeners], (fn) => fn(value), "navigation modifier");
}
function install() {
if (installed || typeof window === "undefined") return;
const onKey = (e: KeyboardEvent) => setHeld(isNavigationHoverGesture(e));
// Also from pointer events: a key released outside the window never reports its keyup.
const onMouse = (e: MouseEvent) => {
pointer = e.target instanceof Element ? e.target : null;
setHeld(isNavigationHoverGesture(e));
};
const onBlur = () => setHeld(false);
const opts = { capture: true, passive: true } as const;
window.addEventListener("keydown", onKey, opts);
window.addEventListener("keyup", onKey, opts);
window.addEventListener("mousemove", onMouse, opts);
window.addEventListener("mousedown", onMouse, opts);
window.addEventListener("blur", onBlur);
installed = () => {
window.removeEventListener("keydown", onKey, opts);
window.removeEventListener("keyup", onKey, opts);
window.removeEventListener("mousemove", onMouse, opts);
window.removeEventListener("mousedown", onMouse, opts);
window.removeEventListener("blur", onBlur);
installed = null;
};
}
/** Whether a navigation hover gesture (⌘/Ctrl, or a bound navigation click's modifiers) is held. */
export function navigationModifierHeld(): boolean {
install();
return held;
}
/** Element under the pointer at the last mouse move (null before any). */
export function pointerTarget(): Element | null {
install();
return pointer;
}
/** Called with the new state whenever the navigation modifier is pressed or released. */
export function onNavigationModifier(fn: (held: boolean) => void): () => void {
install();
listeners.add(fn);
return () => listeners.delete(fn);
}
/** Tests: forget the tracked state and listeners. */
export function resetNavigationModifier() {
installed?.();
held = false;
pointer = null;
listeners.clear();
}