Files
Netcatty/components/terminal/runtime/middleClickBehavior.ts
T
chin 427ef3ee70 fix(terminal): honor fullscreen-apps menu setting in capture-phase interceptor
Copilot review caught that the new showContextMenuOverFullscreenApps flag
only affected the TerminalContextMenu suppression helpers, but the
capture-phase contextmenu interceptor in useTerminalEffects
(shouldInterceptMouseTrackingContextMenu) still called preventDefault +
stopImmediatePropagation on every non-Shift right-click while mouse
tracking was active. That killed the contextmenu event before it could
reach Radix, so the menu never opened even with the setting on.

Route an unmodified right-click through the same path as Shift+right-click
when the setting is on and rightClickBehavior is 'context-menu':
- shouldInterceptMouseTrackingContextMenu returns false (let contextmenu
  reach Radix so the app menu opens)
- shouldStopShiftRightClickMouseTrackingMouseDown returns true (stop the
  button press from reaching the TUI, so tmux/vim don't also react)

The setting is menu-only: paste / select-word right-click behaviors are
unchanged. Adds predicate coverage in middleClickBehavior.test.ts.
2026-08-01 13:38:44 +08:00

160 lines
5.0 KiB
TypeScript

import type { MiddleClickBehavior, RightClickBehavior, TerminalSettings } from "../../../domain/models";
type MiddleClickSettings = Partial<Pick<TerminalSettings, "middleClickBehavior" | "middleClickPaste">>;
const MIDDLE_CONTEXT_MENU_EVENT_KEY = "__netcattyMiddleContextMenu";
type MiddleClickContextMenuEvent = MouseEvent & {
[MIDDLE_CONTEXT_MENU_EVENT_KEY]?: boolean;
};
const SHIFT_SELECTION_REPLAY_EVENT_KEY = "__netcattyShiftSelectionReplay";
type ShiftSelectionReplayMouseEvent = MouseEvent & {
[SHIFT_SELECTION_REPLAY_EVENT_KEY]?: boolean;
};
export interface MouseTrackingContextMenuCaptureState {
event: MouseEvent;
mouseTracking: boolean;
status?: string | null;
/** The user's configured right-click action. */
rightClickBehavior?: RightClickBehavior;
/** When true, show the app context menu over fullscreen apps (tmux/vim). */
forceMenuInAlternateScreen?: boolean;
}
export interface ShiftMouseSelectionReplayState {
event: MouseEvent;
mouseTracking: boolean;
status?: string | null;
isMacPlatform: boolean;
}
export interface ShiftRightClickMouseDownCaptureState {
event: MouseEvent;
mouseTracking: boolean;
status?: string | null;
/** The user's configured right-click action. */
rightClickBehavior?: RightClickBehavior;
/** When true, show the app context menu over fullscreen apps (tmux/vim). */
forceMenuInAlternateScreen?: boolean;
}
export const resolveMiddleClickBehavior = (
settings?: MiddleClickSettings | null,
): MiddleClickBehavior => {
const behavior = settings?.middleClickBehavior;
if (
behavior === "context-menu" ||
behavior === "paste" ||
behavior === "disabled"
) {
return behavior;
}
return settings?.middleClickPaste === false ? "disabled" : "paste";
};
export const markMiddleClickContextMenuEvent = (event: MouseEvent): MouseEvent => {
Object.defineProperty(event, MIDDLE_CONTEXT_MENU_EVENT_KEY, {
value: true,
configurable: true,
});
return event;
};
export const isMiddleClickContextMenuEvent = (event: MouseEvent): boolean =>
(event as MiddleClickContextMenuEvent)[MIDDLE_CONTEXT_MENU_EVENT_KEY] === true;
export const markShiftSelectionReplayMouseEvent = (event: MouseEvent): MouseEvent => {
Object.defineProperty(event, SHIFT_SELECTION_REPLAY_EVENT_KEY, {
value: true,
configurable: true,
});
return event;
};
export const isShiftSelectionReplayMouseEvent = (event: MouseEvent): boolean =>
(event as ShiftSelectionReplayMouseEvent)[SHIFT_SELECTION_REPLAY_EVENT_KEY] === true;
// When the "show context menu over fullscreen apps" setting is on and the
// right-click action is the context menu, an unmodified right-click should
// behave like Shift+right-click: let the contextmenu event through so Radix
// opens the app menu, and stop the button press from reaching the TUI. Paste /
// select-word actions are unaffected — the setting is about the menu only.
const forcesMenuOverMouseTracking = ({
rightClickBehavior,
forceMenuInAlternateScreen,
}: {
rightClickBehavior?: RightClickBehavior;
forceMenuInAlternateScreen?: boolean;
}): boolean => Boolean(forceMenuInAlternateScreen && rightClickBehavior === "context-menu");
export const shouldInterceptMouseTrackingContextMenu = ({
event,
mouseTracking,
status,
rightClickBehavior,
forceMenuInAlternateScreen,
}: MouseTrackingContextMenuCaptureState): boolean =>
mouseTracking
&& status === "connected"
&& !event.shiftKey
&& !isMiddleClickContextMenuEvent(event)
&& !forcesMenuOverMouseTracking({ rightClickBehavior, forceMenuInAlternateScreen });
export const shouldReplayShiftMouseSelectionAsMacOption = ({
event,
mouseTracking,
status,
isMacPlatform,
}: ShiftMouseSelectionReplayState): boolean =>
isMacPlatform
&& mouseTracking
&& status === "connected"
&& event.button === 0
&& event.shiftKey
&& !event.altKey
&& !event.ctrlKey
&& !event.metaKey
&& !isShiftSelectionReplayMouseEvent(event);
export const shouldStopShiftRightClickMouseTrackingMouseDown = ({
event,
mouseTracking,
status,
rightClickBehavior,
forceMenuInAlternateScreen,
}: ShiftRightClickMouseDownCaptureState): boolean =>
mouseTracking
&& status === "connected"
&& event.button === 2
&& (event.shiftKey || forcesMenuOverMouseTracking({ rightClickBehavior, forceMenuInAlternateScreen }));
export const createMacOptionForcedSelectionMouseEvent = (event: MouseEvent): MouseEvent =>
markShiftSelectionReplayMouseEvent(new MouseEvent(event.type, {
bubbles: event.bubbles,
cancelable: event.cancelable,
composed: event.composed,
detail: event.detail,
view: event.view,
screenX: event.screenX,
screenY: event.screenY,
clientX: event.clientX,
clientY: event.clientY,
ctrlKey: event.ctrlKey,
altKey: true,
shiftKey: false,
metaKey: event.metaKey,
button: event.button,
buttons: event.buttons,
relatedTarget: event.relatedTarget,
}));
export const captureMiddleClickTerminalMouseEvent = (event: MouseEvent): boolean => {
if (event.button !== 1) return false;
event.preventDefault();
event.stopImmediatePropagation();
return true;
};