Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with SetNextWindowScroll #8196

Closed
pozemka opened this issue Nov 30, 2024 · 1 comment
Closed

Issue with SetNextWindowScroll #8196

pozemka opened this issue Nov 30, 2024 · 1 comment

Comments

@pozemka
Copy link

pozemka commented Nov 30, 2024

Version/Branch of Dear ImGui:

v1.91.5 docking-experimental

Back-ends:

imgui_impl_glfw.h + imgui_impl_opengl3.h

Compiler, OS:

Windows 10 + MSVC 2022

Full config/build information:

Dear ImGui 1.91.5 (19150)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1942
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000483
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigViewportsNoDecoration
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1280.00,720.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

Hello!

I am trying to scroll to the certain part of the table when window is just presented to user and let the user to scroll freely after that. I am using flag to call scroll function only on the first frame.

I found that using ImGui::SetScrollY will introduce 1 frame delay, which is visible in some situations.

Then I tried to use SetNextWindowScroll which should work on the same frame. But whatever I try it won't work on the first frame. Only on second. I found #6349 issue but adding SetNextWindowContentSize doesn't help.

In console I see that ContentSize was set but Scroll was not:

[00001] Inside window: Scroll=0.000/16269.000, ContentSize=17000.000

Here is my minimal code. Please note that not entire window is scrollable but only the table.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

const int   elements_count = 1000;
const float scroll_to_y = 200 * ImGui::GetTextLineHeightWithSpacing();
const float scroll_area_height = static_cast<float>(elements_count) * ImGui::GetTextLineHeightWithSpacing();

ImGui::SetNextWindowSize({400.f, 800.f}, ImGuiCond_Always);
ImGui::Begin("MyWindow", nullptr, ImGuiWindowFlags_NoSavedSettings);
ImGui::Text("This should be");
ImGui::Text("above scrollable table");

static int frame = 0;
// if (frame == 1) { //Work ok
if (frame == 0) { // Doesn't work
    ImGui::SetNextWindowContentSize(ImVec2(0, scroll_area_height));
    ImGui::SetNextWindowScroll(ImVec2(0.0f, scroll_to_y));
}
++frame;

static constexpr ImGuiTableFlags table_flags = ImGuiTableFlags_ScrollY;
if (ImGui::BeginTable("Table", 1, table_flags, ImGui::GetContentRegionAvail())) {
    for (int i = 0; i < elements_count; ++i) {
        ImGui::TableNextColumn();
        ImGui::Text("Element %d", i);
    }

    ImGuiWindow* window = GImGui->CurrentWindow;
    IMGUI_DEBUG_LOG("Inside window: Scroll=%.3f/%.3f, ContentSize=%.3f\n",
                    window->Scroll.y,
                    window->ScrollMax.y,
                    window->ContentSize.y);

    ImGui::EndTable();
}

ImGui::End();
ocornut added a commit that referenced this issue Dec 2, 2024
…() during the first frame or when scrolling flags have changed. (#8196)
@ocornut
Copy link
Owner

ocornut commented Dec 2, 2024

Thank you for your report and the repro which helped me look at it swiftly.
The issue was because tables are resetting scroll themselves on scrolling flags changes and init:

        // Reset scroll if we are reactivating it
        if ((previous_flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) == 0)
            SetNextWindowScroll(ImVec2(0.0f, 0.0f));

It should now be fixed with 43c51eb.
Thanks again!

@ocornut ocornut closed this as completed Dec 2, 2024
matthew-mccall pushed a commit to matthew-mccall/imgui that referenced this issue Jan 1, 2025
…() during the first frame or when scrolling flags have changed. (ocornut#8196)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants