Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AndroidWorkspaceNavigation.android.tsx

204 lines7.5 KB

1import { selectedNavigationTab, visibleNavigationTabs } from "./navigation-tab-order";
2import { requireNativeView } from "expo";
3import { CommonActions, useNavigation } from "@react-navigation/native";
4import {
5 Keyboard,
6 processColor,
7 useWindowDimensions,
8 View,
9 type NativeSyntheticEvent,
10 type ViewProps,
11} from "react-native";
12import { useSafeAreaInsets } from "react-native-safe-area-context";
13import { useKeyboardState } from "react-native-keyboard-controller";
14​
15import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing";
16import { useToolbarLayout } from "../../components/ui/useToolbarLayout";
17import { rootNavigationRef } from "../../lib/rootNavigation";
18import { useWorkspaceConnectionState } from "../../state/workspace";
19import { androidWorkspaceNavigationColors } from "./android-workspace-navigation-colors";
20import {
21 androidWorkspaceTabNavigationState,
22 type AndroidWorkspaceNavigationTab,
23} from "./android-workspace-tabs";
24import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
25import { useAndroidWorkspaceRoot } from "./AndroidWorkspaceTabsProvider";
26import { useNavigationTabOrder, useHiddenNavigationTabs } from "./navigation-tabs";
27import { useWorkspaceTabs } from "./WorkspaceTabsContext";
28import { FLOATING_TABS, floatingTabsMetrics } from "./floating-workspace-tabs";
29​
30// Matches RAIL_WIDTH in the native rail.
31export const ANDROID_WORKSPACE_RAIL_WIDTH = 72;
32​
33interface NativeNavigationProps extends ViewProps {
34 readonly rail: boolean;
35 readonly floating: boolean;
36 readonly tabs: readonly string[];
37 readonly selectedTab: AndroidWorkspaceNavigationTab;
38 readonly dark: boolean;
39 readonly textScale: number;
40 readonly newEnabled: boolean;
41 readonly safeAreaTop: number;
42 readonly safeAreaBottom: number;
43 readonly headerHeight: number;
44 readonly containerColor: number;
45 readonly selectedColor: number;
46 readonly indicatorColor: number;
47 readonly unselectedColor: number;
48 readonly actionColor: number;
49 readonly actionContentColor: number;
50 readonly onTabSelected: (
51 event: NativeSyntheticEvent<{ readonly tab: AndroidWorkspaceNavigationTab }>,
52 ) => void;
53 readonly onNewThread: () => void;
54 readonly onOverlayChange: (event: NativeSyntheticEvent<{ readonly overlay: boolean }>) => void;
56​
57const NativeNavigation = requireNativeView<NativeNavigationProps>("T3WorkspaceNavigation");
58​
59function nativeColor(value: string) {
60 const color = processColor(value);
61 return typeof color === "number" ? color : 0;
63​
64export function AndroidWorkspaceNavigation(props: {
65 readonly onNewThread: () => void;
66 readonly rail?: boolean;
67 readonly floating?: boolean;
68 readonly fillWidth?: boolean;
69 readonly paneWidth?: number;
70 readonly onSelect?: () => void;
71 /** The rail needs the whole workspace while it widens over the list. */
72 readonly onOverlayChange?: (overlay: boolean) => void;
73}) {
74 const workspace = useWorkspaceTabs()!;
75 const order = useNavigationTabOrder();
76 const hidden = useHiddenNavigationTabs();
77 const root = useAndroidWorkspaceRoot();
78 const navigation = useNavigation();
79 const insets = useSafeAreaInsets();
80 const toolbar = useToolbarLayout();
81 const connectionState = useWorkspaceConnectionState();
82 const keyboardVisible = useKeyboardState((state) => state.isVisible);
83 const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
84 const { scale, fabSize } = useAndroidControlSizing();
85 const window = useWindowDimensions();
86 const rail = Boolean(props.rail);
87 const floating = !rail && Boolean(props.floating);
88 const tabs = [...visibleNavigationTabs(order, hidden, rail ? 5 : 4), "settings"];
89 const { containerColor, indicatorColor, selectedColor, unselectedColor } =
90 androidWorkspaceNavigationColors(colors, rail, floating);
91 const selectedTab = selectedNavigationTab(
92 order,
93 workspace.state.selectedTab,
94 root?.settingsOpen ?? false,
95 hidden,
96 rail ? 5 : 4,
97 );
98 // FloatingWorkspaceTabs starts the bar at the left inset, so a cutout narrows the room.
99 const barWidth =
100 props.paneWidth ??
101 window.width - (Math.max(insets.left, FLOATING_TABS.edge) - FLOATING_TABS.edge);
102 // A phone draws compose on the Chats list only; the split sidebar draws it on every tab
103 // except the Settings and Usage pages.
104 const composeBesideBar =
105 props.paneWidth === undefined
106 ? selectedTab === "threads"
107 : selectedTab !== "settings" && selectedTab !== "usage";
108 const metrics = floatingTabsMetrics(
109 tabs.length,
110 scale,
111 fabSize,
112 barWidth,
113 props.fillWidth,
114 composeBesideBar && connectionState.hasConnections,
115 );
116 if (!rail && keyboardVisible) return null;
117​
118 return (
119 <View
120 style={
121 // The rail draws its own surface and pads the system bars; while open it spans the
122 // workspace, so the rest of it must stay transparent.
123 rail
124 ? { flex: 1 }
125 : floating
126 ? undefined
127 : { backgroundColor: containerColor, paddingBottom: insets.bottom }
128 }
129 >
130 <NativeNavigation
131 rail={rail}
132 floating={floating}
133 tabs={tabs}
134 selectedTab={selectedTab}
135 dark={themeAppearance === "dark"}
136 textScale={scale}
137 newEnabled={connectionState.hasConnections}
138 safeAreaTop={toolbar.paddingTop}
139 safeAreaBottom={insets.bottom}
140 headerHeight={toolbar.height}
141 containerColor={nativeColor(containerColor)}
142 selectedColor={nativeColor(selectedColor)}
143 indicatorColor={nativeColor(indicatorColor)}
144 unselectedColor={nativeColor(unselectedColor)}
145 actionColor={nativeColor(colors["--color-primary"])}
146 actionContentColor={nativeColor(colors["--color-primary-foreground"])}
147 onTabSelected={(event) => {
148 Keyboard.dismiss();
149 const tab = event.nativeEvent.tab;
150 if (tab !== "settings") workspace.dispatch({ type: "select", tab });
151 // Reset the root stack even when a nested settings navigator has focus.
152 // The provider renders inside the root stack's layout, so the container's root state is
153 // that stack's current state.
154 const rootState = rootNavigationRef.getRootState();
155 if (root && rootState) {
156 navigation.dispatch({
157 ...CommonActions.reset(androidWorkspaceTabNavigationState(rootState, tab, rail)),
158 target: root.key,
159 });
160 }
161 props.onSelect?.();
162 }}
163 onNewThread={() => {
164 Keyboard.dismiss();
165 props.onNewThread();
166 }}
167 onOverlayChange={(event) => props.onOverlayChange?.(event.nativeEvent.overlay)}
168 style={
169 rail
170 ? { flex: 1 }
171 : floating
172 ? {
173 width: metrics.pill + 2 * FLOATING_TABS.shadow,
174 height: metrics.height + 2 * FLOATING_TABS.shadow,
175 }
176 : { height: 80 * Math.max(1, scale) }
177 }
178 />
179 </View>
180 );
182​
183export function FloatingWorkspaceTabs(props: {
184 readonly fillWidth?: boolean;
185 readonly onNewThread: () => void;
186 readonly paneWidth?: number;
187 readonly onSelect?: () => void;
188}) {
189 const insets = useSafeAreaInsets();
190 return (
191 <View
192 pointerEvents="box-none"
193 style={{
194 position: "absolute",
195 left: Math.max(insets.left, FLOATING_TABS.edge) - FLOATING_TABS.shadow,
196 right: 0,
197 bottom: insets.bottom + FLOATING_TABS.bottom - FLOATING_TABS.shadow,
198 alignItems: "flex-start",
199 }}
200 >
201 <AndroidWorkspaceNavigation {...props} floating />
202 </View>
203 );