CheckboxFlags: Display mixed-value/tristate marker when passed flags that have multiple bits set and stored value matches neither zero neither the full set.

This commit is contained in:
ocornut
2020-10-07 15:13:04 +02:00
parent 4fd43a8b64
commit 12d9505534
2 changed files with 26 additions and 9 deletions

View File

@ -1071,7 +1071,8 @@ bool ImGui::Checkbox(const char* label, bool* v)
RenderNavHighlight(total_bb, id);
RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding);
ImU32 check_col = GetColorU32(ImGuiCol_CheckMark);
if (window->DC.ItemFlags & ImGuiItemFlags_MixedValue)
bool mixed_value = (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) != 0;
if (mixed_value)
{
// Undocumented tristate/mixed/indeterminate checkbox (#2644)
ImVec2 pad(ImMax(1.0f, IM_FLOOR(square_sz / 3.6f)), ImMax(1.0f, IM_FLOOR(square_sz / 3.6f)));
@ -1084,7 +1085,7 @@ bool ImGui::Checkbox(const char* label, bool* v)
}
if (g.LogEnabled)
LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]");
LogRenderedText(&total_bb.Min, mixed_value ? "[~]" : *v ? "[x]" : "[ ]");
if (label_size.x > 0.0f)
RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label);
@ -1095,7 +1096,21 @@ bool ImGui::Checkbox(const char* label, bool* v)
bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value)
{
bool v = ((*flags & flags_value) == flags_value);
bool pressed = Checkbox(label, &v);
bool pressed;
if (v == false && (*flags & flags_value) != 0)
{
// Mixed value (FIXME: find a way to expose neatly to Checkbox?)
ImGuiWindow* window = GetCurrentWindow();
const ImGuiItemFlags backup_item_flags = window->DC.ItemFlags;
window->DC.ItemFlags |= ImGuiItemFlags_MixedValue;
pressed = Checkbox(label, &v);
window->DC.ItemFlags = backup_item_flags;
}
else
{
// Regular checkbox
pressed = Checkbox(label, &v);
}
if (pressed)
{
if (v)