| 5169 | //----------------------------------------------------------------------------- |
| 5170 | |
| 5171 | static void DemoWindowPopups() |
| 5172 | { |
| 5173 | IMGUI_DEMO_MARKER("Popups"); |
| 5174 | if (!ImGui::CollapsingHeader("Popups & Modal windows")) |
| 5175 | return; |
| 5176 | |
| 5177 | // The properties of popups windows are: |
| 5178 | // - They block normal mouse hovering detection outside them. (*) |
| 5179 | // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. |
| 5180 | // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as |
| 5181 | // we are used to with regular Begin() calls. User can manipulate the visibility state by calling OpenPopup(). |
| 5182 | // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even |
| 5183 | // when normally blocked by a popup. |
| 5184 | // Those three properties are connected. The library needs to hold their visibility state BECAUSE it can close |
| 5185 | // popups at any time. |
| 5186 | |
| 5187 | // Typical use for regular windows: |
| 5188 | // bool my_tool_is_active = false; if (ImGui::Button("Open")) my_tool_is_active = true; [...] if (my_tool_is_active) Begin("My Tool", &my_tool_is_active) { [...] } End(); |
| 5189 | // Typical use for popups: |
| 5190 | // if (ImGui::Button("Open")) ImGui::OpenPopup("MyPopup"); if (ImGui::BeginPopup("MyPopup")) { [...] EndPopup(); } |
| 5191 | |
| 5192 | // With popups we have to go through a library call (here OpenPopup) to manipulate the visibility state. |
| 5193 | // This may be a bit confusing at first but it should quickly make sense. Follow on the examples below. |
| 5194 | |
| 5195 | IMGUI_DEMO_MARKER("Popups/Popups"); |
| 5196 | if (ImGui::TreeNode("Popups")) |
| 5197 | { |
| 5198 | ImGui::TextWrapped( |
| 5199 | "When a popup is active, it inhibits interacting with windows that are behind the popup. " |
| 5200 | "Clicking outside the popup closes it."); |
| 5201 | |
| 5202 | static int selected_fish = -1; |
| 5203 | const char* names[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" }; |
| 5204 | static bool toggles[] = { true, false, false, false, false }; |
| 5205 | |
| 5206 | // Simple selection popup (if you want to show the current selection inside the Button itself, |
| 5207 | // you may want to build a string using the "###" operator to preserve a constant ID with a variable label) |
| 5208 | if (ImGui::Button("Select..")) |
| 5209 | ImGui::OpenPopup("my_select_popup"); |
| 5210 | ImGui::SameLine(); |
| 5211 | ImGui::TextUnformatted(selected_fish == -1 ? "<None>" : names[selected_fish]); |
| 5212 | if (ImGui::BeginPopup("my_select_popup")) |
| 5213 | { |
| 5214 | ImGui::SeparatorText("Aquarium"); |
| 5215 | for (int i = 0; i < IM_ARRAYSIZE(names); i++) |
| 5216 | if (ImGui::Selectable(names[i])) |
| 5217 | selected_fish = i; |
| 5218 | ImGui::EndPopup(); |
| 5219 | } |
| 5220 | |
| 5221 | // Showing a menu with toggles |
| 5222 | if (ImGui::Button("Toggle..")) |
| 5223 | ImGui::OpenPopup("my_toggle_popup"); |
| 5224 | if (ImGui::BeginPopup("my_toggle_popup")) |
| 5225 | { |
| 5226 | for (int i = 0; i < IM_ARRAYSIZE(names); i++) |
| 5227 | ImGui::MenuItem(names[i], "", &toggles[i]); |
| 5228 | if (ImGui::BeginMenu("Sub-menu")) |
no test coverage detected