import { MenuView, type MenuAction, type NativeActionEvent } from "@react-native-menu/menu"; import { useEffect, useImperativeHandle, useRef, useState, type Ref } from "react"; import { Modal, Platform, Pressable, ScrollView, StyleSheet, useColorScheme, View, type StyleProp, type ViewStyle } from "react-native"; import { Text } from "@/components/fixed-text"; import { useAppWindowDimensions as useWindowDimensions } from "@/lib/window-dimensions"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import Animated, { Easing, FadeIn, FadeOut } from "react-native-reanimated"; import { haptics } from "@/lib/haptics"; import { colors, palette } from "@/theme/colors"; import { radius, type } from "@/theme/type"; import { Icon } from "./icon"; // Menus: the system's UIMenu on iOS; on Android a floating card in the app's // own style (Android's popup menu is a dated checkbox list). Same props as // MenuView, so callers don't care which they get. export interface AppMenuProps { title?: string; actions: MenuAction[]; onPressAction: (event: NativeActionEvent) => void; onOpenMenu?: () => void; shouldOpenOnLongPress?: boolean; style?: StyleProp; children: React.ReactNode; } export interface FloatingMenuHandle { open(): void; } /** * The floating card opened by the caller (a row's own long press), for * children that handle their own touches — wrapping them in another Pressable * would never see the long press. On iOS too: a UIMenu wrapped round a row * takes its taps, and a Link's context menu hid the row from VoiceOver. */ export function OwnedFloatingMenu({ handle, ...props }: Omit & { handle: Ref }) { return ; } /** * The new-architecture MenuView always sends an image colour, 0 (clear) when * none is given, and the menu tints the symbol with it: every icon came out * invisible. Give each one the colour the menu would have used. */ function tinted(actions: MenuAction[], dark: boolean): MenuAction[] { return actions.map((action) => ({ ...action, ...(action.image && action.imageColor === undefined ? { imageColor: action.attributes?.destructive ? (dark ? "#ff453a" : "#ff3b30") : dark ? palette.dark.label : palette.light.label } : {}), ...(action.subactions ? { subactions: tinted(action.subactions, dark) } : {}), })); } export function AppMenu(props: AppMenuProps) { const dark = useColorScheme() === "dark"; if (Platform.OS === "ios") { return ( {props.children} ); } return ; } interface Anchor { geometry: string; x: number; y: number; width: number; height: number; } const CARD_MIN = 232; const GUTTER = 12; function FloatingMenu({ title, actions, onPressAction, onOpenMenu, shouldOpenOnLongPress, style, children, handle, }: AppMenuProps & { handle?: Ref }) { const anchorRef = useRef(null); const measurement = useRef(0); const [anchor, setAnchor] = useState(null); const [headerHeight, setHeaderHeight] = useState(0); // Submenus push onto this stack. const [stack, setStack] = useState<{ title?: string; actions: MenuAction[] }[]>([]); const window = useWindowDimensions(); const insets = useSafeAreaInsets(); const geometry = [window.width, window.height, insets.top, insets.right, insets.bottom, insets.left].join(":"); // Native measurement can finish after rotation, dismissal, or unmount. useEffect(() => { measurement.current += 1; setAnchor(null); return () => { measurement.current += 1; }; }, [geometry]); const open = () => { const request = ++measurement.current; anchorRef.current?.measureInWindow((x, y, width, height) => { if (request !== measurement.current || width <= 0 || height <= 0) return; haptics.selection(); onOpenMenu?.(); setStack([{ title, actions }]); setAnchor({ geometry, x, y, width, height }); }); }; useImperativeHandle(handle, () => ({ open })); const close = () => { measurement.current += 1; setAnchor(null); }; const choose = (action: MenuAction) => { if (action.subactions?.length) { haptics.selection(); setStack((current) => [...current, { title: action.title, actions: action.subactions! }]); return; } close(); if (action.id) onPressAction({ nativeEvent: { event: action.id } } as NativeActionEvent); }; const level = stack[stack.length - 1]; const safeLeft = insets.left + GUTTER; const safeRight = window.width - insets.right - GUTTER; const safeTop = insets.top + GUTTER; const safeBottom = window.height - insets.bottom - GUTTER; const width = Math.max(0, Math.min(safeRight - safeLeft, Math.max(CARD_MIN, anchor?.width ?? CARD_MIN))); const left = Math.max(safeLeft, Math.min(anchor?.x ?? safeLeft, safeRight - width)); const anchorTop = Math.max(safeTop, Math.min(anchor?.y ?? safeTop, safeBottom)); const anchorBottom = Math.max(safeTop, Math.min((anchor?.y ?? safeTop) + (anchor?.height ?? 0), safeBottom)); const below = Math.max(0, safeBottom - anchorBottom - 6); const above = Math.max(0, anchorTop - safeTop - 6); const openUp = below < 280 && above > below; const maxHeight = openUp ? above : below; const dark = useColorScheme() === "dark"; return ( <> {handle ? ( {children} ) : ( {children} )} {anchor && anchor.geometry === geometry && level ? ( {/* The page dims and the row it belongs to stays lit above it, the way a context menu lifts its source. */} {stack.length > 1 || level.title ? ( setHeaderHeight(event.nativeEvent.layout.height)} style={{ flexDirection: "row", alignItems: "center", gap: 6, paddingHorizontal: 14, paddingTop: 12, paddingBottom: 6 }}> {stack.length > 1 ? ( setStack((current) => current.slice(0, -1))} accessibilityRole="button" accessibilityLabel="返回" style={{ width: 44, height: 44, alignItems: "center", justifyContent: "center" }}> ) : null} {/* Two lines: a title can carry a short note on what the choice does. */} {level.title} ) : null} 1 || level.title ? headerHeight : 0)) }} contentContainerStyle={{ paddingVertical: 6 }}> {level.actions.map((action, index) => { const disabled = action.attributes?.disabled === true; const destructive = action.attributes?.destructive === true; const checked = action.state === "on"; return ( choose(action)} android_ripple={{ color: colors.fill as string }} style={({ pressed }) => ({ flexDirection: "row", alignItems: "center", gap: 10, minHeight: 48, paddingHorizontal: 16, paddingVertical: 8, opacity: disabled ? 0.4 : 1, // A held row is washed with `fill`, as list rows are; Android has its ripple. backgroundColor: pressed && Platform.OS !== "android" ? colors.fill : undefined, })} > {action.title} {action.subtitle ? ( {action.subtitle} ) : null} {action.subactions?.length ? ( ) : checked ? ( ) : action.image && Platform.OS === "ios" ? ( // Actions name SF Symbols; Android's card goes without, as its own menus do. ) : null} ); })} {/* Over the card, so its shadow doesn't fall across the row. */} {handle ? ( {children} ) : null} ) : null} ); }