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

Unable to programmatically resize a child #8020

Open
redmercury opened this issue Sep 26, 2024 · 5 comments
Open

Unable to programmatically resize a child #8020

redmercury opened this issue Sep 26, 2024 · 5 comments

Comments

@redmercury
Copy link

Version/Branch of Dear ImGui:

Latest master (9644c51)

Back-ends:

imgui_impl_dx12.cpp + imgui_impl_win32.cpp

Compiler, OS:

Windows 11, MSVC 2022

Full config/build information:

1.91.3 WIP (19121)

Details:

My Issue/Question:
I'm trying to resize some children from their initially created size based on some factors and miserably failing. The children resize with the horizontal resize bars, but if I try to force it from code the size doesn't apply. Am I holding it wrong?

In the MCVE I set a certain size on the first frame which is applied to the window and subsequently try to set a different size on later frames.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// Here's some code anyone can copy and paste to reproduce your issue
    ImGui::SetNextWindowSize(ImVec2(300, 600));
    if (!ImGui::Begin("Test")) {
        ImGui::End();
    }

    const ImGuiChildFlags group_flags = ImGuiChildFlags_ResizeY | ImGuiChildFlags_Border;

    static float height;
    static bool first = true;

    if (first) {
        height = 50;
        first = false;
    } else {
        height = 500;
    }

    ImGui::SetNextWindowSize(ImVec2(-1, height));
    if (ImGui::BeginChild("groups", ImVec2(200, height), ImGuiChildFlags_ResizeY | ImGuiChildFlags_Border, ImGuiWindowFlags_NoSavedSettings)) {
        ImGui::Text("Hello");
    }
    ImGui::EndChild();
    ImGui::End();
@redmercury
Copy link
Author

I should add to this that I want the user to be able to dynamically resize the child (hence ImGuiChildFlags_ResizeY) as well as force it in code.

@ocornut
Copy link
Owner

ocornut commented Sep 26, 2024

I should add to this that I want the user to be able to dynamically resize the child (hence ImGuiChildFlags_ResizeY) as well as force it in code.

Presumably not at the same time? As in, you are not calling SetNextWindowSizs() every frame, but only on certain events?

I will investigate this when possible, it is possible that there may be an issue.

@GamingMinds-DanielC
Copy link
Contributor

If you force the height every frame, the user won't be able to resize at all, you need to restrict the forced height to those frames where you actually want to set it. Besides that, for user resizable children the user specified size is prioritized and overwrites the size specified by code, so you might need to get a bit creative.

One option is to automatically resize with ImGuiChildFlags_AutoResizeY (for one frame), you can set the content size beforehand with ImGui::SetNextWindowContentSize(). That should work if you want to resize to the actual content size.

Another option is to (also for one frame) limit the size with ImGui::SetNextWindowSizeConstraints(). You can set the minimum and maximum height to the same value to force it. For the width you can specify 0.0f and FLT_MAX to not affect the current value.

@ocornut
Copy link
Owner

ocornut commented Sep 26, 2024

I can confirm that this currently doesn't work:

if (ImGui::Button("Set Height to 500"))
    ImGui::SetNextWindowSize(ImVec2(-FLT_MIN, 500.0f));

ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_FrameBg));
if (ImGui::BeginChild("ResizableChild", ImVec2(-FLT_MIN, ImGui::GetTextLineHeightWithSpacing() * 8), ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeY))
    for (int n = 0; n < 10; n++)
        ImGui::Text("Line %04d", n);
ImGui::PopStyleColor();
ImGui::EndChild();

Aka SetNextWindowSize() is (and was always) completely ignored by BeginChild(), because it itself takes a size parameter.
This was ok for a long time, but with the addition of e.g. ImGuiChildFlags_ResizeY which makes the size parameter an "initial size" it would make sense that SetNextWindowSize() was honored as a possible override.

ocornut added a commit that referenced this issue Sep 26, 2024
…n a child window using ImGuiChildFlags_ResizeX/ImGuiChildFlags_ResizeY. (#1710, #8020)
ocornut added a commit to ocornut/imgui_test_engine that referenced this issue Sep 26, 2024
@ocornut
Copy link
Owner

ocornut commented Sep 26, 2024

I have pushed a fix 797101a for the case in my previous post. I'm not entirely sure this is what you wanted but it seems useful to allow it. Unfortunately we can't easily support SetNextWindowSize() conditional flags in that path, but they make much less sense.

Amended test:
ocornut/imgui_test_engine@473e9ef

Let me know if that solves your problem!

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

3 participants