2024-02-23 02:26:42 +08:00
|
|
|
import { findByProps } from "@lib/metro/filters";
|
2024-02-28 14:09:42 +08:00
|
|
|
import { ReactNative } from "@/core/preinit";
|
|
|
|
|
import { instead } from "@lib/api/patcher";
|
2024-02-09 12:33:39 +08:00
|
|
|
|
2024-02-23 02:26:42 +08:00
|
|
|
const { ContextMenu: _ContextMenu } = findByProps("ContextMenu");
|
|
|
|
|
const { AccessibilityInfo } = ReactNative
|
2024-02-09 12:33:39 +08:00
|
|
|
|
2024-02-23 02:26:42 +08:00
|
|
|
type ContextMenuItem = {
|
|
|
|
|
label: string,
|
|
|
|
|
iconSource: import("react-native").ImageSourcePropType,
|
|
|
|
|
action: () => unknown,
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ContextMenuProps = {
|
2024-02-09 12:33:39 +08:00
|
|
|
triggerOnLongPress: boolean;
|
2024-02-23 02:26:42 +08:00
|
|
|
items: ContextMenuItem[] | ContextMenuItem[][];
|
2024-02-09 12:33:39 +08:00
|
|
|
align: "left" | "right" | "above" | "below" | "auto" | null;
|
|
|
|
|
title: string;
|
|
|
|
|
children: React.FC;
|
|
|
|
|
[key: string]: any;
|
2024-02-23 02:26:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function ContextMenu(props: ContextMenuProps) {
|
|
|
|
|
const ref = React.useRef(null);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
// hacky discord fix for android where pressing back with context menu open crashes the client
|
|
|
|
|
if (ReactNative.Platform.OS !== "android") return;
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const ctxMenuReactTag = ref.current?._children?.[0]?._nativeTag;
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (!ctxMenuReactTag) return;
|
|
|
|
|
|
|
|
|
|
const unpatch = instead("setAccessibilityFocus", AccessibilityInfo, ([tag], orig) => {
|
|
|
|
|
if (tag !== ctxMenuReactTag) {
|
|
|
|
|
return orig.apply(AccessibilityInfo, [tag]);
|
|
|
|
|
} else {
|
|
|
|
|
unpatch(); // and skip this call
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return <ReactNative.View ref={ref}>
|
|
|
|
|
<_ContextMenu
|
|
|
|
|
{...props}
|
|
|
|
|
title={props.title}
|
|
|
|
|
children={props.children}
|
|
|
|
|
triggerOnLongPress={props.triggerOnLongPress}
|
|
|
|
|
items={props.items}
|
|
|
|
|
align={props.align}
|
|
|
|
|
/>
|
|
|
|
|
</ReactNative.View>
|
|
|
|
|
}
|