Skip to content

Commit f1d1a8d

Browse files
committed
Windows: use relative mouse movement for border resize when the border geometry has moved. (ocornut#1710)
(e.g. resizing a child window triggering parent scroll) to avoid resizing feedback loop.
1 parent 9235352 commit f1d1a8d

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

docs/CHANGELOG.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Other changes:
9696
to false when popup is closed in ways other than clicking the close button. (#6900)
9797
- Double-clicking lower-left resize grip auto-resize (like lower-rightone).
9898
- Double-clicking bottom or right window border auto-resize on a singles axis.
99+
- Use relative mouse movement for border resize when the border geometry has moved
100+
(e.g. resizing a child window triggering parent scroll) in order to avoid resizing
101+
feedback loops. Unless manually mouse-wheeling while border resizing. (#1710)
99102
- Separators:
100103
- Altered end-points to use more standard boundaries. (#205, #4787, #1643)
101104
Left position is always current cursor X position, right position is always work-rect

imgui.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5964,7 +5964,7 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si
59645964
ImGuiID border_id = window->GetID(border_n + 4); // == GetWindowResizeBorderID()
59655965
ItemAdd(border_rect, border_id, NULL, ImGuiItemFlags_NoNav);
59665966
ButtonBehavior(border_rect, border_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus);
5967-
//GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255));
5967+
//GetForegroundDrawList(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255));
59685968
if (hovered && g.HoveredIdTimer <= WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER)
59695969
hovered = false;
59705970
if (hovered || held)
@@ -5983,14 +5983,44 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si
59835983
}
59845984
else if (held)
59855985
{
5986+
// Switch to relative resizing mode when border geometry moved (e.g. resizing a child altering parent scroll), in order to avoid resizing feedback loop.
5987+
// Currently only using relative mode on resizable child windows, as the problem to solve is more likely noticeable for them, but could apply for all windows eventually.
5988+
// FIXME: May want to generalize this idiom at lower-level, so more widgets can use it!
5989+
const bool just_scrolled_manually_while_resizing = (g.WheelingWindow != NULL && g.WheelingWindowScrolledFrame == g.FrameCount && IsWindowChildOf(window, g.WheelingWindow, false));
5990+
if (g.ActiveIdIsJustActivated || just_scrolled_manually_while_resizing)
5991+
{
5992+
g.WindowResizeBorderExpectedRect = border_rect;
5993+
g.WindowResizeRelativeMode = false;
5994+
}
5995+
if ((window->Flags & ImGuiWindowFlags_ChildWindow) && memcmp(&g.WindowResizeBorderExpectedRect, &border_rect, sizeof(ImRect)) != 0)
5996+
g.WindowResizeRelativeMode = true;
5997+
5998+
const ImVec2 border_curr = (window->Pos + ImMin(def.SegmentN1, def.SegmentN2) * window->Size);
5999+
const float border_target_rel_mode_for_axis = border_curr[axis] + g.IO.MouseDelta[axis];
6000+
const float border_target_abs_mode_for_axis = g.IO.MousePos[axis] - g.ActiveIdClickOffset[axis] + WINDOWS_HOVER_PADDING; // Match ButtonBehavior() padding above.
6001+
6002+
// Use absolute mode position
6003+
ImVec2 border_target = window->Pos;
6004+
border_target[axis] = border_target_abs_mode_for_axis;
6005+
6006+
// Use relative mode target for child window, ignore resize when moving back toward the ideal absolute position.
6007+
bool ignore_resize = false;
6008+
if (g.WindowResizeRelativeMode)
6009+
{
6010+
//GetForegroundDrawList()->AddText(GetMainViewport()->WorkPos, IM_COL32_WHITE, "Relative Mode");
6011+
border_target[axis] = border_target_rel_mode_for_axis;
6012+
if (g.IO.MouseDelta[axis] == 0.0f || (g.IO.MouseDelta[axis] > 0.0f) == (border_target_rel_mode_for_axis > border_target_abs_mode_for_axis))
6013+
ignore_resize = true;
6014+
}
6015+
6016+
// Clamp, apply
59866017
ImVec2 clamp_min(border_n == ImGuiDir_Right ? clamp_rect.Min.x : -FLT_MAX, border_n == ImGuiDir_Down || (border_n == ImGuiDir_Up && window_move_from_title_bar) ? clamp_rect.Min.y : -FLT_MAX);
59876018
ImVec2 clamp_max(border_n == ImGuiDir_Left ? clamp_rect.Max.x : +FLT_MAX, border_n == ImGuiDir_Up ? clamp_rect.Max.y : +FLT_MAX);
5988-
ImVec2 border_target = window->Pos;
5989-
border_target[axis] = g.IO.MousePos[axis] - g.ActiveIdClickOffset[axis] + WINDOWS_HOVER_PADDING;
59906019
border_target = ImClamp(border_target, clamp_min, clamp_max);
59916020
if (window->Flags & ImGuiWindowFlags_ChildWindow) // Clamp resizing of childs within parent
59926021
border_target = ImClamp(border_target, window->ParentWindow->InnerClipRect.Min, window->ParentWindow->InnerClipRect.Max);
5993-
CalcResizePosSizeFromAnyCorner(window, border_target, ImMin(def.SegmentN1, def.SegmentN2), &pos_target, &size_target);
6022+
if (!ignore_resize)
6023+
CalcResizePosSizeFromAnyCorner(window, border_target, ImMin(def.SegmentN1, def.SegmentN2), &pos_target, &size_target);
59946024
}
59956025
if (hovered)
59966026
*border_hovered = border_n;
@@ -6043,6 +6073,10 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si
60436073
if (size_target.x != FLT_MAX || size_target.y != FLT_MAX || pos_target.x != FLT_MAX || pos_target.y != FLT_MAX)
60446074
MarkIniSettingsDirty(window);
60456075

6076+
// Recalculate next expected border expected coordinates
6077+
if (*border_held != -1)
6078+
g.WindowResizeBorderExpectedRect = GetResizeBorderRect(window, *border_held, grip_hover_inner_size, WINDOWS_HOVER_PADDING);
6079+
60466080
return ret_auto_fit_mask;
60476081
}
60486082

@@ -8931,13 +8965,15 @@ void ImGui::UpdateMouseWheel()
89318965
float max_step = window->InnerRect.GetWidth() * 0.67f;
89328966
float scroll_step = ImTrunc(ImMin(2 * window->CalcFontSize(), max_step));
89338967
SetScrollX(window, window->Scroll.x - wheel.x * scroll_step);
8968+
g.WheelingWindowScrolledFrame = g.FrameCount;
89348969
}
89358970
if (do_scroll[ImGuiAxis_Y])
89368971
{
89378972
LockWheelingWindow(window, wheel.y);
89388973
float max_step = window->InnerRect.GetHeight() * 0.67f;
89398974
float scroll_step = ImTrunc(ImMin(5 * window->CalcFontSize(), max_step));
89408975
SetScrollY(window, window->Scroll.y - wheel.y * scroll_step);
8976+
g.WheelingWindowScrolledFrame = g.FrameCount;
89418977
}
89428978
}
89438979
}

imgui_internal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,7 @@ struct ImGuiContext
18991899
ImGuiWindow* WheelingWindow; // Track the window we started mouse-wheeling on. Until a timer elapse or mouse has moved, generally keep scrolling the same window even if during the course of scrolling the mouse ends up hovering a child window.
19001900
ImVec2 WheelingWindowRefMousePos;
19011901
int WheelingWindowStartFrame; // This may be set one frame before WheelingWindow is != NULL
1902+
int WheelingWindowScrolledFrame;
19021903
float WheelingWindowReleaseTimer;
19031904
ImVec2 WheelingWindowWheelRemainder;
19041905
ImVec2 WheelingAxisAvg;
@@ -2091,6 +2092,8 @@ struct ImGuiContext
20912092
ImU32 ColorEditSavedColor; // RGB value with alpha set to 0.
20922093
ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker.
20932094
ImGuiComboPreviewData ComboPreviewData;
2095+
ImRect WindowResizeBorderExpectedRect; // Expected border rect, switch to relative edit if moving
2096+
bool WindowResizeRelativeMode;
20942097
float SliderGrabClickOffset;
20952098
float SliderCurrentAccum; // Accumulated slider delta when using navigation controls.
20962099
bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it?
@@ -2187,7 +2190,7 @@ struct ImGuiContext
21872190
HoveredWindowUnderMovingWindow = NULL;
21882191
MovingWindow = NULL;
21892192
WheelingWindow = NULL;
2190-
WheelingWindowStartFrame = -1;
2193+
WheelingWindowStartFrame = WheelingWindowScrolledFrame = -1;
21912194
WheelingWindowReleaseTimer = 0.0f;
21922195

21932196
DebugHookIdInfo = 0;
@@ -2289,6 +2292,7 @@ struct ImGuiContext
22892292
ColorEditCurrentID = ColorEditSavedID = 0;
22902293
ColorEditSavedHue = ColorEditSavedSat = 0.0f;
22912294
ColorEditSavedColor = 0;
2295+
WindowResizeRelativeMode = false;
22922296
SliderGrabClickOffset = 0.0f;
22932297
SliderCurrentAccum = 0.0f;
22942298
SliderCurrentAccumDirty = false;

0 commit comments

Comments
 (0)