@@ -68,6 +68,44 @@ function readViewportWidth(): number {
6868 return window . innerWidth ;
6969}
7070
71+ // getWindowFullscreenState is a synchronous IPC round trip, so the snapshot is cached
72+ // and only re-read when a subscription starts.
73+ let windowFullscreenSnapshot : boolean | null = null ;
74+
75+ function macosDesktopBridge ( ) {
76+ if ( ! isElectron || ! isMacPlatform ( navigator . platform ) ) return null ;
77+ return window . desktopBridge ?? null ;
78+ }
79+
80+ function subscribeToWindowFullscreen ( onChange : ( ) => void ) : ( ) => void {
81+ const bridge = macosDesktopBridge ( ) ;
82+ if (
83+ typeof bridge ?. getWindowFullscreenState !== "function" ||
84+ typeof bridge . onWindowFullscreenStateChange !== "function"
85+ ) {
86+ return ( ) => { } ;
87+ }
88+ const unsubscribe = bridge . onWindowFullscreenStateChange ( ( fullscreen ) => {
89+ windowFullscreenSnapshot = fullscreen ;
90+ onChange ( ) ;
91+ } ) ;
92+ // The window may have changed state between the first render and this subscription.
93+ windowFullscreenSnapshot = bridge . getWindowFullscreenState ( ) ;
94+ return ( ) => {
95+ unsubscribe ( ) ;
96+ windowFullscreenSnapshot = null ;
97+ } ;
98+ }
99+
100+ function readWindowFullscreen ( ) : boolean {
101+ if ( windowFullscreenSnapshot === null ) {
102+ const getWindowFullscreenState = macosDesktopBridge ( ) ?. getWindowFullscreenState ;
103+ windowFullscreenSnapshot =
104+ typeof getWindowFullscreenState === "function" ? getWindowFullscreenState ( ) : false ;
105+ }
106+ return windowFullscreenSnapshot ;
107+ }
108+
71109function readInitialThreadSidebarWidth ( ) : number {
72110 try {
73111 return resolveInitialThreadSidebarWidth (
@@ -245,12 +283,10 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
245283 }
246284 setSidebarWidth ( resolveInitialThreadSidebarWidth ( null , viewportWidth ) ) ;
247285 } ;
248- const [ isWindowFullscreen , setIsWindowFullscreen ] = useState ( ( ) => {
249- const getWindowFullscreenState = window . desktopBridge ?. getWindowFullscreenState ;
250- return isMacosDesktop && typeof getWindowFullscreenState === "function"
251- ? getWindowFullscreenState ( )
252- : false ;
253- } ) ;
286+ const isWindowFullscreen = useSyncExternalStore (
287+ subscribeToWindowFullscreen ,
288+ readWindowFullscreen ,
289+ ) ;
254290 const windowControlsInsetStyle = (
255291 isMacosDesktop && ! isWindowFullscreen
256292 ? { "--workspace-controls-left" : MACOS_TRAFFIC_LIGHTS_LEFT_INSET }
@@ -262,23 +298,6 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
262298 ...windowControlsInsetStyle ,
263299 } as CSSProperties ;
264300
265- useEffect ( ( ) => {
266- if ( ! isMacosDesktop ) return ;
267- const bridge = window . desktopBridge ;
268- if ( ! bridge ) return ;
269- const { getWindowFullscreenState, onWindowFullscreenStateChange } = bridge ;
270- if (
271- typeof getWindowFullscreenState !== "function" ||
272- typeof onWindowFullscreenStateChange !== "function"
273- ) {
274- return ;
275- }
276-
277- const unsubscribe = onWindowFullscreenStateChange ( setIsWindowFullscreen ) ;
278- setIsWindowFullscreen ( getWindowFullscreenState ( ) ) ;
279- return unsubscribe ;
280- } , [ isMacosDesktop ] ) ;
281-
282301 useEffect ( ( ) => {
283302 const onMenuAction = window . desktopBridge ?. onMenuAction ;
284303 if ( typeof onMenuAction !== "function" ) {
0 commit comments