Skip to content

Commit 936f532

Browse files
committed
Internals: maintaining focus order inside windows + only storing root windows in WindowsFocusOrder[] array. (toward #2304)
1 parent 770f9da commit 936f532

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

imgui.cpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4034,7 +4034,7 @@ void ImGui::NewFrame()
40344034
UpdateTabFocus();
40354035

40364036
// Mark all windows as not visible and compact unused memory.
4037-
IM_ASSERT(g.WindowsFocusOrder.Size == g.Windows.Size);
4037+
IM_ASSERT(g.WindowsFocusOrder.Size <= g.Windows.Size);
40384038
const float memory_compact_start_time = (g.GcCompactAll || g.IO.ConfigMemoryCompactTimer < 0.0f) ? FLT_MAX : (float)g.Time - g.IO.ConfigMemoryCompactTimer;
40394039
for (int i = 0; i != g.Windows.Size; i++)
40404040
{
@@ -5143,7 +5143,12 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags)
51435143
window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0);
51445144
}
51455145

5146-
g.WindowsFocusOrder.push_back(window);
5146+
if (!(flags & ImGuiWindowFlags_ChildWindow))
5147+
{
5148+
g.WindowsFocusOrder.push_back(window);
5149+
window->FocusOrder = (short)(g.WindowsFocusOrder.Size - 1);
5150+
}
5151+
51475152
if (flags & ImGuiWindowFlags_NoBringToFrontOnFocus)
51485153
g.Windows.push_front(window); // Quite slow but rare and only once
51495154
else
@@ -6378,15 +6383,22 @@ void ImGui::End()
63786383
void ImGui::BringWindowToFocusFront(ImGuiWindow* window)
63796384
{
63806385
ImGuiContext& g = *GImGui;
6386+
IM_ASSERT(window == window->RootWindow);
6387+
6388+
const int cur_order = window->FocusOrder;
6389+
IM_ASSERT(g.WindowsFocusOrder[cur_order] == window);
63816390
if (g.WindowsFocusOrder.back() == window)
63826391
return;
6383-
for (int i = g.WindowsFocusOrder.Size - 2; i >= 0; i--) // We can ignore the top-most window
6384-
if (g.WindowsFocusOrder[i] == window)
6385-
{
6386-
memmove(&g.WindowsFocusOrder[i], &g.WindowsFocusOrder[i + 1], (size_t)(g.WindowsFocusOrder.Size - i - 1) * sizeof(ImGuiWindow*));
6387-
g.WindowsFocusOrder[g.WindowsFocusOrder.Size - 1] = window;
6388-
break;
6389-
}
6392+
6393+
const int new_order = g.WindowsFocusOrder.Size - 1;
6394+
for (int n = cur_order; n < new_order; n++)
6395+
{
6396+
g.WindowsFocusOrder[n] = g.WindowsFocusOrder[n + 1];
6397+
g.WindowsFocusOrder[n]->FocusOrder--;
6398+
IM_ASSERT(g.WindowsFocusOrder[n]->FocusOrder == n);
6399+
}
6400+
g.WindowsFocusOrder[new_order] = window;
6401+
window->FocusOrder = (short)new_order;
63906402
}
63916403

63926404
void ImGui::BringWindowToDisplayFront(ImGuiWindow* window)
@@ -6466,18 +6478,13 @@ void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWind
64666478
{
64676479
ImGuiContext& g = *GImGui;
64686480

6469-
int start_idx = g.WindowsFocusOrder.Size - 1;
6470-
if (under_this_window != NULL)
6471-
{
6472-
int under_this_window_idx = FindWindowFocusIndex(under_this_window);
6473-
if (under_this_window_idx != -1)
6474-
start_idx = under_this_window_idx - 1;
6475-
}
6481+
const int start_idx = ((under_this_window != NULL) ? FindWindowFocusIndex(under_this_window) : g.WindowsFocusOrder.Size) - 1;
64766482
for (int i = start_idx; i >= 0; i--)
64776483
{
64786484
// We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user.
64796485
ImGuiWindow* window = g.WindowsFocusOrder[i];
6480-
if (window != ignore_window && window->WasActive && !(window->Flags & ImGuiWindowFlags_ChildWindow))
6486+
IM_ASSERT(window == window->RootWindow);
6487+
if (window != ignore_window && window->WasActive)
64816488
if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs))
64826489
{
64836490
ImGuiWindow* focus_window = NavRestoreLastChildNavWindow(window);
@@ -9479,13 +9486,12 @@ static void ImGui::NavEndFrame()
94799486
}
94809487
}
94819488

9482-
static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) // FIXME-OPT O(N)
9489+
static int ImGui::FindWindowFocusIndex(ImGuiWindow* window)
94839490
{
94849491
ImGuiContext& g = *GImGui;
9485-
for (int i = g.WindowsFocusOrder.Size - 1; i >= 0; i--)
9486-
if (g.WindowsFocusOrder[i] == window)
9487-
return i;
9488-
return -1;
9492+
int order = window->FocusOrder;
9493+
IM_ASSERT(g.WindowsFocusOrder[order] == window);
9494+
return order;
94899495
}
94909496

94919497
static ImGuiWindow* FindWindowNavFocusable(int i_start, int i_stop, int dir) // FIXME-OPT O(N)

imgui_internal.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ struct ImGuiContext
13221322

13231323
// Windows state
13241324
ImVector<ImGuiWindow*> Windows; // Windows, sorted in display order, back to front
1325-
ImVector<ImGuiWindow*> WindowsFocusOrder; // Windows, sorted in focus order, back to front. (FIXME: We could only store root windows here! Need to sort out the Docking equivalent which is RootWindowDockStop and is unfortunately a little more dynamic)
1325+
ImVector<ImGuiWindow*> WindowsFocusOrder; // Root windows, sorted in focus order, back to front.
13261326
ImVector<ImGuiWindow*> WindowsTempSortBuffer; // Temporary buffer used in EndFrame() to reorder windows so parents are kept before their child
13271327
ImVector<ImGuiWindow*> CurrentWindowStack;
13281328
ImGuiStorage WindowsById; // Map window's ImGuiID to ImGuiWindow*
@@ -1786,8 +1786,9 @@ struct IMGUI_API ImGuiWindow
17861786
bool HasCloseButton; // Set when the window has a close button (p_open != NULL)
17871787
signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3)
17881788
short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs)
1789-
short BeginOrderWithinParent; // Order within immediate parent window, if we are a child window. Otherwise 0.
1790-
short BeginOrderWithinContext; // Order within entire imgui context. This is mostly used for debugging submission order related issues.
1789+
short BeginOrderWithinParent; // Begin() order within immediate parent window, if we are a child window. Otherwise 0.
1790+
short BeginOrderWithinContext; // Begin() order within entire imgui context. This is mostly used for debugging submission order related issues.
1791+
short FocusOrder; // Order within WindowsFocusOrder[], altered when windows are focused.
17911792
ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)
17921793
ImS8 AutoFitFramesX, AutoFitFramesY;
17931794
ImS8 AutoFitChildAxises;

0 commit comments

Comments
 (0)