OS window size adaptive to Imgui window size


#1

I am looking to build a “tool application”, just one single Imgui (no frame, etc) window at once.
My problem is that I would like to have different “pages” (windows) from different sizes and I would like that the OS windows fits the Imgui window size. Ideally it will automatic adjust to the Imgui window size.

An example would be something like:
welcome window with a “Press OK to continue”, then after press OK, a new window different size will appear and the welcome window will disapear.

Will this be possible to do with Imgui and other lib ( glfw, SDL2, etc ) what would be the best way to do it?
Can I do it by resizing a windows, etc or will I need to close and create a new OS window / GL context ever time?


#2

Here are my findings on this subject.

IMGUI related:

Create a ImGui window in the position (0,0), with flags that remove not need stuff:

ImGui::SetNextWindowPos( ImVec2(0,0) );
ImGui::Begin( “MainWindow” , 0 , ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse );

winSize = ImGui::GetWindowSize();
ImGui::End();

This winSize will be the need size for resize the window.

GLFW related:

Create a window not resizable, without decoration (if desired), also it is possible on latest glfw version to make the window transparent.

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_DECORATED, GL_FALSE);
//glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GL_TRUE); // Only on latest versions
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
GLFWwindow* window = glfwCreateWindow(360, 161, “ImGui GLFW+OpenGL2 example”, NULL, NULL);

mode will be used to know the screensize in order to center the window.

if( ( (int)winSize_old.x != (int)winSize.x ) || ( (int)winSize_old.y != (int)winSize.y ) )
{
winSize_old = winSize;
glfwSetWindowSize( window, (int)winSize.x, (int)winSize.y ); // Resize
glfwSetWindowPos( window, (mode->width - (int)winSize.x) / 2, (mode->height-(int)winSize.y) / 2 ); // Center window
}

Mind there could be differences while resizing the window due the OS frame decoration size vs actual frame buffer size.


#3

Yes that would work. You may also want to check the “viewport” which is a generalization of the concept of imgui interacting with the underlying windowing system to manipulate multiple os windows/graphic contexts, which include moving and resizing os windows.


#4

Thanks for the tip, I will have a look.

btw, I found that the first time it loops, the GetWindowSize is returning 32,32
I guess that is the default WindowMinSize.
Is it the way it is expected to work or there is some way I can use to know the size early?


#5

Most windows are hidden during the first frame they appear and don’t have a proper size yet, they are using this frame to do a first pass measuring the expected window size. So yes, that’s expected.