Version/Branch of Dear ImGui:
Version: dear imgui, v1.73
Branch: master
Screenshots/Video
My Issue/Question:
Hello!
Is there any possibility to restrict / constraint the movement of windows to a specific area, in my case to a fixed minimum y position? (like a SetNextWindowPosConstraints or similar)
I have a menu and an icon bar at the top and would like to prevent users to move any other Dear ImGui window over these two bars (prevent overlapping).
What I have tried (not working):
ImGui::Begin("Settings");
ImGuiContext& g = *GImGui;
if (g.MovingWindow != NULL && g.IO.MousePos.y < (topY + g.ActiveIdClickOffset.y))
{
g.IO.MousePos.y = topY + g.ActiveIdClickOffset.y;
// also tried to set the g.IO.MousePos.y invalid here
}
ImGui::End();
Thanks!
While I'm not against a potential SetNextWindowPosConstraints() api call, I think the problem you are trying to solve is common and rather specific and I believe we should come up with a more dedicated solution.
In OS terminology there is usually a "Work Area" per monitor, which is the monitor rectangle minor task-bar etc. and we could envision having a similar system per viewport. So instead of using a positioning constraint, the menus/bars windows would register themselves are eating space off the work area.
Windows consuming space from the work area sounds even better.
Are you aware of any work-around for now?
I don't think there is, no.
My current work-around (consuming the resize grip area without displaying content there):
void Begin(const char* name, bool* p_open, ImGuiWindowFlags flags)
{
ImGui::Begin(name, p_open, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
const auto height{ ImGui::GetWindowContentRegionMax().y - ImGui::GetWindowContentRegionMin().y };
ImGui::BeginChild("1_Content", ImVec2(0, height - Style::resizeGripHeight), false, flags);
}
void End()
{
ImGui::EndChild();
ImGui::BeginChild("2_ResizeGrip", ImVec2(0.0f, Style::resizeGripHeight), false, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
ImGui::EndChild();
ImGui::End();
}
Note that 75de34e281141f851f7e5f9f6a923573167bc0f0 added a per-viewport work-area in the docking branch, and position clamping uses this work area. So in docking branch if you have a main-menu bar you can position a window entirely behind it.