| 5321 | //----------------------------------------------------------------------------- |
| 5322 | |
| 5323 | static void DemoWindowPopups() |
| 5324 | { |
| 5325 | if (!ImGui::CollapsingHeader("Popups & Modal windows")) |
| 5326 | return; |
| 5327 | |
| 5328 | // The properties of popups windows are: |
| 5329 | // - They block normal mouse hovering detection outside them. (*) |
| 5330 | // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. |
| 5331 | // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as |
| 5332 | // we are used to with regular Begin() calls. User can manipulate the visibility state by calling OpenPopup(). |
| 5333 | // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even |
| 5334 | // when normally blocked by a popup. |
| 5335 | // Those three properties are connected. The library needs to hold their visibility state BECAUSE it can close |
| 5336 | // popups at any time. |
| 5337 | |
| 5338 | // Typical use for regular windows: |
| 5339 | // 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(); |
| 5340 | // Typical use for popups: |
| 5341 | // if (ImGui::Button("Open")) ImGui::OpenPopup("MyPopup"); if (ImGui::BeginPopup("MyPopup")) { [...] EndPopup(); } |
| 5342 | |
| 5343 | // With popups we have to go through a library call (here OpenPopup) to manipulate the visibility state. |
| 5344 | // This may be a bit confusing at first but it should quickly make sense. Follow on the examples below. |
| 5345 | |
| 5346 | if (ImGui::TreeNode("Popups")) |
| 5347 | { |
| 5348 | IMGUI_DEMO_MARKER("Popups/Popups"); |
| 5349 | ImGui::TextWrapped( |
| 5350 | "When a popup is active, it inhibits interacting with windows that are behind the popup. " |
| 5351 | "Clicking outside the popup closes it."); |
| 5352 | |
| 5353 | static int selected_fish = -1; |
| 5354 | const char* names[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" }; |
| 5355 | static bool toggles[] = { true, false, false, false, false }; |
| 5356 | |
| 5357 | // Simple selection popup (if you want to show the current selection inside the Button itself, |
| 5358 | // you may want to build a string using the "###" operator to preserve a constant ID with a variable label) |
| 5359 | if (ImGui::Button("Select..")) |
| 5360 | ImGui::OpenPopup("my_select_popup"); |
| 5361 | ImGui::SameLine(); |
| 5362 | ImGui::TextUnformatted(selected_fish == -1 ? "<None>" : names[selected_fish]); |
| 5363 | if (ImGui::BeginPopup("my_select_popup")) |
| 5364 | { |
| 5365 | ImGui::SeparatorText("Aquarium"); |
| 5366 | for (int i = 0; i < IM_COUNTOF(names); i++) |
| 5367 | if (ImGui::Selectable(names[i])) |
| 5368 | selected_fish = i; |
| 5369 | ImGui::EndPopup(); |
| 5370 | } |
| 5371 | |
| 5372 | // Showing a menu with toggles |
| 5373 | if (ImGui::Button("Toggle..")) |
| 5374 | ImGui::OpenPopup("my_toggle_popup"); |
| 5375 | if (ImGui::BeginPopup("my_toggle_popup")) |
| 5376 | { |
| 5377 | for (int i = 0; i < IM_COUNTOF(names); i++) |
| 5378 | ImGui::MenuItem(names[i], "", &toggles[i]); |
| 5379 | if (ImGui::BeginMenu("Sub-menu")) |
| 5380 | { |
no test coverage detected