Skip to content

Commit

Permalink
MultiSelect: Box-Select: handle Esc to disable box-select.
Browse files Browse the repository at this point in the history
This avoid remove a one-frame delay when finishing box-select, where Esc wouldn't be routed to selection but to child.
  • Loading branch information
ocornut committed Jul 18, 2024
1 parent 2697cfe commit 1b63522
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7377,20 +7377,6 @@ ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, int sel
request_clear = true;
}

if (ms->IsFocused)
{
// Shortcut: Clear selection (Escape)
// Only claim shortcut if selection is not empty, allowing further presses on Escape to e.g. leave current child window.
if ((flags & ImGuiMultiSelectFlags_ClearOnEscape) && (selection_size != 0))
if (Shortcut(ImGuiKey_Escape))
request_clear = true;

// Shortcut: Select all (CTRL+A)
if (!(flags & ImGuiMultiSelectFlags_SingleSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll))
if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A))
request_select_all = true;
}

// Box-select handling: update active state.
ImGuiBoxSelectState* bs = &g.BoxSelectState;
if (flags & (ImGuiMultiSelectFlags_BoxSelect1d | ImGuiMultiSelectFlags_BoxSelect2d))
Expand All @@ -7401,6 +7387,28 @@ ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, int sel
request_clear |= bs->RequestClear;
}

if (ms->IsFocused)
{
// Shortcut: Clear selection (Escape)
// - Only claim shortcut if selection is not empty, allowing further presses on Escape to e.g. leave current child window.
// - Box select also handle Escape and needs to pass an id to bypass ActiveIdUsingAllKeyboardKeys lock.
if (flags & ImGuiMultiSelectFlags_ClearOnEscape)
{
if (selection_size != 0 || bs->IsActive)
if (Shortcut(ImGuiKey_Escape, ImGuiInputFlags_None, bs->IsActive ? bs->ID : 0))
{
request_clear = true;
if (bs->IsActive)
BoxSelectDeactivateDrag(bs);
}
}

// Shortcut: Select all (CTRL+A)
if (!(flags & ImGuiMultiSelectFlags_SingleSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll))
if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A))
request_select_all = true;
}

if (request_clear || request_select_all)
{
ImGuiSelectionRequest req = { ImGuiSelectionRequestType_SetAll, request_select_all, 0, ImGuiSelectionUserData_Invalid, ImGuiSelectionUserData_Invalid };
Expand Down

0 comments on commit 1b63522

Please sign in to comment.