Add method to modify default size/pos of demo window

I am working on an Android example, and one issue that I have been having is that the default code for the demo window’s position and size is hardcoded, and that makes it awkward on mobile since the demo window tends to open up near the far right of my device (most Android devices are not very wide except tablets I believe; reference img.

I want to see if there might be any desire to change imgui_demo.cpp. Here’s a minimal and complete example using example_sdl_opengl3 for reference would be:

ShowDemoWindow(bool) {


    ImGuiIO io = ImGui::GetIO();
    float display_width = (float) io.DisplaySize.x;
    float display_height = (float)io.DisplaySize.y;
    float demo_window_pos_x = display_width * 0.10;
    float demo_window_pos_y = display_height * 0.10;
    float demo_window_size_x = display_width * 0.80;
    float demo_window_size_y = display_height * 0.60;

    // We specify a default position/size in case there's no data in the .ini file. Typically this isn't required! We only do it to make the Demo applications a little more welcoming.
    ImGui::SetNextWindowPos(ImVec2(demo_window_pos_x, demo_window_pos_y), ImGuiCond_FirstUseEver);
    ImGui::SetNextWindowSize(ImVec2(demo_window_size_x, demo_window_size_y), ImGuiCond_FirstUseEver);

People frequently copy this code without looking at it twice so I would rather avoid making it any larger.

You may call SetNextWindowPos+SetNextWindowSize+ a dummy Begin+End in your main.cpp to enforce those settings, thought I would hope to eventually provide more consistent access to settings (in particular, Docking has requirements that are similar to this, where there are various ways which the user may want to access settings prior to the creation of a node/window)

You may call SetNextWindowPos + SetNextWindowSize + a dummy Begin + End in your main.cpp to enforce those settings

Around ImGui::ShowDemoWindow(&show_demo_window); ? That won’t override the pos/size settings within the function though unless I’m mistaken. What about allowing the function to take in pos/size parameters and having default parameters instead?

Calling what I said anywhere before the call to ShowDemoWindow (in main function BEFORE the main loop) will setup the pos/size value and since the demo is using ImGuiCond_FirstTimeEver the demo value won’t be used.

I’m getting a seg fault trying to set the size and building the dummy window before the main loop: example_sdl_opengl3: ../../imgui.cpp:4799: bool ImGui::Begin(const char *, bool*, ImGuiWindowFlags): Assertion 'g.FrameScopeActive' failed.

Here’s what my code looks like (the only difference from other examples is before the main loop):

You need to be calling NewFrame/EndFrame.

1 Like