ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
60.62k stars 10.23k forks source link

Previous window geometry #7284

Closed pozemka closed 8 months ago

pozemka commented 8 months ago

Version/Branch of Dear ImGui:

dear imgui, v1.90.2 WIP docking

Back-ends:

imgui_impl_win32.h + imgui_impl_dx11.h

Compiler, OS:

Windows 10 + MSVC 2022

Full config/build information:

No response

Details:

Hello I am experimenting with docking branch and trying to position one window next to another. Intent is to show window with help text next to related window.

Ideally I want something like this:

showTools()
{
  ImGui::Begin("My Tools Window");
  //Displaying all toolls
  ImGui::End();
}
showHelp(const char* help_text)
{
  auto prev_window_geometry = ImGui::GetLastWindowGeometry(); //<--- What should be here?
  // Some positioning calculation
  ImGui::SetNextWindowPos(help_position);
  ImGui::Begin("help_window");
  ImGui::Text(help_text);
  ImGui::End();
}
...
showTools();
showHelp("This is tools window");

Question is how to get position and size of the last Begin-End displayed window? Sorry if I've missed something obvious.

For now my workaround is to save current window position (ImGui::GetWindowPos()) inside Begin-End and then reuse it later to position next window.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

GamingMinds-DanielC commented 8 months ago

As far as I know there is no intended way to do this. You could add your own wrapper function to get the last window if you don't mind poking around in ImGui internals. If you want to do this, you will find the CurrentWindowStack in the context structure. If a window has been ended and no new window has begun yet, you will find the window at the end iterator. You should add a few safeguards as well (check if stack size is smaller than capacity, check the last active frame of the window you find, check its parent against the current window), otherwise your function will give "random" results if used at a time other than between ending a window and beginning a new one.

ocornut commented 8 months ago

Answer seems radically too literal. I think realistically the user shouldn't mind referring to that window identifier, so you can do:

ImGuiWindow* tool_window = ImGui::FindWindowByName("My Tools Window");
// then use tool_window->Pos, tool_window->Size.

I don't think it would be wise that we recorded the pointer/id of the "last" window:

So querying by identifier/name is better IMHO.