Open History to see the last commit
1import { useCallback, useRef, useState, type ComponentProps } from "react";
2import {
3 useWindowDimensions,
4 View,
5 type NativeScrollEvent,
6 type NativeSyntheticEvent,
7} from "react-native";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9import { NewThreadButton } from "../../components/NewThreadButton";
10import {
11 androidHomeFabPosition,
12 type AndroidHomeFabLayout as SharedAndroidHomeFabLayout,
13} from "./AndroidHomeFab.shared";
14import { useWorkspaceState } from "../../state/workspace";
15import { FabScrollContext } from "./FabScrollContext";
16import { updateFabScroll } from "./fab-scroll";
17import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
18
19export function AndroidHomeFabLayout(props: ComponentProps<typeof SharedAndroidHomeFabLayout>) {
20 const insets = useSafeAreaInsets();
21 const { appearance } = useAppearancePreferences();
22 const { fontScale } = useWindowDimensions();
23 const [layoutWidth, setLayoutWidth] = useState<number | null>(null);
24 const { state } = useWorkspaceState();
25 const [expanded, setExpanded] = useState(true);
26 const scrollState = useRef({ anchor: 0, expanded: true });
27 const onScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
28 const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
29 const next = updateFabScroll(
30 scrollState.current,
31 contentOffset.y,
32 contentSize.height - layoutMeasurement.height,
33 );
34 if (next.expanded !== scrollState.current.expanded) setExpanded(next.expanded);
35 scrollState.current = next;
36 }, []);
37
38 // Remount only the FAB when its font or pane changes to clear the cached expanded width.
39 // The split-view sidebar has no FAB: the navigation rail carries New thread. On phones it
40 // keeps Material's 16dp corner margin; the bottom bar already consumes the gesture inset.
41 return (
42 <View className="flex-1" onLayout={(event) => setLayoutWidth(event.nativeEvent.layout.width)}>
43 <FabScrollContext value={onScroll}>{props.children}</FabScrollContext>
44 {!props.disabled && !props.sidebar && state.hasConnections && layoutWidth !== null ? (
45 <NewThreadButton
46 key={`${appearance.baseFontSize}:${fontScale}:${layoutWidth}`}
47 extended
48 expanded={expanded}
49 onPress={props.onStartNewTask}
50 className="absolute"
51 style={androidHomeFabPosition(insets.bottom, props.sidebar)}
52 />
53 ) : null}
54 </View>
55 );
56}