Skip to content

SetNextWindowSize() on single axis doesn't perform auto-fit calculation correctly #9060

Closed

Description

@achabense

Version/Branch of Dear ImGui:

Version 1.92.2b

Back-ends:

Any

Compiler, OS:

Any

Full config/build information:

No response

Details:

I think window's size calculation should take account of whether the window uses scrollbar. Take the screenshots for example, both auto-resized windows (ImGuiWindowFlags_AlwaysAutoResize) and resizable windows (auto-fitting) are affected by this.

Screenshots/Video:

  1. Auto-resized window: (| of the longest string becomes hidden by the scrollbar)
    Image

  2. Resizable window: (| of the longest string becomes hidden after auto-fitting)
    Image

Minimal, Complete and Verifiable Example code:

static void issue() {
    static bool auto_resize = true;
    static bool fix_height = false; // * auto_resize -> scrollbar.
    if (fix_height) {
        ImGui::SetNextWindowSize({ 0,ImGui::GetTextLineHeightWithSpacing() * 10 });
    }
    if (ImGui::Begin("Issue", 0, (auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : ImGuiWindowFlags_None) | ImGuiWindowFlags_NoSavedSettings)) {
        ImGui::Checkbox("AutoResize", &auto_resize);
        ImGui::Checkbox("FixHeight", &fix_height);
        for (const int n : {1, 5, 2, 3, 9, 6, 3, 1}) {
            ImGui::TextUnformatted((std::string(n + 10, 'a') + "|").c_str());
        }
    }
    ImGui::End();
}

Activity

  1. changed the title [-]Window-size calculation should take account of scrollbar[/-] [+]SetNextWindowSize() on single axis + _AlwaysAutoResize conflict[/+] on Nov 10, 2025
  2. changed the title [-]SetNextWindowSize() on single axis + _AlwaysAutoResize conflict[/-] [+]SetNextWindowSize() on single axis doesn't perform auto-fit calculation correctly[/+] on Nov 10, 2025
  3. ocornut commented on Nov 10, 2025

    @ocornut
    Owner

    Window-size calculation should take account of scrollbar[/-]

    In most cases it does, but it seems like ImGui::SetNextWindowSize({ 0,ImGui::GetTextLineHeightWithSpacing() * 10 }); actually breaks the logic for auto-fitting indeed.

    Note that this works:

    if (fix_height)
    {
        float height = ImGui::GetTextLineHeightWithSpacing() * 10;
    --  ImGui::SetNextWindowSize({ 0, height });
    ++  ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, height), ImVec2(FLT_MAX, height));
    }
  4. achabense commented on Nov 10, 2025

    @achabense
    ContributorAuthor

    Thanks! Changing SetNextWindowSize to SetNextWindowSizeConstraints solves my original problem.

    So the second screenshot (auto-fitting) seems a separate problem. Initially I thought they are the same one.

    This is enough to reproduce the second screenshot. (When the window has vertical scrollbar and auto-fit horizontally.)

    {
        if (ImGui::Begin("Auto", 0, ImGuiWindowFlags_NoSavedSettings)) {
            for (const int n : {1, 5, 2, 3, 9, 6, 3, 1}) {
                ImGui::TextUnformatted((std::string(n + 10, 'a') + "|").c_str());
            }
        }
        ImGui::End();
    }
  5. ocornut commented on Nov 10, 2025

    @ocornut
    Owner

    Right, this correspond to this FIXME in UpdateWindowManualResize():

    // Double-clicking bottom or right border auto-fit on this axis
    // FIXME: CalcWindowAutoFitSize() doesn't take into account that only one side may be auto-fit when calculating scrollbars.
    if (border_n == 1 || border_n == 3) // Right and bottom border
    {
        size_target[axis] = CalcWindowSizeAfterConstraint(window, size_auto_fit)[axis];
        ret_auto_fit_mask |= (1 << axis);
        hovered = held = false; // So border doesn't show highlighted at new position
    }

    Since both uses of size_auto_fit are on specific actions, we could remove this parameter and instead redesign CalcWindowAutoFitSize() to support single-axis mode, and call this in UpdateWindowManualResize().

  6. ocornut commented on Nov 10, 2025

    @ocornut
    Owner

    I believe this should fix the second issue, and it's passing imgui_test_suite, and can be foundation to fix the first issue too.

    But I'll work on adding more tests first.

  7. ocornut commented on Nov 10, 2025

    @ocornut
    Owner
  8. ocornut commented on Nov 11, 2025

    @ocornut
    Owner

    Note that 7537ba2 actually removed the need for the hack added in 29439bd and tested by "window_child_resize". (#1710)

  9. ocornut commented on Nov 11, 2025

    @ocornut
    Owner

    Both cases should be fixed now.
    Because window sizing code is extremely sensitive I zealously split the changes in many commits.

    It's not actually many changes but out of experience this is generally going to be useful if I need to come back to this later.

    Now bracing for impact....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions