MultiSelect: clear selection when leaving a scope with a nav directional request.

May need to clarify how to depends on actions being performed (e.g. click doesn't).
May become optional?
diff --git a/imgui.cpp b/imgui.cpp
index 26f0f36..19beed8 100644
--- a/imgui.cpp
+++ b/imgui.cpp
@@ -12251,6 +12251,7 @@
 
     // Process navigation init request (select first/default focus)
     g.NavJustMovedToId = 0;
+    g.NavJustMovedToFocusScopeId = g.NavJustMovedFromFocusScopeId = 0;
     if (g.NavInitResult.ID != 0)
         NavInitRequestApplyResult();
     g.NavInitRequest = false;
@@ -12403,6 +12404,7 @@
     ImGuiNavItemData* result = &g.NavInitResult;
     if (g.NavId != result->ID)
     {
+        g.NavJustMovedFromFocusScopeId = g.NavFocusScopeId;
         g.NavJustMovedToId = result->ID;
         g.NavJustMovedToFocusScopeId = result->FocusScopeId;
         g.NavJustMovedToKeyMods = 0;
@@ -12661,6 +12663,7 @@
     // PageUp/PageDown however sets always set NavJustMovedTo (vs Home/End which doesn't) mimicking Windows behavior.
     if ((g.NavId != result->ID || (g.NavMoveFlags & ImGuiNavMoveFlags_IsPageMove)) && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSelect) == 0)
     {
+        g.NavJustMovedFromFocusScopeId = g.NavFocusScopeId;
         g.NavJustMovedToId = result->ID;
         g.NavJustMovedToFocusScopeId = result->FocusScopeId;
         g.NavJustMovedToKeyMods = g.NavMoveKeyMods;
diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp
index 9ccb63d..94123de 100644
--- a/imgui_widgets.cpp
+++ b/imgui_widgets.cpp
@@ -7165,10 +7165,7 @@
     ms->BeginIO.NavIdItem = ms->EndIO.NavIdItem = storage->NavIdItem;
     ms->BeginIO.NavIdSelected = ms->EndIO.NavIdSelected = (storage->NavIdSelected == 1) ? true : false;
 
-    if (!ms->IsFocused)
-        return &ms->BeginIO; // This is cleared at this point.
-
-    // Auto clear when using Navigation to move within the selection
+    // Clear when using Navigation to move within the scope
     // (we compare FocusScopeId so it possible to use multiple selections inside a same window)
     if (g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == ms->FocusScopeId && g.NavJustMovedToHasSelectionData)
     {
@@ -7179,18 +7176,27 @@
         if ((ms->KeyMods & (ImGuiMod_Ctrl | ImGuiMod_Shift)) == 0)
             ms->BeginIO.RequestClear = true;
     }
-
-    // Shortcut: Select all (CTRL+A)
-    if (!(flags & ImGuiMultiSelectFlags_SingleSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll))
-        if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A))
-            ms->BeginIO.RequestSelectAll = true;
-
-    // Shortcut: Clear selection (Escape)
-    // FIXME-MULTISELECT: Only hog shortcut if selection is not null, meaning we need "has selection or "selection size" data here.
-    // Otherwise may be done by caller but it means Shortcut() needs to be exposed.
-    if (flags & ImGuiMultiSelectFlags_ClearOnEscape)
-        if (Shortcut(ImGuiKey_Escape))
+    else if (g.NavJustMovedFromFocusScopeId == ms->FocusScopeId)
+    {
+        // Also clear on leaving scope (may be optional?)
+        if ((ms->KeyMods & (ImGuiMod_Ctrl | ImGuiMod_Shift)) == 0)
             ms->BeginIO.RequestClear = true;
+    }
+
+    if (ms->IsFocused)
+    {
+        // Shortcut: Select all (CTRL+A)
+        if (!(flags & ImGuiMultiSelectFlags_SingleSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll))
+            if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A))
+                ms->BeginIO.RequestSelectAll = true;
+
+        // Shortcut: Clear selection (Escape)
+        // FIXME-MULTISELECT: Only hog shortcut if selection is not null, meaning we need "has selection or "selection size" data here.
+        // Otherwise may be done by caller but it means Shortcut() needs to be exposed.
+        if (flags & ImGuiMultiSelectFlags_ClearOnEscape)
+            if (Shortcut(ImGuiKey_Escape))
+                ms->BeginIO.RequestClear = true;
+    }
 
     if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection)
         DebugLogMultiSelectRequests("BeginMultiSelect", &ms->BeginIO);