Open History to see the last commit
1import { createContext, use, useState, type ReactNode } from "react";
2import { Platform } from "react-native";
3import { deriveDeviceType, isDuoDevice, type DeviceType } from "../lib/device-type";
4import type { NativeLayoutMetrics } from "../lib/reserved-regions";
5import { NativeLayoutObserver } from "./NativeLayoutObserver";
6
7const IS_PAD = Platform.OS === "ios" && Platform.isPad;
8
9const NativeLayoutContext = createContext<NativeLayoutMetrics | null>(null);
10// Separate from the metrics so device checks skip every resize and safe-area update.
11const DeviceTypeContext = createContext<DeviceType>(
12 deriveDeviceType({ isPad: IS_PAD, metrics: null }),
13);
14// Column reservations come from UIKit's layout, outside the resizing navigator.
15export const NativeColumnLayoutMetricsContext = createContext<NativeLayoutMetrics | null>(null);
16
17// The latest metrics outside React, for decisions made before the first screen renders.
18let latestMetrics: NativeLayoutMetrics | null = null;
19const metricsWaiters = new Set<(metrics: NativeLayoutMetrics) => void>();
20
21/** Resolves with the first native layout metrics, or null after `timeoutMs` without any. */
22export function firstNativeLayoutMetrics(timeoutMs: number) {
23 if (latestMetrics) return Promise.resolve(latestMetrics);
24 return new Promise<NativeLayoutMetrics | null>((resolve) => {
25 const settle = (metrics: NativeLayoutMetrics | null) => {
26 metricsWaiters.delete(settle);
27 clearTimeout(timeout);
28 resolve(metrics);
29 };
30 const timeout = setTimeout(() => settle(null), timeoutMs);
31 metricsWaiters.add(settle);
32 });
33}
34
35export function currentNativeLayoutMetrics() {
36 return latestMetrics;
37}
38
39export function NativeLayoutMetricsProvider(props: { readonly children: ReactNode }) {
40 const [metrics, setMetrics] = useState<NativeLayoutMetrics | null>(null);
41 const publish = (next: NativeLayoutMetrics) => {
42 latestMetrics = next;
43 for (const settle of [...metricsWaiters]) settle(next);
44 setMetrics(next);
45 };
46 const deviceType = deriveDeviceType({ isPad: IS_PAD, metrics });
47 return (
48 <NativeLayoutContext value={metrics}>
49 <DeviceTypeContext value={deviceType}>
50 <NativeLayoutObserver onChange={publish} />
51 {props.children}
52 </DeviceTypeContext>
53 </NativeLayoutContext>
54 );
55}
56
57export function useNativeLayoutMetrics() {
58 return use(NativeLayoutContext);
59}
60
61export function useNativeColumnLayoutMetrics() {
62 return use(NativeColumnLayoutMetricsContext);
63}
64
65export function useDeviceType() {
66 return use(DeviceTypeContext);
67}
68
69/**
70 * Where the Duo's floating bottom controls end in this column. Its tab bar floats below the safe
71 * area, and the controls beside it sit on the same line.
72 */
73export function useDuoBottomBarInset() {
74 const deviceType = useDeviceType();
75 const metrics = use(NativeColumnLayoutMetricsContext);
76 // Sheets have no rail to align with and can report zero for the bar region.
77 // Their controls must keep using the sheet's bottom safe area.
78 const inset = metrics?.bottomBarInset;
79 return isDuoDevice(deviceType) && inset !== undefined && inset > 0 ? inset : undefined;
80}