import { AppMenu } from "@/components/app-menu"; import { Stack } from "expo-router"; import { Platform, Pressable, View } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { useContentWidth } from "@/lib/content-width"; import { haptics } from "@/lib/haptics"; import { usesCustomLiveToolbarItem } from "@/lib/home-layout"; import { colors } from "@/theme/colors"; import { Icon, type IconProps } from "./icon"; import { StatusDot } from "./status"; type Glyph = Pick; export interface HeaderMenuItem { title: string; /** A line under the title, for an action whose name alone doesn't say what it does. */ subtitle?: string; icon: Glyph; destructive?: boolean; /** Items sharing one, next to each other, are drawn as one group between separators (iOS). */ section?: string; /** A submenu of these instead of an action (iOS); Android lists them in place. */ children?: HeaderMenuItem[]; /** A child's title when listed in place, if the parent's title before its own doesn't read. */ flatTitle?: string; onPress?: () => void; } /** Submenus spelled out in place, for a menu that has none. */ function flat(items: HeaderMenuItem[]): HeaderMenuItem[] { return items.flatMap((item) => item.children?.map((child) => ({ ...child, title: child.flatTitle ?? `${item.title}${child.title}` })) ?? [item]); } /** Consecutive items of one section, in order. */ function sections(items: HeaderMenuItem[]): HeaderMenuItem[][] { const out: HeaderMenuItem[][] = []; for (const item of items) { const last = out[out.length - 1]; if (last && last[0]!.section === item.section) last.push(item); else out.push([item]); } return out; } export type HeaderAction = | { kind: "button"; key: string; icon: Glyph; label: string; /** Given: the button can show the app's live dot at its icon's corner (true: something behind it is at work). */ live?: boolean; onPress: () => void; } | { kind: "menu"; key: string; icon: Glyph; label: string; items: HeaderMenuItem[] }; /** * Header buttons for both platforms: iOS gets the system toolbar (liquid glass * buttons, UIMenu), Android gets Material icon buttons and a popup menu, since * the Android toolbar can't draw SF Symbols. */ export function HeaderActions({ actions }: { actions: HeaderAction[] }) { const systemInsets = useSafeAreaInsets(); if (Platform.OS === "ios") { return ( {actions.map((action) => action.kind === "button" ? usesCustomLiveToolbarItem(Platform.OS, systemInsets, action.live) ? ( ) : ( {action.label} ) : ( {action.label} {sections(action.items).map((group) => ( {group.map((item) => item.children ? ( {item.children.map((child) => ( {child.title} ))} ) : ( {item.title} ))} ))} ), )} ); } return }} />; } /** * The widest a custom header title can be beside the back button and `count` * actions. The native header never sizes a custom title to the room it has, so * a wider one slides under the buttons. */ export function useHeaderTitleWidth(count: number): number { const width = useContentWidth(); // iOS 26: the back button is a 44pt circle 16 from the edge; the actions share // one capsule, 44 each and 14 apart. The bar keeps 12 between it and the title. // Android: the title starts 56 in, then 44 per icon button and a 16 margin. const room = Platform.OS === "ios" ? width - (16 + 44 + 12) - (12 + 44 * count + 14 * Math.max(count - 1, 0) + 16) : width - 56 - (12 + 44 * count + 16); return Math.max(80, Math.min(room, 320)); } function IconButton({ icon, label, live, onPress }: { icon: Glyph; label: string; live?: boolean; onPress?: () => void }) { // Inside a menu the menu handles the tap; a nested pressable would swallow it. if (!onPress) { return ( ); } return ( {live ? ( ) : null} ); } function MaterialActions({ actions }: { actions: HeaderAction[] }) { return ( {actions.map((action) => action.kind === "button" ? ( ) : ( ({ id: String(index), title: item.title, attributes: { destructive: item.destructive }, }))} onOpenMenu={() => haptics.selection()} onPressAction={({ nativeEvent }) => flat(action.items)[Number(nativeEvent.event)]?.onPress?.()} shouldOpenOnLongPress={false} > ), )} ); }