Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Menu): add missing arg to showContextMenu and document it #116

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/components/Dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ReactNode, FC } from 'react';
import { CommonUIModule } from '../webpack';
import { ItemProps } from './Item';
import { createPropListRegex } from '../utils';
import type { ContextMenuPositionOptions } from './Menu';

export interface SingleDropdownOption {
data: any;
Expand All @@ -20,19 +21,14 @@ export interface MultiDropdownOption {

export type DropdownOption = SingleDropdownOption | MultiDropdownOption;

export interface DropdownMenuPositionOptions {
[_: string]: unknown
bMatchWidth?: boolean
}

export interface DropdownProps {
rgOptions: DropdownOption[];
selectedOption: any;
disabled?: boolean;
onMenuWillOpen?(showMenu: () => void): void;
onMenuOpened?(): void;
onChange?(data: SingleDropdownOption): void;
contextMenuPositionOptions?: DropdownMenuPositionOptions;
contextMenuPositionOptions?: ContextMenuPositionOptions;
menuLabel?: string;
strDefaultLabel?: string;
renderButtonValue?(element: ReactNode): ReactNode;
Expand Down
125 changes: 122 additions & 3 deletions src/components/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,128 @@ import { FC, ReactNode } from 'react';
import { Export, findModuleByExport, findModuleExport } from '../webpack';
import { FooterLegendProps } from './FooterLegend';

export const showContextMenu: (children: ReactNode, parent?: EventTarget) => void = findModuleExport(
(e: Export) => typeof e === 'function' && e.toString().includes('GetContextMenuManagerFromWindow(')
&& e.toString().includes('.CreateContextMenuInstance('),
interface PopupCreationOptions {
/**
* Initially hidden, make it appear with {@link ContextMenuInstance.Show}.
*/
bCreateHidden?: boolean;

bModal?: boolean;

/**
* Document title.
*/
title?: string;
}

// Separate interface, since one of webpack module exports uses this exact object,
// so maybe it could be reused elsewhere.
interface MonitorOptions {
targetMonitor: {
flMonitorScale: number;
nScreenLeft: number;
nScreenTop: number;
nScreenWidth: number;
nScreenHeight: number;
};
flGamepadScale: number;
}

export interface ContextMenuPositionOptions extends PopupCreationOptions, Partial<MonitorOptions> {
/**
* When {@link bForcePopup} is true, makes the window appear above everything else.
*/
bAlwaysOnTop?: boolean;

/**
* Disables the mouse overlay, granting the ability to click anywhere while
* the menu's active.
*/
bDisableMouseOverlay?: boolean;

/**
* Disables the {@link bPreferPopTop} behavior.
*/
bDisablePopTop?: boolean;

bFitToWindow?: boolean;

/**
* Forces the menu to open in a separate window instead of inside the parent.
*/
bForcePopup?: boolean;

/**
* Like {@link bMatchWidth}, but don't shrink below the menu's minimum width.
*/
bGrowToElementWidth?: boolean;

/**
* Match the parent's exact height.
*/
bMatchHeight?: boolean;

/**
* Match the parent's exact width.
*/
bMatchWidth?: boolean;

bNoFocusWhenShown?: boolean;

/**
* Makes the menu **invisible**, instead of getting removed from the DOM.
*/
bRetainOnHide?: boolean;

bScreenCoordinates?: boolean;

/**
* Set to `true` to not account for the parent's width.
*/
bOverlapHorizontal?: boolean;

/**
* Set to `true` to not account for the parent's height.
*/
bOverlapVertical?: boolean;

/**
* Set to `true` to make the entire menu try to appear on the left side from
* the parent.
*/
bPreferPopLeft?: boolean;

/**
* Set to `true` to make the entire menu try to appear above the parent.
*/
bPreferPopTop?: boolean;

bShiftToFitWindow?: boolean;

// different window creation flags (StandaloneContextMenu vs PopupContextMenu)
bStandalone?: boolean;

/**
* Class name **replacement** for the container element, i.e. it replaces the
* default class.
*/
strClassName?: string;
}

interface ContextMenuInstance {
Hide(): void;
Show(): void;
}

export const showContextMenu: (
children: ReactNode,
parent?: EventTarget,
options?: ContextMenuPositionOptions,
) => ContextMenuInstance = findModuleExport(
(e: Export) =>
typeof e === 'function' &&
e.toString().includes('GetContextMenuManagerFromWindow(') &&
e.toString().includes('.CreateContextMenuInstance('),
);

export interface MenuProps extends FooterLegendProps {
Expand Down