mirror of
https://github.com/Drezil/imgui.git
synced 2024-11-15 01:17:00 +00:00
WIP Menus: Fixed remaining inconsistency with stacks of popups. Activating/closing a menu close the parent popup. Added test case. (#126)
This commit is contained in:
parent
90cf77b191
commit
3a6e6645e6
36
imgui.cpp
36
imgui.cpp
@ -3072,32 +3072,35 @@ void ImGui::OpenPopup(const char* str_id)
|
|||||||
g.OpenedPopupStack.back() = ImGuiPopupRef(id, window, window->GetID("##menus"));
|
g.OpenedPopupStack.back() = ImGuiPopupRef(id, window, window->GetID("##menus"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void ClosePopupToLevel(int remaining)
|
||||||
|
{
|
||||||
|
ImGuiState& g = *GImGui;
|
||||||
|
if (remaining > 0)
|
||||||
|
FocusWindow(g.OpenedPopupStack[remaining-1].Window);
|
||||||
|
else
|
||||||
|
FocusWindow(g.OpenedPopupStack[0].ParentWindow);
|
||||||
|
g.OpenedPopupStack.resize(remaining);
|
||||||
|
}
|
||||||
|
|
||||||
static void ClosePopup(const char* str_id) // not exposed because 'id' scope is misleading
|
static void ClosePopup(const char* str_id) // not exposed because 'id' scope is misleading
|
||||||
{
|
{
|
||||||
ImGuiState& g = *GImGui;
|
ImGuiState& g = *GImGui;
|
||||||
ImGuiWindow* window = GetCurrentWindow();
|
ImGuiWindow* window = GetCurrentWindow();
|
||||||
const ImGuiID id = window->GetID(str_id);
|
const ImGuiID id = window->GetID(str_id);
|
||||||
if (IsPopupOpen(id))
|
if (IsPopupOpen(id))
|
||||||
g.OpenedPopupStack.resize(g.CurrentPopupStack.size());
|
ClosePopupToLevel((int)g.CurrentPopupStack.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the popup we have begin-ed into.
|
||||||
void ImGui::CloseCurrentPopup()
|
void ImGui::CloseCurrentPopup()
|
||||||
{
|
{
|
||||||
ImGuiState& g = *GImGui;
|
ImGuiState& g = *GImGui;
|
||||||
int popup_idx = (int)g.CurrentPopupStack.size() - 1;
|
int popup_idx = (int)g.CurrentPopupStack.size() - 1;
|
||||||
if (popup_idx < 0 || popup_idx > (int)g.OpenedPopupStack.size() || g.CurrentPopupStack[popup_idx].PopupID != g.OpenedPopupStack[popup_idx].PopupID)
|
if (popup_idx < 0 || popup_idx > (int)g.OpenedPopupStack.size() || g.CurrentPopupStack[popup_idx].PopupID != g.OpenedPopupStack[popup_idx].PopupID)
|
||||||
return;
|
return;
|
||||||
if (g.CurrentWindow->PopupID == g.OpenedPopupStack[popup_idx].PopupID && g.Windows.size() > 1)
|
while (popup_idx > 0 && g.OpenedPopupStack[popup_idx].Window && (g.OpenedPopupStack[popup_idx].Window->Flags & ImGuiWindowFlags_ChildMenu))
|
||||||
FocusWindow(g.Windows[g.Windows.size()-2]);
|
popup_idx--;
|
||||||
g.OpenedPopupStack.resize(popup_idx);
|
ClosePopupToLevel(popup_idx);
|
||||||
}
|
|
||||||
|
|
||||||
static void CloseCurrentMenus()
|
|
||||||
{
|
|
||||||
// Close all popups
|
|
||||||
// FIXME-MENUS: invalid for popup->menus with current BeginMenu() scheme
|
|
||||||
ImGuiState& g = *GImGui;
|
|
||||||
g.OpenedPopupStack.resize(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ClearSetNextWindowData()
|
static void ClearSetNextWindowData()
|
||||||
@ -7357,9 +7360,7 @@ static bool SelectableEx(const char* label, bool selected, const ImVec2& size_ar
|
|||||||
if (flags & ImGuiSelectableFlags_Disabled) ImGui::PopStyleColor();
|
if (flags & ImGuiSelectableFlags_Disabled) ImGui::PopStyleColor();
|
||||||
|
|
||||||
// Automatically close popups
|
// Automatically close popups
|
||||||
if (pressed && (window->Flags & ImGuiWindowFlags_ChildMenu) && !(flags & ImGuiSelectableFlags_DontClosePopups))
|
if (pressed && !(flags & ImGuiSelectableFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup))
|
||||||
CloseCurrentMenus();
|
|
||||||
else if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups))
|
|
||||||
ImGui::CloseCurrentPopup();
|
ImGui::CloseCurrentPopup();
|
||||||
return pressed;
|
return pressed;
|
||||||
}
|
}
|
||||||
@ -10557,6 +10558,11 @@ void ImGui::ShowTestWindow(bool* opened)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < IM_ARRAYSIZE(names); i++)
|
for (int i = 0; i < IM_ARRAYSIZE(names); i++)
|
||||||
ImGui::MenuItem(names[i], "", &toggles[i]);
|
ImGui::MenuItem(names[i], "", &toggles[i]);
|
||||||
|
if (ImGui::BeginMenu("Sub-menu"))
|
||||||
|
{
|
||||||
|
ImGui::MenuItem("Click me");
|
||||||
|
ImGui::EndMenu();
|
||||||
|
}
|
||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
}
|
}
|
||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
|
2
imgui.h
2
imgui.h
@ -168,7 +168,7 @@ namespace ImGui
|
|||||||
IMGUI_API void OpenPopup(const char* str_id); // mark popup as open. close childs popups if any. will close popup when user click outside, or activate menu items, or CloseCurrentPopup() is called within a BeginPopup/EndPopup block.
|
IMGUI_API void OpenPopup(const char* str_id); // mark popup as open. close childs popups if any. will close popup when user click outside, or activate menu items, or CloseCurrentPopup() is called within a BeginPopup/EndPopup block.
|
||||||
IMGUI_API bool BeginPopup(const char* str_id); // return true if popup if opened and start outputting to it. only call EndPopup() if BeginPopup() returned true!
|
IMGUI_API bool BeginPopup(const char* str_id); // return true if popup if opened and start outputting to it. only call EndPopup() if BeginPopup() returned true!
|
||||||
IMGUI_API void EndPopup();
|
IMGUI_API void EndPopup();
|
||||||
IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into
|
IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup.
|
||||||
|
|
||||||
// Layout
|
// Layout
|
||||||
IMGUI_API void BeginGroup(); // once closing a group it is seen as a single item (so you can use IsItemHovered() on a group, SameLine() between groups, etc.
|
IMGUI_API void BeginGroup(); // once closing a group it is seen as a single item (so you can use IsItemHovered() on a group, SameLine() between groups, etc.
|
||||||
|
Loading…
Reference in New Issue
Block a user