@@ -378,6 +378,16 @@ define(function (require, exports, module) {
378378 if ( ! _statusBarToggleInProgress ) {
379379 AnimationUtils . animateUsingClass ( $statusBarPanelToggle [ 0 ] , "flash" , 800 ) ;
380380 }
381+ // When the container collapses while tabs are still open (e.g. chevron
382+ // click or status-bar toggle), move those panels from the "shown" stack
383+ // to the "hidden" stack so the Escape-key handler doesn't silently close
384+ // tabs that the user can't even see.
385+ let openIds = PanelView . getOpenBottomPanelIDs ( ) ;
386+ for ( let i = 0 ; i < openIds . length ; i ++ ) {
387+ lastShownBottomPanelStack = lastShownBottomPanelStack . filter ( item => item !== openIds [ i ] ) ;
388+ lastHiddenBottomPanelStack = lastHiddenBottomPanelStack . filter ( item => item !== openIds [ i ] ) ;
389+ lastHiddenBottomPanelStack . push ( openIds [ i ] ) ;
390+ }
381391 } ) ;
382392
383393 $bottomPanelContainer . on ( "panelExpanded" , function ( ) {
@@ -388,6 +398,14 @@ define(function (require, exports, module) {
388398 if ( ! _statusBarToggleInProgress ) {
389399 AnimationUtils . animateUsingClass ( $statusBarPanelToggle [ 0 ] , "flash" , 800 ) ;
390400 }
401+ // When the container re-expands, move the open panels back from
402+ // the "hidden" stack to the "shown" stack.
403+ let openIds = PanelView . getOpenBottomPanelIDs ( ) ;
404+ for ( let i = 0 ; i < openIds . length ; i ++ ) {
405+ lastHiddenBottomPanelStack = lastHiddenBottomPanelStack . filter ( item => item !== openIds [ i ] ) ;
406+ lastShownBottomPanelStack = lastShownBottomPanelStack . filter ( item => item !== openIds [ i ] ) ;
407+ lastShownBottomPanelStack . push ( openIds [ i ] ) ;
408+ }
391409 } ) ;
392410
393411 // Sidebar is a special case: it isn't a Panel, and is not created dynamically. Need to explicitly
@@ -574,10 +592,15 @@ define(function (require, exports, module) {
574592 function _handleEscapeKey ( ) {
575593 let allPanelsIDs = getAllPanelIDs ( ) ;
576594 // first we see if there is any least recently shown panel
577- if ( lastShownBottomPanelStack . length > 0 ) {
595+ if ( lastShownBottomPanelStack . length > 0 ) {
578596 let panelToHide = getPanelForID ( lastShownBottomPanelStack . pop ( ) ) ;
579- panelToHide . hide ( ) ;
580- return true ;
597+ // Guard: only hide if the panel is actually visible. When the
598+ // container is collapsed the stacks are updated via panelCollapsed,
599+ // but this acts as a safety net against stale entries.
600+ if ( panelToHide && panelToHide . isVisible ( ) ) {
601+ panelToHide . hide ( ) ;
602+ return true ;
603+ }
581604 }
582605 // if not, see if there is any open panels that are not yet tracked in the least recently used stacks.
583606 for ( let panelID of allPanelsIDs ) {
0 commit comments