From 0e5b64ecd2e291eac3475e10ab8aa1fa773a3a03 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 1 Jul 2015 19:01:27 -0600 Subject: [PATCH] SetScrollPosHere() Minor hack for effective "scroll to top" to appear like user expect it (#150) Compensate the difference between WindowPadding and ItemSpacing --- imgui.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 0405e9119..78be5fec6 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -4808,11 +4808,13 @@ float ImGui::GetScrollMaxY() void ImGui::SetScrollPosHere(float center_y_ratio) { // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size + ImGuiState& g = *GImGui; IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); ImGuiWindow* window = GetCurrentWindow(); window->ScrollTargetRelY = (float)(int)(window->ScrollY + window->DC.CursorPosPrevLine.y - window->Pos.y + (window->DC.PrevLineHeight) * center_y_ratio); - float line_spacing = (window->DC.CursorPos.y - window->DC.CursorPosPrevLine.y) - (window->DC.PrevLineHeight); - window->ScrollTargetRelY += line_spacing * (center_y_ratio - 0.5f) * 2.0f; + window->ScrollTargetRelY += g.Style.ItemSpacing.y * (center_y_ratio - 0.5f) * 2.0f; + if (center_y_ratio <= 0.0f && window->ScrollTargetRelY <= window->WindowPadding.y) // Minor hack to make "scroll to top" take account of WindowPadding, else it would scroll to (WindowPadding.y - ItemSpacing.y) + window->ScrollTargetRelY = 0.0f; window->ScrollTargetCenterRatioY = center_y_ratio; }