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

AdaptiveWorkspaceLayout.tsx

842 lines31.6 KB

1import type {
2 EnvironmentProject,
3 EnvironmentThreadShell,
4} from "@t3tools/client-runtime/state/shell";
5import { EnvironmentId, ThreadId, type SidebarProjectGroupingMode } from "@t3tools/contracts";
6import { useAtomValue } from "@effect/atom-react";
7import { useFocusEffect } from "@react-navigation/native";
8import {
9 CommonActions,
10 NavigationContext,
11 NavigationRouteContext,
12 StackActions,
13 useNavigation,
14} from "@react-navigation/native";
15import {
16 createContext,
17 use,
18 useEffect,
19 useEffectEvent,
20 useMemo,
21 useRef,
22 useState,
23 type ReactNode,
24} from "react";
25import { Platform, useWindowDimensions, View } from "react-native";
26import Animated, {
27 runOnJS,
28 useAnimatedStyle,
29 useDerivedValue,
30 useSharedValue,
31 withTiming,
32} from "react-native-reanimated";
33import { AsyncResult } from "effect/reactivity";
34​
35import {
36 deriveFileInspectorPaneLayout,
37 alignListPaneToHinge,
38 deriveLayout,
39 type VerticalHinge,
40 deriveWorkspacePaneLayout,
41 isPrimarySidebarPinned,
42 type FileInspectorPaneLayout,
43 type Layout,
44 type WorkspaceAuxiliaryPaneRole,
45 type WorkspacePaneLayout,
46} from "../../lib/layout";
47import {
48 resolveThreadSelectionNavigationAction,
49 resolveThreadSelectionOverlayState,
50} from "../../lib/adaptive-navigation";
51import { scopedThreadKey } from "../../lib/scopedEntities";
52import type { ThreadListFilters } from "../../persistence/mobile-preferences";
53import { mobilePreferencesAtom } from "../../state/preferences";
54import {
55 DEFAULT_MOBILE_PROJECT_GROUPING_SETTINGS,
56 resolveMobileProjectGroupingSettings,
57} from "../../state/project-grouping";
58import {
59 parseActiveThreadPath,
60 useHardwareKeyboardCommand,
61} from "../keyboard/hardwareKeyboardCommands";
62import { AndroidHomeFabLayout } from "../home/AndroidHomeFab";
63import {
64 AndroidWorkspaceNavigation,
65 ANDROID_WORKSPACE_RAIL_WIDTH,
66} from "../workspace/AndroidWorkspaceNavigation";
67import { AndroidWorkspaceSidebar } from "../workspace/AndroidWorkspaceSidebar";
68import { useAndroidWorkspaceSelection } from "../workspace/useAndroidWorkspaceSelection";
69import { HomeListOptionsProvider } from "../home/home-list-options";
70import { usePendingTaskListActions } from "../home/usePendingTaskListActions";
71import { ThreadNavigationSidebar } from "../threads/ThreadNavigationSidebar";
72import { useOpenThreadPullRequests } from "../threads/use-open-thread-pull-requests";
73import { RenderErrorBoundary, RenderFailureView } from "../../components/RenderErrorBoundary";
74import { WORKSPACE_PANE_TIMING } from "./workspace-pane-animation";
75import { WorkspaceInspectorPane } from "./workspace-inspector-pane";
76import { WorkspaceContentWidthContext } from "./workspace-content-width";
77import {
78 NativeWorkspaceModeContext,
79 NativeWorkspaceInspectorContext,
80} from "../../native/v5-workspace-context";
81​
82import { useNativeLayoutMetrics } from "../../native/native-layout-metrics";
83import { AndroidHingeObserver } from "../../native/AndroidHingeObserver";
84import { WindowHingeContext } from "../../native/window-hinge-context";
85​
86interface AdaptiveWorkspaceContextValue {
87 readonly layout: Layout;
88 readonly panes: WorkspacePaneLayout;
89 readonly fileInspector: FileInspectorPaneLayout;
90 readonly primarySidebarSearchQuery: string;
91 readonly selectThread: (thread: EnvironmentThreadShell) => void;
92 readonly activateAuxiliaryPaneRole: (role: WorkspaceAuxiliaryPaneRole) => () => void;
93 readonly registerWorkspaceInspector: (render: () => ReactNode) => () => void;
94 readonly setPrimarySidebarSearchQuery: (query: string) => void;
95 readonly showAuxiliaryPane: (role: WorkspaceAuxiliaryPaneRole) => void;
96 readonly toggleAuxiliaryPane: () => void;
97 readonly togglePrimarySidebar: () => void;
98 readonly setAuxiliaryPaneWidth: (width: number) => void;
100​
101const compactLayout = deriveLayout({ width: 0, height: 0 });
102const compactPanes = deriveWorkspacePaneLayout({
103 layout: compactLayout,
104 viewportWidth: 0,
105 primarySidebarPreferredVisible: true,
106 auxiliaryPanePreferredVisible: true,
107});
108const compactFileInspector = deriveFileInspectorPaneLayout({
109 layout: compactLayout,
110 viewportWidth: 0,
111});
112const AdaptiveWorkspaceContext = createContext<AdaptiveWorkspaceContextValue>({
113 layout: compactLayout,
114 panes: compactPanes,
115 fileInspector: compactFileInspector,
116 primarySidebarSearchQuery: "",
117 selectThread: () => undefined,
118 activateAuxiliaryPaneRole: () => () => undefined,
119 registerWorkspaceInspector: () => () => undefined,
120 setPrimarySidebarSearchQuery: () => undefined,
121 showAuxiliaryPane: () => undefined,
122 toggleAuxiliaryPane: () => undefined,
123 togglePrimarySidebar: () => undefined,
124 setAuxiliaryPaneWidth: () => undefined,
125});
126​
127export function useAdaptiveWorkspaceLayout(): AdaptiveWorkspaceContextValue {
128 return use(AdaptiveWorkspaceContext);
130​
131export function useAdaptiveWorkspacePaneRole(role: WorkspaceAuxiliaryPaneRole) {
132 const { activateAuxiliaryPaneRole } = useAdaptiveWorkspaceLayout();
133​
134 useFocusEffect(() => activateAuxiliaryPaneRole(role));
136​
137/**
138 * The column renders BESIDE the navigator — outside any screen — so the
139 * registering screen's navigation and route contexts are captured here and
140 * re-provided around the portal content.
141 *
142 * Registration is FOCUS-scoped, driven by navigation events rather than the
143 * screen's own render cycle: react-native-screens freezes blurred screens, so a
144 * cleanup that depends on the blurred subtree re-rendering never runs and would
145 * leak the pane into the next route.
146 */
147export function useRegisterWorkspaceInspector(render: (() => ReactNode) | undefined) {
148 const { registerWorkspaceInspector } = useAdaptiveWorkspaceLayout();
149 const navigation = use(NavigationContext);
150 const route = use(NavigationRouteContext);
151​
152 const wrappedRender = (() => {
153 if (render === undefined) {
154 return undefined;
155 }
156 return () => (
157 <NavigationContext.Provider value={navigation}>
158 <NavigationRouteContext.Provider value={route}>{render()}</NavigationRouteContext.Provider>
159 </NavigationContext.Provider>
160 );
161 })();
162​
163 const focusedRef = useRef(false);
164 const deactivateRef = useRef<(() => void) | null>(null);
165​
166 // An effect event reads this render's pane without making the focus
167 // subscription re-run (and deactivate) every time the pane closure changes.
168 const syncRegistration = useEffectEvent(() => {
169 if (!focusedRef.current || wrappedRender === undefined) {
170 deactivateRef.current?.();
171 return;
172 }
173 deactivateRef.current = registerWorkspaceInspector(wrappedRender);
174 });
175​
176 useFocusEffect(() => {
177 focusedRef.current = true;
178 syncRegistration();
179 return () => {
180 focusedRef.current = false;
181 syncRegistration();
182 };
183 });
184​
185 useEffect(() => {
186 if (focusedRef.current) {
187 syncRegistration();
188 }
189 }, [wrappedRender]);
190​
191 useEffect(
192 () => () => {
193 deactivateRef.current?.();
194 deactivateRef.current = null;
195 },
196 [],
197 );
199​
200export function AdaptiveWorkspaceLayout(props: {
201 readonly children: ReactNode;
202 readonly pathname: string;
203 readonly workspaceRouteKey: string | undefined;
204 readonly selectedPullRequestKey?: string | null;
205}) {
206 const preferencesResult = useAtomValue(mobilePreferencesAtom);
207 if (!AsyncResult.isSuccess(preferencesResult)) {
208 return AsyncResult.isFailure(preferencesResult) ? (
209 <AdaptiveWorkspaceLayoutContent
210 {...props}
211 projectGroupingMode={DEFAULT_MOBILE_PROJECT_GROUPING_SETTINGS.sidebarProjectGroupingMode}
212 />
213 ) : null;
214 }
215 const groupingSettings = resolveMobileProjectGroupingSettings(preferencesResult.value);
216 return (
217 <AdaptiveWorkspaceLayoutContent
218 {...props}
219 projectGroupingMode={groupingSettings.sidebarProjectGroupingMode}
220 savedThreadListFilters={preferencesResult.value.threadListFilters}
221 />
222 );
224​
225function AdaptiveWorkspaceLayoutContent(
226 props: {
227 readonly children: ReactNode;
228 readonly pathname: string;
229 readonly workspaceRouteKey: string | undefined;
230 readonly selectedPullRequestKey?: string | null;
231 } & {
232 readonly projectGroupingMode: SidebarProjectGroupingMode;
233 readonly savedThreadListFilters?: ThreadListFilters | undefined;
234 },
235) {
236 const projectGroupingMode = props.projectGroupingMode;
237 const nativeWorkspace = use(NativeWorkspaceModeContext);
238 const windowDimensions = useWindowDimensions();
239 const nativeMetrics = useNativeLayoutMetrics();
240 const { width: viewportWidth, height } = nativeMetrics ?? windowDimensions;
241 const pathname = props.pathname;
242 // Android's Settings tab is a full workspace page: the rail stays, the sidebar and
243 // auxiliary panes hide.
244 const workspacePage = Platform.OS === "android" && pathname.startsWith("/settings");
245 const navigation = useNavigation();
246 const activeRoleOwner = useRef<symbol | null>(null);
247 const [primarySidebarPreferredVisible, setPrimarySidebarPreferredVisible] = useState(true);
248 const [supplementaryPanePreferredVisible, setSupplementaryPanePreferredVisible] = useState(true);
249 const [supplementaryPanePreferredWidth, setSupplementaryPanePreferredWidth] = useState<
250 number | null
251 >(null);
252 const [fileInspectorPreferredVisible, setFileInspectorPreferredVisible] = useState(true);
253 const [fileInspectorPreferredWidth, setFileInspectorPreferredWidth] = useState<number | null>(
254 null,
255 );
256 const [primarySidebarSearchQuery, setPrimarySidebarSearchQuery] = useState("");
257 const [railOverlay, setRailOverlay] = useState(false);
258 const [focusedAuxiliaryPaneRole, setFocusedAuxiliaryPaneRole] =
259 useState<WorkspaceAuxiliaryPaneRole | null>(null);
260 // The compiler can't memoize these derivations: they stay live across later hook calls. Fresh
261 // objects here change the workspace context on every render, so every workspace screen
262 // re-renders on any layout state change, and the inspector registration then loops: each
263 // re-register re-renders its screen, which hands over a new inspector renderer.
264 const baseLayout = useMemo(
265 () => deriveLayout({ width: viewportWidth, height, nativeMetrics }),
266 [height, viewportWidth, nativeMetrics],
267 );
268 const primarySidebarPinned = isPrimarySidebarPinned({
269 pathname,
270 nativeWorkspace,
271 usesSplitView: baseLayout.usesSplitView,
272 });
273 const showPrimarySidebar =
274 !workspacePage && (primarySidebarPinned || primarySidebarPreferredVisible);
275 const shouldRenderNavigationRail = Platform.OS === "android" && baseLayout.usesSplitView;
276 const navigationRailWidth =
277 shouldRenderNavigationRail && (showPrimarySidebar || workspacePage)
278 ? ANDROID_WORKSPACE_RAIL_WIDTH
279 : 0;
280 const [hinge, setHinge] = useState<VerticalHinge | null>(null);
281 const [windowHinge, setWindowHinge] = useState<VerticalHinge | null>(null);
282 const handleHingeChange = (next: VerticalHinge | null, nextWindow: VerticalHinge | null) => {
283 setHinge(next);
284 setWindowHinge(nextWindow);
285 };
286 const layout = useMemo(
287 () =>
288 alignListPaneToHinge({
289 layout: baseLayout,
290 hinge,
291 viewportWidth,
292 leadingWidth: navigationRailWidth,
293 }),
294 [baseLayout, hinge, navigationRailWidth, viewportWidth],
295 );
296 const width = viewportWidth - navigationRailWidth;
297 // The sidebar stays mounted under a workspace page, so its list is ready on the way back.
298 const shouldRenderPrimarySidebar = layout.usesSplitView;
299 const fileInspector = useMemo(
300 () =>
301 deriveFileInspectorPaneLayout({
302 layout,
303 viewportWidth: width,
304 preferredWidth: fileInspectorPreferredWidth ?? undefined,
305 reservedLeadingWidth:
306 shouldRenderPrimarySidebar && showPrimarySidebar
307 ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0)
308 : 0,
309 }),
310 [fileInspectorPreferredWidth, layout, showPrimarySidebar, shouldRenderPrimarySidebar, width],
311 );
312 const auxiliaryPaneRole: WorkspaceAuxiliaryPaneRole =
313 focusedAuxiliaryPaneRole ?? (/\/files(?:\/|$)/.test(pathname) ? "inspector" : "supplementary");
314 const auxiliaryPanePreferredVisible =
315 !workspacePage &&
316 (auxiliaryPaneRole === "inspector"
317 ? fileInspectorPreferredVisible
318 : supplementaryPanePreferredVisible);
319 const auxiliaryPanePreferredWidth =
320 auxiliaryPaneRole === "inspector"
321 ? fileInspectorPreferredWidth
322 : supplementaryPanePreferredWidth;
323 const panes = useMemo(
324 () =>
325 deriveWorkspacePaneLayout({
326 layout,
327 viewportWidth: width,
328 primarySidebarPreferredVisible: showPrimarySidebar,
329 auxiliaryPanePreferredVisible,
330 auxiliaryPaneRole,
331 auxiliaryPanePreferredWidth: auxiliaryPanePreferredWidth ?? undefined,
332 }),
333 [
334 auxiliaryPanePreferredVisible,
335 auxiliaryPaneRole,
336 auxiliaryPanePreferredWidth,
337 layout,
338 showPrimarySidebar,
339 width,
340 ],
341 );
342 const activeThread = parseActiveThreadPath(pathname);
343 const environmentId = activeThread?.environmentId ?? null;
344 const threadId = activeThread?.threadId ?? null;
345 const selectedThreadKey = (() => {
346 if (environmentId === null || threadId === null) {
347 return null;
348 }
349 try {
350 return scopedThreadKey(EnvironmentId.make(environmentId), ThreadId.make(threadId));
351 } catch {
352 return null;
353 }
354 })();
355 const [workspaceInspector, setWorkspaceInspector] = useState<{
356 readonly render: () => ReactNode;
357 readonly active: boolean;
358 } | null>(null);
359 const workspaceInspectorOwner = useRef<symbol | null>(null);
360 const registerWorkspaceInspector = (render: () => ReactNode) => {
361 const owner = Symbol("workspace-inspector");
362 workspaceInspectorOwner.current = owner;
363 setWorkspaceInspector({ render, active: true });
364​
365 return () => {
366 // During a push/replace the outgoing screen deactivates AFTER the
367 // incoming screen registered — only the current owner may deactivate.
368 if (workspaceInspectorOwner.current !== owner) {
369 return;
370 }
371 setWorkspaceInspector((current) => (current === null ? null : { ...current, active: false }));
372 };
373 };
374 const handleWorkspaceInspectorClosed = () => {
375 setWorkspaceInspector((current) => (current !== null && !current.active ? null : current));
376 };
377 const activateAuxiliaryPaneRole = (role: WorkspaceAuxiliaryPaneRole) => {
378 const owner = Symbol(role);
379 activeRoleOwner.current = owner;
380 setFocusedAuxiliaryPaneRole(role);
381​
382 return () => {
383 if (activeRoleOwner.current !== owner) {
384 return;
385 }
386 activeRoleOwner.current = null;
387 setFocusedAuxiliaryPaneRole(null);
388 };
389 };
390 const togglePrimarySidebar = () => {
391 if (primarySidebarPinned) {
392 return;
393 }
394 if (!panes.primarySidebarVisible && panes.primarySidebarSuppressedByAuxiliary) {
395 setFileInspectorPreferredVisible(false);
396 setPrimarySidebarPreferredVisible(true);
397 return;
398 }
399 setPrimarySidebarPreferredVisible((current) => !current);
400 };
401 const revealPrimarySidebar = () => {
402 if (panes.primarySidebarSuppressedByAuxiliary) {
403 setFileInspectorPreferredVisible(false);
404 }
405 setPrimarySidebarPreferredVisible(true);
406 };
407 const handleToggleSidebarCommand = () => {
408 togglePrimarySidebar();
409 return true;
410 };
411 const sidebarCommands = primarySidebarPinned ? [] : (["toggleSidebar"] as const);
412 useHardwareKeyboardCommand(sidebarCommands, handleToggleSidebarCommand);
413 const showAuxiliaryPane = (role: WorkspaceAuxiliaryPaneRole) => {
414 if (role === "inspector") {
415 setFocusedAuxiliaryPaneRole("inspector");
416 setFileInspectorPreferredVisible(true);
417 return;
418 }
419 setFocusedAuxiliaryPaneRole("supplementary");
420 setSupplementaryPanePreferredVisible(true);
421 };
422 const handleOpenFilesCommand = () => {
423 const activeThread = parseActiveThreadPath(pathname);
424 if (!layout.usesSplitView || !fileInspector.supported || activeThread === null) {
425 return false;
426 }
427 showAuxiliaryPane("inspector");
428 if (/\/files(?:\/|$)/.test(pathname)) {
429 return true;
430 }
431 navigation.navigate("ThreadFiles", activeThread);
432 return true;
433 };
434 useHardwareKeyboardCommand("files", handleOpenFilesCommand);
435 const toggleAuxiliaryPane = () => {
436 if (auxiliaryPaneRole === "inspector") {
437 setFileInspectorPreferredVisible((current) => !current);
438 return;
439 }
440 setSupplementaryPanePreferredVisible((current) => !current);
441 };
442 const setAuxiliaryPaneWidth = (nextWidth: number) => {
443 if (auxiliaryPaneRole === "inspector") {
444 setFileInspectorPreferredWidth(nextWidth);
445 return;
446 }
447 setSupplementaryPanePreferredWidth(nextWidth);
448 };
449 const handleOpenSettings = () => {
450 navigation.navigate("SettingsSheet", {
451 screen: "SettingsContent",
452 params: { screen: "Settings" },
453 });
454 };
455​
456 const handleStartNewTask = () => {
457 navigation.navigate("NewTaskSheet", { screen: "NewTask" });
458 };
459​
460 // Minted here (root stack navigation) so the sidebar pane stays free of
461 // navigation hooks — on iOS it renders inside an independent nav tree.
462 const handleOpenEnvironmentSettings = () => {
463 navigation.navigate("SettingsSheet", {
464 screen: "SettingsContent",
465 params: { screen: "SettingsEnvironments" },
466 });
467 };
468​
469 const handleOpenThreadPullRequests = useOpenThreadPullRequests();
470 const { openPendingTask: handleOpenPendingTask } = usePendingTaskListActions();
471​
472 const handleNewThreadOnBranch = (thread: EnvironmentThreadShell) => {
473 navigation.navigate("NewTaskSheet", {
474 screen: "NewTaskDraft",
475 params: {
476 environmentId: String(thread.environmentId),
477 projectId: String(thread.projectId),
478 branch: thread.branch,
479 worktreePath: thread.worktreePath,
480 },
481 });
482 };
483​
484 const handleNewThreadInProject = (project: EnvironmentProject) => {
485 navigation.navigate("NewTaskSheet", {
486 screen: "NewTaskDraft",
487 params: {
488 environmentId: String(project.environmentId),
489 projectId: String(project.id),
490 title: project.title,
491 },
492 });
493 };
494​
495 const inspectorColumnTargetWidth =
496 workspaceInspector !== null && workspaceInspector.active && panes.auxiliaryPaneVisible
497 ? (panes.auxiliaryPaneWidth ?? 0)
498 : 0;
499 const contentTargetWidth = layout.usesSplitView
500 ? Math.max(0, panes.contentPaneWidth - inspectorColumnTargetWidth)
501 : null;
502 const [contentSizing, setContentSizing] = useState({
503 viewportWidth,
504 workspacePage,
505 target: contentTargetWidth,
506 sidebarVisible: panes.primarySidebarVisible,
507 width: contentTargetWidth,
508 listPaneTarget: layout.listPaneWidth,
509 listPaneFloor: layout.listPaneWidth,
510 listPaneCeiling: layout.listPaneWidth,
511 revision: 0,
512 });
513 if (
514 Platform.OS === "android" &&
515 (contentSizing.viewportWidth !== viewportWidth ||
516 contentSizing.workspacePage !== workspacePage ||
517 contentSizing.target !== contentTargetWidth ||
518 contentSizing.sidebarVisible !== panes.primarySidebarVisible ||
519 contentSizing.listPaneTarget !== layout.listPaneWidth)
520 ) {
521 // A workspace page swaps the sidebar in the same frame, so nothing animates to wait for.
522 const sameViewport =
523 contentSizing.viewportWidth === viewportWidth &&
524 contentSizing.workspacePage === workspacePage;
525 const openingSidebar =
526 sameViewport && !contentSizing.sidebarVisible && panes.primarySidebarVisible;
527 // A book-posture hinge resizes the visible list pane instead of revealing it.
528 const resizingSidebar =
529 sameViewport &&
530 contentSizing.sidebarVisible &&
531 panes.primarySidebarVisible &&
532 contentSizing.listPaneTarget !== layout.listPaneWidth;
533 const keepWide =
534 (openingSidebar || resizingSidebar) &&
535 contentSizing.width !== null &&
536 contentTargetWidth !== null;
537 setContentSizing({
538 viewportWidth,
539 workspacePage,
540 target: contentTargetWidth,
541 sidebarVisible: panes.primarySidebarVisible,
542 // Clip the wide navigator while the sidebar opens or widens, then reflow once at the
543 // end. Narrowing it first exposes an empty strip before the sidebar arrives.
544 width: keepWide
545 ? Math.max(contentSizing.width ?? 0, contentTargetWidth ?? 0)
546 : contentTargetWidth,
547 listPaneTarget: layout.listPaneWidth,
548 // While a hinge resizes the list, its width follows the pane edge within this span. A
549 // retarget mid-animation widens the span, since the edge is still somewhere inside it.
550 listPaneFloor: resizingSidebar
551 ? Math.min(contentSizing.listPaneFloor ?? 0, layout.listPaneWidth ?? 0)
552 : layout.listPaneWidth,
553 listPaneCeiling: resizingSidebar
554 ? Math.max(contentSizing.listPaneCeiling ?? 0, layout.listPaneWidth ?? 0)
555 : layout.listPaneWidth,
556 revision: contentSizing.revision + 1,
557 });
558 }
559 const settleContentWidth = (revision: number) => {
560 setContentSizing((current) =>
561 current.revision !== revision ||
562 (current.width === current.target &&
563 current.listPaneFloor === current.listPaneTarget &&
564 current.listPaneCeiling === current.listPaneTarget)
565 ? current
566 : {
567 ...current,
568 width: current.target,
569 listPaneFloor: current.listPaneTarget,
570 listPaneCeiling: current.listPaneTarget,
571 },
572 );
573 };
574 const listPaneFloor = contentSizing.listPaneFloor ?? 0;
575 const listPaneCeiling = contentSizing.listPaneCeiling ?? 0;
576 const contentSettledWidth = Platform.OS === "android" ? contentSizing.width : contentTargetWidth;
577 const contentRevision = contentSizing.revision;
578​
579 const renderedSidebarWidth = useSharedValue(
580 panes.primarySidebarVisible ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0) : 0,
581 );
582 const renderedRailWidth = useSharedValue(navigationRailWidth);
583 const leavingWorkspacePage = useRef(false);
584 useEffect(() => {
585 if (nativeWorkspace) return;
586 renderedRailWidth.set(withTiming(navigationRailWidth, WORKSPACE_PANE_TIMING));
587 // A workspace page hides the sidebar through React instead, in the same commit that mounts
588 // the page. Animating it here would hide it a few frames before the page appears.
589 if (workspacePage) {
590 leavingWorkspacePage.current = true;
591 return;
592 }
593 const snap = leavingWorkspacePage.current;
594 leavingWorkspacePage.current = false;
595 const targetWidth = panes.primarySidebarVisible
596 ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0)
597 : 0;
598 // The content beside it already took its final width when the page closed.
599 renderedSidebarWidth.set(
600 withTiming(targetWidth, snap ? { duration: 0 } : WORKSPACE_PANE_TIMING, (finished) => {
601 if (finished && Platform.OS === "android") runOnJS(settleContentWidth)(contentRevision);
602 }),
603 );
604 }, [
605 nativeWorkspace,
606 navigationRailWidth,
607 layout.listPaneWidth,
608 layout.listPaneGap,
609 panes.primarySidebarVisible,
610 renderedSidebarWidth,
611 renderedRailWidth,
612 settleContentWidth,
613 contentRevision,
614 workspacePage,
615 ]);
616 const railAnimatedStyle = useAnimatedStyle(() => ({ width: renderedRailWidth.value }));
617 // Showing or hiding the sidebar clips a fixed-width list. A hinge resize instead tracks the
618 // animated edge, so the header actions slide with it rather than jumping or being covered.
619 const listPaneGap = layout.listPaneGap ?? 0;
620 const listPaneAnimatedStyle = useAnimatedStyle(() => ({
621 width: Math.min(
622 listPaneCeiling,
623 Math.max(listPaneFloor, renderedSidebarWidth.value - listPaneGap),
624 ),
625 }));
626 const sidebarAnimatedStyle = useAnimatedStyle(() => ({
627 // Fading a UIKit glass ancestor can leave remounted header effects missing.
628 opacity: Platform.OS === "ios" ? 1 : Math.min(1, renderedSidebarWidth.value / 80),
629 width: renderedSidebarWidth.value,
630 }));
631​
632 // The navigator (native header + markdown feed) lays out ONCE per pane toggle
633 // instead of re-measuring on every animation frame — the animating columns
634 // merely clip/reveal it over a matching background.
635 const renderedInspectorWidth = useSharedValue(inspectorColumnTargetWidth);
636 const renderedContentWidth = useDerivedValue(() =>
637 Math.max(
638 0,
639 viewportWidth -
640 renderedRailWidth.value -
641 renderedSidebarWidth.value -
642 renderedInspectorWidth.value,
643 ),
644 );
645​
646 const selectAndroidWorkspace = useAndroidWorkspaceSelection();
647 const handleSelectThread = (thread: EnvironmentThreadShell) => {
648 const params = {
649 environmentId: String(thread.environmentId),
650 threadId: String(thread.id),
651 };
652 if (Platform.OS === "android") {
653 setFileInspectorPreferredVisible(false);
654 selectAndroidWorkspace({ name: "Thread", params });
655 return;
656 }
657 const navigationAction = resolveThreadSelectionNavigationAction({
658 usesSplitView: layout.usesSplitView,
659 pathname,
660 });
661 const overlayState = resolveThreadSelectionOverlayState({
662 state: navigation.getState(),
663 workspaceRouteKey: props.workspaceRouteKey,
664 action: navigationAction,
665 params,
666 });
667 if (overlayState !== null) {
668 setFileInspectorPreferredVisible(false);
669 navigation.dispatch(CommonActions.reset(overlayState));
670 return;
671 }
672 if (navigationAction === "set-params") {
673 const nextThreadKey = scopedThreadKey(thread.environmentId, thread.id);
674 if (nextThreadKey === selectedThreadKey) {
675 return;
676 }
677 setFileInspectorPreferredVisible(false);
678 navigation.navigate("Thread", params);
679 return;
680 }
681 if (navigationAction === "replace") {
682 setFileInspectorPreferredVisible(false);
683 navigation.dispatch(StackActions.replace("Thread", params));
684 return;
685 }
686 navigation.navigate("Thread", params);
687 };
688​
689 const contextValue = {
690 layout,
691 panes,
692 fileInspector,
693 primarySidebarSearchQuery,
694 selectThread: handleSelectThread,
695 activateAuxiliaryPaneRole,
696 registerWorkspaceInspector,
697 setPrimarySidebarSearchQuery,
698 showAuxiliaryPane,
699 toggleAuxiliaryPane,
700 togglePrimarySidebar,
701 setAuxiliaryPaneWidth,
702 };
703​
704 if (nativeWorkspace) {
705 return (
706 <HomeListOptionsProvider
707 projectGroupingMode={projectGroupingMode}
708 savedFilters={props.savedThreadListFilters}
709 >
710 <AdaptiveWorkspaceContext value={contextValue}>
711 <NativeWorkspaceInspectorContext
712 value={{ render: workspaceInspector?.render, visible: inspectorColumnTargetWidth > 0 }}
713 >
714 {props.children}
715 </NativeWorkspaceInspectorContext>
716 </AdaptiveWorkspaceContext>
717 </HomeListOptionsProvider>
718 );
719 }
720 return (
721 <HomeListOptionsProvider
722 projectGroupingMode={projectGroupingMode}
723 savedFilters={props.savedThreadListFilters}
724 >
725 <AdaptiveWorkspaceContext.Provider value={contextValue}>
726 <WindowHingeContext value={windowHinge}>
727 <View
728 testID="adaptive-workspace-layout"
729 className={Platform.OS === "android" ? "flex-1 flex-row bg-header" : "flex-1 flex-row"}
730 >
731 <AndroidHingeObserver onChange={handleHingeChange} />
732 {shouldRenderNavigationRail ? (
733 <Animated.View className="self-stretch" style={railAnimatedStyle} />
734 ) : null}
735 {shouldRenderPrimarySidebar && layout.listPaneWidth !== null ? (
736 <Animated.View
737 className="self-stretch overflow-hidden"
738 accessibilityElementsHidden={!panes.primarySidebarVisible}
739 collapsable={false}
740 importantForAccessibility={
741 panes.primarySidebarVisible ? "auto" : "no-hide-descendants"
742 }
743 pointerEvents={panes.primarySidebarVisible ? "auto" : "none"}
744 // Under a workspace page the sidebar leaves the row but keeps its size, so its
745 // lists never lay out at zero size and are ready, unchanged, on the way back. The
746 // content pane, laid out after it, covers it, in the same mount as the page.
747 style={[
748 workspacePage ? { position: "absolute", top: 0, bottom: 0, left: 0 } : null,
749 sidebarAnimatedStyle,
750 ]}
751 >
752 <Animated.View className="flex-1" style={listPaneAnimatedStyle}>
753 <RenderErrorBoundary
754 renderFallback={(fallback) => (
755 <RenderFailureView
756 {...fallback}
757 title="The sidebar couldn't be displayed"
758 exit={{ label: "Open settings", onPress: handleOpenSettings }}
759 />
760 )}
761 >
762 <AndroidWorkspaceSidebar
763 onRequestVisibility={revealPrimarySidebar}
764 selectedPullRequestKey={props.selectedPullRequestKey ?? null}
765 >
766 <AndroidHomeFabLayout sidebar onStartNewTask={handleStartNewTask}>
767 <ThreadNavigationSidebar
768 width={layout.listPaneWidth}
769 visible={panes.primarySidebarVisible}
770 onRequestVisibility={revealPrimarySidebar}
771 selectedThreadKey={selectedThreadKey}
772 onOpenSettings={handleOpenSettings}
773 onOpenEnvironmentSettings={handleOpenEnvironmentSettings}
774 onNewThreadInProject={handleNewThreadInProject}
775 onNewThreadOnBranch={handleNewThreadOnBranch}
776 onOpenPendingTask={handleOpenPendingTask}
777 onSelectThread={handleSelectThread}
778 onOpenThreadPullRequests={handleOpenThreadPullRequests}
779 onSearchQueryChange={setPrimarySidebarSearchQuery}
780 searchQuery={primarySidebarSearchQuery}
781 />
782 </AndroidHomeFabLayout>
783 </AndroidWorkspaceSidebar>
784 </RenderErrorBoundary>
785 </Animated.View>
786 </Animated.View>
787 ) : null}
788 <View
789 className={
790 Platform.OS === "android"
791 ? "flex-1 overflow-hidden bg-header"
792 : "flex-1 overflow-hidden bg-screen"
793 }
794 collapsable={false}
795 >
796 <View
797 collapsable={false}
798 style={
799 contentSettledWidth !== null
800 ? {
801 flex: 1,
802 width: contentSettledWidth,
803 }
804 : { flex: 1 }
805 }
806 >
807 <WorkspaceContentWidthContext
808 value={layout.usesSplitView ? renderedContentWidth : null}
809 >
810 {props.children}
811 </WorkspaceContentWidthContext>
812 </View>
813 </View>
814 <WorkspaceInspectorPane
815 pathname={props.pathname}
816 renderedInspectorWidth={renderedInspectorWidth}
817 active={workspaceInspector?.active ?? false}
818 panes={panes}
819 renderInspector={workspaceInspector?.render}
820 setAuxiliaryPaneWidth={setAuxiliaryPaneWidth}
821 onClosed={handleWorkspaceInspectorClosed}
822 />
823 {shouldRenderNavigationRail && navigationRailWidth > 0 ? (
824 // The rail reserves its slot above and draws over the panes, so it can widen over
825 // the list in place. It takes the whole workspace only while it is open.
826 <View
827 className="absolute inset-y-0 left-0"
828 style={railOverlay ? { right: 0 } : { width: navigationRailWidth }}
829 >
830 <AndroidWorkspaceNavigation
831 rail
832 onSelect={revealPrimarySidebar}
833 onOverlayChange={setRailOverlay}
834 />
835 </View>
836 ) : null}
837 </View>
838 </WindowHingeContext>
839 </AdaptiveWorkspaceContext.Provider>
840 </HomeListOptionsProvider>
841 );