From 00c4b8f2a345f5dbf34fa4471f1acd8b117fdbd7 Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 2 Apr 2020 16:46:53 +0200 Subject: [PATCH] MultiSelect: Fix testing key mods from after the nav request (remove need to hold the mod longer) --- imgui_internal.h | 2 ++ imgui_widgets.cpp | 15 +++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/imgui_internal.h b/imgui_internal.h index dd5853466..005df3065 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -2123,6 +2123,7 @@ struct ImGuiContext bool MultiSelectEnabled; ImGuiMultiSelectFlags MultiSelectFlags; ImGuiMultiSelectState MultiSelectState; // We currently don't support recursing/stacking multi-select + ImGuiKeyChord MultiSelectKeyMods; // Render float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) @@ -2389,6 +2390,7 @@ struct ImGuiContext MultiSelectEnabled = false; MultiSelectFlags = ImGuiMultiSelectFlags_None; + MultiSelectKeyMods = ImGuiMod_None; DimBgRatio = 0.0f; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index ba59b802c..5c344c7b2 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -7138,6 +7138,9 @@ ImGuiMultiSelectData* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, void* g.MultiSelectEnabled = true; g.MultiSelectFlags = flags; + // Use copy of keyboard mods at the time of the request, otherwise we would requires mods to be held for an extra frame. + g.MultiSelectKeyMods = g.NavJustMovedToId ? g.NavJustMovedToKeyMods : g.IO.KeyMods; + if ((flags & ImGuiMultiSelectFlags_NoMultiSelect) == 0) { ms->In.RangeSrc = ms->Out.RangeSrc = range_ref; @@ -7148,9 +7151,9 @@ ImGuiMultiSelectData* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, void* // FIXME: Polling key mods after the fact (frame following the move request) is incorrect, but latching it would requires non-trivial change in MultiSelectItemFooter() if (g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == ms->FocusScopeId && g.NavJustMovedToHasSelectionData) { - if (g.IO.KeyShift) + if (g.MultiSelectKeyMods & ImGuiMod_Shift) ms->InRequestSetRangeNav = true; - if (!g.IO.KeyCtrl && !g.IO.KeyShift) + if ((g.MultiSelectKeyMods & (ImGuiMod_Ctrl | ImGuiMod_Shift)) == 0) ms->In.RequestClear = true; } @@ -7233,13 +7236,13 @@ void ImGui::MultiSelectItemHeader(ImGuiID id, bool* p_selected) if (ms->InRequestSetRangeNav) { IM_ASSERT(id != 0); - IM_ASSERT(g.IO.KeyShift); + IM_ASSERT((g.MultiSelectKeyMods & ImGuiMod_Shift) != 0); const bool is_range_dst = !ms->InRangeDstPassedBy && g.NavJustMovedToId == id; // Assume that g.NavJustMovedToId is not clipped. if (is_range_dst) ms->InRangeDstPassedBy = true; if (is_range_src || is_range_dst || ms->In.RangeSrcPassedBy != ms->InRangeDstPassedBy) selected = ms->In.RangeValue; - else if (!g.IO.KeyCtrl) + else if ((g.MultiSelectKeyMods & ImGuiMod_Ctrl) == 0) selected = false; } @@ -7256,9 +7259,9 @@ void ImGui::MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed) bool selected = *p_selected; bool pressed = *p_pressed; - bool is_ctrl = g.IO.KeyCtrl; - bool is_shift = g.IO.KeyShift; const bool is_multiselect = (g.MultiSelectFlags & ImGuiMultiSelectFlags_NoMultiSelect) == 0; + bool is_ctrl = (g.MultiSelectKeyMods & ImGuiMod_Ctrl) != 0; + bool is_shift = (g.MultiSelectKeyMods & ImGuiMod_Shift) != 0; // Auto-select as you navigate a list if (g.NavJustMovedToId == id)