RangeSelect/MultiSelect: Fix testing key mods from after the nav request (remove need to hold the mod longer)

This commit is contained in:
omar 2020-04-02 16:46:53 +02:00 committed by ocornut
parent 910d83951d
commit a7977d1e8b
2 changed files with 11 additions and 6 deletions

View File

@ -1721,6 +1721,7 @@ struct ImGuiContext
bool MultiSelectEnabled; bool MultiSelectEnabled;
ImGuiMultiSelectFlags MultiSelectFlags; ImGuiMultiSelectFlags MultiSelectFlags;
ImGuiMultiSelectState MultiSelectState; // We currently don't support recursing/stacking multi-select ImGuiMultiSelectState MultiSelectState; // We currently don't support recursing/stacking multi-select
ImGuiKeyModFlags MultiSelectKeyMods;
// Render // Render
float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list)
@ -1920,6 +1921,7 @@ struct ImGuiContext
MultiSelectEnabled = false; MultiSelectEnabled = false;
MultiSelectFlags = ImGuiMultiSelectFlags_None; MultiSelectFlags = ImGuiMultiSelectFlags_None;
MultiSelectKeyMods = ImGuiKeyModFlags_None;
DimBgRatio = 0.0f; DimBgRatio = 0.0f;
MouseCursor = ImGuiMouseCursor_Arrow; MouseCursor = ImGuiMouseCursor_Arrow;

View File

@ -6387,6 +6387,9 @@ ImGuiMultiSelectData* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, void*
g.MultiSelectEnabled = true; g.MultiSelectEnabled = true;
g.MultiSelectFlags = flags; 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) if ((flags & ImGuiMultiSelectFlags_NoMultiSelect) == 0)
{ {
state->In.RangeSrc = state->Out.RangeSrc = range_ref; state->In.RangeSrc = state->Out.RangeSrc = range_ref;
@ -6397,9 +6400,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() // 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 == state->FocusScopeId && g.NavJustMovedToHasSelectionData) if (g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == state->FocusScopeId && g.NavJustMovedToHasSelectionData)
{ {
if (g.IO.KeyShift) if (g.MultiSelectKeyMods & ImGuiKeyModFlags_Shift)
state->InRequestSetRangeNav = true; state->InRequestSetRangeNav = true;
if (!g.IO.KeyCtrl && !g.IO.KeyShift) if ((g.MultiSelectKeyMods & (ImGuiKeyModFlags_Ctrl | ImGuiKeyModFlags_Shift)) == 0)
state->In.RequestClear = true; state->In.RequestClear = true;
} }
@ -6484,13 +6487,13 @@ void ImGui::MultiSelectItemHeader(ImGuiID id, bool* p_selected)
if (state->InRequestSetRangeNav) if (state->InRequestSetRangeNav)
{ {
IM_ASSERT(id != 0); IM_ASSERT(id != 0);
IM_ASSERT(g.IO.KeyShift); IM_ASSERT((g.MultiSelectKeyMods & ImGuiKeyModFlags_Shift) != 0);
const bool is_range_dst = !state->InRangeDstPassedBy && g.NavJustMovedToId == id; // Assume that g.NavJustMovedToId is not clipped. const bool is_range_dst = !state->InRangeDstPassedBy && g.NavJustMovedToId == id; // Assume that g.NavJustMovedToId is not clipped.
if (is_range_dst) if (is_range_dst)
state->InRangeDstPassedBy = true; state->InRangeDstPassedBy = true;
if (is_range_src || is_range_dst || state->In.RangeSrcPassedBy != state->InRangeDstPassedBy) if (is_range_src || is_range_dst || state->In.RangeSrcPassedBy != state->InRangeDstPassedBy)
selected = state->In.RangeValue; selected = state->In.RangeValue;
else if (!g.IO.KeyCtrl) else if ((g.MultiSelectKeyMods & ImGuiKeyModFlags_Ctrl) == 0)
selected = false; selected = false;
} }
@ -6508,9 +6511,9 @@ void ImGui::MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed)
bool selected = *p_selected; bool selected = *p_selected;
bool pressed = *p_pressed; 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; const bool is_multiselect = (g.MultiSelectFlags & ImGuiMultiSelectFlags_NoMultiSelect) == 0;
bool is_ctrl = (g.MultiSelectKeyMods & ImGuiKeyModFlags_Ctrl) != 0;
bool is_shift = (g.MultiSelectKeyMods & ImGuiKeyModFlags_Shift) != 0;
// Auto-select as you navigate a list // Auto-select as you navigate a list
if (g.NavJustMovedToId == id) if (g.NavJustMovedToId == id)