@@ -5,7 +5,13 @@ import {
55import { useAndroidFloatingTabs } from "../settings/appearance/AppearancePreferencesProvider" ;
66import { useSoftKeyboardShowing } from "../keyboard/useSoftKeyboardShowing" ;
77import { SafeAreaInsetsContext , useSafeAreaInsets } from "react-native-safe-area-context" ;
8- import { AdaptiveWorkspaceContext , useAdaptiveWorkspaceLayout } from "./adaptive-workspace-context" ;
8+ import {
9+ AdaptiveWorkspaceContext ,
10+ WorkspacePaneStateContext ,
11+ useAdaptiveWorkspaceLayout ,
12+ useWorkspacePaneState ,
13+ type AdaptiveWorkspaceActions ,
14+ } from "./adaptive-workspace-context" ;
915import { threadSelectionDeferral } from "./deferred-navigation" ;
1016import { uuidv4 } from "../../lib/uuid" ;
1117import { useToolbarLayout } from "../../components/ui/useToolbarLayout" ;
@@ -14,9 +20,9 @@ import { useAtomValue } from "@effect/atom-react";
1420import { NavigationContext , NavigationRouteContext , useNavigation } from "@react-navigation/native" ;
1521import {
1622 use ,
17- useDeferredValue ,
1823 useEffect ,
1924 useEffectEvent ,
25+ useLayoutEffect ,
2026 useMemo ,
2127 useRef ,
2228 useState ,
@@ -30,11 +36,7 @@ import {
3036 useWindowDimensions ,
3137 View ,
3238} from "react-native" ;
33- import {
34- KeyboardController ,
35- useKeyboardContext ,
36- useKeyboardState ,
37- } from "react-native-keyboard-controller" ;
39+ import { KeyboardController , useKeyboardContext } from "react-native-keyboard-controller" ;
3840import Animated , {
3941 runOnJS ,
4042 useAnimatedStyle ,
@@ -118,7 +120,7 @@ import { workspaceRoutesOf } from "./workspace-overlay-routes";
118120import { SettingsWorkspaceLayout } from "../settings/SettingsWorkspaceLayout" ;
119121import { WorkspaceThreadSidebar } from "./workspace/WorkspaceThreadSidebar" ;
120122
121- export { useAdaptiveWorkspaceLayout } ;
123+ export { useAdaptiveWorkspaceLayout , useWorkspacePaneState } ;
122124
123125/**
124126 * Shows these panes in the workspace's inspector while this screen is focused. Register on focus
@@ -191,6 +193,35 @@ export function Inspector(props: {
191193 return null ;
192194}
193195
196+ /**
197+ * Actions whose identities never change, so a pane toggle doesn't re-render every screen that
198+ * holds one. Each call runs the latest render's handler.
199+ */
200+ function useStableActions ( actions : Required < AdaptiveWorkspaceActions > ) : AdaptiveWorkspaceActions {
201+ const latest = useRef ( actions ) ;
202+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- points the stable actions at the handlers this commit rendered
203+ useLayoutEffect ( ( ) => {
204+ latest . current = actions ;
205+ } ) ;
206+ const [ stable ] = useState ( ( ) : AdaptiveWorkspaceActions => ( {
207+ startNewTask : ( ) => latest . current . startNewTask ( ) ,
208+ selectDetail : ( detail , sheet ) => latest . current . selectDetail ( detail , sheet ) ,
209+ registerWorkspaceInspector : ( content , routeKey , revealKey ) =>
210+ latest . current . registerWorkspaceInspector ( content , routeKey , revealKey ) ,
211+ setPrimarySidebarSearchQuery : ( query ) => latest . current . setPrimarySidebarSearchQuery ( query ) ,
212+ showInspector : ( ) => latest . current . showInspector ( ) ,
213+ toggleAuxiliaryPane : ( ) => latest . current . toggleAuxiliaryPane ( ) ,
214+ togglePrimarySidebar : ( ) => latest . current . togglePrimarySidebar ( ) ,
215+ revealPrimarySidebar : ( ) => latest . current . revealPrimarySidebar ( ) ,
216+ setPrimarySidebarVisible : ( visible ) => latest . current . setPrimarySidebarVisible ( visible ) ,
217+ openWorkspaceTab : ( tab , options ) => latest . current . openWorkspaceTab ( tab , options ) ,
218+ setAuxiliaryPaneWidth : ( width ) => latest . current . setAuxiliaryPaneWidth ( width ) ,
219+ setNativeInspectorWidth : ( width ) => latest . current . setNativeInspectorWidth ( width ) ,
220+ setComposerEditing : ( editing ) => latest . current . setComposerEditing ( editing ) ,
221+ } ) ) ;
222+ return stable ;
223+ }
224+
194225export function AdaptiveWorkspaceLayout ( props : {
195226 readonly children : ReactNode ;
196227 readonly pathname : string ;
@@ -296,11 +327,10 @@ function AdaptiveWorkspaceLayoutContent(
296327 const softKeyboardVisible = useSoftKeyboardShowing ( ) ;
297328 // Keyed on the soft keyboard, not focus: a hardware keyboard leaves room for the sidebar, and
298329 // Back closes the keyboard while the editor keeps focus. The sidebar returns as the keyboard
299- // starts hiding, not after. Deferred so the composer commits its keyboard render first;
300- // relaying out in the same commit held it behind the keyboard.
301- const composerKeyboardActive = useDeferredValue (
302- Platform . OS === "android" && baseLayout . usesSplitView && composerEditing && softKeyboardVisible ,
303- ) ;
330+ // starts hiding, not after. The header, the pane width, and the sidebar's exit all change in
331+ // the keyboard's first commit; the sidebar itself follows the keyboard on the UI thread.
332+ const composerKeyboardActive =
333+ Platform . OS === "android" && baseLayout . usesSplitView && composerEditing && softKeyboardVisible ;
304334 const sidebarWithoutKeyboard =
305335 ! workspacePage && ( primarySidebarPinned || preferredSidebarVisible ) ;
306336 const showPrimarySidebar =
@@ -319,10 +349,6 @@ function AdaptiveWorkspaceLayoutContent(
319349 ! sidebarFloats &&
320350 ( composerEditing || composerKeyboardActive ) ;
321351 const keyboardProgress = useKeyboardContext ( ) . reanimated . progress ;
322- // From the moment the keyboard starts showing until it has fully hidden. The detail pane keeps
323- // the sidebar's room for that whole span, so on the way back the sidebar pushes the wide pane
324- // aside instead of arriving beside one that already shrank.
325- const keyboardOnScreen = useKeyboardState ( ( state ) => state . isVisible ) ;
326352 const shouldRenderNavigationRail =
327353 Platform . OS === "android" && baseLayout . usesSplitView && ! floatingTabsEnabled ;
328354 const enabledTabs = useEnabledNavigationTabs ( ) ;
@@ -671,36 +697,19 @@ function AdaptiveWorkspaceLayoutContent(
671697 const contentEndGap = Platform . OS === "android" && layout . usesSplitView ? 4 : 0 ;
672698 // Android's split is one strip: a sidebar slot of fixed width, then the detail pane. The
673699 // sidebar never resizes; both slide by one offset, so together they always fill the width.
674- // The pane spans the sidebar's room too unless the sidebar has finished arriving: a sidebar on
675- // its way in pushes the wide pane past the edge, and one leaving uncovers it.
676700 const stripSidebar =
677701 Platform . OS === "android" &&
678702 layout . usesSplitView &&
679703 layout . listPaneWidth !== null &&
680704 ! workspacePage &&
681705 ! sidebarFloats ;
682706 const sidebarSlotWidth = ( layout . listPaneWidth ?? 0 ) + ( layout . listPaneGap ?? 0 ) ;
707+ // The pane takes its new width in the commit that starts the sidebar moving, so it rewraps as
708+ // the strip starts to slide rather than after it lands: a sidebar on its way in (or back from
709+ // the keyboard) finds the narrow pane, and one leaving uncovers the wide pane.
683710 const sidebarTargetOpen = keyboardHoldsSidebar ? sidebarRestsOpen : panes . primarySidebarVisible ;
684- const [ sidebarMotion , setSidebarMotion ] = useState ( {
685- strip : stripSidebar ,
686- settledOpen : sidebarTargetOpen ,
687- } ) ;
688- if ( sidebarMotion . strip !== stripSidebar ) {
689- // Entering or leaving the strip (a workspace page, a fold) snaps the sidebar into place.
690- setSidebarMotion ( { strip : stripSidebar , settledOpen : sidebarTargetOpen } ) ;
691- } else if ( sidebarMotion . settledOpen && ! sidebarTargetOpen ) {
692- setSidebarMotion ( { strip : stripSidebar , settledOpen : false } ) ;
693- }
694- const sidebarSettledOpen =
695- sidebarMotion . strip === stripSidebar ? sidebarMotion . settledOpen : sidebarTargetOpen ;
696- const markSidebarSettled = ( open : boolean ) =>
697- setSidebarMotion ( ( current ) =>
698- current . settledOpen === open ? current : { ...current , settledOpen : open } ,
699- ) ;
700- // The keyboard covers the sidebar's share from the moment it starts showing until it is gone.
701711 const contentSpansSidebar =
702- stripSidebar &&
703- ( ! sidebarSettledOpen || ! sidebarTargetOpen || ( keyboardHoldsSidebar && keyboardOnScreen ) ) ;
712+ stripSidebar && ( ! sidebarTargetOpen || ( keyboardHoldsSidebar && composerKeyboardActive ) ) ;
704713 const contentTargetWidth = ! layout . usesSplitView
705714 ? null
706715 : Math . max (
@@ -813,6 +822,8 @@ function AdaptiveWorkspaceLayoutContent(
813822 const renderedRailWidth = useSharedValue ( navigationRailWidth ) ;
814823 const keyboardHold = useSharedValue ( keyboardHoldsSidebar ) ;
815824 const leavingWorkspacePage = useRef ( false ) ;
825+ // The slide starts after the commit that lays the pane out, not in the press handler: Reanimated
826+ // commits the shadow tree every frame, and that starves a large React commit until it stops.
816827 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- animates the Reanimated rail and sidebar width shared values toward the layout's targets
817828 useEffect ( ( ) => {
818829 if ( nativeWorkspace ) return ;
@@ -836,9 +847,9 @@ function AdaptiveWorkspaceLayoutContent(
836847 // The content beside it already took its final width when the page closed.
837848 renderedSidebarWidth . set (
838849 withTiming ( targetWidth , snap ? { duration : 0 } : WORKSPACE_PANE_TIMING , ( finished ) => {
839- if ( finished && Platform . OS === "android" ) {
850+ // Only a hinge resize widens the list's span; a toggle has nothing to settle.
851+ if ( finished && Platform . OS === "android" && listPaneFloor !== listPaneCeiling ) {
840852 runOnJS ( settleListPane ) ( listPaneRevision ) ;
841- runOnJS ( markSidebarSettled ) ( targetWidth > 0 ) ;
842853 }
843854 } ) ,
844855 ) ;
@@ -855,7 +866,8 @@ function AdaptiveWorkspaceLayoutContent(
855866 renderedSidebarWidth ,
856867 renderedRailWidth ,
857868 settleListPane ,
858- markSidebarSettled ,
869+ listPaneFloor ,
870+ listPaneCeiling ,
859871 listPaneRevision ,
860872 workspacePage ,
861873 ] ) ;
@@ -988,32 +1000,47 @@ function AdaptiveWorkspaceLayoutContent(
9881000 [ sidebarInLandscape , sidebarInPortrait , sidebarOverRotates ] ,
9891001 ) ;
9901002
991- const contextValue = {
992- layout,
993- panes,
994- fileInspector,
995- primarySidebarSearchQuery,
1003+ const actions = useStableActions ( {
9961004 startNewTask : handleStartNewTask ,
9971005 selectDetail : handleSelectDetail ,
9981006 registerWorkspaceInspector,
9991007 setPrimarySidebarSearchQuery,
10001008 showInspector,
10011009 toggleAuxiliaryPane,
1002- sidebarToggleable : layout . usesSplitView && ! primarySidebarPinned && ! workspacePage ,
10031010 togglePrimarySidebar,
10041011 revealPrimarySidebar,
10051012 setPrimarySidebarVisible,
10061013 openWorkspaceTab,
10071014 setAuxiliaryPaneWidth : setFileInspectorPreferredWidth ,
1008- nativeInspectorWidth,
10091015 setNativeInspectorWidth : ( width : number ) =>
10101016 setNativeInspectorWidthFraction ( inspectorWidthFor ( width / viewportWidth ) / viewportWidth ) ,
1011- composerKeyboardActive,
1012- keyboardHoldsSidebar,
10131017 setComposerEditing,
1014- sidebarByOrientation,
1015- workspacePageWidth,
1016- } ;
1018+ } ) ;
1019+ const contextValue = useMemo (
1020+ ( ) => ( { ...actions , layout, nativeInspectorWidth, workspacePageWidth } ) ,
1021+ [ actions , layout , nativeInspectorWidth , workspacePageWidth ] ,
1022+ ) ;
1023+ const sidebarToggleable = layout . usesSplitView && ! primarySidebarPinned && ! workspacePage ;
1024+ const paneState = useMemo (
1025+ ( ) => ( {
1026+ panes,
1027+ fileInspector,
1028+ primarySidebarSearchQuery,
1029+ sidebarToggleable,
1030+ composerKeyboardActive,
1031+ keyboardHoldsSidebar,
1032+ sidebarByOrientation,
1033+ } ) ,
1034+ [
1035+ panes ,
1036+ fileInspector ,
1037+ primarySidebarSearchQuery ,
1038+ sidebarToggleable ,
1039+ composerKeyboardActive ,
1040+ keyboardHoldsSidebar ,
1041+ sidebarByOrientation ,
1042+ ] ,
1043+ ) ;
10171044
10181045 const inspectorSheet = (
10191046 < WorkspaceInspectorSheet
@@ -1029,16 +1056,18 @@ function AdaptiveWorkspaceLayoutContent(
10291056 savedFilters = { props . savedThreadListFilters }
10301057 >
10311058 < AdaptiveWorkspaceContext value = { contextValue } >
1032- < NativeWorkspaceInspectorContext
1033- value = { {
1034- // Without a column the sheet shows it; the hidden native column must not mount it too.
1035- content : fileInspector . supported ? workspaceInspector ?. content : undefined ,
1036- visible : inspectorColumnTargetWidth > 0 ,
1037- } }
1038- >
1039- { props . children }
1040- { inspectorSheet }
1041- </ NativeWorkspaceInspectorContext >
1059+ < WorkspacePaneStateContext value = { paneState } >
1060+ < NativeWorkspaceInspectorContext
1061+ value = { {
1062+ // Without a column the sheet shows it; the hidden native column must not mount it too.
1063+ content : fileInspector . supported ? workspaceInspector ?. content : undefined ,
1064+ visible : inspectorColumnTargetWidth > 0 ,
1065+ } }
1066+ >
1067+ { props . children }
1068+ { inspectorSheet }
1069+ </ NativeWorkspaceInspectorContext >
1070+ </ WorkspacePaneStateContext >
10421071 </ AdaptiveWorkspaceContext >
10431072 </ HomeListOptionsProvider >
10441073 ) ;
@@ -1256,17 +1285,19 @@ function AdaptiveWorkspaceLayoutContent(
12561285 projectGroupingMode = { projectGroupingMode }
12571286 savedFilters = { props . savedThreadListFilters }
12581287 >
1259- < AdaptiveWorkspaceContext . Provider value = { contextValue } >
1260- < WindowHingeContext value = { windowHinge } >
1261- { usesPresentationHost ? (
1262- < WorkspaceColumnsRendererContext value = { renderColumns } >
1263- { props . children }
1264- </ WorkspaceColumnsRendererContext >
1265- ) : (
1266- renderColumns ( props . children )
1267- ) }
1268- </ WindowHingeContext >
1269- </ AdaptiveWorkspaceContext . Provider >
1288+ < AdaptiveWorkspaceContext value = { contextValue } >
1289+ < WorkspacePaneStateContext value = { paneState } >
1290+ < WindowHingeContext value = { windowHinge } >
1291+ { usesPresentationHost ? (
1292+ < WorkspaceColumnsRendererContext value = { renderColumns } >
1293+ { props . children }
1294+ </ WorkspaceColumnsRendererContext >
1295+ ) : (
1296+ renderColumns ( props . children )
1297+ ) }
1298+ </ WindowHingeContext >
1299+ </ WorkspacePaneStateContext >
1300+ </ AdaptiveWorkspaceContext >
12701301 </ HomeListOptionsProvider >
12711302 ) ;
12721303}
0 commit comments