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

WorkspaceToolbar.ios.tsx

260 lines10.7 KB

1import { Toolbar } from "../../components/ui/Toolbar";
2import { useFocusEffect, useNavigation } from "@react-navigation/native";
3import { use, useCallback, useEffect, useRef, type ReactNode } from "react";
4import { Platform, Pressable, type TextInputInstance } from "react-native";
5import { useReanimatedKeyboardAnimation } from "react-native-keyboard-controller";
6import Animated, { useAnimatedStyle } from "react-native-reanimated";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8import { isDuoDevice } from "../../lib/device-type";
9import { useDeviceType, useDuoBottomBarInset } from "../../native/native-layout-metrics";
10import type { SearchBarCommands } from "react-native-screens";
11​
12import { SymbolView } from "../../components/ui/AppSymbol";
13import { GlassSurface } from "../../components/ui/GlassSurface";
14import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
15import { NativeStackScreenOptions } from "../../native/StackHeader";
16import { NativeWorkspaceTabActivityContext } from "../../native/v5-workspace-context";
17import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
18import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
19import { useWorkspaceTabs } from "./WorkspaceTabsContext";
20import { WorkspaceSearchField } from "./WorkspaceSearchField";
21import { WORKSPACE_TOOLBAR } from "./workspace-tabs-state";
22import { useSidebarWorkspaceTabs } from "./useSystemWorkspaceTabBar";
23import { useSelectWorkspaceTab } from "./useSelectWorkspaceTab";
24import { useMobileNavigationTheme } from "../../lib/useMobileNavigationTheme";
25​
26// Tabler's git-pull-request geometry, shared with the UIKit tab bar.
27const PULL_REQUEST_ICON = require("../../../assets/icons/pull-request.png");
28​
29/**
30 * Rises once the keyboard reaches its content. UIKit also reports keyboards without height, with a
31 * hardware keyboard and around a fold, and those must not move it.
32 */
33function AboveKeyboard(props: { readonly bottom: number; readonly children: ReactNode }) {
34 const keyboard = useReanimatedKeyboardAnimation();
35 const { bottom } = props;
36 const lift = useAnimatedStyle(() => ({
37 transform: [{ translateY: Math.min(0, keyboard.height.value + bottom - 8) }],
38 }));
39 return (
40 <Animated.View
41 pointerEvents="box-none"
42 className="absolute inset-x-0 bottom-0 flex-row items-center"
43 style={[
44 {
45 gap: WORKSPACE_TOOLBAR.gap,
46 paddingHorizontal: WORKSPACE_TOOLBAR.edge,
47 paddingBottom: bottom,
48 },
49 lift,
50 ]}
51 >
52 {props.children}
53 </Animated.View>
54 );
56​
57export function WorkspaceToolbar(props: {
58 readonly onNewTask: () => void;
59 readonly sidebar?: boolean;
60}) {
61 const workspace = useWorkspaceTabs()!;
62 const navigation = useNavigation();
63 const deviceType = useDeviceType();
64 const sidebarTabs = useSidebarWorkspaceTabs();
65 const selectTab = useSelectWorkspaceTab();
66 const selectedTint = useMobileNavigationTheme().colors.primary;
67 // On the Duo, compose belongs to the threads tab, in the detail column's bar when unfolded.
68 const nativeToolbarCompose =
69 Platform.OS === "ios" &&
70 (isDuoDevice(deviceType)
71 ? deviceType === "duo-folded" && workspace.state.selectedTab === "threads"
72 : deviceType === "pad");
73 // The Duo searches from a field at the bottom of the list in every pose. UIKit has no such
74 // placement once the vertical bar holds the toolbar, and re-placing its search controller on
75 // each fold replaces the search field.
76 const listSearch = isDuoDevice(deviceType);
77 const insets = useSafeAreaInsets();
78 const bottomBarInset = useDuoBottomBarInset();
79 const { isTabActive } = workspace;
80 const tabActive = use(NativeWorkspaceTabActivityContext)?.active ?? true;
81 const { panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
82 const searchRef = useRef<SearchBarCommands>(null);
83 const fieldRef = useRef<TextInputInstance>(null);
84 const tab = workspace.state.tabs[workspace.state.selectedTab];
85 const selectedTab = workspace.state.selectedTab;
86 const enterSearch = useCallback(() => {
87 workspace.dispatch({
88 type: "scroll",
89 tab: selectedTab,
90 offset: workspace.getScrollOffset(selectedTab),
91 });
92 workspace.dispatch({ type: "search", tab: selectedTab });
93 if (props.sidebar && !panes.primarySidebarVisible) togglePrimarySidebar();
94 }, [workspace, selectedTab, props.sidebar, panes.primarySidebarVisible, togglePrimarySidebar]);
95 const focusSearch = useCallback(() => {
96 if (!workspace.isTabActive(selectedTab)) return false;
97 if (listSearch) {
98 fieldRef.current?.focus();
99 return true;
100 }
101 if (!NATIVE_LIQUID_GLASS_SUPPORTED) enterSearch();
102 searchRef.current?.focus();
103 return true;
104 }, [workspace, selectedTab, listSearch, enterSearch]);
105 const cancelSearch = useCallback(() => {
106 workspace.recordScrollOffset(selectedTab, tab.searchEntryOffset);
107 workspace.dispatch({ type: "cancel", tab: selectedTab });
108 }, [workspace, selectedTab, tab.searchEntryOffset]);
109 const searchFocused = useCallback(
110 () =>
111 tab.searchMode === "resting"
112 ? enterSearch()
113 : workspace.dispatch({ type: "search", tab: selectedTab }),
114 [tab.searchMode, enterSearch, workspace, selectedTab],
115 );
116 useHardwareKeyboardCommand("focusSearch", focusSearch);
117 const restoreSearch = useCallback(() => {
118 if (isTabActive(selectedTab) && tab.searchMode !== "resting")
119 searchRef.current?.setText(tab.query);
120 }, [isTabActive, selectedTab, tab.searchMode, tab.query]);
121 useFocusEffect(
122 useCallback(() => {
123 const frame = requestAnimationFrame(restoreSearch);
124 return () => cancelAnimationFrame(frame);
125 }, [restoreSearch]),
126 );
127 useEffect(() => {
128 if (!tabActive) return;
129 const frame = requestAnimationFrame(restoreSearch);
130 return () => cancelAnimationFrame(frame);
131 }, [restoreSearch, tabActive]);
132 return (
133 <>
134 <NativeStackScreenOptions
135 optionsVersion={[selectedTab, tab.searchMode]}
136 options={{
137 // Liquid Glass keeps search mounted as UIKit's own header button, which
138 // morphs into the field; swapping a stacked bar in and out cannot animate.
139 headerSearchBarOptions:
140 !listSearch && (NATIVE_LIQUID_GLASS_SUPPORTED || tab.searchMode !== "resting")
141 ? {
142 ref: searchRef,
143 autoFocus: tab.searchMode === "active",
144 autoCapitalize: "none",
145 hideNavigationBar: false,
146 hideWhenScrolling: false,
147 obscureBackground: false,
148 placement: NATIVE_LIQUID_GLASS_SUPPORTED ? "integratedButton" : "stacked",
149 allowToolbarIntegration: NATIVE_LIQUID_GLASS_SUPPORTED,
150 placeholder:
151 selectedTab === "pullRequests" ? "Search pull requests" : "Search threads",
152 cancelButtonText: "✕",
153 onFocus: searchFocused,
154 onBlur: () => workspace.dispatch({ type: "expand", tab: selectedTab }),
155 onSearchButtonPress: () => searchRef.current?.blur(),
156 onChangeText: ({ nativeEvent }) =>
157 workspace.dispatch({
158 type: "query",
159 query: nativeEvent.text,
160 tab: selectedTab,
161 }),
162 onCancelButtonPress: cancelSearch,
163 }
164 : undefined,
165 }}
166 />
167 {/* UIKit lays trailing items out from the edge inward. */}
168 <Toolbar placement="right">
169 <Toolbar.Button
170 accessibilityLabel="Settings"
171 icon="gearshape"
172 onPress={() =>
173 navigation.navigate("SettingsSheet", {
174 screen: "SettingsContent",
175 params: { screen: "Settings" },
176 })
177 }
178 />
179 {NATIVE_LIQUID_GLASS_SUPPORTED ? null : (
180 <Toolbar.Button
181 accessibilityLabel={
182 selectedTab === "pullRequests" ? "Search pull requests" : "Search threads"
183 }
184 icon="magnifyingglass"
185 onPress={focusSearch}
186 />
187 )}
188 </Toolbar>
189 <Toolbar placement="bottom">
190 {sidebarTabs === "toolbar" ? (
191 <Toolbar.Button
192 identifier="workspace-tab-threads"
193 accessibilityLabel="Threads"
194 icon={selectedTab === "threads" ? "text.bubble.fill" : "text.bubble"}
195 tintColor={selectedTab === "threads" ? selectedTint : undefined}
196 onPress={() => selectTab("threads")}
197 />
198 ) : null}
199 {sidebarTabs === "toolbar" ? (
200 <Toolbar.Button
201 identifier="workspace-tab-pull-requests"
202 accessibilityLabel="Pull requests"
203 icon={{ type: "image", source: PULL_REQUEST_ICON, tinted: true }}
204 tintColor={selectedTab === "pullRequests" ? selectedTint : undefined}
205 onPress={() => selectTab("pullRequests")}
206 />
207 ) : null}
208 {nativeToolbarCompose ? <Toolbar.Spacer flexible /> : null}
209 {nativeToolbarCompose ? (
210 <Toolbar.Button
211 identifier="workspace-compose"
212 accessibilityLabel="New chat"
213 icon="square.and.pencil"
214 onPress={props.onNewTask}
215 separateBackground
216 />
217 ) : null}
218 </Toolbar>
219 {listSearch ? (
220 <AboveKeyboard bottom={bottomBarInset ?? Math.max(insets.bottom, 12)}>
221 <WorkspaceSearchField
222 ref={fieldRef}
223 tab={selectedTab}
224 query={tab.query}
225 onChangeQuery={(query) =>
226 workspace.dispatch({ type: "query", query, tab: selectedTab })
227 }
228 onFocus={searchFocused}
229 onBlur={() => workspace.dispatch({ type: "expand", tab: selectedTab })}
230 onClear={() => {
231 workspace.dispatch({ type: "query", query: "", tab: selectedTab });
232 fieldRef.current?.focus();
233 }}
234 />
235 {tab.searchMode === "resting" ? null : (
236 <GlassSurface
237 style={{
238 width: WORKSPACE_TOOLBAR.height,
239 height: WORKSPACE_TOOLBAR.height,
240 borderRadius: WORKSPACE_TOOLBAR.height / 2,
241 }}
242 >
243 <Pressable
244 accessibilityRole="button"
245 accessibilityLabel="Cancel search"
246 onPress={() => {
247 cancelSearch();
248 fieldRef.current?.blur();
249 }}
250 className="flex-1 items-center justify-center"
251 >
252 <SymbolView name="xmark" size={20} tintColorClassName="accent-icon" />
253 </Pressable>
254 </GlassSurface>
255 )}
256 </AboveKeyboard>
257 ) : null}
258 </>
259 );