From 37958ca1b5ecabeba1636a3d2e80661bf011a31d Mon Sep 17 00:00:00 2001
From: ocornut <omarcornut@gmail.com>
Date: Thu, 3 Feb 2022 00:19:31 +0100
Subject: [PATCH] Docking: Fixed CTRL+TAB back into a docked window not
 selecting menu layer when no item are on main layer.

Could merge on master.
---
 docs/CHANGELOG.txt | 1 +
 imgui.cpp          | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt
index 971015de..1979d1da 100644
--- a/docs/CHANGELOG.txt
+++ b/docs/CHANGELOG.txt
@@ -235,6 +235,7 @@ Docking+Viewports Branch:
 - Docking: Tabs use their own identifier instead of the Window identifier.
   (This will invalidate some stored .ini data such as last selected tab, sorry!)
 - Docking: Fixed size constraints not working on single window holding on a dock id (still doesn't work on docked windows).
+- Docking: Fixed CTRL+TAB back into a docked window not selecting menu layer when no item are on main layer.
 - Viewports, IO: Added io.AddMouseViewportEvent() function to queue hovered viewport change (when known by backend).
 - Viewports: Relaxed specs for backend supporting ImGuiBackendFlags_HasMouseHoveredViewport: it is now _optional_
   for the backend to have to ignore viewports with the _NoInputs flag when call io.AddMouseViewportEvent(). It is
diff --git a/imgui.cpp b/imgui.cpp
index 0aa1a82c..676d830e 100644
--- a/imgui.cpp
+++ b/imgui.cpp
@@ -6964,7 +6964,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
 
         window->DC.NavLayerCurrent = ImGuiNavLayer_Main;
         window->DC.NavLayersActiveMask = window->DC.NavLayersActiveMaskNext;
-        window->DC.NavLayersActiveMaskNext = 0x00;
         window->DC.NavHideHighlightOneFrame = false;
         window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f);
 
@@ -7125,6 +7124,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
                 skip_items = true;
         window->SkipItems = skip_items;
 
+        // Only clear NavLayersActiveMaskNext when marked as visible, so a CTRL+Tab back can use a safe value.
+        if (!window->SkipItems)
+            window->DC.NavLayersActiveMaskNext = 0x00;
+
         // Sanity check: there are two spots which can set Appearing = true
         // - when 'window_just_activated_by_user' is set -> HiddenFramesCannotSkipItems is set -> SkipItems always false
         // - in BeginDocked() path when DockNodeIsVisible == DockTabIsVisible == true -> hidden _should_ be all zero // FIXME: Not formally proven, hence the assert.