ocornut / imgui_test_engine

Dear ImGui Automation Engine & Test Suite
386 stars 40 forks source link

CaptureScreenshotWindow on docked window leads to a black capture #34

Closed pthom closed 9 months ago

pthom commented 9 months ago

Hello Omar,

Since the capture hides all other windows, the parent window of a docked window will be hidden and as a result, the screenshot will be black.

There is no emergency. I do not need that at the moment.

Steps to reproduce

Add and call this inside app_minimal_main.cpp

void ShowDockedWindows()
{
    // Create parent DockSpace inside a window
    auto dockSpaceId =  ImGui::GetID("ParentDockSpace");
    ImGui::SetNextWindowSize(ImVec2(400.f, 400.f));
    ImGui::Begin("ParentDockWindow");
    ImGui::DockSpace(dockSpaceId, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_PassthruCentralNode);

    // Create two initially docked windows

    ImGui::SetNextWindowDockID(dockSpaceId, ImGuiCond_Appearing);
    ImGui::Begin("Window1");
    ImGui::Text("Window1");
    ImGui::End();

    ImGui::SetNextWindowDockID(dockSpaceId, ImGuiCond_Appearing);
    ImGui::Begin("Window2");
    ImGui::Text("Window2");
    ImGui::End();

    // End DockSpace's window
    ImGui::End();
}

Add this inside app_minimal_tests.cpp:

    t = IM_REGISTER_TEST(e, "demo_tests", "capture_docked_window");
    t->TestFunc = [](ImGuiTestContext* ctx)
    {
        ctx->ItemClick("Window1");
        ctx->CaptureScreenshotWindow("Window1");
    };

Attempted fix

I tried the following fix, but it is not enough:

imgui_te_context.cpp

bool ImGuiTestContext::CaptureAddWindow(ImGuiTestRef ref)
{
    ImGuiWindow* window = GetWindowByRef(ref);

    IM_CHECK_SILENT_RETV(window != NULL, false);
    CaptureArgs->InCaptureWindows.push_back(window);

    // Try to keep parent window visible if the window is docked: this is not enough
    ImGuiDockNode* dockNode = window->DockNode;
    if (dockNode != nullptr)
        CaptureArgs->InCaptureWindows.push_back(dockNode->HostWindow);

    return true;
}
ocornut commented 9 months ago

I pushed a fix, however this may be subject to further design decision, as currently you will get:

capture_docked_window_0000

Technically it is inconsistent with what we usually attempt for single-window, as "Window 2" tab is visible, but one could argue as soon as you attempt to capture docked window you are capturing neighboring state either way.