Imgui: Specify that a window should be collapsed by default

Created on 25 Nov 2014  路  5Comments  路  Source: ocornut/imgui

It would be very nice to have the option to say a window should be collapsed by default. I suggest simply adding ImGuiWindowFlags_DefaultCollapsed

Most helpful comment

https://github.com/ocornut/imgui/commit/e9e0e36f988a12e0719b1e3b109dafac9fb4471f
Added new API:

void SetWindowPos(const ImVec2& pos, ImGuiSetCondition cond = 0);        // set current window position.
void SetWindowSize(const ImVec2& size, ImGuiSetCondition cond = 0);      // set current window size. set to ImVec2(0,0) to force an auto-fit
void SetWindowCollapsed(bool collapsed, ImGuiSetCondition cond = 0);     // set current window collapsed state.

void SetNextWindowPos(const ImVec2& pos, ImGuiSetCondition cond = 0);    // set next window position.
void SetNextWindowSize(const ImVec2& size, ImGuiSetCondition cond = 0);  // set next window size. set to ImVec2(0,0) to force an auto-fit
void SetNextWindowCollapsed(bool collapsed, ImGuiSetCondition cond = 0); // set next window collapsed state.

This is the enum:

// Condition flags for ImGui::SetWindow***() and SetNextWindow***() functions
// Those functions treat 0 as a shortcut to ImGuiSetCondition_Always
enum ImGuiSetCondition_
{
    ImGuiSetCondition_Always              = 1 << 0, // Set the variable
    ImGuiSetCondition_FirstUseThisSession = 1 << 1, // Only set the variable on the first call for this window (once per session)
    ImGuiSetCondition_FirstUseEver        = 1 << 2, // Only set the variable if the window doesn't exist in the .ini file
};

With that change, it means that the "size" parameter of Begin() is essentially a shortcut to doing

ImGui::SetNextWindowSize(size, ImGuiSetCondition_FirstUseEver);

I would like to remove that parameter from Begin() because I think it should rarely be used and its current behaviour may be misleading. But I am hesitant to break this API because it may be the most used function and I can imagine lots of people filled in 3+ parameters of Begin().

All 5 comments

It would be possible but consider that the "collapsed" state is saved in the .ini file and so is persistent per user. So the existence of a way in code to set the default may be a bit anecdotal and not easy to test because the default is overridden after the first use.

There's a similar problem with other saved settings such as "position" and "size". My intuition would be to add a simple API allowing to do either of those:
A. set the value in the case of first use (no records of this window in the .ini file)
B. set the value in the case of first use during this session (the user can subsequently modify the value).
C. set the value

Where you mainly thinking of A or of B ?
Either way I can see a case for also allowing to set the default "non-collapsed" (it makes senses for B) so it may take a full-pledged API rather than just a flag.

Currently there's an API called SetNewWindowDefaultPos() which does (A) for position and is a bit awkward, I actually only created it for the sample application to start with nicely positioned windows.

One of the idea behind ImGui is to reduce user maintaining persistent state so I always perceived "default" and "stored settings" as easy-to-ditch convenience. But I realize we need the flexibility to decide of how to treat those settings.

I want it for use case A. In particular, I am using ImGui on an iPad and I'm re-installing it often during development which resets the .ini file. I feel that for a any GUI library it is quite important to be able to set good defaults (position, size, collapse:ness). The same goes for things like TreeNode:s.

That's fair. I will aim to add the complete set of features but feel free to add a temporary flag solution locally. Three steps:

  • add flag in imgui.h
  • in ImGui::Begin() pass 'flags' to the call to FindWindowSettings(), add a new parameter here
  • in FindWindowSettings() test for it when setting the 'collapsed' field.

I'll rework this later to make it neat (probably split FindWindowSettings into another CreateWindowSettings, etc. and add the new API to set all pos/size/collapsed settings).

Note that in your situation cases A and B are the same, and B is generally a more common case.

I'm working on those new API now, something like:

SetNextWindowPos(pos);
SetNextWindowPos(pos, ImGuiSetValue_FirstUseThisSession);
SetNextWindowPos(pos, ImGuiSetValue_FirstUseEver);

And equivalent for Size and Collapsed.

What I am unsure is whether to preserve SetWindowPos(), which does have a forced frame of latency and so is a bit flaky. However I can see it being convenient in some contexts.

Then perhaps we also need

SetWindowPos(pos);
SetWindowPos(pos, ImGuiSetValue_FirstUseThisSession);
SetWindowPos(pos, ImGuiSetValue_FirstUseEver);

So that would be a total of 6 functions

SetWindowPos()
SetNextWindowPos()
SetWindowSize()
SetNextWindowSize()
SetWindowCollapsed()
SetNextWindowCollapsed()

I would prefer to have 3 functions allowing both cases but unsure of how to parametrize the "next window" vs "current window" information? A real bad use of a boolean. Adding an enum makes more sense but suddenly things would become quite verbose. May stick with 6 functions.

https://github.com/ocornut/imgui/commit/e9e0e36f988a12e0719b1e3b109dafac9fb4471f
Added new API:

void SetWindowPos(const ImVec2& pos, ImGuiSetCondition cond = 0);        // set current window position.
void SetWindowSize(const ImVec2& size, ImGuiSetCondition cond = 0);      // set current window size. set to ImVec2(0,0) to force an auto-fit
void SetWindowCollapsed(bool collapsed, ImGuiSetCondition cond = 0);     // set current window collapsed state.

void SetNextWindowPos(const ImVec2& pos, ImGuiSetCondition cond = 0);    // set next window position.
void SetNextWindowSize(const ImVec2& size, ImGuiSetCondition cond = 0);  // set next window size. set to ImVec2(0,0) to force an auto-fit
void SetNextWindowCollapsed(bool collapsed, ImGuiSetCondition cond = 0); // set next window collapsed state.

This is the enum:

// Condition flags for ImGui::SetWindow***() and SetNextWindow***() functions
// Those functions treat 0 as a shortcut to ImGuiSetCondition_Always
enum ImGuiSetCondition_
{
    ImGuiSetCondition_Always              = 1 << 0, // Set the variable
    ImGuiSetCondition_FirstUseThisSession = 1 << 1, // Only set the variable on the first call for this window (once per session)
    ImGuiSetCondition_FirstUseEver        = 1 << 2, // Only set the variable if the window doesn't exist in the .ini file
};

With that change, it means that the "size" parameter of Begin() is essentially a shortcut to doing

ImGui::SetNextWindowSize(size, ImGuiSetCondition_FirstUseEver);

I would like to remove that parameter from Begin() because I think it should rarely be used and its current behaviour may be misleading. But I am hesitant to break this API because it may be the most used function and I can imagine lots of people filled in 3+ parameters of Begin().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mnemode2 picture mnemode2  路  3Comments

bogdaNNNN1 picture bogdaNNNN1  路  3Comments

ILoveImgui picture ILoveImgui  路  3Comments

mkanakis picture mkanakis  路  3Comments

ghost picture ghost  路  3Comments