MovingWindow: Track click offset based on MovingWindow and not its RootWindow. Followup to 3849bb4470aebb4ff276113170f26cc82f990f49. Comments + adding a local to ease patch #1345.
diff --git a/imgui.cpp b/imgui.cpp
index ff6b871..fee26e2 100644
--- a/imgui.cpp
+++ b/imgui.cpp
@@ -2392,10 +2392,17 @@
IM_ASSERT(g.MovingWindow->MoveId == g.MovingWindowMoveId);
if (g.IO.MouseDown[0])
{
+ // MovingWindow = window we clicked on, could be a child window. We track it to preserve Focus and so that ActiveIdWindow == MovingWindow and ActiveId == MovingWindow->MoveId for consistency.
+ // actually_moving_window = MovingWindow->RootWindow.
+ ImGuiWindow* actually_moving_window = g.MovingWindow->RootWindow;
ImVec2 pos = g.IO.MousePos - g.ActiveIdClickOffset;
- if (g.MovingWindow->RootWindow->PosFloat.x != pos.x || g.MovingWindow->RootWindow->PosFloat.y != pos.y)
- MarkIniSettingsDirty(g.MovingWindow->RootWindow);
- g.MovingWindow->RootWindow->PosFloat = pos;
+ if (actually_moving_window != g.MovingWindow)
+ pos += actually_moving_window->PosFloat - g.MovingWindow->PosFloat;
+ if (actually_moving_window->PosFloat.x != pos.x || actually_moving_window->PosFloat.y != pos.y)
+ {
+ MarkIniSettingsDirty(actually_moving_window);
+ actually_moving_window->PosFloat = pos;
+ }
FocusWindow(g.MovingWindow);
}
else
@@ -2950,7 +2957,7 @@
// Set ActiveId even if the _NoMove flag is set, without it dragging away from a window with _NoMove would activate hover on other windows.
FocusWindow(g.HoveredWindow);
SetActiveID(g.HoveredWindow->MoveId, g.HoveredWindow);
- g.ActiveIdClickOffset = g.IO.MousePos - g.HoveredRootWindow->Pos;
+ g.ActiveIdClickOffset = g.IO.MousePos - g.HoveredWindow->Pos;
if (!(g.HoveredWindow->Flags & ImGuiWindowFlags_NoMove) && !(g.HoveredRootWindow->Flags & ImGuiWindowFlags_NoMove))
{
g.MovingWindow = g.HoveredWindow;
diff --git a/imgui_internal.h b/imgui_internal.h
index 87a4a13..797f707 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -534,7 +534,7 @@
bool ActiveIdAllowOverlap; // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always)
ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior)
ImGuiWindow* ActiveIdWindow;
- ImGuiWindow* MovingWindow; // Track the child window we clicked on to move a window.
+ ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actually window that is moved is generally MovingWindow->RootWindow.
ImGuiID MovingWindowMoveId; // == MovingWindow->MoveId
ImVector<ImGuiColMod> ColorModifiers; // Stack for PushStyleColor()/PopStyleColor()
ImVector<ImGuiStyleMod> StyleModifiers; // Stack for PushStyleVar()/PopStyleVar()