@@ -262,6 +262,9 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
262262 const POSITION_SYNC_DELAY = 100 ;
263263 const BOUNDS_UPDATE_DELAY = 100 ;
264264
265+ const isInitialMountRef = useRef ( true ) ;
266+ const hasRestoredPositionRef = useRef ( false ) ;
267+
265268 const [ position , setPosition ] = useState < { x : number ; y : number } > ( ( ) => {
266269 const savedPosition = localStorage . getItem ( STORAGE_KEY ) ;
267270
@@ -270,13 +273,16 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
270273 const parsed = JSON . parse ( savedPosition ) ;
271274 const windowWidth = window . innerWidth ;
272275 const windowHeight = window . innerHeight ;
276+
277+ const margin = 50 ;
273278
274279 if (
275280 parsed . x >= 0
276- && parsed . x <= windowWidth - 200
281+ && parsed . x <= windowWidth - margin
277282 && parsed . y >= 0
278- && parsed . y <= windowHeight - 100
283+ && parsed . y <= windowHeight - margin
279284 ) {
285+ hasRestoredPositionRef . current = true ;
280286 return parsed ;
281287 }
282288 } catch ( e ) {
@@ -330,11 +336,9 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
330336 lastInteractionRef . current = Date . now ( ) ;
331337 } , [ ] ) ;
332338
333- // Smart recovery for window focus/visibility changes
334339 const smartRecovery = useCallback ( ( ) => {
335340 const timeSinceInteraction = Date . now ( ) - lastInteractionRef . current ;
336341 if ( timeSinceInteraction > 10000 ) {
337- // Aggressive recovery after 10 seconds of no interaction
338342 windowsCommands . removeFakeWindow ( "control" )
339343 . then ( ( ) => setTimeout ( updateOverlayBounds , POSITION_SYNC_DELAY ) )
340344 . catch ( console . error ) ;
@@ -344,12 +348,19 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
344348 trackInteraction ( ) ;
345349 } , [ updateOverlayBounds , trackInteraction ] ) ;
346350
347- // Sync actual DOM position with React state
348351 const syncActualPosition = useCallback ( ( element : HTMLDivElement | null ) => {
349352 if ( ! element ) {
350353 return ;
351354 }
352355
356+ if ( isInitialMountRef . current && hasRestoredPositionRef . current ) {
357+ isInitialMountRef . current = false ;
358+ setTimeout ( ( ) => {
359+ updateOverlayBounds ( ) ;
360+ } , POSITION_SYNC_DELAY * 2 ) ;
361+ return ;
362+ }
363+
353364 setTimeout ( ( ) => {
354365 const rect = element . getBoundingClientRect ( ) ;
355366 const actualPosition = { x : rect . left , y : rect . top } ;
@@ -362,13 +373,14 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
362373 setPosition ( actualPosition ) ;
363374 }
364375 } , POSITION_SYNC_DELAY ) ;
365- } , [ position ] ) ;
376+ } , [ position , updateOverlayBounds ] ) ;
366377
367- // Handle drag start
368378 const handleDragStart = useCallback ( ( e : React . MouseEvent ) => {
369379 e . preventDefault ( ) ;
370380 e . stopPropagation ( ) ;
371381
382+ isInitialMountRef . current = false ;
383+
372384 setIsDragging ( true ) ;
373385 setDragOffset ( {
374386 x : e . clientX - position . x ,
@@ -377,13 +389,11 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
377389 trackInteraction ( ) ;
378390 } , [ position , trackInteraction ] ) ;
379391
380- // Save position to localStorage
381392 useEffect ( ( ) => {
382393 localStorage . setItem ( STORAGE_KEY , JSON . stringify ( position ) ) ;
383394 debouncedUpdateBounds ( ) ;
384395 } , [ position , debouncedUpdateBounds ] ) ;
385396
386- // Handle global mouse events
387397 useEffect ( ( ) => {
388398 const handleMouseMove = ( e : MouseEvent ) => {
389399 if ( ! isDragging ) {
@@ -417,7 +427,6 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
417427 } ;
418428 } , [ isDragging , dragOffset , toolbarRef , updateOverlayBounds ] ) ;
419429
420- // Handle window events
421430 useEffect ( ( ) => {
422431 const handleWindowFocus = ( ) => smartRecovery ( ) ;
423432 const handleVisibilityChange = ( ) => {
@@ -431,7 +440,6 @@ function useFloatingPosition(toolbarRef: React.RefObject<HTMLDivElement>) {
431440 window . addEventListener ( "resize" , handleWindowResize ) ;
432441 document . addEventListener ( "visibilitychange" , handleVisibilityChange ) ;
433442
434- // Initial bounds setup
435443 setTimeout ( updateOverlayBounds , 200 ) ;
436444
437445 return ( ) => {
0 commit comments